xref: /petsc/src/dm/interface/dm.c (revision adc219578d10939800eed519846cb0a0577dc4e2)
1d0295fc0SJunchao Zhang #include <petscvec.h>
2af0996ceSBarry Smith #include <petsc/private/dmimpl.h>      /*I      "petscdm.h"          I*/
3c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h> /*I      "petscdmlabel.h"     I*/
4e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h> /*I      "petscds.h"     I*/
53e922f36SToby Isaac #include <petscdmplex.h>
6d2b2dc1eSMatthew G. Knepley #include <petscdmceed.h>
7f19dbd58SToby Isaac #include <petscdmfield.h>
80c312b8eSJed Brown #include <petscsf.h>
92764a2aaSMatthew G. Knepley #include <petscds.h>
1047c6ae99SBarry Smith 
11f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
12f918ec44SMatthew G. Knepley   #include <petscfeceed.h>
13f918ec44SMatthew G. Knepley #endif
14f918ec44SMatthew G. Knepley 
15cea3dcb8SSatish Balay #if !defined(PETSC_HAVE_WINDOWS_COMPILERS)
16cea3dcb8SSatish Balay   #include <petsc/private/valgrind/memcheck.h>
1700d952a4SJed Brown #endif
1800d952a4SJed Brown 
19732e2eb9SMatthew G Knepley PetscClassId DM_CLASSID;
20d67d17b1SMatthew G. Knepley PetscClassId DMLABEL_CLASSID;
21708be2fdSJed Brown PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction, DM_CreateInjection, DM_CreateMatrix, DM_CreateMassMatrix, DM_Load, DM_AdaptInterpolator, DM_ProjectFunction;
2267a56275SMatthew G Knepley 
23ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[]          = {"NONE", "GHOSTED", "MIRROR", "PERIODIC", "TWIST", "DMBoundaryType", "DM_BOUNDARY_", NULL};
24d1b3049bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID", "ESSENTIAL", "NATURAL", "INVALID", "INVALID", "ESSENTIAL_FIELD", "NATURAL_FIELD", "INVALID", "INVALID", "ESSENTIAL_BD_FIELD", "NATURAL_RIEMANN", "DMBoundaryConditionType", "DM_BC_", NULL};
250e762ea3SJed Brown const char *const DMBlockingTypes[]          = {"TOPOLOGICAL_POINT", "FIELD_NODE", "DMBlockingType", "DM_BLOCKING_", NULL};
26476787b7SMatthew G. Knepley const char *const DMPolytopeTypes[] =
27476787b7SMatthew G. Knepley   {"vertex",  "segment",      "tensor_segment", "triangle", "quadrilateral",  "tensor_quad",  "tetrahedron", "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism", "pyramid", "FV_ghost_cell", "interior_ghost_cell",
28476787b7SMatthew G. Knepley    "unknown", "unknown_cell", "unknown_face",   "invalid",  "DMPolytopeType", "DM_POLYTOPE_", NULL};
292cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace", "keep", "fail", "DMCopyLabelsMode", "DM_COPY_LABELS_", NULL};
3060c22052SBarry Smith 
31a4121054SBarry Smith /*@
32bb7acecfSBarry Smith   DMCreate - Creates an empty `DM` object. `DM`s are the abstract objects in PETSc that mediate between meshes and discretizations and the
33bb7acecfSBarry Smith   algebraic solvers, time integrators, and optimization algorithms.
34a4121054SBarry Smith 
35d083f849SBarry Smith   Collective
36a4121054SBarry Smith 
37a4121054SBarry Smith   Input Parameter:
38bb7acecfSBarry Smith . comm - The communicator for the `DM` object
39a4121054SBarry Smith 
40a4121054SBarry Smith   Output Parameter:
41bb7acecfSBarry Smith . dm - The `DM` object
42a4121054SBarry Smith 
43a4121054SBarry Smith   Level: beginner
44a4121054SBarry Smith 
45bb7acecfSBarry Smith   Notes:
46bb7acecfSBarry Smith   See `DMType` for a brief summary of available `DM`.
47bb7acecfSBarry Smith 
48bb7acecfSBarry Smith   The type must then be set with `DMSetType()`. If you never call `DMSetType()` it will generate an
49bb7acecfSBarry Smith   error when you try to use the dm.
50bb7acecfSBarry Smith 
511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMType`, `DMDACreate()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK`
52a4121054SBarry Smith @*/
53d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreate(MPI_Comm comm, DM *dm)
54d71ae5a4SJacob Faibussowitsch {
55a4121054SBarry Smith   DM      v;
56e5e52638SMatthew G. Knepley   PetscDS ds;
57a4121054SBarry Smith 
58a4121054SBarry Smith   PetscFunctionBegin;
594f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
600298fd71SBarry Smith   *dm = NULL;
619566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
62a4121054SBarry Smith 
639566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
64e7c4fc90SDmitry Karpeev 
6562e5d2d2SJDBetteridge   ((PetscObject)v)->non_cyclic_references = &DMCountNonCyclicReferences;
6662e5d2d2SJDBetteridge 
6749be4549SMatthew G. Knepley   v->setupcalled          = PETSC_FALSE;
6849be4549SMatthew G. Knepley   v->setfromoptionscalled = PETSC_FALSE;
690298fd71SBarry Smith   v->ltogmap              = NULL;
70a4ea9b21SRichard Tran Mills   v->bind_below           = 0;
711411c6eeSJed Brown   v->bs                   = 1;
72171400e9SBarry Smith   v->coloringtype         = IS_COLORING_GLOBAL;
739566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
749566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
75c58f1c22SToby Isaac   v->labels                    = NULL;
7634aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7734aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
78c58f1c22SToby Isaac   v->depthLabel                = NULL;
79ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
801bb6d2a8SBarry Smith   v->localSection              = NULL;
811bb6d2a8SBarry Smith   v->globalSection             = NULL;
823b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
833b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
8479769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
856858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
866858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
876858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8896173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
89435a35e8SMatthew G Knepley   {
90435a35e8SMatthew G Knepley     PetscInt i;
91435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
920298fd71SBarry Smith       v->nullspaceConstructors[i]     = NULL;
93f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
94435a35e8SMatthew G Knepley     }
95435a35e8SMatthew G Knepley   }
969566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
9707218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(v, NULL, NULL, ds, NULL));
989566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
999566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
10014f150ffSMatthew G. Knepley   v->dmBC              = NULL;
101a8fb8f29SToby Isaac   v->coarseMesh        = NULL;
102f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
103cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
1049566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v, VECSTANDARD));
1059566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v, MATAIJ));
1064a7a4c06SLawrence Mitchell 
1071411c6eeSJed Brown   *dm = v;
1083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
109a4121054SBarry Smith }
110a4121054SBarry Smith 
11138221697SMatthew G. Knepley /*@
112bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
11338221697SMatthew G. Knepley 
114d083f849SBarry Smith   Collective
11538221697SMatthew G. Knepley 
11638221697SMatthew G. Knepley   Input Parameter:
117bb7acecfSBarry Smith . dm - The original `DM` object
11838221697SMatthew G. Knepley 
11938221697SMatthew G. Knepley   Output Parameter:
120bb7acecfSBarry Smith . newdm - The new `DM` object
12138221697SMatthew G. Knepley 
12238221697SMatthew G. Knepley   Level: beginner
12338221697SMatthew G. Knepley 
1241cb8cacdSPatrick Sanan   Notes:
125bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
126bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
127bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1281bb6d2a8SBarry Smith 
129bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
13089706ed2SPatrick Sanan 
131bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
132bb7acecfSBarry Smith 
13360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMConvert()`
13438221697SMatthew G. Knepley @*/
135d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm)
136d71ae5a4SJacob Faibussowitsch {
13738221697SMatthew G. Knepley   PetscSF  sf;
13838221697SMatthew G. Knepley   Vec      coords;
13938221697SMatthew G. Knepley   void    *ctx;
1406858538eSMatthew G. Knepley   PetscInt dim, cdim, i;
14138221697SMatthew G. Knepley 
14238221697SMatthew G. Knepley   PetscFunctionBegin;
14338221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1444f572ea9SToby Isaac   PetscAssertPointer(newdm, 2);
1459566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1469566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
147ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
148ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
149c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
150fc214432SJed Brown   (*newdm)->prealloc_skip = dm->prealloc_skip;
1519566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1529566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1539566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1549566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1559566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1569566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
157dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1583f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1599566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1609566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1619566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1629566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
1636858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1646858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
165be4c1c3eSMatthew G. Knepley       DM           ncdm;
166be4c1c3eSMatthew G. Knepley       PetscSection cs;
1675a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
168be4c1c3eSMatthew G. Knepley 
1696858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1709566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
171712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1725a0206caSToby Isaac       if (pEndMax >= 0) {
1736858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1746858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1759566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1766858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1776858538eSMatthew G. Knepley         else PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1789566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
179be4c1c3eSMatthew G. Knepley       }
180be4c1c3eSMatthew G. Knepley     }
1816858538eSMatthew G. Knepley   }
1829566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1839566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1849566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
18538221697SMatthew G. Knepley   if (coords) {
1869566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
18738221697SMatthew G. Knepley   } else {
1889566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1899566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
19038221697SMatthew G. Knepley   }
1916858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
1926858538eSMatthew G. Knepley   if (coords) {
1936858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
1946858538eSMatthew G. Knepley   } else {
1956858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
1966858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
1976858538eSMatthew G. Knepley   }
19890b157c4SStefano Zampini   {
1994fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
2006858538eSMatthew G. Knepley 
2014fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
2024fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L));
203c6b900c6SMatthew G. Knepley   }
20434aa8a36SMatthew G. Knepley   {
20534aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
20634aa8a36SMatthew G. Knepley 
2079566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2089566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
20934aa8a36SMatthew G. Knepley   }
2103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21138221697SMatthew G. Knepley }
21238221697SMatthew G. Knepley 
2139a42bb27SBarry Smith /*@C
214bb7acecfSBarry Smith   DMSetVecType - Sets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2159a42bb27SBarry Smith 
21620f4b53cSBarry Smith   Logically Collective
2179a42bb27SBarry Smith 
218147403d9SBarry Smith   Input Parameters:
21932546409SMatthew G. Knepley + dm    - initial distributed array
220bb7acecfSBarry Smith - ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2219a42bb27SBarry Smith 
22220f4b53cSBarry Smith   Options Database Key:
223147403d9SBarry Smith . -dm_vec_type ctype - the type of vector to create
2249a42bb27SBarry Smith 
2259a42bb27SBarry Smith   Level: intermediate
2269a42bb27SBarry Smith 
22760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
228bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2299a42bb27SBarry Smith @*/
23032546409SMatthew G. Knepley PetscErrorCode DMSetVecType(DM dm, VecType ctype)
231d71ae5a4SJacob Faibussowitsch {
23232546409SMatthew G. Knepley   char *tmp;
23332546409SMatthew G. Knepley 
2349a42bb27SBarry Smith   PetscFunctionBegin;
23532546409SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23632546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
23732546409SMatthew G. Knepley   tmp = (char *)dm->vectype;
23832546409SMatthew G. Knepley   PetscCall(PetscStrallocpy(ctype, (char **)&dm->vectype));
23932546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
2403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2419a42bb27SBarry Smith }
2429a42bb27SBarry Smith 
243c0dedaeaSBarry Smith /*@C
244bb7acecfSBarry Smith   DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
245c0dedaeaSBarry Smith 
24620f4b53cSBarry Smith   Logically Collective
247c0dedaeaSBarry Smith 
248c0dedaeaSBarry Smith   Input Parameter:
249c0dedaeaSBarry Smith . da - initial distributed array
250c0dedaeaSBarry Smith 
251c0dedaeaSBarry Smith   Output Parameter:
252c0dedaeaSBarry Smith . ctype - the vector type
253c0dedaeaSBarry Smith 
254c0dedaeaSBarry Smith   Level: intermediate
255c0dedaeaSBarry Smith 
25660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
257c0dedaeaSBarry Smith @*/
258d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
259d71ae5a4SJacob Faibussowitsch {
260c0dedaeaSBarry Smith   PetscFunctionBegin;
261c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
262c0dedaeaSBarry Smith   *ctype = da->vectype;
2633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
264c0dedaeaSBarry Smith }
265c0dedaeaSBarry Smith 
2665f1ad066SMatthew G Knepley /*@
267bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2685f1ad066SMatthew G Knepley 
26920f4b53cSBarry Smith   Not Collective
2705f1ad066SMatthew G Knepley 
2715f1ad066SMatthew G Knepley   Input Parameter:
272bb7acecfSBarry Smith . v - The `Vec`
2735f1ad066SMatthew G Knepley 
2745f1ad066SMatthew G Knepley   Output Parameter:
275bb7acecfSBarry Smith . dm - The `DM`
2765f1ad066SMatthew G Knepley 
2775f1ad066SMatthew G Knepley   Level: intermediate
2785f1ad066SMatthew G Knepley 
279bb7acecfSBarry Smith   Note:
280bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
281bb7acecfSBarry Smith 
2821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2835f1ad066SMatthew G Knepley @*/
284d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
285d71ae5a4SJacob Faibussowitsch {
2865f1ad066SMatthew G Knepley   PetscFunctionBegin;
2875f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2884f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
2899566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
2903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2915f1ad066SMatthew G Knepley }
2925f1ad066SMatthew G Knepley 
2935f1ad066SMatthew G Knepley /*@
294bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
2955f1ad066SMatthew G Knepley 
29620f4b53cSBarry Smith   Not Collective
2975f1ad066SMatthew G Knepley 
2985f1ad066SMatthew G Knepley   Input Parameters:
299bb7acecfSBarry Smith + v  - The `Vec`
300bb7acecfSBarry Smith - dm - The `DM`
3015f1ad066SMatthew G Knepley 
30220f4b53cSBarry Smith   Level: developer
30320f4b53cSBarry Smith 
304bb7acecfSBarry Smith   Note:
305bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
306d9805387SMatthew G. Knepley 
307bb7acecfSBarry 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.
308bb7acecfSBarry Smith 
3091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3105f1ad066SMatthew G Knepley @*/
311d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
312d71ae5a4SJacob Faibussowitsch {
3135f1ad066SMatthew G Knepley   PetscFunctionBegin;
3145f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
315d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3169566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3185f1ad066SMatthew G Knepley }
3195f1ad066SMatthew G Knepley 
320521d9a4cSLisandro Dalcin /*@C
321bb7acecfSBarry Smith   DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3228f1509bcSBarry Smith 
32320f4b53cSBarry Smith   Logically Collective
3248f1509bcSBarry Smith 
3258f1509bcSBarry Smith   Input Parameters:
326bb7acecfSBarry Smith + dm    - the `DM` context
3278f1509bcSBarry Smith - ctype - the matrix type
3288f1509bcSBarry Smith 
32920f4b53cSBarry Smith   Options Database Key:
3308f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3318f1509bcSBarry Smith 
3328f1509bcSBarry Smith   Level: intermediate
3338f1509bcSBarry Smith 
3341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
335bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3368f1509bcSBarry Smith @*/
337d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
338d71ae5a4SJacob Faibussowitsch {
3398f1509bcSBarry Smith   PetscFunctionBegin;
3408f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3418f1509bcSBarry Smith   dm->coloringtype = ctype;
3423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3438f1509bcSBarry Smith }
3448f1509bcSBarry Smith 
3458f1509bcSBarry Smith /*@C
346bb7acecfSBarry Smith   DMGetISColoringType - Gets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
347521d9a4cSLisandro Dalcin 
34820f4b53cSBarry Smith   Logically Collective
349521d9a4cSLisandro Dalcin 
350521d9a4cSLisandro Dalcin   Input Parameter:
351bb7acecfSBarry Smith . dm - the `DM` context
3528f1509bcSBarry Smith 
3538f1509bcSBarry Smith   Output Parameter:
3548f1509bcSBarry Smith . ctype - the matrix type
3558f1509bcSBarry Smith 
35620f4b53cSBarry Smith   Options Database Key:
3578f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3588f1509bcSBarry Smith 
3598f1509bcSBarry Smith   Level: intermediate
3608f1509bcSBarry Smith 
3611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
36242747ad1SJacob Faibussowitsch           `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3638f1509bcSBarry Smith @*/
364d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
365d71ae5a4SJacob Faibussowitsch {
3668f1509bcSBarry Smith   PetscFunctionBegin;
3678f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3688f1509bcSBarry Smith   *ctype = dm->coloringtype;
3693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3708f1509bcSBarry Smith }
3718f1509bcSBarry Smith 
3728f1509bcSBarry Smith /*@C
373bb7acecfSBarry Smith   DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3748f1509bcSBarry Smith 
37520f4b53cSBarry Smith   Logically Collective
3768f1509bcSBarry Smith 
3778f1509bcSBarry Smith   Input Parameters:
378bb7acecfSBarry Smith + dm    - the `DM` context
379bb7acecfSBarry Smith - ctype - the matrix type, for example `MATMPIAIJ`
380521d9a4cSLisandro Dalcin 
38120f4b53cSBarry Smith   Options Database Key:
382bb7acecfSBarry Smith . -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
383521d9a4cSLisandro Dalcin 
384521d9a4cSLisandro Dalcin   Level: intermediate
385521d9a4cSLisandro Dalcin 
38642747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
387521d9a4cSLisandro Dalcin @*/
388d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
389d71ae5a4SJacob Faibussowitsch {
39032546409SMatthew G. Knepley   char *tmp;
39132546409SMatthew G. Knepley 
392521d9a4cSLisandro Dalcin   PetscFunctionBegin;
393521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39432546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
39532546409SMatthew G. Knepley   tmp = (char *)dm->mattype;
3969566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
39732546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
3983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
399521d9a4cSLisandro Dalcin }
400521d9a4cSLisandro Dalcin 
401c0dedaeaSBarry Smith /*@C
40220f4b53cSBarry Smith   DMGetMatType - Gets the type of matrix that would be created with `DMCreateMatrix()`
403c0dedaeaSBarry Smith 
40420f4b53cSBarry Smith   Logically Collective
405c0dedaeaSBarry Smith 
406c0dedaeaSBarry Smith   Input Parameter:
407bb7acecfSBarry Smith . dm - the `DM` context
408c0dedaeaSBarry Smith 
409c0dedaeaSBarry Smith   Output Parameter:
410c0dedaeaSBarry Smith . ctype - the matrix type
411c0dedaeaSBarry Smith 
412c0dedaeaSBarry Smith   Level: intermediate
413c0dedaeaSBarry Smith 
41442747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`
415c0dedaeaSBarry Smith @*/
416d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
417d71ae5a4SJacob Faibussowitsch {
418c0dedaeaSBarry Smith   PetscFunctionBegin;
419c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
420c0dedaeaSBarry Smith   *ctype = dm->mattype;
4213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
422c0dedaeaSBarry Smith }
423c0dedaeaSBarry Smith 
424c688c046SMatthew G Knepley /*@
425bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
426c688c046SMatthew G Knepley 
42720f4b53cSBarry Smith   Not Collective
428c688c046SMatthew G Knepley 
429c688c046SMatthew G Knepley   Input Parameter:
430bb7acecfSBarry Smith . A - The `Mat`
431c688c046SMatthew G Knepley 
432c688c046SMatthew G Knepley   Output Parameter:
433bb7acecfSBarry Smith . dm - The `DM`
434c688c046SMatthew G Knepley 
435c688c046SMatthew G Knepley   Level: intermediate
436c688c046SMatthew G Knepley 
437bb7acecfSBarry Smith   Note:
438bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
439bb7acecfSBarry Smith 
44060225df5SJacob Faibussowitsch   Developer Notes:
441bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4428f1509bcSBarry Smith 
4431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
444c688c046SMatthew G Knepley @*/
445d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
446d71ae5a4SJacob Faibussowitsch {
447c688c046SMatthew G Knepley   PetscFunctionBegin;
448c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4494f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
4509566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
4513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
452c688c046SMatthew G Knepley }
453c688c046SMatthew G Knepley 
454c688c046SMatthew G Knepley /*@
455bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
456c688c046SMatthew G Knepley 
45720f4b53cSBarry Smith   Not Collective
458c688c046SMatthew G Knepley 
459c688c046SMatthew G Knepley   Input Parameters:
46020f4b53cSBarry Smith + A  - The `Mat`
46120f4b53cSBarry Smith - dm - The `DM`
462c688c046SMatthew G Knepley 
463bb7acecfSBarry Smith   Level: developer
464c688c046SMatthew G Knepley 
465bb7acecfSBarry Smith   Note:
466bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
467bb7acecfSBarry Smith 
46860225df5SJacob Faibussowitsch   Developer Notes:
469bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
470bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4718f1509bcSBarry Smith 
4721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
473c688c046SMatthew G Knepley @*/
474d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
475d71ae5a4SJacob Faibussowitsch {
476c688c046SMatthew G Knepley   PetscFunctionBegin;
477c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4788865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4799566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
4803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
481c688c046SMatthew G Knepley }
482c688c046SMatthew G Knepley 
4839a42bb27SBarry Smith /*@C
484bb7acecfSBarry Smith   DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4859a42bb27SBarry Smith 
48620f4b53cSBarry Smith   Logically Collective
4879a42bb27SBarry Smith 
488d8d19677SJose E. Roman   Input Parameters:
48960225df5SJacob Faibussowitsch + dm     - the `DM` context
490bb7acecfSBarry Smith - prefix - the prefix to prepend
4919a42bb27SBarry Smith 
49220f4b53cSBarry Smith   Level: advanced
49320f4b53cSBarry Smith 
49420f4b53cSBarry Smith   Note:
4959a42bb27SBarry Smith   A hyphen (-) must NOT be given at the beginning of the prefix name.
4969a42bb27SBarry Smith   The first character of all runtime options is AUTOMATICALLY the hyphen.
4979a42bb27SBarry Smith 
4981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
4999a42bb27SBarry Smith @*/
500d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
501d71ae5a4SJacob Faibussowitsch {
5029a42bb27SBarry Smith   PetscFunctionBegin;
5039a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5049566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
5051baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
5061baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
5073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5089a42bb27SBarry Smith }
5099a42bb27SBarry Smith 
51031697293SDave May /*@C
511da81f932SPierre Jolivet   DMAppendOptionsPrefix - Appends an additional string to an already existing prefix used for searching for
512bb7acecfSBarry Smith   `DM` options in the options database.
51331697293SDave May 
51420f4b53cSBarry Smith   Logically Collective
51531697293SDave May 
51631697293SDave May   Input Parameters:
517bb7acecfSBarry Smith + dm     - the `DM` context
518bb7acecfSBarry Smith - prefix - the string to append to the current prefix
51931697293SDave May 
52020f4b53cSBarry Smith   Level: advanced
52120f4b53cSBarry Smith 
52220f4b53cSBarry Smith   Note:
523bb7acecfSBarry 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.
52431697293SDave May   A hyphen (-) must NOT be given at the beginning of the prefix name.
52531697293SDave May   The first character of all runtime options is AUTOMATICALLY the hyphen.
52631697293SDave May 
5271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
52831697293SDave May @*/
529d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
530d71ae5a4SJacob Faibussowitsch {
53131697293SDave May   PetscFunctionBegin;
53231697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5339566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
5343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
53531697293SDave May }
53631697293SDave May 
53731697293SDave May /*@C
53831697293SDave May   DMGetOptionsPrefix - Gets the prefix used for searching for all
539bb7acecfSBarry Smith   DM options in the options database.
54031697293SDave May 
54131697293SDave May   Not Collective
54231697293SDave May 
5432fe279fdSBarry Smith   Input Parameter:
544bb7acecfSBarry Smith . dm - the `DM` context
54531697293SDave May 
5462fe279fdSBarry Smith   Output Parameter:
54731697293SDave May . prefix - pointer to the prefix string used is returned
54831697293SDave May 
54931697293SDave May   Level: advanced
55031697293SDave May 
55160225df5SJacob Faibussowitsch   Fortran Notes:
55220f4b53cSBarry Smith   Pass in a string 'prefix' of
55320f4b53cSBarry Smith   sufficient length to hold the prefix.
55420f4b53cSBarry Smith 
5551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
55631697293SDave May @*/
557d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
558d71ae5a4SJacob Faibussowitsch {
55931697293SDave May   PetscFunctionBegin;
56031697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5619566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
5623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
56331697293SDave May }
56431697293SDave May 
56562e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
566d71ae5a4SJacob Faibussowitsch {
5676eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
56888bdff64SToby Isaac 
56988bdff64SToby Isaac   PetscFunctionBegin;
570aab5bcd8SJed Brown   *ncrefct = 0;
57188bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
57288bdff64SToby Isaac     refct--;
57388bdff64SToby Isaac     if (recurseCoarse) {
57488bdff64SToby Isaac       PetscInt coarseCount;
57588bdff64SToby Isaac 
57662e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
57788bdff64SToby Isaac       refct += coarseCount;
57888bdff64SToby Isaac     }
57988bdff64SToby Isaac   }
58088bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
58188bdff64SToby Isaac     refct--;
58288bdff64SToby Isaac     if (recurseFine) {
58388bdff64SToby Isaac       PetscInt fineCount;
58488bdff64SToby Isaac 
58562e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
58688bdff64SToby Isaac       refct += fineCount;
58788bdff64SToby Isaac     }
58888bdff64SToby Isaac   }
58988bdff64SToby Isaac   *ncrefct = refct;
5903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59188bdff64SToby Isaac }
59288bdff64SToby Isaac 
59362e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */
59462e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct)
59562e5d2d2SJDBetteridge {
59662e5d2d2SJDBetteridge   PetscFunctionBegin;
59762e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct));
5983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59962e5d2d2SJDBetteridge }
60062e5d2d2SJDBetteridge 
601d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
602d71ae5a4SJacob Faibussowitsch {
6035d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
604354557abSToby Isaac 
605354557abSToby Isaac   PetscFunctionBegin;
606354557abSToby Isaac   /* destroy the labels */
607354557abSToby Isaac   while (next) {
608354557abSToby Isaac     DMLabelLink tmp = next->next;
609354557abSToby Isaac 
6105d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
611ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
6129566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
6139566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
614354557abSToby Isaac     next = tmp;
615354557abSToby Isaac   }
6165d80c0bfSVaclav Hapla   dm->labels = NULL;
6173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
618354557abSToby Isaac }
619354557abSToby Isaac 
62066976f2fSJacob Faibussowitsch static PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
621d71ae5a4SJacob Faibussowitsch {
6226858538eSMatthew G. Knepley   PetscFunctionBegin;
6236858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6246858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6256858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6266858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6276858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6296858538eSMatthew G. Knepley }
6306858538eSMatthew G. Knepley 
6311fb7b255SJunchao Zhang /*@C
632bb7acecfSBarry Smith   DMDestroy - Destroys a `DM`.
63347c6ae99SBarry Smith 
63420f4b53cSBarry Smith   Collective
63547c6ae99SBarry Smith 
63647c6ae99SBarry Smith   Input Parameter:
637bb7acecfSBarry Smith . dm - the `DM` object to destroy
63847c6ae99SBarry Smith 
63947c6ae99SBarry Smith   Level: developer
64047c6ae99SBarry Smith 
6411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
64247c6ae99SBarry Smith @*/
643d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
644d71ae5a4SJacob Faibussowitsch {
6456eb26441SStefano Zampini   PetscInt cnt;
64647c6ae99SBarry Smith 
64747c6ae99SBarry Smith   PetscFunctionBegin;
6483ba16761SJacob Faibussowitsch   if (!*dm) PetscFunctionReturn(PETSC_SUCCESS);
6496bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm), DM_CLASSID, 1);
65087e657c6SBarry Smith 
65188bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
65262e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
65388bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
6549371c9d4SSatish Balay   if (--cnt > 0) {
6559371c9d4SSatish Balay     *dm = NULL;
6563ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6579371c9d4SSatish Balay   }
6583ba16761SJacob Faibussowitsch   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(PETSC_SUCCESS);
6596bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6606eb26441SStefano Zampini 
6619566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6629566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
663974ca4ecSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(*dm));
664974ca4ecSStefano Zampini   PetscCall(DMClearNamedLocalVectors(*dm));
6652348bcf4SPeter Brune 
666b17ce1afSJed Brown   /* Destroy the list of hooks */
667c833c3b5SJed Brown   {
668c833c3b5SJed Brown     DMCoarsenHookLink link, next;
669b17ce1afSJed Brown     for (link = (*dm)->coarsenhook; link; link = next) {
670b17ce1afSJed Brown       next = link->next;
6719566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
672b17ce1afSJed Brown     }
6730298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
674c833c3b5SJed Brown   }
675c833c3b5SJed Brown   {
676c833c3b5SJed Brown     DMRefineHookLink link, next;
677c833c3b5SJed Brown     for (link = (*dm)->refinehook; link; link = next) {
678c833c3b5SJed Brown       next = link->next;
6799566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
680c833c3b5SJed Brown     }
6810298fd71SBarry Smith     (*dm)->refinehook = NULL;
682c833c3b5SJed Brown   }
683be081cd6SPeter Brune   {
684be081cd6SPeter Brune     DMSubDomainHookLink link, next;
685be081cd6SPeter Brune     for (link = (*dm)->subdomainhook; link; link = next) {
686be081cd6SPeter Brune       next = link->next;
6879566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
688be081cd6SPeter Brune     }
6890298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
690be081cd6SPeter Brune   }
691baf369e7SPeter Brune   {
692baf369e7SPeter Brune     DMGlobalToLocalHookLink link, next;
693baf369e7SPeter Brune     for (link = (*dm)->gtolhook; link; link = next) {
694baf369e7SPeter Brune       next = link->next;
6959566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
696baf369e7SPeter Brune     }
6970298fd71SBarry Smith     (*dm)->gtolhook = NULL;
698baf369e7SPeter Brune   }
699d4d07f1eSToby Isaac   {
700d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link, next;
701d4d07f1eSToby Isaac     for (link = (*dm)->ltoghook; link; link = next) {
702d4d07f1eSToby Isaac       next = link->next;
7039566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
704d4d07f1eSToby Isaac     }
705d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
706d4d07f1eSToby Isaac   }
707aa1993deSMatthew G Knepley   /* Destroy the work arrays */
708aa1993deSMatthew G Knepley   {
709aa1993deSMatthew G Knepley     DMWorkLink link, next;
710936381afSPierre Jolivet     PetscCheck(!(*dm)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out %p %p", (void *)(*dm)->workout, (void *)(*dm)->workout->mem);
711aa1993deSMatthew G Knepley     for (link = (*dm)->workin; link; link = next) {
712aa1993deSMatthew G Knepley       next = link->next;
7139566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
7149566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
715aa1993deSMatthew G Knepley     }
7160298fd71SBarry Smith     (*dm)->workin = NULL;
717aa1993deSMatthew G Knepley   }
718c58f1c22SToby Isaac   /* destroy the labels */
7199566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
720f4cdcedcSVaclav Hapla   /* destroy the fields */
7219566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
722f4cdcedcSVaclav Hapla   /* destroy the boundaries */
723e6f8dbb6SToby Isaac   {
724e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
725e6f8dbb6SToby Isaac     while (next) {
726e6f8dbb6SToby Isaac       DMBoundary b = next;
727e6f8dbb6SToby Isaac 
728e6f8dbb6SToby Isaac       next = b->next;
7299566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
730e6f8dbb6SToby Isaac     }
731e6f8dbb6SToby Isaac   }
732b17ce1afSJed Brown 
7339566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7349566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7359566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
73652536dc3SBarry Smith 
73748a46eb9SPierre Jolivet   if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7389566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7399566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7409566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7419566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
74288ed4aceSMatthew G Knepley 
7439566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7449566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
745*adc21957SMatthew G. Knepley   PetscCall(PetscFree((*dm)->reorderSectionType));
7469566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7479566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7489566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7499566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7509566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
75148a46eb9SPierre Jolivet   if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7529566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
753e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(*dm));
7549566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
75548a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7566eb26441SStefano Zampini 
7579566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
75848a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7599566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7604fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7619566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7629566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7636858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7646858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7659566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7669566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7679566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7686725e60dSJed Brown   PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local));
7696725e60dSJed Brown   PetscCall(VecDestroy(&(*dm)->periodic.affine));
7706636e97aSMatthew G Knepley 
7719566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7729566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
773e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7749566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
775732e2eb9SMatthew G Knepley 
776213acdd3SPierre Jolivet   PetscTryTypeMethod(*dm, destroy);
7779566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
778d2b2dc1eSMatthew G. Knepley   PetscCall(DMCeedDestroy(&(*dm)->dmceed));
779f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7809566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7819566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
782f918ec44SMatthew G. Knepley #endif
783435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7849566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
7853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78647c6ae99SBarry Smith }
78747c6ae99SBarry Smith 
788d7bf68aeSBarry Smith /*@
789bb7acecfSBarry Smith   DMSetUp - sets up the data structures inside a `DM` object
790d7bf68aeSBarry Smith 
79120f4b53cSBarry Smith   Collective
792d7bf68aeSBarry Smith 
793d7bf68aeSBarry Smith   Input Parameter:
794bb7acecfSBarry Smith . dm - the `DM` object to setup
795d7bf68aeSBarry Smith 
796bb7acecfSBarry Smith   Level: intermediate
797d7bf68aeSBarry Smith 
798bb7acecfSBarry Smith   Note:
799bb7acecfSBarry Smith   This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
800bb7acecfSBarry Smith 
8011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
802d7bf68aeSBarry Smith @*/
803d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
804d71ae5a4SJacob Faibussowitsch {
805d7bf68aeSBarry Smith   PetscFunctionBegin;
806171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8073ba16761SJacob Faibussowitsch   if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
808dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8098387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
8103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
811d7bf68aeSBarry Smith }
812d7bf68aeSBarry Smith 
813d7bf68aeSBarry Smith /*@
814bb7acecfSBarry Smith   DMSetFromOptions - sets parameters in a `DM` from the options database
815d7bf68aeSBarry Smith 
81620f4b53cSBarry Smith   Collective
817d7bf68aeSBarry Smith 
818d7bf68aeSBarry Smith   Input Parameter:
819bb7acecfSBarry Smith . dm - the `DM` object to set options for
820d7bf68aeSBarry Smith 
82120f4b53cSBarry Smith   Options Database Keys:
822bb7acecfSBarry Smith + -dm_preallocate_only                               - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
823bb7acecfSBarry Smith . -dm_vec_type <type>                                - type of vector to create inside `DM`
824bb7acecfSBarry Smith . -dm_mat_type <type>                                - type of matrix to create inside `DM`
825a4ea9b21SRichard Tran Mills . -dm_is_coloring_type                               - <global or local>
82620f4b53cSBarry 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`
82720f4b53cSBarry Smith . -dm_plex_filename <str>                            - File containing a mesh
8289318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>                   - File containing a mesh boundary
829cd7e8a5eSksagiyam . -dm_plex_name <str>                                - Name of the mesh in the file
8305dca41c3SJed Brown . -dm_plex_shape <shape>                             - The domain shape, such as `BOX`, `SPHERE`, etc.
8319318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                                 - Cell shape
8329318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool>              - Use a reference cell domain
8339318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                                 - Set the topological dimension
834bb7acecfSBarry Smith . -dm_plex_simplex <bool>                            - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
835bb7acecfSBarry Smith . -dm_plex_interpolate <bool>                        - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8369318fe57SMatthew G. Knepley . -dm_plex_scale <sc>                                - Scale factor for mesh coordinates
837be664eb1SMatthew G. Knepley . -dm_coord_remap <bool>                             - Map coordinates using a function
838be664eb1SMatthew G. Knepley . -dm_coord_map <mapname>                            - Select a builtin coordinate map
839be664eb1SMatthew G. Knepley . -dm_coord_map_params <p0,p1,p2,...>                - Set coordinate mapping parameters
8409318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>                         - Number of faces along each dimension
8419318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>                         - Specify lower-left-bottom coordinates for the box
8429318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>                         - Specify upper-right-top coordinates for the box
843bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>                         - Specify the `DMBoundaryType` for each direction
8449318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>                         - The sphere radius
8459318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>                           - Radius of the ball
8469318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>                          - Boundary type in the z direction
8479318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>                   - Number of wedges around the cylinder
848bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>                           - Reorder the mesh using the specified algorithm
8499318fe57SMatthew G. Knepley . -dm_refine_pre <n>                                 - The number of refinements before distribution
8509318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>                      - Flag for uniform refinement before distribution
8519318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>                    - The maximum cell volume after refinement before distribution
8529318fe57SMatthew G. Knepley . -dm_refine <n>                                     - The number of refinements after distribution
853bdf63967SMatthew G. Knepley . -dm_extrude <l>                                    - Activate extrusion and specify the number of layers to extrude
854d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
855d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
856d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
857d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
858d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
859909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells                     - Flag to create finite volume ghost cells on the boundary
860909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name>               - Label name for ghost cells boundary
8619318fe57SMatthew G. Knepley . -dm_distribute <bool>                              - Flag to redistribute a mesh among processes
8629318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>                         - The size of the overlap halo
8639318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>                           - Set adjacency direction
86420f4b53cSBarry Smith . -dm_plex_adj_closure <bool>                        - Set adjacency size
865d2b2dc1eSMatthew G. Knepley . -dm_plex_use_ceed <bool>                           - Use LibCEED as the FEM backend
86620f4b53cSBarry Smith . -dm_plex_check_symmetry                            - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
867bb7acecfSBarry Smith . -dm_plex_check_skeleton                            - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
868bb7acecfSBarry 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()`
869bb7acecfSBarry Smith . -dm_plex_check_geometry                            - Check that cells have positive volume - `DMPlexCheckGeometry()`
870bb7acecfSBarry Smith . -dm_plex_check_pointsf                             - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
871bb7acecfSBarry Smith . -dm_plex_check_interface_cones                     - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
872384a6580SVaclav Hapla - -dm_plex_check_all                                 - Perform all the checks above
873d7bf68aeSBarry Smith 
87495eb5ee5SVaclav Hapla   Level: intermediate
87595eb5ee5SVaclav Hapla 
8761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
877bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
87860225df5SJacob Faibussowitsch          `DMSetOptionsPrefix()`, `DMType`, `DMPLEX`, `DMDA`
879d7bf68aeSBarry Smith @*/
880d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
881d71ae5a4SJacob Faibussowitsch {
8827781c08eSBarry Smith   char      typeName[256];
883ca266f36SBarry Smith   PetscBool flg;
884d7bf68aeSBarry Smith 
885d7bf68aeSBarry Smith   PetscFunctionBegin;
886171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88749be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
8889566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
8899566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
890dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
891d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
8929566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
8939566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
8941baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
8959566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
8961baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
897863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
8989566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
8999566063dSJacob 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));
900dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
901f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
902dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
903d0609cedSBarry Smith   PetscOptionsEnd();
9043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
905d7bf68aeSBarry Smith }
906d7bf68aeSBarry Smith 
907fc9bc008SSatish Balay /*@C
908bb7acecfSBarry Smith   DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
909fe2efc57SMark 
91020f4b53cSBarry Smith   Collective
911fe2efc57SMark 
912fe2efc57SMark   Input Parameters:
913bb7acecfSBarry Smith + dm   - the `DM` object
91420f4b53cSBarry Smith . obj  - optional object that provides the prefix for the options database (if `NULL` then the prefix in obj is used)
91560225df5SJacob Faibussowitsch - name - option string that is used to activate viewing
916fe2efc57SMark 
917fe2efc57SMark   Level: intermediate
918bb7acecfSBarry Smith 
919bb7acecfSBarry Smith   Note:
920bb7acecfSBarry Smith   See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
921bb7acecfSBarry Smith 
92260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`
923fe2efc57SMark @*/
924d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
925d71ae5a4SJacob Faibussowitsch {
926fe2efc57SMark   PetscFunctionBegin;
927fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9289566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
930fe2efc57SMark }
931fe2efc57SMark 
932fe2efc57SMark /*@C
933bb7acecfSBarry 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
934bb7acecfSBarry Smith   save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
93547c6ae99SBarry Smith 
93620f4b53cSBarry Smith   Collective
93747c6ae99SBarry Smith 
938d8d19677SJose E. Roman   Input Parameters:
939bb7acecfSBarry Smith + dm - the `DM` object to view
94047c6ae99SBarry Smith - v  - the viewer
94147c6ae99SBarry Smith 
94220f4b53cSBarry Smith   Level: beginner
94320f4b53cSBarry Smith 
944cd7e8a5eSksagiyam   Notes:
945bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
946bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
947bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
948cd7e8a5eSksagiyam 
9491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
95047c6ae99SBarry Smith @*/
951d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
952d71ae5a4SJacob Faibussowitsch {
95332c0f0efSBarry Smith   PetscBool         isbinary;
95476a8abe0SBarry Smith   PetscMPIInt       size;
95576a8abe0SBarry Smith   PetscViewerFormat format;
95647c6ae99SBarry Smith 
95747c6ae99SBarry Smith   PetscFunctionBegin;
958171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
95948a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
960b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
96174903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
96274903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
96374903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
96474903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
96574903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
96674903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
96774903a4fSStefano Zampini      in an error here */
96874903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9699566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
970b1b135c8SBarry Smith 
9719566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9729566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
9733ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
9749566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9759566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
97632c0f0efSBarry Smith   if (isbinary) {
97755849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
97832c0f0efSBarry Smith     char     type[256];
97932c0f0efSBarry Smith 
9809566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
981c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
9829566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
98332c0f0efSBarry Smith   }
984dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
9853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
98647c6ae99SBarry Smith }
98747c6ae99SBarry Smith 
98847c6ae99SBarry Smith /*@
989bb7acecfSBarry 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,
990bb7acecfSBarry Smith   that is it has no ghost locations.
99147c6ae99SBarry Smith 
99220f4b53cSBarry Smith   Collective
99347c6ae99SBarry Smith 
99447c6ae99SBarry Smith   Input Parameter:
995bb7acecfSBarry Smith . dm - the `DM` object
99647c6ae99SBarry Smith 
99747c6ae99SBarry Smith   Output Parameter:
99847c6ae99SBarry Smith . vec - the global vector
99947c6ae99SBarry Smith 
1000073dac72SJed Brown   Level: beginner
100147c6ae99SBarry Smith 
10021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1003bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
100447c6ae99SBarry Smith @*/
1005d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1006d71ae5a4SJacob Faibussowitsch {
100747c6ae99SBarry Smith   PetscFunctionBegin;
1008171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10094f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1010dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
101176bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1012c6b011d8SStefano Zampini     DM vdm;
1013c6b011d8SStefano Zampini 
10149566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10157a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1016c6b011d8SStefano Zampini   }
10173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
101847c6ae99SBarry Smith }
101947c6ae99SBarry Smith 
102047c6ae99SBarry Smith /*@
1021bb7acecfSBarry Smith   DMCreateLocalVector - Creates a local vector from a `DM` object.
102247c6ae99SBarry Smith 
102347c6ae99SBarry Smith   Not Collective
102447c6ae99SBarry Smith 
102547c6ae99SBarry Smith   Input Parameter:
1026bb7acecfSBarry Smith . dm - the `DM` object
102747c6ae99SBarry Smith 
102847c6ae99SBarry Smith   Output Parameter:
102947c6ae99SBarry Smith . vec - the local vector
103047c6ae99SBarry Smith 
1031073dac72SJed Brown   Level: beginner
103247c6ae99SBarry Smith 
103320f4b53cSBarry Smith   Note:
1034bb7acecfSBarry 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.
1035bb7acecfSBarry Smith 
10361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1037bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
103847c6ae99SBarry Smith @*/
1039d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1040d71ae5a4SJacob Faibussowitsch {
104147c6ae99SBarry Smith   PetscFunctionBegin;
1042171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10434f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1044dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
104576bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1046c6b011d8SStefano Zampini     DM vdm;
1047c6b011d8SStefano Zampini 
10489566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10497a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1050c6b011d8SStefano Zampini   }
10513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
105247c6ae99SBarry Smith }
105347c6ae99SBarry Smith 
10541411c6eeSJed Brown /*@
1055bb7acecfSBarry Smith   DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10561411c6eeSJed Brown 
105720f4b53cSBarry Smith   Collective
10581411c6eeSJed Brown 
10591411c6eeSJed Brown   Input Parameter:
1060bb7acecfSBarry Smith . dm - the `DM` that provides the mapping
10611411c6eeSJed Brown 
10621411c6eeSJed Brown   Output Parameter:
10631411c6eeSJed Brown . ltog - the mapping
10641411c6eeSJed Brown 
1065bb7acecfSBarry Smith   Level: advanced
10661411c6eeSJed Brown 
10671411c6eeSJed Brown   Notes:
1068bb7acecfSBarry Smith   The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10691411c6eeSJed Brown 
1070bb7acecfSBarry Smith   Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1071bb7acecfSBarry Smith   need to use this function with those objects.
1072bb7acecfSBarry Smith 
1073bb7acecfSBarry Smith   This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1074bb7acecfSBarry Smith 
107560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1076bb7acecfSBarry Smith           `DMCreateMatrix()`
10771411c6eeSJed Brown @*/
1078d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1079d71ae5a4SJacob Faibussowitsch {
10800be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10811411c6eeSJed Brown 
10821411c6eeSJed Brown   PetscFunctionBegin;
10831411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10844f572ea9SToby Isaac   PetscAssertPointer(ltog, 2);
10851411c6eeSJed Brown   if (!dm->ltogmap) {
108637d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
108737d0c07bSMatthew G Knepley 
10889566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
108937d0c07bSMatthew G Knepley     if (section) {
1090a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
109137d0c07bSMatthew G Knepley       PetscInt       *ltog;
1092ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
109337d0c07bSMatthew G Knepley 
10949566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
10959566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
10969566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
10979566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
109837d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1099e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
110037d0c07bSMatthew G Knepley 
110137d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11029566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11039566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11049566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11059566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11061a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11071a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1108ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11095227eafbSStefano Zampini 
1110e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11115227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1112e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1113e6befd46SJed Brown             cind++;
1114e6befd46SJed Brown           } else {
11155227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1116e6befd46SJed Brown           }
111737d0c07bSMatthew G Knepley         }
111837d0c07bSMatthew G Knepley       }
1119bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11209371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11219371c9d4SSatish Balay       bsLocal[1] = bs;
11229566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11239371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11249371c9d4SSatish Balay         bs = 1;
11259371c9d4SSatish Balay       } else {
11269371c9d4SSatish Balay         bs = bsMinMax[0];
11279371c9d4SSatish Balay       }
11287591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11297591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1130ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1131ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1132ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1133ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1134ca469d19SJed Brown         }
1135ccf3bd66SMatthew G. Knepley         n /= bs;
1136ccf3bd66SMatthew G. Knepley       }
11379566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1138dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
113937d0c07bSMatthew G Knepley   }
11401411c6eeSJed Brown   *ltog = dm->ltogmap;
11413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11421411c6eeSJed Brown }
11431411c6eeSJed Brown 
11441411c6eeSJed Brown /*@
1145bb7acecfSBarry Smith   DMGetBlockSize - Gets the inherent block size associated with a `DM`
11461411c6eeSJed Brown 
11471411c6eeSJed Brown   Not Collective
11481411c6eeSJed Brown 
11491411c6eeSJed Brown   Input Parameter:
1150bb7acecfSBarry Smith . dm - the `DM` with block structure
11511411c6eeSJed Brown 
11521411c6eeSJed Brown   Output Parameter:
11531411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure
11541411c6eeSJed Brown 
11551411c6eeSJed Brown   Level: intermediate
11561411c6eeSJed Brown 
1157bb7acecfSBarry Smith   Note:
1158bb7acecfSBarry Smith   This might be the number of degrees of freedom at each grid point for a structured grid.
1159bb7acecfSBarry Smith 
1160bb7acecfSBarry Smith   Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1161bb7acecfSBarry Smith   rather different locations in the vectors may have a different block size.
1162bb7acecfSBarry Smith 
11631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11641411c6eeSJed Brown @*/
1165d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1166d71ae5a4SJacob Faibussowitsch {
11671411c6eeSJed Brown   PetscFunctionBegin;
11681411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11694f572ea9SToby Isaac   PetscAssertPointer(bs, 2);
11707a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11711411c6eeSJed Brown   *bs = dm->bs;
11723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11731411c6eeSJed Brown }
11741411c6eeSJed Brown 
117548eeb7c8SBarry Smith /*@C
1176bb7acecfSBarry Smith   DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1177bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
117847c6ae99SBarry Smith 
117920f4b53cSBarry Smith   Collective
118047c6ae99SBarry Smith 
1181d8d19677SJose E. Roman   Input Parameters:
1182bb7acecfSBarry Smith + dmc - the `DM` object
1183bb7acecfSBarry Smith - dmf - the second, finer `DM` object
118447c6ae99SBarry Smith 
1185d8d19677SJose E. Roman   Output Parameters:
118647c6ae99SBarry Smith + mat - the interpolation
1187bb7acecfSBarry Smith - vec - the scaling (optional), see `DMCreateInterpolationScale()`
118847c6ae99SBarry Smith 
118947c6ae99SBarry Smith   Level: developer
119047c6ae99SBarry Smith 
119195452b02SPatrick Sanan   Notes:
1192bb7acecfSBarry 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
1193bb7acecfSBarry Smith   DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1194d52bd9f3SBarry Smith 
1195bb7acecfSBarry Smith   For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1196bb7acecfSBarry Smith   vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
119785afcc9aSBarry Smith 
11981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
119947c6ae99SBarry Smith @*/
1200d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1201d71ae5a4SJacob Faibussowitsch {
120247c6ae99SBarry Smith   PetscFunctionBegin;
1203a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1204a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12054f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12069566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1207dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12089566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
121047c6ae99SBarry Smith }
121147c6ae99SBarry Smith 
12123ad4599aSBarry Smith /*@
1213a4e35b19SJacob Faibussowitsch   DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is
1214a4e35b19SJacob Faibussowitsch   the transpose of the interpolation between the `DM`.
12152ed6491fSPatrick Sanan 
12162ed6491fSPatrick Sanan   Input Parameters:
1217bb7acecfSBarry Smith + dac - `DM` that defines a coarse mesh
1218bb7acecfSBarry Smith . daf - `DM` that defines a fine mesh
12192ed6491fSPatrick Sanan - mat - the restriction (or interpolation operator) from fine to coarse
12202ed6491fSPatrick Sanan 
12212ed6491fSPatrick Sanan   Output Parameter:
12222ed6491fSPatrick Sanan . scale - the scaled vector
12232ed6491fSPatrick Sanan 
1224bb7acecfSBarry Smith   Level: advanced
12252ed6491fSPatrick Sanan 
1226a4e35b19SJacob Faibussowitsch   Notes:
1227a4e35b19SJacob Faibussowitsch   xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual)
1228a4e35b19SJacob Faibussowitsch   restriction. In other words xcoarse is the coarse representation of xfine.
1229a4e35b19SJacob Faibussowitsch 
123060225df5SJacob Faibussowitsch   Developer Notes:
1231bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1232e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1233e9c74fd6SRichard Tran Mills 
123460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, `DMCreateRestriction()`, `DMCreateGlobalVector()`
12352ed6491fSPatrick Sanan @*/
1236d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1237d71ae5a4SJacob Faibussowitsch {
12382ed6491fSPatrick Sanan   Vec         fine;
12392ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12409704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1241e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12429704db99SRichard Tran Mills #endif
12432ed6491fSPatrick Sanan 
12442ed6491fSPatrick Sanan   PetscFunctionBegin;
12459566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12469566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12479566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12489704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12499704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12509704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12519704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12529566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1253e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12549566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12559566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12569566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
125783aa49f4SRichard Tran Mills   }
12589704db99SRichard Tran Mills #endif
12599566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12609566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12619566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12632ed6491fSPatrick Sanan }
12642ed6491fSPatrick Sanan 
12652ed6491fSPatrick Sanan /*@
1266bb7acecfSBarry Smith   DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1267bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12683ad4599aSBarry Smith 
126920f4b53cSBarry Smith   Collective
12703ad4599aSBarry Smith 
1271d8d19677SJose E. Roman   Input Parameters:
1272bb7acecfSBarry Smith + dmc - the `DM` object
1273bb7acecfSBarry Smith - dmf - the second, finer `DM` object
12743ad4599aSBarry Smith 
12753ad4599aSBarry Smith   Output Parameter:
12763ad4599aSBarry Smith . mat - the restriction
12773ad4599aSBarry Smith 
12783ad4599aSBarry Smith   Level: developer
12793ad4599aSBarry Smith 
1280bb7acecfSBarry Smith   Note:
1281bb7acecfSBarry Smith   This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1282bb7acecfSBarry Smith   matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12833ad4599aSBarry Smith 
12841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12853ad4599aSBarry Smith @*/
1286d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1287d71ae5a4SJacob Faibussowitsch {
12883ad4599aSBarry Smith   PetscFunctionBegin;
1289a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1290a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12914f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12929566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1293dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
12949566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
12953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12963ad4599aSBarry Smith }
12973ad4599aSBarry Smith 
129847c6ae99SBarry Smith /*@
1299a4e35b19SJacob Faibussowitsch   DMCreateInjection - Gets injection matrix between two `DM` objects.
130047c6ae99SBarry Smith 
130120f4b53cSBarry Smith   Collective
130247c6ae99SBarry Smith 
1303d8d19677SJose E. Roman   Input Parameters:
1304bb7acecfSBarry Smith + dac - the `DM` object
1305bb7acecfSBarry Smith - daf - the second, finer `DM` object
130647c6ae99SBarry Smith 
130747c6ae99SBarry Smith   Output Parameter:
13086dbf9973SLawrence Mitchell . mat - the injection
130947c6ae99SBarry Smith 
131047c6ae99SBarry Smith   Level: developer
131147c6ae99SBarry Smith 
1312a4e35b19SJacob Faibussowitsch   Notes:
1313a4e35b19SJacob Faibussowitsch   This is an operator that applied to a vector obtained with `DMCreateGlobalVector()` on the
1314a4e35b19SJacob Faibussowitsch   fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting
1315a4e35b19SJacob Faibussowitsch   the values on the coarse grid points. This compares to the operator obtained by
1316a4e35b19SJacob Faibussowitsch   `DMCreateRestriction()` or the transpose of the operator obtained by
1317a4e35b19SJacob Faibussowitsch   `DMCreateInterpolation()` that uses a "local weighted average" of the values around the
1318a4e35b19SJacob Faibussowitsch   coarse grid point as the coarse grid value.
1319a4e35b19SJacob Faibussowitsch 
1320bb7acecfSBarry 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
1321bb7acecfSBarry Smith   `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
132285afcc9aSBarry Smith 
13231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1324bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
132547c6ae99SBarry Smith @*/
1326d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1327d71ae5a4SJacob Faibussowitsch {
132847c6ae99SBarry Smith   PetscFunctionBegin;
1329a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1330a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13314f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13329566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1333dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13349566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
133647c6ae99SBarry Smith }
133747c6ae99SBarry Smith 
1338b412c318SBarry Smith /*@
1339bb7acecfSBarry 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
1340bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1341bd041c0cSMatthew G. Knepley 
134220f4b53cSBarry Smith   Collective
1343bd041c0cSMatthew G. Knepley 
1344d8d19677SJose E. Roman   Input Parameters:
1345bb7acecfSBarry Smith + dmc - the target `DM` object
1346bb7acecfSBarry Smith - dmf - the source `DM` object
1347bd041c0cSMatthew G. Knepley 
1348bd041c0cSMatthew G. Knepley   Output Parameter:
1349b4937a87SMatthew G. Knepley . mat - the mass matrix
1350bd041c0cSMatthew G. Knepley 
1351bd041c0cSMatthew G. Knepley   Level: developer
1352bd041c0cSMatthew G. Knepley 
1353bb7acecfSBarry Smith   Notes:
1354bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1355bb7acecfSBarry Smith 
135620f4b53cSBarry 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()`
1357bb7acecfSBarry Smith 
135842747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1359bd041c0cSMatthew G. Knepley @*/
1360d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1361d71ae5a4SJacob Faibussowitsch {
1362bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1363b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1364b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13654f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13665b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1367dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13685b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
13693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1370b4937a87SMatthew G. Knepley }
1371b4937a87SMatthew G. Knepley 
1372b4937a87SMatthew G. Knepley /*@
1373bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1374b4937a87SMatthew G. Knepley 
137520f4b53cSBarry Smith   Collective
1376b4937a87SMatthew G. Knepley 
1377b4937a87SMatthew G. Knepley   Input Parameter:
1378bb7acecfSBarry Smith . dm - the `DM` object
1379b4937a87SMatthew G. Knepley 
1380b4937a87SMatthew G. Knepley   Output Parameter:
1381bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1382b4937a87SMatthew G. Knepley 
1383b4937a87SMatthew G. Knepley   Level: developer
1384b4937a87SMatthew G. Knepley 
1385bb7acecfSBarry Smith   Note:
1386bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1387bb7acecfSBarry Smith 
138860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1389b4937a87SMatthew G. Knepley @*/
1390d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1391d71ae5a4SJacob Faibussowitsch {
1392b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1393b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13944f572ea9SToby Isaac   PetscAssertPointer(lm, 2);
1395dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
13963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1397bd041c0cSMatthew G. Knepley }
1398bd041c0cSMatthew G. Knepley 
1399bd041c0cSMatthew G. Knepley /*@
1400bb7acecfSBarry Smith   DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1401bb7acecfSBarry Smith   of a PDE on the `DM`.
140247c6ae99SBarry Smith 
140320f4b53cSBarry Smith   Collective
140447c6ae99SBarry Smith 
1405d8d19677SJose E. Roman   Input Parameters:
1406bb7acecfSBarry Smith + dm    - the `DM` object
1407bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
140847c6ae99SBarry Smith 
140947c6ae99SBarry Smith   Output Parameter:
141047c6ae99SBarry Smith . coloring - the coloring
141147c6ae99SBarry Smith 
14121bf8429eSBarry Smith   Level: developer
14131bf8429eSBarry Smith 
1414ec5066bdSBarry Smith   Notes:
1415bb7acecfSBarry 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
1416bb7acecfSBarry Smith   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1417ec5066bdSBarry Smith 
1418bb7acecfSBarry Smith   This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14191bf8429eSBarry 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,
14201bf8429eSBarry Smith   otherwise an error will be generated.
1421ec5066bdSBarry Smith 
14221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1423aab9d709SJed Brown @*/
1424d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1425d71ae5a4SJacob Faibussowitsch {
142647c6ae99SBarry Smith   PetscFunctionBegin;
1427171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14284f572ea9SToby Isaac   PetscAssertPointer(coloring, 3);
1429dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
143147c6ae99SBarry Smith }
143247c6ae99SBarry Smith 
1433b412c318SBarry Smith /*@
1434bb7acecfSBarry Smith   DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
143547c6ae99SBarry Smith 
143620f4b53cSBarry Smith   Collective
143747c6ae99SBarry Smith 
143847c6ae99SBarry Smith   Input Parameter:
1439bb7acecfSBarry Smith . dm - the `DM` object
144047c6ae99SBarry Smith 
144147c6ae99SBarry Smith   Output Parameter:
144247c6ae99SBarry Smith . mat - the empty Jacobian
144347c6ae99SBarry Smith 
144420f4b53cSBarry Smith   Options Database Key:
1445bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1446f27dd7c6SMatthew G. Knepley 
144720f4b53cSBarry Smith   Level: beginner
144820f4b53cSBarry Smith 
144995452b02SPatrick Sanan   Notes:
145095452b02SPatrick Sanan   This properly preallocates the number of nonzeros in the sparse matrix so you
145194013140SBarry Smith   do not need to do it yourself.
145294013140SBarry Smith 
145394013140SBarry Smith   By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1454bb7acecfSBarry Smith   the nonzero pattern call `DMSetMatrixPreallocateOnly()`
145594013140SBarry Smith 
1456bb7acecfSBarry Smith   For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
145794013140SBarry Smith   internally by PETSc.
145894013140SBarry Smith 
1459bb7acecfSBarry Smith   For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1460bb7acecfSBarry Smith   `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
146194013140SBarry Smith 
14621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1463aab9d709SJed Brown @*/
1464d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1465d71ae5a4SJacob Faibussowitsch {
146647c6ae99SBarry Smith   PetscFunctionBegin;
1467171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14684f572ea9SToby Isaac   PetscAssertPointer(mat, 2);
14699566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14709566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1471dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
147276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1473c6b011d8SStefano Zampini     DM mdm;
1474c6b011d8SStefano Zampini 
14759566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14767a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1477c6b011d8SStefano Zampini   }
1478e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1479e5e52638SMatthew G. Knepley   if (dm->Nf) {
1480e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1481649ef022SMatthew Knepley     PetscInt     Nf, f;
1482e571a35bSMatthew G. Knepley 
14839566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1484649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1485649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14869566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14879566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14889566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1489649ef022SMatthew Knepley         break;
1490e571a35bSMatthew G. Knepley       }
1491649ef022SMatthew Knepley     }
1492649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1493649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
14949566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
14959566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
14969566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1497e571a35bSMatthew G. Knepley       }
1498e571a35bSMatthew G. Knepley     }
1499e571a35bSMatthew G. Knepley   }
15009566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
150247c6ae99SBarry Smith }
150347c6ae99SBarry Smith 
1504732e2eb9SMatthew G Knepley /*@
1505a4e35b19SJacob Faibussowitsch   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and
1506a4e35b19SJacob Faibussowitsch   `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the
1507a4e35b19SJacob Faibussowitsch   matrices will not be preallocated.
1508aa0f6e3cSJed Brown 
150920f4b53cSBarry Smith   Logically Collective
1510aa0f6e3cSJed Brown 
1511aa0f6e3cSJed Brown   Input Parameters:
1512bb7acecfSBarry Smith + dm   - the `DM`
1513bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1514aa0f6e3cSJed Brown 
1515aa0f6e3cSJed Brown   Level: developer
1516aa0f6e3cSJed Brown 
1517a4e35b19SJacob Faibussowitsch   Notes:
1518a4e35b19SJacob Faibussowitsch   This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and
1519a4e35b19SJacob Faibussowitsch   `MatSetValuesCOO()` will be used.
1520a4e35b19SJacob Faibussowitsch 
15211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1522aa0f6e3cSJed Brown @*/
1523d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1524d71ae5a4SJacob Faibussowitsch {
1525aa0f6e3cSJed Brown   PetscFunctionBegin;
1526aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1527aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1529aa0f6e3cSJed Brown }
1530aa0f6e3cSJed Brown 
1531aa0f6e3cSJed Brown /*@
1532bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1533732e2eb9SMatthew G Knepley   preallocated but the nonzero structure and zero values will not be set.
1534732e2eb9SMatthew G Knepley 
153520f4b53cSBarry Smith   Logically Collective
1536732e2eb9SMatthew G Knepley 
1537d8d19677SJose E. Roman   Input Parameters:
1538bb7acecfSBarry Smith + dm   - the `DM`
1539bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1540732e2eb9SMatthew G Knepley 
154120f4b53cSBarry Smith   Options Database Key:
1542bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1543f27dd7c6SMatthew G. Knepley 
154420f4b53cSBarry Smith   Level: developer
154520f4b53cSBarry Smith 
15461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1547732e2eb9SMatthew G Knepley @*/
1548d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1549d71ae5a4SJacob Faibussowitsch {
1550732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1551732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1552732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1554732e2eb9SMatthew G Knepley }
1555732e2eb9SMatthew G Knepley 
1556b06ff27eSHong Zhang /*@
1557bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1558bb7acecfSBarry Smith   but the array for numerical values will not be allocated.
1559b06ff27eSHong Zhang 
156020f4b53cSBarry Smith   Logically Collective
1561b06ff27eSHong Zhang 
1562d8d19677SJose E. Roman   Input Parameters:
1563bb7acecfSBarry Smith + dm   - the `DM`
1564da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1565b06ff27eSHong Zhang 
1566b06ff27eSHong Zhang   Level: developer
1567bb7acecfSBarry Smith 
15681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1569b06ff27eSHong Zhang @*/
1570d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1571d71ae5a4SJacob Faibussowitsch {
1572b06ff27eSHong Zhang   PetscFunctionBegin;
1573b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1574b06ff27eSHong Zhang   dm->structure_only = only;
15753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1576b06ff27eSHong Zhang }
1577b06ff27eSHong Zhang 
1578863027abSJed Brown /*@
1579863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1580863027abSJed Brown 
158120f4b53cSBarry Smith   Logically Collective
1582863027abSJed Brown 
1583863027abSJed Brown   Input Parameters:
1584863027abSJed Brown + dm    - the `DM`
1585863027abSJed Brown - btype - block by topological point or field node
1586863027abSJed Brown 
158720f4b53cSBarry Smith   Options Database Key:
15880e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1589863027abSJed Brown 
159020f4b53cSBarry Smith   Level: advanced
159120f4b53cSBarry Smith 
15921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1593863027abSJed Brown @*/
1594863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1595863027abSJed Brown {
1596863027abSJed Brown   PetscFunctionBegin;
1597863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1598863027abSJed Brown   dm->blocking_type = btype;
15993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1600863027abSJed Brown }
1601863027abSJed Brown 
1602863027abSJed Brown /*@
1603863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1604863027abSJed Brown 
1605863027abSJed Brown   Not Collective
1606863027abSJed Brown 
16072fe279fdSBarry Smith   Input Parameter:
1608863027abSJed Brown . dm - the `DM`
1609863027abSJed Brown 
16102fe279fdSBarry Smith   Output Parameter:
1611863027abSJed Brown . btype - block by topological point or field node
1612863027abSJed Brown 
1613863027abSJed Brown   Level: advanced
1614863027abSJed Brown 
16151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1616863027abSJed Brown @*/
1617863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1618863027abSJed Brown {
1619863027abSJed Brown   PetscFunctionBegin;
1620863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16214f572ea9SToby Isaac   PetscAssertPointer(btype, 2);
1622863027abSJed Brown   *btype = dm->blocking_type;
16233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1624863027abSJed Brown }
1625863027abSJed Brown 
1626a89ea682SMatthew G Knepley /*@C
1627bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1628a89ea682SMatthew G Knepley 
1629a89ea682SMatthew G Knepley   Not Collective
1630a89ea682SMatthew G Knepley 
1631a89ea682SMatthew G Knepley   Input Parameters:
1632bb7acecfSBarry Smith + dm    - the `DM` object
1633a5b23f4aSJose E. Roman . count - The minimum size
163420f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1635a89ea682SMatthew G Knepley 
1636a89ea682SMatthew G Knepley   Output Parameter:
163760225df5SJacob Faibussowitsch . mem - the work array
1638a89ea682SMatthew G Knepley 
1639a89ea682SMatthew G Knepley   Level: developer
1640a89ea682SMatthew G Knepley 
1641bb7acecfSBarry Smith   Note:
1642da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1643bb7acecfSBarry Smith 
1644bb7acecfSBarry Smith   The array may contain nonzero values
1645bb7acecfSBarry Smith 
16461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1647a89ea682SMatthew G Knepley @*/
1648d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1649d71ae5a4SJacob Faibussowitsch {
1650aa1993deSMatthew G Knepley   DMWorkLink  link;
165169291d52SBarry Smith   PetscMPIInt dsize;
1652a89ea682SMatthew G Knepley 
1653a89ea682SMatthew G Knepley   PetscFunctionBegin;
1654a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16554f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1656442f3b32SStefano Zampini   if (!count) {
1657442f3b32SStefano Zampini     *(void **)mem = NULL;
1658442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1659442f3b32SStefano Zampini   }
1660aa1993deSMatthew G Knepley   if (dm->workin) {
1661aa1993deSMatthew G Knepley     link       = dm->workin;
1662aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1663aa1993deSMatthew G Knepley   } else {
16644dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1665a89ea682SMatthew G Knepley   }
1666b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1667b77fa653SStefano Zampini      Get size directly */
1668b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1669b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1670b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1671b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1672b77fa653SStefano Zampini #endif
1673b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1674b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1675b77fa653SStefano Zampini #endif
1676b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1677b77fa653SStefano Zampini 
16785056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16799566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16809566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1681854ce69bSBarry Smith     link->bytes = dsize * count;
1682aa1993deSMatthew G Knepley   }
1683aa1993deSMatthew G Knepley   link->next  = dm->workout;
1684aa1993deSMatthew G Knepley   dm->workout = link;
1685cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
168600d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
168700d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
168800d952a4SJed Brown #endif
1689aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
16903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1691a89ea682SMatthew G Knepley }
1692a89ea682SMatthew G Knepley 
1693aa1993deSMatthew G Knepley /*@C
1694bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1695aa1993deSMatthew G Knepley 
1696aa1993deSMatthew G Knepley   Not Collective
1697aa1993deSMatthew G Knepley 
1698aa1993deSMatthew G Knepley   Input Parameters:
1699bb7acecfSBarry Smith + dm    - the `DM` object
1700a5b23f4aSJose E. Roman . count - The minimum size
170120f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1702aa1993deSMatthew G Knepley 
1703aa1993deSMatthew G Knepley   Output Parameter:
170460225df5SJacob Faibussowitsch . mem - the work array
1705aa1993deSMatthew G Knepley 
1706aa1993deSMatthew G Knepley   Level: developer
1707aa1993deSMatthew G Knepley 
170860225df5SJacob Faibussowitsch   Developer Notes:
1709bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1710147403d9SBarry Smith 
17111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1712aa1993deSMatthew G Knepley @*/
1713d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1714d71ae5a4SJacob Faibussowitsch {
1715aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1716aa1993deSMatthew G Knepley 
1717aa1993deSMatthew G Knepley   PetscFunctionBegin;
1718aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17194f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1720a4e35b19SJacob Faibussowitsch   (void)count;
1721a4e35b19SJacob Faibussowitsch   (void)dtype;
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`:
1747bb7acecfSBarry Smith + dm        - The present `DM`
1748bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1749147403d9SBarry Smith . field     - The field number in dm
1750147403d9SBarry Smith - nullSpace - The nullspace for the given field
17518cda7954SMatthew G. Knepley 
175249762cbcSSatish Balay   Level: intermediate
175349762cbcSSatish Balay 
17541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1755147403d9SBarry Smith @*/
1756a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1757d71ae5a4SJacob Faibussowitsch {
1758435a35e8SMatthew G Knepley   PetscFunctionBegin;
1759435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17607a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1761435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1763435a35e8SMatthew G Knepley }
1764435a35e8SMatthew G Knepley 
17658cda7954SMatthew G. Knepley /*@C
1766bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17678cda7954SMatthew G. Knepley 
176820f4b53cSBarry Smith   Not Collective; No Fortran Support
17698cda7954SMatthew G. Knepley 
17708cda7954SMatthew G. Knepley   Input Parameters:
1771bb7acecfSBarry Smith + dm    - The `DM`
17728cda7954SMatthew G. Knepley - field - The field number for the nullspace
17738cda7954SMatthew G. Knepley 
17748cda7954SMatthew G. Knepley   Output Parameter:
17758cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17768cda7954SMatthew G. Knepley 
177720f4b53cSBarry Smith   Calling sequence of `nullsp`:
1778147403d9SBarry Smith + dm        - The present DM
1779147403d9SBarry Smith . origField - The field number given above, in the original DM
1780147403d9SBarry Smith . field     - The field number in dm
1781147403d9SBarry Smith - nullSpace - The nullspace for the given field
17828cda7954SMatthew G. Knepley 
178349762cbcSSatish Balay   Level: intermediate
178449762cbcSSatish Balay 
17851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1786147403d9SBarry Smith @*/
1787a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1788d71ae5a4SJacob Faibussowitsch {
17890a50eb56SMatthew G. Knepley   PetscFunctionBegin;
17900a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17914f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
17927a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
17930a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
17943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17950a50eb56SMatthew G. Knepley }
17960a50eb56SMatthew G. Knepley 
17978cda7954SMatthew G. Knepley /*@C
1798bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
17998cda7954SMatthew G. Knepley 
180020f4b53cSBarry Smith   Logically Collective; No Fortran Support
18018cda7954SMatthew G. Knepley 
18028cda7954SMatthew G. Knepley   Input Parameters:
1803bb7acecfSBarry Smith + dm     - The `DM`
18048cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18058cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18068cda7954SMatthew G. Knepley 
180720f4b53cSBarry Smith   Calling sequence of `nullsp`:
1808bb7acecfSBarry Smith + dm        - The present `DM`
1809bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1810147403d9SBarry Smith . field     - The field number in dm
1811147403d9SBarry Smith - nullSpace - The nullspace for the given field
18128cda7954SMatthew G. Knepley 
181349762cbcSSatish Balay   Level: intermediate
181449762cbcSSatish Balay 
18151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1816bb7acecfSBarry Smith           `MatNullSpace`
1817147403d9SBarry Smith @*/
1818a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1819d71ae5a4SJacob Faibussowitsch {
1820f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1821f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18227a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1823f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1825f9d4088aSMatthew G. Knepley }
1826f9d4088aSMatthew G. Knepley 
18278cda7954SMatthew G. Knepley /*@C
1828bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18298cda7954SMatthew G. Knepley 
183020f4b53cSBarry Smith   Not Collective; No Fortran Support
18318cda7954SMatthew G. Knepley 
18328cda7954SMatthew G. Knepley   Input Parameters:
1833bb7acecfSBarry Smith + dm    - The `DM`
18348cda7954SMatthew G. Knepley - field - The field number for the nullspace
18358cda7954SMatthew G. Knepley 
18368cda7954SMatthew G. Knepley   Output Parameter:
18378cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18388cda7954SMatthew G. Knepley 
183920f4b53cSBarry Smith   Calling sequence of `nullsp`:
1840bb7acecfSBarry Smith + dm        - The present `DM`
1841bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1842147403d9SBarry Smith . field     - The field number in dm
1843147403d9SBarry Smith - nullSpace - The nullspace for the given field
18448cda7954SMatthew G. Knepley 
184549762cbcSSatish Balay   Level: intermediate
184649762cbcSSatish Balay 
18471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1848bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1849147403d9SBarry Smith @*/
1850a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1851d71ae5a4SJacob Faibussowitsch {
1852f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1853f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18544f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18557a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1856f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1858f9d4088aSMatthew G. Knepley }
1859f9d4088aSMatthew G. Knepley 
18604f3b5142SJed Brown /*@C
1861bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18624d343eeaSMatthew G Knepley 
186320f4b53cSBarry Smith   Not Collective; No Fortran Support
18644d343eeaSMatthew G Knepley 
18654d343eeaSMatthew G Knepley   Input Parameter:
1866bb7acecfSBarry Smith . dm - the `DM` object
18674d343eeaSMatthew G Knepley 
18684d343eeaSMatthew G Knepley   Output Parameters:
186920f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
187020f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
187120f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
18724d343eeaSMatthew G Knepley 
18734d343eeaSMatthew G Knepley   Level: intermediate
18744d343eeaSMatthew G Knepley 
1875bb7acecfSBarry Smith   Note:
187621c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1877bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1878bb7acecfSBarry Smith   `PetscFree()`.
187921c9b008SJed Brown 
188060225df5SJacob Faibussowitsch   Developer Notes:
1881bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1882bb7acecfSBarry Smith   likely be removed.
1883bb7acecfSBarry Smith 
18841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1885bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18864d343eeaSMatthew G Knepley @*/
1887d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1888d71ae5a4SJacob Faibussowitsch {
188937d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
18904d343eeaSMatthew G Knepley 
18914d343eeaSMatthew G Knepley   PetscFunctionBegin;
18924d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
189369ca1f37SDmitry Karpeev   if (numFields) {
18944f572ea9SToby Isaac     PetscAssertPointer(numFields, 2);
189569ca1f37SDmitry Karpeev     *numFields = 0;
189669ca1f37SDmitry Karpeev   }
189737d0c07bSMatthew G Knepley   if (fieldNames) {
18984f572ea9SToby Isaac     PetscAssertPointer(fieldNames, 3);
18990298fd71SBarry Smith     *fieldNames = NULL;
190069ca1f37SDmitry Karpeev   }
190169ca1f37SDmitry Karpeev   if (fields) {
19024f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
19030298fd71SBarry Smith     *fields = NULL;
190469ca1f37SDmitry Karpeev   }
19059566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
190637d0c07bSMatthew G Knepley   if (section) {
19073a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
190837d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
190937d0c07bSMatthew G Knepley 
19109566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19119566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19129566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19139566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
191437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
191537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19169566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
191737d0c07bSMatthew G Knepley     }
191837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
191937d0c07bSMatthew G Knepley       PetscInt gdof;
192037d0c07bSMatthew G Knepley 
19219566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
192237d0c07bSMatthew G Knepley       if (gdof > 0) {
192337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19243a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
192537d0c07bSMatthew G Knepley 
19269566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19279566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19283a544194SStefano Zampini           fpdof = fdof - fcdof;
19293a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19303a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19313a544194SStefano Zampini             fieldNc[f] = 1;
19323a544194SStefano Zampini           }
19333a544194SStefano Zampini           fieldSizes[f] += fpdof;
193437d0c07bSMatthew G Knepley         }
193537d0c07bSMatthew G Knepley       }
193637d0c07bSMatthew G Knepley     }
193737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19389566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
193937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
194037d0c07bSMatthew G Knepley     }
194137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
194237d0c07bSMatthew G Knepley       PetscInt gdof, goff;
194337d0c07bSMatthew G Knepley 
19449566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
194537d0c07bSMatthew G Knepley       if (gdof > 0) {
19469566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
194737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
194837d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
194937d0c07bSMatthew G Knepley 
19509566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19519566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1952ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
195337d0c07bSMatthew G Knepley         }
195437d0c07bSMatthew G Knepley       }
195537d0c07bSMatthew G Knepley     }
19568865f1eaSKarl Rupp     if (numFields) *numFields = nF;
195737d0c07bSMatthew G Knepley     if (fieldNames) {
19589566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
195937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
196037d0c07bSMatthew G Knepley         const char *fieldName;
196137d0c07bSMatthew G Knepley 
19629566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19639566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
196437d0c07bSMatthew G Knepley       }
196537d0c07bSMatthew G Knepley     }
196637d0c07bSMatthew G Knepley     if (fields) {
19679566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
196837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19693a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19703a544194SStefano Zampini 
19719566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19723a544194SStefano Zampini         in[0] = -fieldNc[f];
19733a544194SStefano Zampini         in[1] = fieldNc[f];
19741c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19753a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19769566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
197737d0c07bSMatthew G Knepley       }
197837d0c07bSMatthew G Knepley     }
19799566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1980dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19824d343eeaSMatthew G Knepley }
19834d343eeaSMatthew G Knepley 
198416621825SDmitry Karpeev /*@C
1985bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1986a4e35b19SJacob Faibussowitsch   corresponding to different fields.
1987e7c4fc90SDmitry Karpeev 
198820f4b53cSBarry Smith   Not Collective; No Fortran Support
1989e7c4fc90SDmitry Karpeev 
1990e7c4fc90SDmitry Karpeev   Input Parameter:
1991bb7acecfSBarry Smith . dm - the `DM` object
1992e7c4fc90SDmitry Karpeev 
1993e7c4fc90SDmitry Karpeev   Output Parameters:
199420f4b53cSBarry Smith + len      - The number of fields (or `NULL` if not requested)
199520f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested)
199620f4b53cSBarry Smith . islist   - The global indices for each field (or `NULL` if not requested)
199720f4b53cSBarry Smith - dmlist   - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
1998e7c4fc90SDmitry Karpeev 
1999e7c4fc90SDmitry Karpeev   Level: intermediate
2000e7c4fc90SDmitry Karpeev 
2001a4e35b19SJacob Faibussowitsch   Notes:
2002a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding field, defined by
2003a4e35b19SJacob Faibussowitsch   `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2004a4e35b19SJacob Faibussowitsch 
2005a4e35b19SJacob Faibussowitsch   The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2006a4e35b19SJacob Faibussowitsch 
2007e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2008bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2009bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2010e7c4fc90SDmitry Karpeev 
201160225df5SJacob Faibussowitsch   Developer Notes:
2012bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2013bb7acecfSBarry Smith 
201460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2015e7c4fc90SDmitry Karpeev @*/
2016d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2017d71ae5a4SJacob Faibussowitsch {
2018e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2019e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20208865f1eaSKarl Rupp   if (len) {
20214f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20228865f1eaSKarl Rupp     *len = 0;
20238865f1eaSKarl Rupp   }
20248865f1eaSKarl Rupp   if (namelist) {
20254f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2026ea78f98cSLisandro Dalcin     *namelist = NULL;
20278865f1eaSKarl Rupp   }
20288865f1eaSKarl Rupp   if (islist) {
20294f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2030ea78f98cSLisandro Dalcin     *islist = NULL;
20318865f1eaSKarl Rupp   }
20328865f1eaSKarl Rupp   if (dmlist) {
20334f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2034ea78f98cSLisandro Dalcin     *dmlist = NULL;
20358865f1eaSKarl Rupp   }
2036f3f0edfdSDmitry Karpeev   /*
2037f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2038f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2039f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2040f3f0edfdSDmitry Karpeev    */
20417a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
204216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2043435a35e8SMatthew G Knepley     PetscSection section;
2044435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2045435a35e8SMatthew G Knepley 
20469566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20479566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2048435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2049f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20509566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20519566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20529566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2053435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2054435a35e8SMatthew G Knepley         const char *fieldName;
2055435a35e8SMatthew G Knepley 
20569566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
205703dc3394SMatthew G. Knepley         if (namelist) {
20589566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20599566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2060435a35e8SMatthew G Knepley         }
206103dc3394SMatthew G. Knepley       }
2062435a35e8SMatthew G Knepley     } else {
20639566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2064e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20650298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2066e7c4fc90SDmitry Karpeev     }
2067dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
206916621825SDmitry Karpeev }
207016621825SDmitry Karpeev 
2071412a4547SAlexis Marboeuf /*@C
207220f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
207320f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2074435a35e8SMatthew G Knepley 
2075435a35e8SMatthew G Knepley   Not collective
2076435a35e8SMatthew G Knepley 
2077435a35e8SMatthew G Knepley   Input Parameters:
2078bb7acecfSBarry Smith + dm        - The `DM` object
2079bb7acecfSBarry Smith . numFields - The number of fields to select
20802adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2081435a35e8SMatthew G Knepley 
2082435a35e8SMatthew G Knepley   Output Parameters:
2083bb7acecfSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`
2084bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2085435a35e8SMatthew G Knepley 
208620f4b53cSBarry Smith   Level: intermediate
208720f4b53cSBarry Smith 
2088bb7acecfSBarry Smith   Note:
2089bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20905d3b26e6SMatthew G. Knepley 
209160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2092435a35e8SMatthew G Knepley @*/
2093d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2094d71ae5a4SJacob Faibussowitsch {
2095435a35e8SMatthew G Knepley   PetscFunctionBegin;
2096435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20974f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
20984f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
20994f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2100dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2102435a35e8SMatthew G Knepley }
2103435a35e8SMatthew G Knepley 
21042adcc780SMatthew G. Knepley /*@C
2105bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21062adcc780SMatthew G. Knepley 
21072adcc780SMatthew G. Knepley   Not collective
21082adcc780SMatthew G. Knepley 
2109d8d19677SJose E. Roman   Input Parameters:
2110bb7acecfSBarry Smith + dms - The `DM` objects
2111bb7acecfSBarry Smith - n   - The number of `DM`s
21122adcc780SMatthew G. Knepley 
21132adcc780SMatthew G. Knepley   Output Parameters:
2114bb7acecfSBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or NULL
2115bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21162adcc780SMatthew G. Knepley 
211720f4b53cSBarry Smith   Level: intermediate
211820f4b53cSBarry Smith 
2119bb7acecfSBarry Smith   Note:
2120bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21215d3b26e6SMatthew G. Knepley 
21221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
21232adcc780SMatthew G. Knepley @*/
2124d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
2125d71ae5a4SJacob Faibussowitsch {
21262adcc780SMatthew G. Knepley   PetscInt i;
21272adcc780SMatthew G. Knepley 
21282adcc780SMatthew G. Knepley   PetscFunctionBegin;
21294f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2130ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21314f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
21324f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2133bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2134bb7acecfSBarry Smith   if (n) {
2135b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2136fa1e479aSStefano Zampini     PetscCheck(dm->ops->createsuperdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No method createsuperdm for DM of type %s\n", ((PetscObject)dm)->type_name);
2137dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21382adcc780SMatthew G. Knepley   }
21393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21402adcc780SMatthew G. Knepley }
21412adcc780SMatthew G. Knepley 
214216621825SDmitry Karpeev /*@C
2143a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2144a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
214516621825SDmitry Karpeev 
214620f4b53cSBarry Smith   Not Collective
214716621825SDmitry Karpeev 
214816621825SDmitry Karpeev   Input Parameter:
2149bb7acecfSBarry Smith . dm - the `DM` object
215016621825SDmitry Karpeev 
215116621825SDmitry Karpeev   Output Parameters:
215220f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
215320f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
21540298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
21550298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
215620f4b53cSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if `NULL` is returned, no `DM`s are defined)
215716621825SDmitry Karpeev 
215816621825SDmitry Karpeev   Level: intermediate
215916621825SDmitry Karpeev 
2160bb7acecfSBarry Smith   Note:
2161a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2162a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2163a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2164a4e35b19SJacob Faibussowitsch 
2165a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2166a4e35b19SJacob Faibussowitsch 
216716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2168bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2169bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
217016621825SDmitry Karpeev 
2171a4e35b19SJacob Faibussowitsch   Developer Notes:
217220f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2173bb7acecfSBarry Smith 
217460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
217516621825SDmitry Karpeev @*/
2176d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2177d71ae5a4SJacob Faibussowitsch {
2178be081cd6SPeter Brune   DMSubDomainHookLink link;
2179be081cd6SPeter Brune   PetscInt            i, l;
218016621825SDmitry Karpeev 
218116621825SDmitry Karpeev   PetscFunctionBegin;
218216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21839371c9d4SSatish Balay   if (n) {
21844f572ea9SToby Isaac     PetscAssertPointer(n, 2);
21859371c9d4SSatish Balay     *n = 0;
21869371c9d4SSatish Balay   }
21879371c9d4SSatish Balay   if (namelist) {
21884f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
21899371c9d4SSatish Balay     *namelist = NULL;
21909371c9d4SSatish Balay   }
21919371c9d4SSatish Balay   if (innerislist) {
21924f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
21939371c9d4SSatish Balay     *innerislist = NULL;
21949371c9d4SSatish Balay   }
21959371c9d4SSatish Balay   if (outerislist) {
21964f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
21979371c9d4SSatish Balay     *outerislist = NULL;
21989371c9d4SSatish Balay   }
21999371c9d4SSatish Balay   if (dmlist) {
22004f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
22019371c9d4SSatish Balay     *dmlist = NULL;
22029371c9d4SSatish Balay   }
2203f3f0edfdSDmitry Karpeev   /*
2204f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2205f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2206f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2207f3f0edfdSDmitry Karpeev    */
22087a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
220916621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2210dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
221114a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2212f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2213be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2214be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22159566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2216be081cd6SPeter Brune         }
2217648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2218e7c4fc90SDmitry Karpeev       }
221914a18fd3SPeter Brune     }
2220bb7acecfSBarry Smith     if (n) *n = l;
222114a18fd3SPeter Brune   }
22223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2223e30e807fSPeter Brune }
2224e30e807fSPeter Brune 
2225e30e807fSPeter Brune /*@C
2226e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2227e30e807fSPeter Brune 
222820f4b53cSBarry Smith   Not Collective
2229e30e807fSPeter Brune 
2230e30e807fSPeter Brune   Input Parameters:
2231bb7acecfSBarry Smith + dm     - the `DM` object
2232e30e807fSPeter Brune . n      - the number of subdomain scatters
2233e30e807fSPeter Brune - subdms - the local subdomains
2234e30e807fSPeter Brune 
2235e30e807fSPeter Brune   Output Parameters:
22366b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2237e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2238e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2239e30e807fSPeter Brune 
224020f4b53cSBarry Smith   Level: developer
224120f4b53cSBarry Smith 
2242bb7acecfSBarry Smith   Note:
2243bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2244e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2245e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2246e30e807fSPeter Brune   solution and residual data.
2247e30e807fSPeter Brune 
2248a4e35b19SJacob Faibussowitsch   Developer Notes:
2249a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2250a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2251bb7acecfSBarry Smith 
22521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2253e30e807fSPeter Brune @*/
2254d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat)
2255d71ae5a4SJacob Faibussowitsch {
2256e30e807fSPeter Brune   PetscFunctionBegin;
2257e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22584f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2259dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2261e7c4fc90SDmitry Karpeev }
2262e7c4fc90SDmitry Karpeev 
226347c6ae99SBarry Smith /*@
2264bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
226547c6ae99SBarry Smith 
226620f4b53cSBarry Smith   Collective
226747c6ae99SBarry Smith 
2268d8d19677SJose E. Roman   Input Parameters:
2269bb7acecfSBarry Smith + dm   - the `DM` object
2270bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
227147c6ae99SBarry Smith 
227247c6ae99SBarry Smith   Output Parameter:
227320f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2274ae0a1c52SMatthew G Knepley 
227520f4b53cSBarry Smith   Options Database Key:
2276412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2277412e9a14SMatthew G. Knepley 
227847c6ae99SBarry Smith   Level: developer
227947c6ae99SBarry Smith 
228020f4b53cSBarry Smith   Note:
228120f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
228220f4b53cSBarry Smith 
22831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
228447c6ae99SBarry Smith @*/
2285d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2286d71ae5a4SJacob Faibussowitsch {
2287c833c3b5SJed Brown   DMRefineHookLink link;
228847c6ae99SBarry Smith 
228947c6ae99SBarry Smith   PetscFunctionBegin;
2290732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22919566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2292dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
22934057135bSMatthew G Knepley   if (*dmf) {
229443842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
22958865f1eaSKarl Rupp 
22969566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
22978865f1eaSKarl Rupp 
2298644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
22990598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2300656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23018865f1eaSKarl Rupp 
23029566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2303c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23041baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2305c833c3b5SJed Brown     }
2306c833c3b5SJed Brown   }
23079566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2309c833c3b5SJed Brown }
2310c833c3b5SJed Brown 
2311bb9467b5SJed Brown /*@C
2312c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2313c833c3b5SJed Brown 
231420f4b53cSBarry Smith   Logically Collective; No Fortran Support
2315c833c3b5SJed Brown 
23164165533cSJose E. Roman   Input Parameters:
2317bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2318bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2319f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
232020f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2321c833c3b5SJed Brown 
232220f4b53cSBarry Smith   Calling sequence of `refinehook`:
2323bb7acecfSBarry Smith + coarse - coarse level `DM`
2324bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2325c833c3b5SJed Brown - ctx    - optional user-defined function context
2326c833c3b5SJed Brown 
232720f4b53cSBarry Smith   Calling sequence of `interphook`:
2328bb7acecfSBarry Smith + coarse - coarse level `DM`
2329c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2330bb7acecfSBarry Smith . fine   - fine level `DM` to update
2331c833c3b5SJed Brown - ctx    - optional user-defined function context
2332c833c3b5SJed Brown 
2333c833c3b5SJed Brown   Level: advanced
2334c833c3b5SJed Brown 
2335c833c3b5SJed Brown   Notes:
2336bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2337bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2338bb7acecfSBarry Smith 
2339bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2340c833c3b5SJed Brown 
2341c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2342c833c3b5SJed Brown 
23431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2344c833c3b5SJed Brown @*/
2345a4e35b19SJacob Faibussowitsch PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM coarse, DM fine, void *ctx), PetscErrorCode (*interphook)(DM coarse, Mat interp, DM fine, void *ctx), void *ctx)
2346d71ae5a4SJacob Faibussowitsch {
2347c833c3b5SJed Brown   DMRefineHookLink link, *p;
2348c833c3b5SJed Brown 
2349c833c3b5SJed Brown   PetscFunctionBegin;
2350c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23513d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23523ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23533d8e3701SJed Brown   }
23549566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2355c833c3b5SJed Brown   link->refinehook = refinehook;
2356c833c3b5SJed Brown   link->interphook = interphook;
2357c833c3b5SJed Brown   link->ctx        = ctx;
23580298fd71SBarry Smith   link->next       = NULL;
2359c833c3b5SJed Brown   *p               = link;
23603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2361c833c3b5SJed Brown }
2362c833c3b5SJed Brown 
23633d8e3701SJed Brown /*@C
2364bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2365bb7acecfSBarry Smith   a nonlinear problem to a finer grid
23663d8e3701SJed Brown 
236720f4b53cSBarry Smith   Logically Collective; No Fortran Support
23683d8e3701SJed Brown 
23694165533cSJose E. Roman   Input Parameters:
2370bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2371bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2372bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
237320f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
23743d8e3701SJed Brown 
23753d8e3701SJed Brown   Level: advanced
23763d8e3701SJed Brown 
2377bb7acecfSBarry Smith   Note:
23783d8e3701SJed Brown   This function does nothing if the hook is not in the list.
23793d8e3701SJed Brown 
23801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
23813d8e3701SJed Brown @*/
2382d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2383d71ae5a4SJacob Faibussowitsch {
23843d8e3701SJed Brown   DMRefineHookLink link, *p;
23853d8e3701SJed Brown 
23863d8e3701SJed Brown   PetscFunctionBegin;
23873d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23883d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
23893d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
23903d8e3701SJed Brown       link = *p;
23913d8e3701SJed Brown       *p   = link->next;
23929566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
23933d8e3701SJed Brown       break;
23943d8e3701SJed Brown     }
23953d8e3701SJed Brown   }
23963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23973d8e3701SJed Brown }
23983d8e3701SJed Brown 
2399c833c3b5SJed Brown /*@
2400bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2401c833c3b5SJed Brown 
2402c833c3b5SJed Brown   Collective if any hooks are
2403c833c3b5SJed Brown 
24044165533cSJose E. Roman   Input Parameters:
2405bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2406bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2407bb7acecfSBarry Smith - fine   - finer `DM` to update
2408c833c3b5SJed Brown 
2409c833c3b5SJed Brown   Level: developer
2410c833c3b5SJed Brown 
241160225df5SJacob Faibussowitsch   Developer Notes:
2412bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2413bb7acecfSBarry Smith   an API with consistent terminology.
2414bb7acecfSBarry Smith 
24151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2416c833c3b5SJed Brown @*/
2417d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2418d71ae5a4SJacob Faibussowitsch {
2419c833c3b5SJed Brown   DMRefineHookLink link;
2420c833c3b5SJed Brown 
2421c833c3b5SJed Brown   PetscFunctionBegin;
2422c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24231baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24244057135bSMatthew G Knepley   }
24253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
242647c6ae99SBarry Smith }
242747c6ae99SBarry Smith 
2428eb3f98d2SBarry Smith /*@
24291f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24301f3379b2SToby Isaac 
243120f4b53cSBarry Smith   Collective
24321f3379b2SToby Isaac 
24334165533cSJose E. Roman   Input Parameters:
2434bb7acecfSBarry Smith + coarse    - coarse `DM`
2435bb7acecfSBarry Smith . fine      - fine `DM`
2436bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2437bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2438bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24391f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
24401f3379b2SToby Isaac 
24414165533cSJose E. Roman   Output Parameter:
24421f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
24431f3379b2SToby Isaac 
24441f3379b2SToby Isaac   Level: developer
24451f3379b2SToby Isaac 
2446bb7acecfSBarry Smith   Note:
2447bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
24481f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24491f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24501f3379b2SToby Isaac   slope-limiting reconstruction.
24511f3379b2SToby Isaac 
245260225df5SJacob Faibussowitsch   Developer Notes:
2453bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2454bb7acecfSBarry Smith 
24551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24561f3379b2SToby Isaac @*/
2457d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2458d71ae5a4SJacob Faibussowitsch {
24591f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24601f3379b2SToby Isaac 
24611f3379b2SToby Isaac   PetscFunctionBegin;
24621f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24631f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24641f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24651f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24661f3379b2SToby Isaac 
24679566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24681f3379b2SToby Isaac   if (interpsol) {
24699566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24701f3379b2SToby Isaac   } else if (interp) {
24719566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
247298921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24741f3379b2SToby Isaac }
24751f3379b2SToby Isaac 
24761f3379b2SToby Isaac /*@
2477bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2478eb3f98d2SBarry Smith 
2479eb3f98d2SBarry Smith   Not Collective
2480eb3f98d2SBarry Smith 
2481eb3f98d2SBarry Smith   Input Parameter:
2482bb7acecfSBarry Smith . dm - the `DM` object
2483eb3f98d2SBarry Smith 
2484eb3f98d2SBarry Smith   Output Parameter:
2485eb3f98d2SBarry Smith . level - number of refinements
2486eb3f98d2SBarry Smith 
2487eb3f98d2SBarry Smith   Level: developer
2488eb3f98d2SBarry Smith 
2489bb7acecfSBarry Smith   Note:
2490bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2491bb7acecfSBarry Smith 
24921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2493eb3f98d2SBarry Smith @*/
2494d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2495d71ae5a4SJacob Faibussowitsch {
2496eb3f98d2SBarry Smith   PetscFunctionBegin;
2497eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2498eb3f98d2SBarry Smith   *level = dm->levelup;
24993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2500eb3f98d2SBarry Smith }
2501eb3f98d2SBarry Smith 
2502fef3a512SBarry Smith /*@
2503bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2504fef3a512SBarry Smith 
2505fef3a512SBarry Smith   Not Collective
2506fef3a512SBarry Smith 
2507d8d19677SJose E. Roman   Input Parameters:
2508bb7acecfSBarry Smith + dm    - the `DM` object
2509fef3a512SBarry Smith - level - number of refinements
2510fef3a512SBarry Smith 
2511fef3a512SBarry Smith   Level: advanced
2512fef3a512SBarry Smith 
251395452b02SPatrick Sanan   Notes:
2514bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2515fef3a512SBarry Smith 
2516bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2517bb7acecfSBarry Smith 
25181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2519fef3a512SBarry Smith @*/
2520d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2521d71ae5a4SJacob Faibussowitsch {
2522fef3a512SBarry Smith   PetscFunctionBegin;
2523fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2524fef3a512SBarry Smith   dm->levelup = level;
25253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2526fef3a512SBarry Smith }
2527fef3a512SBarry Smith 
2528d410b0cfSMatthew G. Knepley /*@
2529bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2530d410b0cfSMatthew G. Knepley 
253120f4b53cSBarry Smith   Collective
2532d410b0cfSMatthew G. Knepley 
2533f1a722f8SMatthew G. Knepley   Input Parameters:
2534bb7acecfSBarry Smith + dm     - the `DM` object
2535d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2536d410b0cfSMatthew G. Knepley 
2537d410b0cfSMatthew G. Knepley   Output Parameter:
253820f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2539d410b0cfSMatthew G. Knepley 
2540d410b0cfSMatthew G. Knepley   Level: developer
2541d410b0cfSMatthew G. Knepley 
254220f4b53cSBarry Smith   Note:
254320f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
254420f4b53cSBarry Smith 
25451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2546d410b0cfSMatthew G. Knepley @*/
2547d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2548d71ae5a4SJacob Faibussowitsch {
2549d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2550d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2551dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2552d410b0cfSMatthew G. Knepley   if (*dme) {
2553d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25549566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2555d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25569566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2557d410b0cfSMatthew G. Knepley   }
25583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2559d410b0cfSMatthew G. Knepley }
2560d410b0cfSMatthew G. Knepley 
2561d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2562d71ae5a4SJacob Faibussowitsch {
2563ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2564ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25654f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2566ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
25673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2568ca3d3a14SMatthew G. Knepley }
2569ca3d3a14SMatthew G. Knepley 
2570d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2571d71ae5a4SJacob Faibussowitsch {
2572ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2573ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25744f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2575ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
25763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2577ca3d3a14SMatthew G. Knepley }
2578ca3d3a14SMatthew G. Knepley 
2579ca3d3a14SMatthew G. Knepley /*@
2580bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2581ca3d3a14SMatthew G. Knepley 
2582ca3d3a14SMatthew G. Knepley   Input Parameter:
258320f4b53cSBarry Smith . dm - The `DM`
2584ca3d3a14SMatthew G. Knepley 
2585ca3d3a14SMatthew G. Knepley   Output Parameter:
258620f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2587ca3d3a14SMatthew G. Knepley 
2588ca3d3a14SMatthew G. Knepley   Level: developer
2589ca3d3a14SMatthew G. Knepley 
25901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2591ca3d3a14SMatthew G. Knepley @*/
2592d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2593d71ae5a4SJacob Faibussowitsch {
2594ca3d3a14SMatthew G. Knepley   Vec tv;
2595ca3d3a14SMatthew G. Knepley 
2596ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2597ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25984f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
25999566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2600ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2602ca3d3a14SMatthew G. Knepley }
2603ca3d3a14SMatthew G. Knepley 
2604d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2605d71ae5a4SJacob Faibussowitsch {
2606ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2607ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2608ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2609ca3d3a14SMatthew G. Knepley 
2610ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26119566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26129566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26139566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26149566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26159566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26169566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26179566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26189566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2619ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26209566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2621ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2622ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2623ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26249566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2625ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26269566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26279566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2628ca3d3a14SMatthew G. Knepley     }
2629ca3d3a14SMatthew G. Knepley   }
26309566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26319566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26329566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2633ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2634ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26359566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2636ca3d3a14SMatthew G. Knepley       if (dof) {
2637ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2638ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2639ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2640ca3d3a14SMatthew G. Knepley 
2641ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26429566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26439566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26449566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2645ca3d3a14SMatthew G. Knepley       }
2646ca3d3a14SMatthew G. Knepley     }
2647ca3d3a14SMatthew G. Knepley   }
26489566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2650ca3d3a14SMatthew G. Knepley }
2651ca3d3a14SMatthew G. Knepley 
2652d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2653d71ae5a4SJacob Faibussowitsch {
2654ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2655ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2656ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2657ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2658ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2659ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2660ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26619566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2663ca3d3a14SMatthew G. Knepley }
2664ca3d3a14SMatthew G. Knepley 
2665bb9467b5SJed Brown /*@C
2666bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2667baf369e7SPeter Brune 
266820f4b53cSBarry Smith   Logically Collective
2669baf369e7SPeter Brune 
26704165533cSJose E. Roman   Input Parameters:
2671bb7acecfSBarry Smith + dm        - the `DM`
2672bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2673bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
267420f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2675baf369e7SPeter Brune 
267620f4b53cSBarry Smith   Calling sequence of `beginhook`:
2677a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2678baf369e7SPeter Brune . g    - global vector
2679baf369e7SPeter Brune . mode - mode
2680baf369e7SPeter Brune . l    - local vector
2681baf369e7SPeter Brune - ctx  - optional user-defined function context
2682baf369e7SPeter Brune 
268320f4b53cSBarry Smith   Calling sequence of `endhook`:
2684a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2685a4e35b19SJacob Faibussowitsch . g    - global vector
2686a4e35b19SJacob Faibussowitsch . mode - mode
2687a4e35b19SJacob Faibussowitsch . l    - local vector
2688baf369e7SPeter Brune - ctx  - optional user-defined function context
2689baf369e7SPeter Brune 
2690baf369e7SPeter Brune   Level: advanced
2691baf369e7SPeter Brune 
2692bb7acecfSBarry Smith   Note:
2693bb7acecfSBarry 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.
2694bb7acecfSBarry Smith 
26951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2696baf369e7SPeter Brune @*/
2697a4e35b19SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM dm, Vec g, InsertMode mode, Vec l, void *ctx), PetscErrorCode (*endhook)(DM dm, Vec g, InsertMode mode, Vec l, void *ctx), void *ctx)
2698d71ae5a4SJacob Faibussowitsch {
2699baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2700baf369e7SPeter Brune 
2701baf369e7SPeter Brune   PetscFunctionBegin;
2702baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2703baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27049566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2705baf369e7SPeter Brune   link->beginhook = beginhook;
2706baf369e7SPeter Brune   link->endhook   = endhook;
2707baf369e7SPeter Brune   link->ctx       = ctx;
27080298fd71SBarry Smith   link->next      = NULL;
2709baf369e7SPeter Brune   *p              = link;
27103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2711baf369e7SPeter Brune }
2712baf369e7SPeter Brune 
2713d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2714d71ae5a4SJacob Faibussowitsch {
27154c274da1SToby Isaac   Mat          cMat;
271679769bd5SJed Brown   Vec          cVec, cBias;
27174c274da1SToby Isaac   PetscSection section, cSec;
27184c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27194c274da1SToby Isaac 
27204c274da1SToby Isaac   PetscFunctionBegin;
2721a4e35b19SJacob Faibussowitsch   (void)g;
2722a4e35b19SJacob Faibussowitsch   (void)ctx;
27234c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27249566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27254c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27265db9a05bSToby Isaac     PetscInt nRows;
27275db9a05bSToby Isaac 
27289566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27293ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27309566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27319566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27329566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27339566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27349566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27354c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27369566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27374c274da1SToby Isaac       if (dof) {
27384c274da1SToby Isaac         PetscScalar *vals;
27399566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27409566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27414c274da1SToby Isaac       }
27424c274da1SToby Isaac     }
27439566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27444c274da1SToby Isaac   }
27453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27464c274da1SToby Isaac }
27474c274da1SToby Isaac 
274847c6ae99SBarry Smith /*@
274901729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
275001729b5cSPatrick Sanan 
275120f4b53cSBarry Smith   Neighbor-wise Collective
275201729b5cSPatrick Sanan 
275301729b5cSPatrick Sanan   Input Parameters:
2754bb7acecfSBarry Smith + dm   - the `DM` object
275501729b5cSPatrick Sanan . g    - the global vector
2756bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
275701729b5cSPatrick Sanan - l    - the local vector
275801729b5cSPatrick Sanan 
275920f4b53cSBarry Smith   Level: beginner
276020f4b53cSBarry Smith 
276101729b5cSPatrick Sanan   Notes:
2762bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2763bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2764bb7acecfSBarry Smith 
2765bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
276601729b5cSPatrick Sanan 
27671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
276860225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2769bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
277001729b5cSPatrick Sanan @*/
2771d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2772d71ae5a4SJacob Faibussowitsch {
277301729b5cSPatrick Sanan   PetscFunctionBegin;
27749566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27759566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
27763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
277701729b5cSPatrick Sanan }
277801729b5cSPatrick Sanan 
277901729b5cSPatrick Sanan /*@
278047c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
278147c6ae99SBarry Smith 
278220f4b53cSBarry Smith   Neighbor-wise Collective
278347c6ae99SBarry Smith 
278447c6ae99SBarry Smith   Input Parameters:
2785bb7acecfSBarry Smith + dm   - the `DM` object
278647c6ae99SBarry Smith . g    - the global vector
2787bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
278847c6ae99SBarry Smith - l    - the local vector
278947c6ae99SBarry Smith 
279001729b5cSPatrick Sanan   Level: intermediate
279147c6ae99SBarry Smith 
2792bb7acecfSBarry Smith   Notes:
2793bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2794bb7acecfSBarry Smith 
2795bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2796bb7acecfSBarry Smith 
2797bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2798bb7acecfSBarry Smith 
2799bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2800bb7acecfSBarry Smith 
280160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
280247c6ae99SBarry Smith @*/
2803d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2804d71ae5a4SJacob Faibussowitsch {
28057128ae9fSMatthew G Knepley   PetscSF                 sf;
2806baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
280747c6ae99SBarry Smith 
280847c6ae99SBarry Smith   PetscFunctionBegin;
2809171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2810baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28111baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2812baf369e7SPeter Brune   }
28139566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28147128ae9fSMatthew G Knepley   if (sf) {
2815ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2816ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2817d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28187128ae9fSMatthew G Knepley 
28197a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28209566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28219566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28229566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28239566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28249566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28257128ae9fSMatthew G Knepley   } else {
2826fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28277128ae9fSMatthew G Knepley   }
28283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
282947c6ae99SBarry Smith }
283047c6ae99SBarry Smith 
283147c6ae99SBarry Smith /*@
283247c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
283347c6ae99SBarry Smith 
283420f4b53cSBarry Smith   Neighbor-wise Collective
283547c6ae99SBarry Smith 
283647c6ae99SBarry Smith   Input Parameters:
2837bb7acecfSBarry Smith + dm   - the `DM` object
283847c6ae99SBarry Smith . g    - the global vector
2839bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
284047c6ae99SBarry Smith - l    - the local vector
284147c6ae99SBarry Smith 
284201729b5cSPatrick Sanan   Level: intermediate
284347c6ae99SBarry Smith 
2844bb7acecfSBarry Smith   Note:
2845bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2846bb7acecfSBarry Smith 
284760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
284847c6ae99SBarry Smith @*/
2849d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2850d71ae5a4SJacob Faibussowitsch {
28517128ae9fSMatthew G Knepley   PetscSF                 sf;
2852ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2853ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2854ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2855baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2856d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
285747c6ae99SBarry Smith 
285847c6ae99SBarry Smith   PetscFunctionBegin;
2859171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28609566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28619566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28627128ae9fSMatthew G Knepley   if (sf) {
28637a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28647128ae9fSMatthew G Knepley 
28659566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28669566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28679566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28689566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28699566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28709566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28717128ae9fSMatthew G Knepley   } else {
2872fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28737128ae9fSMatthew G Knepley   }
28749566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2875baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28769566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2877baf369e7SPeter Brune   }
28783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
287947c6ae99SBarry Smith }
288047c6ae99SBarry Smith 
2881d4d07f1eSToby Isaac /*@C
2882d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2883d4d07f1eSToby Isaac 
288420f4b53cSBarry Smith   Logically Collective
2885d4d07f1eSToby Isaac 
28864165533cSJose E. Roman   Input Parameters:
2887bb7acecfSBarry Smith + dm        - the `DM`
2888bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2889bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
289020f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2891d4d07f1eSToby Isaac 
289220f4b53cSBarry Smith   Calling sequence of `beginhook`:
2893a4e35b19SJacob Faibussowitsch + global - global `DM`
2894d4d07f1eSToby Isaac . l      - local vector
2895d4d07f1eSToby Isaac . mode   - mode
2896d4d07f1eSToby Isaac . g      - global vector
2897d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2898d4d07f1eSToby Isaac 
289920f4b53cSBarry Smith   Calling sequence of `endhook`:
2900bb7acecfSBarry Smith + global - global `DM`
2901d4d07f1eSToby Isaac . l      - local vector
2902d4d07f1eSToby Isaac . mode   - mode
2903d4d07f1eSToby Isaac . g      - global vector
2904d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2905d4d07f1eSToby Isaac 
2906d4d07f1eSToby Isaac   Level: advanced
2907d4d07f1eSToby Isaac 
29081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2909d4d07f1eSToby Isaac @*/
2910a4e35b19SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM global, Vec l, InsertMode mode, Vec g, void *ctx), PetscErrorCode (*endhook)(DM global, Vec l, InsertMode mode, Vec g, void *ctx), void *ctx)
2911d71ae5a4SJacob Faibussowitsch {
2912d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2913d4d07f1eSToby Isaac 
2914d4d07f1eSToby Isaac   PetscFunctionBegin;
2915d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2916d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29179566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2918d4d07f1eSToby Isaac   link->beginhook = beginhook;
2919d4d07f1eSToby Isaac   link->endhook   = endhook;
2920d4d07f1eSToby Isaac   link->ctx       = ctx;
2921d4d07f1eSToby Isaac   link->next      = NULL;
2922d4d07f1eSToby Isaac   *p              = link;
29233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2924d4d07f1eSToby Isaac }
2925d4d07f1eSToby Isaac 
2926d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2927d71ae5a4SJacob Faibussowitsch {
29284c274da1SToby Isaac   Mat          cMat;
29294c274da1SToby Isaac   Vec          cVec;
29304c274da1SToby Isaac   PetscSection section, cSec;
29314c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
29324c274da1SToby Isaac 
29334c274da1SToby Isaac   PetscFunctionBegin;
2934a4e35b19SJacob Faibussowitsch   (void)g;
2935a4e35b19SJacob Faibussowitsch   (void)ctx;
29364c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29379566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29384c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
29395db9a05bSToby Isaac     PetscInt nRows;
29405db9a05bSToby Isaac 
29419566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29423ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29439566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29449566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29459566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29464c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29479566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29484c274da1SToby Isaac       if (dof) {
29494c274da1SToby Isaac         PetscInt     d;
29504c274da1SToby Isaac         PetscScalar *vals;
29519566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29529566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29534c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29544c274da1SToby Isaac          * we just extracted */
2955ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29564c274da1SToby Isaac       }
29574c274da1SToby Isaac     }
29589566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29599566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29604c274da1SToby Isaac   }
29613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29624c274da1SToby Isaac }
296301729b5cSPatrick Sanan /*@
296401729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
296501729b5cSPatrick Sanan 
296620f4b53cSBarry Smith   Neighbor-wise Collective
296701729b5cSPatrick Sanan 
296801729b5cSPatrick Sanan   Input Parameters:
2969bb7acecfSBarry Smith + dm   - the `DM` object
297001729b5cSPatrick Sanan . l    - the local vector
2971bb7acecfSBarry 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.
297201729b5cSPatrick Sanan - g    - the global vector
297301729b5cSPatrick Sanan 
297420f4b53cSBarry Smith   Level: beginner
297520f4b53cSBarry Smith 
297601729b5cSPatrick Sanan   Notes:
297701729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
2978bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
297901729b5cSPatrick Sanan 
2980bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2981bb7acecfSBarry Smith 
2982bb7acecfSBarry 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.
2983bb7acecfSBarry Smith 
2984bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
298501729b5cSPatrick Sanan 
29861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
298701729b5cSPatrick Sanan @*/
2988d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
2989d71ae5a4SJacob Faibussowitsch {
299001729b5cSPatrick Sanan   PetscFunctionBegin;
29919566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
29929566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
29933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
299401729b5cSPatrick Sanan }
29954c274da1SToby Isaac 
299647c6ae99SBarry Smith /*@
299701729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
29989a42bb27SBarry Smith 
299920f4b53cSBarry Smith   Neighbor-wise Collective
30009a42bb27SBarry Smith 
30019a42bb27SBarry Smith   Input Parameters:
3002bb7acecfSBarry Smith + dm   - the `DM` object
3003f6813fd5SJed Brown . l    - the local vector
3004aa624791SPierre 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.
30051eb28f2eSBarry Smith - g    - the global vector
30069a42bb27SBarry Smith 
300720f4b53cSBarry Smith   Level: intermediate
300820f4b53cSBarry Smith 
300995452b02SPatrick Sanan   Notes:
3010bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3011bb7acecfSBarry Smith 
3012bb7acecfSBarry 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.
3013bb7acecfSBarry Smith 
3014bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3015bb7acecfSBarry Smith 
3016bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3017bb7acecfSBarry Smith 
3018bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30199a42bb27SBarry Smith 
30201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30219a42bb27SBarry Smith @*/
3022d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3023d71ae5a4SJacob Faibussowitsch {
30247128ae9fSMatthew G Knepley   PetscSF                 sf;
302584330215SMatthew G. Knepley   PetscSection            s, gs;
3026d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3027ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3028ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3029ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3030fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3031d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30329a42bb27SBarry Smith 
30339a42bb27SBarry Smith   PetscFunctionBegin;
3034171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3035d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30361baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3037d4d07f1eSToby Isaac   }
30389566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30399566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30409566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30417128ae9fSMatthew G Knepley   switch (mode) {
30427128ae9fSMatthew G Knepley   case INSERT_VALUES:
30437128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3044d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3045d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3046d71ae5a4SJacob Faibussowitsch     break;
30477128ae9fSMatthew G Knepley   case ADD_VALUES:
30487128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3049d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3050d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3051d71ae5a4SJacob Faibussowitsch     break;
3052d71ae5a4SJacob Faibussowitsch   default:
3053d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30547128ae9fSMatthew G Knepley   }
3055ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30569566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3057ca3d3a14SMatthew G. Knepley     if (transform) {
30589566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30599566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30609566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30619566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3062fa88e482SJed Brown     } else if (isInsert) {
30639566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3064fa88e482SJed Brown     } else {
30659566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3066fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3067ca3d3a14SMatthew G. Knepley     }
3068fa88e482SJed Brown     if (s && isInsert) {
30699566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3070fa88e482SJed Brown     } else {
30719566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3072fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3073fa88e482SJed Brown     }
3074ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30759566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
307684330215SMatthew G. Knepley     } else if (s && isInsert) {
307784330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
307884330215SMatthew G. Knepley 
30799566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
30809566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
30819566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
308284330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3083b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
308484330215SMatthew G. Knepley 
30859566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
30869566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
30879566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
30889566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
30899566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
30909566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3091b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
309203442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
30937a8be351SBarry 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);
3094b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3095b3b16f48SMatthew G. Knepley         if (!gcdof) {
309684330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3097b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3098b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
309984330215SMatthew G. Knepley           const PetscInt *cdofs;
310084330215SMatthew G. Knepley           PetscInt        cind = 0;
310184330215SMatthew G. Knepley 
31029566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3103b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31049371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31059371c9d4SSatish Balay               ++cind;
31069371c9d4SSatish Balay               continue;
31079371c9d4SSatish Balay             }
3108b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
310984330215SMatthew G. Knepley           }
31107a8be351SBarry 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);
311184330215SMatthew G. Knepley       }
3112ca3d3a14SMatthew G. Knepley     }
3113fa88e482SJed Brown     if (g_inplace) {
31149566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3115fa88e482SJed Brown     } else {
31169566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3117fa88e482SJed Brown     }
3118ca3d3a14SMatthew G. Knepley     if (transform) {
31199566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31209566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3121fa88e482SJed Brown     } else if (l_inplace) {
31229566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3123ca3d3a14SMatthew G. Knepley     } else {
31249566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3125ca3d3a14SMatthew G. Knepley     }
31267128ae9fSMatthew G Knepley   } else {
3127fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalbegin, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
31287128ae9fSMatthew G Knepley   }
31293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31309a42bb27SBarry Smith }
31319a42bb27SBarry Smith 
31329a42bb27SBarry Smith /*@
31339a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
313447c6ae99SBarry Smith 
313520f4b53cSBarry Smith   Neighbor-wise Collective
313647c6ae99SBarry Smith 
313747c6ae99SBarry Smith   Input Parameters:
3138bb7acecfSBarry Smith + dm   - the `DM` object
3139f6813fd5SJed Brown . l    - the local vector
3140bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3141f6813fd5SJed Brown - g    - the global vector
314247c6ae99SBarry Smith 
314301729b5cSPatrick Sanan   Level: intermediate
314447c6ae99SBarry Smith 
3145bb7acecfSBarry Smith   Note:
3146bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3147bb7acecfSBarry Smith 
314860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
314947c6ae99SBarry Smith @*/
3150d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3151d71ae5a4SJacob Faibussowitsch {
31527128ae9fSMatthew G Knepley   PetscSF                 sf;
315384330215SMatthew G. Knepley   PetscSection            s;
3154d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3155ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
315647c6ae99SBarry Smith 
315747c6ae99SBarry Smith   PetscFunctionBegin;
3158171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31599566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31609566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31617128ae9fSMatthew G Knepley   switch (mode) {
31627128ae9fSMatthew G Knepley   case INSERT_VALUES:
3163d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3164d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3165d71ae5a4SJacob Faibussowitsch     break;
31667128ae9fSMatthew G Knepley   case ADD_VALUES:
3167d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3168d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3169d71ae5a4SJacob Faibussowitsch     break;
3170d71ae5a4SJacob Faibussowitsch   default:
3171d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31727128ae9fSMatthew G Knepley   }
317384330215SMatthew G. Knepley   if (sf && !isInsert) {
3174ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3175ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3176ca3d3a14SMatthew G. Knepley     Vec                tmpl;
317784330215SMatthew G. Knepley 
31789566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3179ca3d3a14SMatthew G. Knepley     if (transform) {
31809566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31819566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3182ca3d3a14SMatthew G. Knepley     } else {
31839566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3184ca3d3a14SMatthew G. Knepley     }
31859566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
31869566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3187ca3d3a14SMatthew G. Knepley     if (transform) {
31889566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31899566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3190ca3d3a14SMatthew G. Knepley     } else {
31919566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3192ca3d3a14SMatthew G. Knepley     }
31939566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
319484330215SMatthew G. Knepley   } else if (s && isInsert) {
31957128ae9fSMatthew G Knepley   } else {
3196fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalend, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
31977128ae9fSMatthew G Knepley   }
3198d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
31999566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3200d4d07f1eSToby Isaac   }
32013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
320247c6ae99SBarry Smith }
320347c6ae99SBarry Smith 
3204f089877aSRichard Tran Mills /*@
3205a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3206a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3207a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3208f089877aSRichard Tran Mills 
320920f4b53cSBarry Smith   Neighbor-wise Collective
3210f089877aSRichard Tran Mills 
3211f089877aSRichard Tran Mills   Input Parameters:
3212bb7acecfSBarry Smith + dm   - the `DM` object
3213bc0a1609SRichard Tran Mills . g    - the original local vector
3214bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3215f089877aSRichard Tran Mills 
3216bc0a1609SRichard Tran Mills   Output Parameter:
3217bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3218f089877aSRichard Tran Mills 
3219f089877aSRichard Tran Mills   Level: intermediate
3220f089877aSRichard Tran Mills 
3221a4e35b19SJacob Faibussowitsch   Notes:
3222a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3223a4e35b19SJacob Faibussowitsch 
322460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `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:
324360225df5SJacob Faibussowitsch + dm   - 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 
325260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `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`:
3318bb7acecfSBarry Smith + fine   - fine level `DM`
3319bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3320b17ce1afSJed Brown - ctx    - optional user-defined function context
3321b17ce1afSJed Brown 
332220f4b53cSBarry Smith   Calling sequence of `restricthook`:
3323bb7acecfSBarry Smith + fine      - fine level `DM`
3324bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3325c833c3b5SJed Brown . rscale    - scaling vector for restriction
3326c833c3b5SJed Brown . inject    - matrix restricting by injection
3327b17ce1afSJed Brown . coarse    - coarse level DM to update
3328b17ce1afSJed Brown - ctx       - optional user-defined function context
3329b17ce1afSJed Brown 
3330b17ce1afSJed Brown   Level: advanced
3331b17ce1afSJed Brown 
3332b17ce1afSJed Brown   Notes:
3333bb7acecfSBarry 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`.
3334b17ce1afSJed Brown 
3335b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3336b17ce1afSJed Brown 
3337b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3338bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3339b17ce1afSJed Brown 
3340bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3341bb7acecfSBarry Smith 
33421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3343b17ce1afSJed Brown @*/
3344a4e35b19SJacob Faibussowitsch PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM fine, DM coarse, void *ctx), PetscErrorCode (*restricthook)(DM fine, Mat mrestrict, Vec rscale, Mat inject, DM coarse, void *ctx), void *ctx)
3345d71ae5a4SJacob Faibussowitsch {
3346b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3347b17ce1afSJed Brown 
3348b17ce1afSJed Brown   PetscFunctionBegin;
3349b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33501e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33513ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33521e3d8eccSJed Brown   }
33539566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3354b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3355b17ce1afSJed Brown   link->restricthook = restricthook;
3356b17ce1afSJed Brown   link->ctx          = ctx;
33570298fd71SBarry Smith   link->next         = NULL;
3358b17ce1afSJed Brown   *p                 = link;
33593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3360b17ce1afSJed Brown }
3361b17ce1afSJed Brown 
3362dc822a44SJed Brown /*@C
3363bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3364dc822a44SJed Brown 
336520f4b53cSBarry Smith   Logically Collective; No Fortran Support
3366dc822a44SJed Brown 
33674165533cSJose E. Roman   Input Parameters:
3368bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3369dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3370bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
337120f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3372dc822a44SJed Brown 
3373dc822a44SJed Brown   Level: advanced
3374dc822a44SJed Brown 
3375bb7acecfSBarry Smith   Note:
3376dc822a44SJed Brown   This function does nothing if the hook is not in the list.
3377dc822a44SJed Brown 
33781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3379dc822a44SJed Brown @*/
3380d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3381d71ae5a4SJacob Faibussowitsch {
3382dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3383dc822a44SJed Brown 
3384dc822a44SJed Brown   PetscFunctionBegin;
3385dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3386dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3387dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3388dc822a44SJed Brown       link = *p;
3389dc822a44SJed Brown       *p   = link->next;
33909566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3391dc822a44SJed Brown       break;
3392dc822a44SJed Brown     }
3393dc822a44SJed Brown   }
33943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3395dc822a44SJed Brown }
3396dc822a44SJed Brown 
3397b17ce1afSJed Brown /*@
3398bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3399b17ce1afSJed Brown 
3400b17ce1afSJed Brown   Collective if any hooks are
3401b17ce1afSJed Brown 
34024165533cSJose E. Roman   Input Parameters:
3403bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3404bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3405e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3406bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
340720f4b53cSBarry Smith - coarse  - coarser `DM` to update
3408b17ce1afSJed Brown 
3409b17ce1afSJed Brown   Level: developer
3410b17ce1afSJed Brown 
341160225df5SJacob Faibussowitsch   Developer Notes:
3412bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3413bb7acecfSBarry Smith 
34141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3415b17ce1afSJed Brown @*/
3416d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3417d71ae5a4SJacob Faibussowitsch {
3418b17ce1afSJed Brown   DMCoarsenHookLink link;
3419b17ce1afSJed Brown 
3420b17ce1afSJed Brown   PetscFunctionBegin;
3421b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34221baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3423b17ce1afSJed Brown   }
34243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
342547c6ae99SBarry Smith }
342647c6ae99SBarry Smith 
3427bb9467b5SJed Brown /*@C
3428be081cd6SPeter Brune   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
34295dbd56e3SPeter Brune 
343020f4b53cSBarry Smith   Logically Collective; No Fortran Support
34315dbd56e3SPeter Brune 
34324165533cSJose E. Roman   Input Parameters:
3433bb7acecfSBarry Smith + global       - global `DM`
3434bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
34355dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
343620f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34375dbd56e3SPeter Brune 
343820f4b53cSBarry Smith   Calling sequence of `ddhook`:
3439bb7acecfSBarry Smith + global - global `DM`
3440bb7acecfSBarry Smith . block  - block `DM`
3441ec4806b8SPeter Brune - ctx    - optional user-defined function context
3442ec4806b8SPeter Brune 
344320f4b53cSBarry Smith   Calling sequence of `restricthook`:
3444bb7acecfSBarry Smith + global - global `DM`
34455dbd56e3SPeter Brune . out    - scatter to the outer (with ghost and overlap points) block vector
34465dbd56e3SPeter Brune . in     - scatter to block vector values only owned locally
3447bb7acecfSBarry Smith . block  - block `DM`
34485dbd56e3SPeter Brune - ctx    - optional user-defined function context
34495dbd56e3SPeter Brune 
34505dbd56e3SPeter Brune   Level: advanced
34515dbd56e3SPeter Brune 
34525dbd56e3SPeter Brune   Notes:
3453bb7acecfSBarry Smith   This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
34545dbd56e3SPeter Brune 
34555dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
34565dbd56e3SPeter Brune 
34575dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3458bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
34595dbd56e3SPeter Brune 
34601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
34615dbd56e3SPeter Brune @*/
3462a4e35b19SJacob Faibussowitsch PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM global, DM block, void *ctx), PetscErrorCode (*restricthook)(DM global, VecScatter out, VecScatter in, DM block, void *ctx), void *ctx)
3463d71ae5a4SJacob Faibussowitsch {
3464be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34655dbd56e3SPeter Brune 
34665dbd56e3SPeter Brune   PetscFunctionBegin;
34675dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3468b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
34693ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3470b3a6b972SJed Brown   }
34719566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
34725dbd56e3SPeter Brune   link->restricthook = restricthook;
3473be081cd6SPeter Brune   link->ddhook       = ddhook;
34745dbd56e3SPeter Brune   link->ctx          = ctx;
34750298fd71SBarry Smith   link->next         = NULL;
34765dbd56e3SPeter Brune   *p                 = link;
34773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
34785dbd56e3SPeter Brune }
34795dbd56e3SPeter Brune 
3480b3a6b972SJed Brown /*@C
3481b3a6b972SJed Brown   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3482b3a6b972SJed Brown 
348320f4b53cSBarry Smith   Logically Collective; No Fortran Support
3484b3a6b972SJed Brown 
34854165533cSJose E. Roman   Input Parameters:
3486bb7acecfSBarry Smith + global       - global `DM`
3487bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3488b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
348920f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3490b3a6b972SJed Brown 
3491b3a6b972SJed Brown   Level: advanced
3492b3a6b972SJed Brown 
34931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3494b3a6b972SJed Brown @*/
3495d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3496d71ae5a4SJacob Faibussowitsch {
3497b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3498b3a6b972SJed Brown 
3499b3a6b972SJed Brown   PetscFunctionBegin;
3500b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3501b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3502b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3503b3a6b972SJed Brown       link = *p;
3504b3a6b972SJed Brown       *p   = link->next;
35059566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3506b3a6b972SJed Brown       break;
3507b3a6b972SJed Brown     }
3508b3a6b972SJed Brown   }
35093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3510b3a6b972SJed Brown }
3511b3a6b972SJed Brown 
35125dbd56e3SPeter Brune /*@
3513bb7acecfSBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
35145dbd56e3SPeter Brune 
35155dbd56e3SPeter Brune   Collective if any hooks are
35165dbd56e3SPeter Brune 
35174165533cSJose E. Roman   Input Parameters:
3518a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3519a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3520a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3521a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
35225dbd56e3SPeter Brune 
35235dbd56e3SPeter Brune   Level: developer
35245dbd56e3SPeter Brune 
35251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`
35265dbd56e3SPeter Brune @*/
3527d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3528d71ae5a4SJacob Faibussowitsch {
3529be081cd6SPeter Brune   DMSubDomainHookLink link;
35305dbd56e3SPeter Brune 
35315dbd56e3SPeter Brune   PetscFunctionBegin;
3532be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35331baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35345dbd56e3SPeter Brune   }
35353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35365dbd56e3SPeter Brune }
35375dbd56e3SPeter Brune 
35385fe1f584SPeter Brune /*@
3539bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35405fe1f584SPeter Brune 
35415fe1f584SPeter Brune   Not Collective
35425fe1f584SPeter Brune 
35435fe1f584SPeter Brune   Input Parameter:
3544bb7acecfSBarry Smith . dm - the `DM` object
35455fe1f584SPeter Brune 
35465fe1f584SPeter Brune   Output Parameter:
35476a7d9d85SPeter Brune . level - number of coarsenings
35485fe1f584SPeter Brune 
35495fe1f584SPeter Brune   Level: developer
35505fe1f584SPeter Brune 
35511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35525fe1f584SPeter Brune @*/
3553d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3554d71ae5a4SJacob Faibussowitsch {
35555fe1f584SPeter Brune   PetscFunctionBegin;
35565fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35574f572ea9SToby Isaac   PetscAssertPointer(level, 2);
35585fe1f584SPeter Brune   *level = dm->leveldown;
35593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35605fe1f584SPeter Brune }
35615fe1f584SPeter Brune 
35629a64c4a8SMatthew G. Knepley /*@
3563bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
35649a64c4a8SMatthew G. Knepley 
356520f4b53cSBarry Smith   Collective
35669a64c4a8SMatthew G. Knepley 
35679a64c4a8SMatthew G. Knepley   Input Parameters:
3568bb7acecfSBarry Smith + dm    - the `DM` object
35699a64c4a8SMatthew G. Knepley - level - number of coarsenings
35709a64c4a8SMatthew G. Knepley 
35719a64c4a8SMatthew G. Knepley   Level: developer
35729a64c4a8SMatthew G. Knepley 
3573bb7acecfSBarry Smith   Note:
3574bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3575bb7acecfSBarry Smith 
357642747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35779a64c4a8SMatthew G. Knepley @*/
3578d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3579d71ae5a4SJacob Faibussowitsch {
35809a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
35819a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35829a64c4a8SMatthew G. Knepley   dm->leveldown = level;
35833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35849a64c4a8SMatthew G. Knepley }
35859a64c4a8SMatthew G. Knepley 
358647c6ae99SBarry Smith /*@C
3587bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
358847c6ae99SBarry Smith 
358920f4b53cSBarry Smith   Collective
359047c6ae99SBarry Smith 
3591d8d19677SJose E. Roman   Input Parameters:
3592bb7acecfSBarry Smith + dm      - the `DM` object
359347c6ae99SBarry Smith - nlevels - the number of levels of refinement
359447c6ae99SBarry Smith 
359547c6ae99SBarry Smith   Output Parameter:
3596bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
359747c6ae99SBarry Smith 
359847c6ae99SBarry Smith   Level: developer
359947c6ae99SBarry Smith 
36001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
360147c6ae99SBarry Smith @*/
3602d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3603d71ae5a4SJacob Faibussowitsch {
360447c6ae99SBarry Smith   PetscFunctionBegin;
3605171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36067a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36073ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36084f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
3609213acdd3SPierre Jolivet   if (dm->ops->refine && !dm->ops->refinehierarchy) {
361047c6ae99SBarry Smith     PetscInt i;
361147c6ae99SBarry Smith 
36129566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
361348a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3614213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
36153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
361647c6ae99SBarry Smith }
361747c6ae99SBarry Smith 
361847c6ae99SBarry Smith /*@C
3619bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
362047c6ae99SBarry Smith 
362120f4b53cSBarry Smith   Collective
362247c6ae99SBarry Smith 
3623d8d19677SJose E. Roman   Input Parameters:
3624bb7acecfSBarry Smith + dm      - the `DM` object
362547c6ae99SBarry Smith - nlevels - the number of levels of coarsening
362647c6ae99SBarry Smith 
362747c6ae99SBarry Smith   Output Parameter:
3628bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
362947c6ae99SBarry Smith 
363047c6ae99SBarry Smith   Level: developer
363147c6ae99SBarry Smith 
36321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
363347c6ae99SBarry Smith @*/
3634d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3635d71ae5a4SJacob Faibussowitsch {
363647c6ae99SBarry Smith   PetscFunctionBegin;
3637171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36387a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36393ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36404f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
3641213acdd3SPierre Jolivet   if (dm->ops->coarsen && !dm->ops->coarsenhierarchy) {
364247c6ae99SBarry Smith     PetscInt i;
364347c6ae99SBarry Smith 
36449566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
364548a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3646213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
36473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
364847c6ae99SBarry Smith }
364947c6ae99SBarry Smith 
36501a266240SBarry Smith /*@C
3651bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36521a266240SBarry Smith 
3653bb7acecfSBarry Smith   Logically Collective if the function is collective
36541a266240SBarry Smith 
36551a266240SBarry Smith   Input Parameters:
3656bb7acecfSBarry Smith + dm      - the `DM` object
36571a266240SBarry Smith - destroy - the destroy function
36581a266240SBarry Smith 
36591a266240SBarry Smith   Level: intermediate
36601a266240SBarry Smith 
36611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3662f07f9ceaSJed Brown @*/
3663d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3664d71ae5a4SJacob Faibussowitsch {
36651a266240SBarry Smith   PetscFunctionBegin;
3666171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36671a266240SBarry Smith   dm->ctxdestroy = destroy;
36683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36691a266240SBarry Smith }
36701a266240SBarry Smith 
3671b07ff414SBarry Smith /*@
3672bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
367347c6ae99SBarry Smith 
367447c6ae99SBarry Smith   Not Collective
367547c6ae99SBarry Smith 
367647c6ae99SBarry Smith   Input Parameters:
3677bb7acecfSBarry Smith + dm  - the `DM` object
367847c6ae99SBarry Smith - ctx - the user context
367947c6ae99SBarry Smith 
368047c6ae99SBarry Smith   Level: intermediate
368147c6ae99SBarry Smith 
3682bb7acecfSBarry Smith   Note:
368335cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3684bb7acecfSBarry Smith 
368560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
368647c6ae99SBarry Smith @*/
3687d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3688d71ae5a4SJacob Faibussowitsch {
368947c6ae99SBarry Smith   PetscFunctionBegin;
3690171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
369147c6ae99SBarry Smith   dm->ctx = ctx;
36923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
369347c6ae99SBarry Smith }
369447c6ae99SBarry Smith 
369547c6ae99SBarry Smith /*@
3696bb7acecfSBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object
369747c6ae99SBarry Smith 
369847c6ae99SBarry Smith   Not Collective
369947c6ae99SBarry Smith 
370047c6ae99SBarry Smith   Input Parameter:
3701bb7acecfSBarry Smith . dm - the `DM` object
370247c6ae99SBarry Smith 
370347c6ae99SBarry Smith   Output Parameter:
370447c6ae99SBarry Smith . ctx - the user context
370547c6ae99SBarry Smith 
370647c6ae99SBarry Smith   Level: intermediate
370747c6ae99SBarry Smith 
3708bb7acecfSBarry Smith   Note:
370935cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3710bb7acecfSBarry Smith 
371142747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
371247c6ae99SBarry Smith @*/
3713d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3714d71ae5a4SJacob Faibussowitsch {
371547c6ae99SBarry Smith   PetscFunctionBegin;
3716171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37171b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
371947c6ae99SBarry Smith }
372047c6ae99SBarry Smith 
372108da532bSDmitry Karpeev /*@C
3722bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
372308da532bSDmitry Karpeev 
372420f4b53cSBarry Smith   Logically Collective
372508da532bSDmitry Karpeev 
3726d8d19677SJose E. Roman   Input Parameters:
372708da532bSDmitry Karpeev + dm - the DM object
372820f4b53cSBarry Smith - f  - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
372908da532bSDmitry Karpeev 
373008da532bSDmitry Karpeev   Level: intermediate
373108da532bSDmitry Karpeev 
37321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3733db781477SPatrick Sanan          `DMSetJacobian()`
373408da532bSDmitry Karpeev @*/
3735d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3736d71ae5a4SJacob Faibussowitsch {
373708da532bSDmitry Karpeev   PetscFunctionBegin;
37385a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
373908da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
374108da532bSDmitry Karpeev }
374208da532bSDmitry Karpeev 
374308da532bSDmitry Karpeev /*@
3744bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
374508da532bSDmitry Karpeev 
374608da532bSDmitry Karpeev   Not Collective
374708da532bSDmitry Karpeev 
374808da532bSDmitry Karpeev   Input Parameter:
3749bb7acecfSBarry Smith . dm - the `DM` object to destroy
375008da532bSDmitry Karpeev 
375108da532bSDmitry Karpeev   Output Parameter:
3752bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
375308da532bSDmitry Karpeev 
375408da532bSDmitry Karpeev   Level: developer
375508da532bSDmitry Karpeev 
37561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
375708da532bSDmitry Karpeev @*/
3758d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3759d71ae5a4SJacob Faibussowitsch {
376008da532bSDmitry Karpeev   PetscFunctionBegin;
37615a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37624f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
376308da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
37643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
376508da532bSDmitry Karpeev }
376608da532bSDmitry Karpeev 
376708da532bSDmitry Karpeev /*@C
3768bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
376908da532bSDmitry Karpeev 
377020f4b53cSBarry Smith   Logically Collective
377108da532bSDmitry Karpeev 
3772f899ff85SJose E. Roman   Input Parameter:
3773bb7acecfSBarry Smith . dm - the `DM` object
377408da532bSDmitry Karpeev 
377560225df5SJacob Faibussowitsch   Output Parameters:
377608da532bSDmitry Karpeev + xl - lower bound
377708da532bSDmitry Karpeev - xu - upper bound
377808da532bSDmitry Karpeev 
3779907376e6SBarry Smith   Level: advanced
3780907376e6SBarry Smith 
378120f4b53cSBarry Smith   Note:
378295452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
378308da532bSDmitry Karpeev 
37841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
378508da532bSDmitry Karpeev @*/
3786d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3787d71ae5a4SJacob Faibussowitsch {
378808da532bSDmitry Karpeev   PetscFunctionBegin;
37895a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
379008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
37915a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3792dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
37933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
379408da532bSDmitry Karpeev }
379508da532bSDmitry Karpeev 
3796b0ae01b7SPeter Brune /*@
3797bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3798b0ae01b7SPeter Brune 
3799b0ae01b7SPeter Brune   Not Collective
3800b0ae01b7SPeter Brune 
3801b0ae01b7SPeter Brune   Input Parameter:
3802b0ae01b7SPeter Brune . dm - the DM object
3803b0ae01b7SPeter Brune 
3804b0ae01b7SPeter Brune   Output Parameter:
3805bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3806b0ae01b7SPeter Brune 
3807b0ae01b7SPeter Brune   Level: developer
3808b0ae01b7SPeter Brune 
38091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3810b0ae01b7SPeter Brune @*/
3811d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3812d71ae5a4SJacob Faibussowitsch {
3813b0ae01b7SPeter Brune   PetscFunctionBegin;
38145a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38154f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3816b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3818b0ae01b7SPeter Brune }
3819b0ae01b7SPeter Brune 
38203ad4599aSBarry Smith /*@
3821bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38223ad4599aSBarry Smith 
38233ad4599aSBarry Smith   Not Collective
38243ad4599aSBarry Smith 
38253ad4599aSBarry Smith   Input Parameter:
3826bb7acecfSBarry Smith . dm - the `DM` object
38273ad4599aSBarry Smith 
38283ad4599aSBarry Smith   Output Parameter:
3829bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38303ad4599aSBarry Smith 
38313ad4599aSBarry Smith   Level: developer
38323ad4599aSBarry Smith 
38331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38343ad4599aSBarry Smith @*/
3835d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3836d71ae5a4SJacob Faibussowitsch {
38373ad4599aSBarry Smith   PetscFunctionBegin;
38385a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38394f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
38403ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38423ad4599aSBarry Smith }
38433ad4599aSBarry Smith 
3844a7058e45SLawrence Mitchell /*@
3845bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3846a7058e45SLawrence Mitchell 
3847a7058e45SLawrence Mitchell   Not Collective
3848a7058e45SLawrence Mitchell 
3849a7058e45SLawrence Mitchell   Input Parameter:
3850bb7acecfSBarry Smith . dm - the `DM` object
3851a7058e45SLawrence Mitchell 
3852a7058e45SLawrence Mitchell   Output Parameter:
3853bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3854a7058e45SLawrence Mitchell 
3855a7058e45SLawrence Mitchell   Level: developer
3856a7058e45SLawrence Mitchell 
38571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3858a7058e45SLawrence Mitchell @*/
3859d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3860d71ae5a4SJacob Faibussowitsch {
3861a7058e45SLawrence Mitchell   PetscFunctionBegin;
38625a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38634f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3864dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3865ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
38663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3867a7058e45SLawrence Mitchell }
3868a7058e45SLawrence Mitchell 
38690298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3870264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3871264ace61SBarry Smith 
3872264ace61SBarry Smith /*@C
3873bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3874264ace61SBarry Smith 
387520f4b53cSBarry Smith   Collective
3876264ace61SBarry Smith 
3877264ace61SBarry Smith   Input Parameters:
3878bb7acecfSBarry Smith + dm     - The `DM` object
3879bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3880264ace61SBarry Smith 
3881264ace61SBarry Smith   Options Database Key:
3882bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3883264ace61SBarry Smith 
3884264ace61SBarry Smith   Level: intermediate
3885264ace61SBarry Smith 
3886bb7acecfSBarry Smith   Note:
38870462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3888bb7acecfSBarry Smith 
38891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3890264ace61SBarry Smith @*/
3891d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3892d71ae5a4SJacob Faibussowitsch {
3893264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3894264ace61SBarry Smith   PetscBool match;
3895264ace61SBarry Smith 
3896264ace61SBarry Smith   PetscFunctionBegin;
3897264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38989566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
38993ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3900264ace61SBarry Smith 
39019566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39029566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39037a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3904264ace61SBarry Smith 
3905dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39069566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39079566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39089566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3910264ace61SBarry Smith }
3911264ace61SBarry Smith 
3912264ace61SBarry Smith /*@C
3913bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3914264ace61SBarry Smith 
3915264ace61SBarry Smith   Not Collective
3916264ace61SBarry Smith 
3917264ace61SBarry Smith   Input Parameter:
3918bb7acecfSBarry Smith . dm - The `DM`
3919264ace61SBarry Smith 
3920264ace61SBarry Smith   Output Parameter:
3921bb7acecfSBarry Smith . type - The `DMType` name
3922264ace61SBarry Smith 
3923264ace61SBarry Smith   Level: intermediate
3924264ace61SBarry Smith 
39251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3926264ace61SBarry Smith @*/
3927d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3928d71ae5a4SJacob Faibussowitsch {
3929264ace61SBarry Smith   PetscFunctionBegin;
3930264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39314f572ea9SToby Isaac   PetscAssertPointer(type, 2);
39329566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3933264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3935264ace61SBarry Smith }
3936264ace61SBarry Smith 
393767a56275SMatthew G Knepley /*@C
3938bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
393967a56275SMatthew G Knepley 
394020f4b53cSBarry Smith   Collective
394167a56275SMatthew G Knepley 
394267a56275SMatthew G Knepley   Input Parameters:
3943bb7acecfSBarry Smith + dm      - the `DM`
3944bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
394567a56275SMatthew G Knepley 
394667a56275SMatthew G Knepley   Output Parameter:
3947bb7acecfSBarry Smith . M - pointer to new `DM`
394867a56275SMatthew G Knepley 
394920f4b53cSBarry Smith   Level: intermediate
395020f4b53cSBarry Smith 
395167a56275SMatthew G Knepley   Notes:
3952bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3953bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3954bb7acecfSBarry Smith   of the input `DM`.
395567a56275SMatthew G Knepley 
39561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
395767a56275SMatthew G Knepley @*/
3958d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3959d71ae5a4SJacob Faibussowitsch {
396067a56275SMatthew G Knepley   DM        B;
396167a56275SMatthew G Knepley   char      convname[256];
3962c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
396367a56275SMatthew G Knepley 
396467a56275SMatthew G Knepley   PetscFunctionBegin;
396567a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
396667a56275SMatthew G Knepley   PetscValidType(dm, 1);
39674f572ea9SToby Isaac   PetscAssertPointer(M, 3);
39689566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
39699566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3970c067b6caSMatthew G. Knepley   if (sametype) {
3971c067b6caSMatthew G. Knepley     *M = dm;
39729566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
39733ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3974c067b6caSMatthew G. Knepley   } else {
39750298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
397667a56275SMatthew G Knepley 
397767a56275SMatthew G Knepley     /*
397867a56275SMatthew G Knepley        Order of precedence:
397967a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
398067a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
398167a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
398267a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
398367a56275SMatthew G Knepley        5) Use a really basic converter.
398467a56275SMatthew G Knepley     */
398567a56275SMatthew G Knepley 
398667a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
39879566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39889566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
39899566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
39909566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
39919566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
39929566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
399367a56275SMatthew G Knepley     if (conv) goto foundconv;
399467a56275SMatthew G Knepley 
399567a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
39969566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
39979566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
39989566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39999566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40009566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40019566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40029566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40039566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
400467a56275SMatthew G Knepley     if (conv) {
40059566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
400667a56275SMatthew G Knepley       goto foundconv;
400767a56275SMatthew G Knepley     }
400867a56275SMatthew G Knepley 
400967a56275SMatthew G Knepley #if 0
401067a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
401167a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40129566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
401367a56275SMatthew G Knepley     if (conv) goto foundconv;
401467a56275SMatthew G Knepley 
401567a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
401667a56275SMatthew G Knepley     if (dm->ops->convert) {
401767a56275SMatthew G Knepley       conv = dm->ops->convert;
401867a56275SMatthew G Knepley     }
401967a56275SMatthew G Knepley     if (conv) goto foundconv;
402067a56275SMatthew G Knepley #endif
402167a56275SMatthew G Knepley 
402267a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
402398921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
402467a56275SMatthew G Knepley 
402567a56275SMatthew G Knepley   foundconv:
40269566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40279566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
402812fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
402990b157c4SStefano Zampini     {
40304fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40316858538eSMatthew G. Knepley 
40324fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40334fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4034c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40359566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40369566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40379566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40389566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
403912fa691eSMatthew G. Knepley     }
40409566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
404167a56275SMatthew G Knepley   }
40429566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
404467a56275SMatthew G Knepley }
4045264ace61SBarry Smith 
4046264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4047264ace61SBarry Smith 
4048264ace61SBarry Smith /*@C
4049bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40501c84c290SBarry Smith 
40511c84c290SBarry Smith   Not Collective
40521c84c290SBarry Smith 
40531c84c290SBarry Smith   Input Parameters:
405420f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
405520f4b53cSBarry Smith - function - The creation routine itself
405620f4b53cSBarry Smith 
405720f4b53cSBarry Smith   Level: advanced
40581c84c290SBarry Smith 
40591c84c290SBarry Smith   Notes:
406020f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
40611c84c290SBarry Smith 
406260225df5SJacob Faibussowitsch   Example Usage:
40631c84c290SBarry Smith .vb
4064bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40651c84c290SBarry Smith .ve
40661c84c290SBarry Smith 
406720f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
40681c84c290SBarry Smith .vb
40691c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
40701c84c290SBarry Smith     DMSetType(DM,"my_da");
40711c84c290SBarry Smith .ve
40721c84c290SBarry Smith   or at runtime via the option
40731c84c290SBarry Smith .vb
40741c84c290SBarry Smith     -da_type my_da
40751c84c290SBarry Smith .ve
4076264ace61SBarry Smith 
40771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4078264ace61SBarry Smith @*/
4079d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4080d71ae5a4SJacob Faibussowitsch {
4081264ace61SBarry Smith   PetscFunctionBegin;
40829566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
40839566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
40843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4085264ace61SBarry Smith }
4086264ace61SBarry Smith 
4087b859378eSBarry Smith /*@C
4088bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4089b859378eSBarry Smith 
409020f4b53cSBarry Smith   Collective
4091b859378eSBarry Smith 
4092b859378eSBarry Smith   Input Parameters:
4093bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4094bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4095bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4096bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4097b859378eSBarry Smith 
4098b859378eSBarry Smith   Level: intermediate
4099b859378eSBarry Smith 
4100b859378eSBarry Smith   Notes:
410155849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4102b859378eSBarry Smith 
4103bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4104bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4105bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4106cd7e8a5eSksagiyam 
41071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4108b859378eSBarry Smith @*/
4109d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4110d71ae5a4SJacob Faibussowitsch {
41119331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4112b859378eSBarry Smith 
4113b859378eSBarry Smith   PetscFunctionBegin;
4114b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4115b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41169566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41179566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41189566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41199566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41209331c7a4SMatthew G. Knepley   if (isbinary) {
41219331c7a4SMatthew G. Knepley     PetscInt classid;
41229331c7a4SMatthew G. Knepley     char     type[256];
4123b859378eSBarry Smith 
41249566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41257a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41269566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41279566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4128dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41299331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4130dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41319331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41329566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4134b859378eSBarry Smith }
4135b859378eSBarry Smith 
41367da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41377da65231SMatthew G Knepley 
41382ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[])
41392ecf6ec3SMatthew G. Knepley {
41402ecf6ec3SMatthew G. Knepley   PetscInt f;
41412ecf6ec3SMatthew G. Knepley 
41422ecf6ec3SMatthew G. Knepley   PetscFunctionBegin;
41432ecf6ec3SMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41442ecf6ec3SMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %" PetscInt_FMT " |\n", x[f]));
41452ecf6ec3SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41462ecf6ec3SMatthew G. Knepley }
41472ecf6ec3SMatthew G. Knepley 
4148d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4149d71ae5a4SJacob Faibussowitsch {
41501d47ebbbSSatish Balay   PetscInt f;
41511b30c384SMatthew G Knepley 
41527da65231SMatthew G Knepley   PetscFunctionBegin;
415363a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
415448a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41567da65231SMatthew G Knepley }
41577da65231SMatthew G Knepley 
41585962854dSMatthew G. Knepley PetscErrorCode DMPrintCellVectorReal(PetscInt c, const char name[], PetscInt len, const PetscReal x[])
41595962854dSMatthew G. Knepley {
41605962854dSMatthew G. Knepley   PetscInt f;
41615962854dSMatthew G. Knepley 
41625962854dSMatthew G. Knepley   PetscFunctionBegin;
41635962854dSMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41645962854dSMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)x[f]));
41655962854dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41665962854dSMatthew G. Knepley }
41675962854dSMatthew G. Knepley 
4168d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4169d71ae5a4SJacob Faibussowitsch {
41701b30c384SMatthew G Knepley   PetscInt f, g;
41717da65231SMatthew G Knepley 
41727da65231SMatthew G Knepley   PetscFunctionBegin;
417363a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41741d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
41759566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
417648a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
41779566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
41787da65231SMatthew G Knepley   }
41793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41807da65231SMatthew G Knepley }
4181e7c4fc90SDmitry Karpeev 
4182d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4183d71ae5a4SJacob Faibussowitsch {
41840c5b8624SToby Isaac   PetscInt           localSize, bs;
41850c5b8624SToby Isaac   PetscMPIInt        size;
41860c5b8624SToby Isaac   Vec                x, xglob;
41870c5b8624SToby Isaac   const PetscScalar *xarray;
4188e759306cSMatthew G. Knepley 
4189e759306cSMatthew G. Knepley   PetscFunctionBegin;
41909566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
41919566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
41929566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
419393ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
41949566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
41950c5b8624SToby Isaac   if (size > 1) {
41969566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
41979566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
41989566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
41999566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42000c5b8624SToby Isaac   } else {
42010c5b8624SToby Isaac     xglob = x;
42020c5b8624SToby Isaac   }
42039566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42040c5b8624SToby Isaac   if (size > 1) {
42059566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42069566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42070c5b8624SToby Isaac   }
42089566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
42093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4210e759306cSMatthew G. Knepley }
4211e759306cSMatthew G. Knepley 
421288ed4aceSMatthew G Knepley /*@
4213bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4214061576a5SJed Brown 
4215061576a5SJed Brown   Input Parameter:
4216bb7acecfSBarry Smith . dm - The `DM`
4217061576a5SJed Brown 
4218061576a5SJed Brown   Output Parameter:
4219bb7acecfSBarry Smith . section - The `PetscSection`
4220061576a5SJed Brown 
422120f4b53cSBarry Smith   Options Database Key:
4222bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4223061576a5SJed Brown 
4224061576a5SJed Brown   Level: advanced
4225061576a5SJed Brown 
4226061576a5SJed Brown   Notes:
4227bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4228061576a5SJed Brown 
4229bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4230061576a5SJed Brown 
42311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4232061576a5SJed Brown @*/
4233d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4234d71ae5a4SJacob Faibussowitsch {
4235061576a5SJed Brown   PetscFunctionBegin;
42369566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4238061576a5SJed Brown }
4239061576a5SJed Brown 
4240061576a5SJed Brown /*@
4241bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
424288ed4aceSMatthew G Knepley 
424388ed4aceSMatthew G Knepley   Input Parameter:
4244bb7acecfSBarry Smith . dm - The `DM`
424588ed4aceSMatthew G Knepley 
424688ed4aceSMatthew G Knepley   Output Parameter:
4247bb7acecfSBarry Smith . section - The `PetscSection`
424888ed4aceSMatthew G Knepley 
424920f4b53cSBarry Smith   Options Database Key:
4250bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4251e5893cccSMatthew G. Knepley 
425288ed4aceSMatthew G Knepley   Level: intermediate
425388ed4aceSMatthew G Knepley 
4254bb7acecfSBarry Smith   Note:
4255bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
425688ed4aceSMatthew G Knepley 
42571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
425888ed4aceSMatthew G Knepley @*/
4259d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4260d71ae5a4SJacob Faibussowitsch {
426188ed4aceSMatthew G Knepley   PetscFunctionBegin;
426288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42634f572ea9SToby Isaac   PetscAssertPointer(section, 2);
42641bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4265e5e52638SMatthew G. Knepley     PetscInt d;
4266e5e52638SMatthew G. Knepley 
426745480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
426845480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
426945480ffeSMatthew G. Knepley       PetscViewer       viewer;
427045480ffeSMatthew G. Knepley       PetscViewerFormat format;
427145480ffeSMatthew G. Knepley       PetscBool         flg;
427245480ffeSMatthew G. Knepley 
42739566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
42749566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
427545480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
42769566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
42779566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
427845480ffeSMatthew G. Knepley       }
427945480ffeSMatthew G. Knepley       if (flg) {
42809566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
42819566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
4282cd791dc2SBarry Smith         PetscCall(PetscOptionsRestoreViewer(&viewer));
428345480ffeSMatthew G. Knepley       }
428445480ffeSMatthew G. Knepley     }
4285dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
42869566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
42872f0f8703SMatthew G. Knepley   }
42881bb6d2a8SBarry Smith   *section = dm->localSection;
42893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
429088ed4aceSMatthew G Knepley }
429188ed4aceSMatthew G Knepley 
429288ed4aceSMatthew G Knepley /*@
4293bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4294061576a5SJed Brown 
4295061576a5SJed Brown   Input Parameters:
4296bb7acecfSBarry Smith + dm      - The `DM`
4297bb7acecfSBarry Smith - section - The `PetscSection`
4298061576a5SJed Brown 
4299061576a5SJed Brown   Level: advanced
4300061576a5SJed Brown 
4301061576a5SJed Brown   Notes:
4302bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4303061576a5SJed Brown 
4304bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4305061576a5SJed Brown 
43061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4307061576a5SJed Brown @*/
4308d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4309d71ae5a4SJacob Faibussowitsch {
4310061576a5SJed Brown   PetscFunctionBegin;
43119566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
43123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4313061576a5SJed Brown }
4314061576a5SJed Brown 
4315061576a5SJed Brown /*@
4316bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
431788ed4aceSMatthew G Knepley 
431888ed4aceSMatthew G Knepley   Input Parameters:
4319bb7acecfSBarry Smith + dm      - The `DM`
4320bb7acecfSBarry Smith - section - The `PetscSection`
432188ed4aceSMatthew G Knepley 
432288ed4aceSMatthew G Knepley   Level: intermediate
432388ed4aceSMatthew G Knepley 
4324bb7acecfSBarry Smith   Note:
4325bb7acecfSBarry Smith   Any existing Section will be destroyed
432688ed4aceSMatthew G Knepley 
43271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
432888ed4aceSMatthew G Knepley @*/
4329d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4330d71ae5a4SJacob Faibussowitsch {
4331c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4332af122d2aSMatthew G Knepley   PetscInt f;
433388ed4aceSMatthew G Knepley 
433488ed4aceSMatthew G Knepley   PetscFunctionBegin;
433588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4336b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43379566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43389566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43391bb6d2a8SBarry Smith   dm->localSection = section;
43409566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4341af122d2aSMatthew G Knepley   if (numFields) {
43429566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4343af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43440f21e855SMatthew G. Knepley       PetscObject disc;
4345af122d2aSMatthew G Knepley       const char *name;
4346af122d2aSMatthew G Knepley 
43479566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43489566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43499566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4350af122d2aSMatthew G Knepley     }
4351af122d2aSMatthew G Knepley   }
4352e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43539566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
43543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
435588ed4aceSMatthew G Knepley }
435688ed4aceSMatthew G Knepley 
4357*adc21957SMatthew G. Knepley /*@C
4358*adc21957SMatthew G. Knepley   DMCreateSectionPermutation - Create a permutation of the `PetscSection` chart and optionally a blokc structure.
4359*adc21957SMatthew G. Knepley 
4360*adc21957SMatthew G. Knepley   Input Parameter:
4361*adc21957SMatthew G. Knepley . dm - The `DM`
4362*adc21957SMatthew G. Knepley 
4363*adc21957SMatthew G. Knepley   Output Parameter:
4364*adc21957SMatthew G. Knepley + perm        - A permutation of the mesh points in the chart
4365*adc21957SMatthew G. Knepley - blockStarts - A high bit is set for the point that begins every block, or NULL for default blocking
4366*adc21957SMatthew G. Knepley 
4367*adc21957SMatthew G. Knepley   Level: developer
4368*adc21957SMatthew G. Knepley 
4369*adc21957SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMGetGlobalSection()`
4370*adc21957SMatthew G. Knepley @*/
4371*adc21957SMatthew G. Knepley PetscErrorCode DMCreateSectionPermutation(DM dm, IS *perm, PetscBT *blockStarts)
4372*adc21957SMatthew G. Knepley {
4373*adc21957SMatthew G. Knepley   PetscFunctionBegin;
4374*adc21957SMatthew G. Knepley   *perm        = NULL;
4375*adc21957SMatthew G. Knepley   *blockStarts = NULL;
4376*adc21957SMatthew G. Knepley   PetscTryTypeMethod(dm, createsectionpermutation, perm, blockStarts);
4377*adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
4378*adc21957SMatthew G. Knepley }
4379*adc21957SMatthew G. Knepley 
43809435951eSToby Isaac /*@
4381bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
43829435951eSToby Isaac 
438320f4b53cSBarry Smith   not Collective
4384e228b242SToby Isaac 
43859435951eSToby Isaac   Input Parameter:
4386bb7acecfSBarry Smith . dm - The `DM`
43879435951eSToby Isaac 
4388d8d19677SJose E. Roman   Output Parameters:
438920f4b53cSBarry 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.
439020f4b53cSBarry 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.
439179769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
43929435951eSToby Isaac 
43939435951eSToby Isaac   Level: advanced
43949435951eSToby Isaac 
4395bb7acecfSBarry Smith   Note:
4396bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
43979435951eSToby Isaac 
43981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
43999435951eSToby Isaac @*/
4400d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4401d71ae5a4SJacob Faibussowitsch {
44029435951eSToby Isaac   PetscFunctionBegin;
44039435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4404dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44053b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44063b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
440779769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44099435951eSToby Isaac }
44109435951eSToby Isaac 
44119435951eSToby Isaac /*@
4412bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44139435951eSToby Isaac 
441420f4b53cSBarry Smith   Collective
4415e228b242SToby Isaac 
44169435951eSToby Isaac   Input Parameters:
4417bb7acecfSBarry Smith + dm      - The `DM`
4418bb7acecfSBarry 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).
441920f4b53cSBarry 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).
442020f4b53cSBarry 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).
44219435951eSToby Isaac 
44229435951eSToby Isaac   Level: advanced
44239435951eSToby Isaac 
442420f4b53cSBarry Smith   Notes:
442520f4b53cSBarry 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()`.
442620f4b53cSBarry Smith 
442720f4b53cSBarry 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.
442820f4b53cSBarry Smith 
4429bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44309435951eSToby Isaac 
44311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
44329435951eSToby Isaac @*/
4433d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4434d71ae5a4SJacob Faibussowitsch {
4435e228b242SToby Isaac   PetscMPIInt result;
44369435951eSToby Isaac 
44379435951eSToby Isaac   PetscFunctionBegin;
44389435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4439e228b242SToby Isaac   if (section) {
4440e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44419566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44427a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4443e228b242SToby Isaac   }
4444e228b242SToby Isaac   if (mat) {
4445e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44469566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44477a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4448e228b242SToby Isaac   }
444979769bd5SJed Brown   if (bias) {
445079769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44519566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
445279769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
445379769bd5SJed Brown   }
44549566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44559566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44563b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44579566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44589566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44593b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44609566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44619566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
446279769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44649435951eSToby Isaac }
44659435951eSToby Isaac 
4466497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4467507e4973SMatthew G. Knepley /*
4468bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4469507e4973SMatthew G. Knepley 
4470507e4973SMatthew G. Knepley   Input Parameters:
4471bb7acecfSBarry Smith + dm - The `DM`
4472bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4473bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4474507e4973SMatthew G. Knepley 
4475507e4973SMatthew G. Knepley   Level: intermediate
4476507e4973SMatthew G. Knepley 
44771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4478507e4973SMatthew G. Knepley */
4479d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4480d71ae5a4SJacob Faibussowitsch {
4481507e4973SMatthew G. Knepley   MPI_Comm        comm;
4482507e4973SMatthew G. Knepley   PetscLayout     layout;
4483507e4973SMatthew G. Knepley   const PetscInt *ranges;
4484507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4485507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4486507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4487507e4973SMatthew G. Knepley 
4488507e4973SMatthew G. Knepley   PetscFunctionBegin;
44899566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4490507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44919566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
44929566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44939566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
44949566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
44959566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
44969566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
44979566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
44989566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
44999566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4500507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4501f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4502507e4973SMatthew G. Knepley 
45039566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45049566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45059566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45069566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45079566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45089566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4509507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45109371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45119371c9d4SSatish 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));
45129371c9d4SSatish Balay       valid = PETSC_FALSE;
45139371c9d4SSatish Balay     }
45149371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45159371c9d4SSatish 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));
45169371c9d4SSatish Balay       valid = PETSC_FALSE;
45179371c9d4SSatish Balay     }
4518507e4973SMatthew G. Knepley     if (gdof < 0) {
4519507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4520507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4521507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4522507e4973SMatthew G. Knepley 
45239566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4524507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45259371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45269371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45279371c9d4SSatish Balay           valid = PETSC_FALSE;
45289371c9d4SSatish Balay           break;
45299371c9d4SSatish Balay         }
4530507e4973SMatthew G. Knepley       }
4531507e4973SMatthew G. Knepley     }
4532507e4973SMatthew G. Knepley   }
45339566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45349566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
45351c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4536507e4973SMatthew G. Knepley   if (!gvalid) {
45379566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4538507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4539507e4973SMatthew G. Knepley   }
45403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4541507e4973SMatthew G. Knepley }
4542f741bcd2SMatthew G. Knepley #endif
4543507e4973SMatthew G. Knepley 
45445f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45456725e60dSJed Brown {
45466725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45476725e60dSJed Brown   PetscFunctionBegin;
45486725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45494f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
45505f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45516725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
45526725e60dSJed Brown   else *sf = dm->sf;
45533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45546725e60dSJed Brown }
45556725e60dSJed Brown 
455688ed4aceSMatthew G Knepley /*@
4557bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
455888ed4aceSMatthew G Knepley 
455920f4b53cSBarry Smith   Collective
45608b1ab98fSJed Brown 
456188ed4aceSMatthew G Knepley   Input Parameter:
4562bb7acecfSBarry Smith . dm - The `DM`
456388ed4aceSMatthew G Knepley 
456488ed4aceSMatthew G Knepley   Output Parameter:
4565bb7acecfSBarry Smith . section - The `PetscSection`
456688ed4aceSMatthew G Knepley 
456788ed4aceSMatthew G Knepley   Level: intermediate
456888ed4aceSMatthew G Knepley 
4569bb7acecfSBarry Smith   Note:
4570bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
457188ed4aceSMatthew G Knepley 
45721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
457388ed4aceSMatthew G Knepley @*/
4574d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4575d71ae5a4SJacob Faibussowitsch {
457688ed4aceSMatthew G Knepley   PetscFunctionBegin;
457788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45784f572ea9SToby Isaac   PetscAssertPointer(section, 2);
45791bb6d2a8SBarry Smith   if (!dm->globalSection) {
4580fd59a867SMatthew G. Knepley     PetscSection s;
45816725e60dSJed Brown     PetscSF      sf;
4582fd59a867SMatthew G. Knepley 
45839566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
45847a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
45857a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45865f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
45876725e60dSJed Brown     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
45889566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
45899566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
45909566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
459188ed4aceSMatthew G Knepley   }
45921bb6d2a8SBarry Smith   *section = dm->globalSection;
45933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
459488ed4aceSMatthew G Knepley }
459588ed4aceSMatthew G Knepley 
4596b21d0597SMatthew G Knepley /*@
4597bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4598b21d0597SMatthew G Knepley 
4599b21d0597SMatthew G Knepley   Input Parameters:
4600bb7acecfSBarry Smith + dm      - The `DM`
46012fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4602b21d0597SMatthew G Knepley 
4603b21d0597SMatthew G Knepley   Level: intermediate
4604b21d0597SMatthew G Knepley 
4605bb7acecfSBarry Smith   Note:
4606bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4607b21d0597SMatthew G Knepley 
46081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4609b21d0597SMatthew G Knepley @*/
4610d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4611d71ae5a4SJacob Faibussowitsch {
4612b21d0597SMatthew G Knepley   PetscFunctionBegin;
4613b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46145080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46159566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46169566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46171bb6d2a8SBarry Smith   dm->globalSection = section;
4618497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46199566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4620507e4973SMatthew G. Knepley #endif
46213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4622b21d0597SMatthew G Knepley }
4623b21d0597SMatthew G Knepley 
462488ed4aceSMatthew G Knepley /*@
4625bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4626bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
462788ed4aceSMatthew G Knepley 
462888ed4aceSMatthew G Knepley   Input Parameter:
4629bb7acecfSBarry Smith . dm - The `DM`
463088ed4aceSMatthew G Knepley 
463188ed4aceSMatthew G Knepley   Output Parameter:
4632bb7acecfSBarry Smith . sf - The `PetscSF`
463388ed4aceSMatthew G Knepley 
463488ed4aceSMatthew G Knepley   Level: intermediate
463588ed4aceSMatthew G Knepley 
4636bb7acecfSBarry Smith   Note:
4637bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
463888ed4aceSMatthew G Knepley 
46391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
464088ed4aceSMatthew G Knepley @*/
4641d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4642d71ae5a4SJacob Faibussowitsch {
464388ed4aceSMatthew G Knepley   PetscInt nroots;
464488ed4aceSMatthew G Knepley 
464588ed4aceSMatthew G Knepley   PetscFunctionBegin;
464688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46474f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
464848a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
46499566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
465088ed4aceSMatthew G Knepley   if (nroots < 0) {
465188ed4aceSMatthew G Knepley     PetscSection section, gSection;
465288ed4aceSMatthew G Knepley 
46539566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
465431ea6d37SMatthew G Knepley     if (section) {
46559566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
46569566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
465731ea6d37SMatthew G Knepley     } else {
46580298fd71SBarry Smith       *sf = NULL;
46593ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
466031ea6d37SMatthew G Knepley     }
466188ed4aceSMatthew G Knepley   }
46621bb6d2a8SBarry Smith   *sf = dm->sectionSF;
46633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
466488ed4aceSMatthew G Knepley }
466588ed4aceSMatthew G Knepley 
466688ed4aceSMatthew G Knepley /*@
4667bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
466888ed4aceSMatthew G Knepley 
466988ed4aceSMatthew G Knepley   Input Parameters:
4670bb7acecfSBarry Smith + dm - The `DM`
4671bb7acecfSBarry Smith - sf - The `PetscSF`
467288ed4aceSMatthew G Knepley 
467388ed4aceSMatthew G Knepley   Level: intermediate
467488ed4aceSMatthew G Knepley 
4675bb7acecfSBarry Smith   Note:
4676bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
467788ed4aceSMatthew G Knepley 
46781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
467988ed4aceSMatthew G Knepley @*/
4680d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4681d71ae5a4SJacob Faibussowitsch {
468288ed4aceSMatthew G Knepley   PetscFunctionBegin;
468388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4684b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46859566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46869566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
46871bb6d2a8SBarry Smith   dm->sectionSF = sf;
46883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
468988ed4aceSMatthew G Knepley }
469088ed4aceSMatthew G Knepley 
469188ed4aceSMatthew G Knepley /*@C
4692bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
469388ed4aceSMatthew G Knepley   describing the data layout.
469488ed4aceSMatthew G Knepley 
469588ed4aceSMatthew G Knepley   Input Parameters:
4696bb7acecfSBarry Smith + dm            - The `DM`
4697bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4698bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
469988ed4aceSMatthew G Knepley 
47001bb6d2a8SBarry Smith   Level: developer
47011bb6d2a8SBarry Smith 
4702bb7acecfSBarry Smith   Note:
4703bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4704bb7acecfSBarry Smith 
470560225df5SJacob Faibussowitsch   Developer Notes:
4706bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4707bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4708bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
47091bb6d2a8SBarry Smith 
47101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
471188ed4aceSMatthew G Knepley @*/
4712d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4713d71ae5a4SJacob Faibussowitsch {
471488ed4aceSMatthew G Knepley   PetscFunctionBegin;
471588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47169566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
47173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
471888ed4aceSMatthew G Knepley }
4719af122d2aSMatthew G Knepley 
4720b21d0597SMatthew G Knepley /*@
4721bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4722bb7acecfSBarry Smith 
4723bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4724b21d0597SMatthew G Knepley 
4725b21d0597SMatthew G Knepley   Input Parameter:
4726bb7acecfSBarry Smith . dm - The `DM`
4727b21d0597SMatthew G Knepley 
4728b21d0597SMatthew G Knepley   Output Parameter:
4729bb7acecfSBarry Smith . sf - The `PetscSF`
4730b21d0597SMatthew G Knepley 
4731b21d0597SMatthew G Knepley   Level: intermediate
4732b21d0597SMatthew G Knepley 
4733bb7acecfSBarry Smith   Note:
4734bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4735b21d0597SMatthew G Knepley 
47361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4737b21d0597SMatthew G Knepley @*/
4738d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4739d71ae5a4SJacob Faibussowitsch {
4740b21d0597SMatthew G Knepley   PetscFunctionBegin;
4741b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47424f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4743b21d0597SMatthew G Knepley   *sf = dm->sf;
47443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4745b21d0597SMatthew G Knepley }
4746b21d0597SMatthew G Knepley 
4747057b4bcdSMatthew G Knepley /*@
4748bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4749bb7acecfSBarry Smith 
475020f4b53cSBarry Smith   Collective
4751057b4bcdSMatthew G Knepley 
4752057b4bcdSMatthew G Knepley   Input Parameters:
4753bb7acecfSBarry Smith + dm - The `DM`
4754bb7acecfSBarry Smith - sf - The `PetscSF`
4755057b4bcdSMatthew G Knepley 
4756057b4bcdSMatthew G Knepley   Level: intermediate
4757057b4bcdSMatthew G Knepley 
47581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4759057b4bcdSMatthew G Knepley @*/
4760d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4761d71ae5a4SJacob Faibussowitsch {
4762057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4763057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4764b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47659566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47669566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4767057b4bcdSMatthew G Knepley   dm->sf = sf;
47683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4769057b4bcdSMatthew G Knepley }
4770057b4bcdSMatthew G Knepley 
47714f37162bSMatthew G. Knepley /*@
4772bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
47734f37162bSMatthew G. Knepley 
47744f37162bSMatthew G. Knepley   Input Parameter:
4775bb7acecfSBarry Smith . dm - The `DM`
47764f37162bSMatthew G. Knepley 
47774f37162bSMatthew G. Knepley   Output Parameter:
4778bb7acecfSBarry Smith . sf - The `PetscSF`
47794f37162bSMatthew G. Knepley 
47804f37162bSMatthew G. Knepley   Level: intermediate
47814f37162bSMatthew G. Knepley 
4782bb7acecfSBarry Smith   Note:
4783bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
47844f37162bSMatthew G. Knepley 
47851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47864f37162bSMatthew G. Knepley @*/
4787d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4788d71ae5a4SJacob Faibussowitsch {
47894f37162bSMatthew G. Knepley   PetscFunctionBegin;
47904f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47914f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
47924f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
47933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
47944f37162bSMatthew G. Knepley }
47954f37162bSMatthew G. Knepley 
47964f37162bSMatthew G. Knepley /*@
47974f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
47984f37162bSMatthew G. Knepley 
47994f37162bSMatthew G. Knepley   Input Parameters:
48004f37162bSMatthew G. Knepley + dm - The DM
48014f37162bSMatthew G. Knepley - sf - The PetscSF
48024f37162bSMatthew G. Knepley 
48034f37162bSMatthew G. Knepley   Level: intermediate
48044f37162bSMatthew G. Knepley 
48051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48064f37162bSMatthew G. Knepley @*/
4807d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4808d71ae5a4SJacob Faibussowitsch {
48094f37162bSMatthew G. Knepley   PetscFunctionBegin;
48104f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48114f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48129566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48139566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48144f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48164f37162bSMatthew G. Knepley }
48174f37162bSMatthew G. Knepley 
4818d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4819d71ae5a4SJacob Faibussowitsch {
482034aa8a36SMatthew G. Knepley   PetscClassId id;
482134aa8a36SMatthew G. Knepley 
482234aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48239566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
482434aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48259566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
482634aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48279566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
482817c1d62eSMatthew G. Knepley   } else {
48299566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
483034aa8a36SMatthew G. Knepley   }
48313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
483234aa8a36SMatthew G. Knepley }
483334aa8a36SMatthew G. Knepley 
4834d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4835d71ae5a4SJacob Faibussowitsch {
483644a7f3ddSMatthew G. Knepley   RegionField *tmpr;
483744a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
483844a7f3ddSMatthew G. Knepley 
483944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
48403ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
48419566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
484244a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
48439371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
48449371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48459371c9d4SSatish Balay     tmpr[f].label       = NULL;
48469371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
48479371c9d4SSatish Balay   }
48489566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
484944a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
485044a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
48513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
485244a7f3ddSMatthew G. Knepley }
485344a7f3ddSMatthew G. Knepley 
485444a7f3ddSMatthew G. Knepley /*@
485520f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
485644a7f3ddSMatthew G. Knepley 
485720f4b53cSBarry Smith   Logically Collective
485844a7f3ddSMatthew G. Knepley 
485944a7f3ddSMatthew G. Knepley   Input Parameter:
486020f4b53cSBarry Smith . dm - The `DM`
486144a7f3ddSMatthew G. Knepley 
486244a7f3ddSMatthew G. Knepley   Level: intermediate
486344a7f3ddSMatthew G. Knepley 
48641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
486544a7f3ddSMatthew G. Knepley @*/
4866d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4867d71ae5a4SJacob Faibussowitsch {
486844a7f3ddSMatthew G. Knepley   PetscInt f;
486944a7f3ddSMatthew G. Knepley 
487044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
487144a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
487244a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
48739566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
48749566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
487544a7f3ddSMatthew G. Knepley   }
48769566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
487744a7f3ddSMatthew G. Knepley   dm->fields = NULL;
487844a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
48793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
488044a7f3ddSMatthew G. Knepley }
488144a7f3ddSMatthew G. Knepley 
4882689b5837SMatthew G. Knepley /*@
488320f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4884689b5837SMatthew G. Knepley 
488520f4b53cSBarry Smith   Not Collective
4886689b5837SMatthew G. Knepley 
4887689b5837SMatthew G. Knepley   Input Parameter:
488820f4b53cSBarry Smith . dm - The `DM`
4889689b5837SMatthew G. Knepley 
4890689b5837SMatthew G. Knepley   Output Parameter:
489160225df5SJacob Faibussowitsch . numFields - The number of fields
4892689b5837SMatthew G. Knepley 
4893689b5837SMatthew G. Knepley   Level: intermediate
4894689b5837SMatthew G. Knepley 
48951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4896689b5837SMatthew G. Knepley @*/
4897d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4898d71ae5a4SJacob Faibussowitsch {
48990f21e855SMatthew G. Knepley   PetscFunctionBegin;
49000f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49014f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
490244a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
49033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4904af122d2aSMatthew G Knepley }
4905af122d2aSMatthew G Knepley 
4906689b5837SMatthew G. Knepley /*@
490720f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4908689b5837SMatthew G. Knepley 
490920f4b53cSBarry Smith   Logically Collective
4910689b5837SMatthew G. Knepley 
4911689b5837SMatthew G. Knepley   Input Parameters:
491220f4b53cSBarry Smith + dm        - The `DM`
491360225df5SJacob Faibussowitsch - numFields - The number of fields
4914689b5837SMatthew G. Knepley 
4915689b5837SMatthew G. Knepley   Level: intermediate
4916689b5837SMatthew G. Knepley 
49171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4918689b5837SMatthew G. Knepley @*/
4919d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4920d71ae5a4SJacob Faibussowitsch {
49210f21e855SMatthew G. Knepley   PetscInt Nf, f;
4922af122d2aSMatthew G Knepley 
4923af122d2aSMatthew G Knepley   PetscFunctionBegin;
4924af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49259566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49260f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49270f21e855SMatthew G. Knepley     PetscContainer obj;
49280f21e855SMatthew G. Knepley 
49299566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49309566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
49319566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4932af122d2aSMatthew G Knepley   }
49333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4934af122d2aSMatthew G Knepley }
4935af122d2aSMatthew G Knepley 
4936c1929be8SMatthew G. Knepley /*@
4937bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4938c1929be8SMatthew G. Knepley 
493920f4b53cSBarry Smith   Not Collective
4940c1929be8SMatthew G. Knepley 
4941c1929be8SMatthew G. Knepley   Input Parameters:
4942bb7acecfSBarry Smith + dm - The `DM`
4943c1929be8SMatthew G. Knepley - f  - The field number
4944c1929be8SMatthew G. Knepley 
494544a7f3ddSMatthew G. Knepley   Output Parameters:
494620f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
494720f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
4948c1929be8SMatthew G. Knepley 
494944a7f3ddSMatthew G. Knepley   Level: intermediate
4950c1929be8SMatthew G. Knepley 
49511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
4952c1929be8SMatthew G. Knepley @*/
4953d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4954d71ae5a4SJacob Faibussowitsch {
4955af122d2aSMatthew G Knepley   PetscFunctionBegin;
4956af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49574f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
49587a8be351SBarry 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);
495944a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4960bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
49613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4962decb47aaSMatthew G. Knepley }
4963decb47aaSMatthew G. Knepley 
4964083401c6SMatthew G. Knepley /* Does not clear the DS */
4965d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4966d71ae5a4SJacob Faibussowitsch {
4967083401c6SMatthew G. Knepley   PetscFunctionBegin;
49689566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
49699566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
49709566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4971083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4972bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
49739566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4974bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
49753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4976083401c6SMatthew G. Knepley }
4977083401c6SMatthew G. Knepley 
497846560f82SMatthew G. Knepley /*@C
4979bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
4980bb7acecfSBarry Smith   the field numbering.
4981c1929be8SMatthew G. Knepley 
498220f4b53cSBarry Smith   Logically Collective
4983c1929be8SMatthew G. Knepley 
4984c1929be8SMatthew G. Knepley   Input Parameters:
4985bb7acecfSBarry Smith + dm    - The `DM`
4986c1929be8SMatthew G. Knepley . f     - The field number
498720f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
4988bb7acecfSBarry Smith - disc  - The discretization object
4989c1929be8SMatthew G. Knepley 
499044a7f3ddSMatthew G. Knepley   Level: intermediate
4991c1929be8SMatthew G. Knepley 
49921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
4993c1929be8SMatthew G. Knepley @*/
4994d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4995d71ae5a4SJacob Faibussowitsch {
4996decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4997decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4998e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
4999bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50007a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5001bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5002bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50039566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
500544a7f3ddSMatthew G. Knepley }
500644a7f3ddSMatthew G. Knepley 
500746560f82SMatthew G. Knepley /*@C
5008bb7acecfSBarry 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)
5009bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
501044a7f3ddSMatthew G. Knepley 
501120f4b53cSBarry Smith   Logically Collective
501244a7f3ddSMatthew G. Knepley 
501344a7f3ddSMatthew G. Knepley   Input Parameters:
5014bb7acecfSBarry Smith + dm    - The `DM`
501520f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5016bb7acecfSBarry Smith - disc  - The discretization object
501744a7f3ddSMatthew G. Knepley 
501844a7f3ddSMatthew G. Knepley   Level: intermediate
501944a7f3ddSMatthew G. Knepley 
5020bb7acecfSBarry Smith   Notes:
5021bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5022bb7acecfSBarry Smith 
5023da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5024bb7acecfSBarry 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
5025bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5026bb7acecfSBarry Smith 
50271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
502844a7f3ddSMatthew G. Knepley @*/
5029d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5030d71ae5a4SJacob Faibussowitsch {
503144a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
503244a7f3ddSMatthew G. Knepley 
503344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
503444a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5035064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5036bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50379566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
503844a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5039bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50409566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5041bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5042bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50439566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5045af122d2aSMatthew G Knepley }
50466636e97aSMatthew G Knepley 
5047e5e52638SMatthew G. Knepley /*@
5048e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5049e0b68406SMatthew Knepley 
505020f4b53cSBarry Smith   Logically Collective
5051e0b68406SMatthew Knepley 
5052e0b68406SMatthew Knepley   Input Parameters:
5053bb7acecfSBarry Smith + dm          - The `DM`
5054e0b68406SMatthew Knepley . f           - The field index
5055bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5056e0b68406SMatthew Knepley 
5057e0b68406SMatthew Knepley   Level: intermediate
5058e0b68406SMatthew Knepley 
50591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5060e0b68406SMatthew Knepley @*/
5061d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5062d71ae5a4SJacob Faibussowitsch {
5063e0b68406SMatthew Knepley   PetscFunctionBegin;
506463a3b9bcSJacob 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);
5065e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
50663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5067e0b68406SMatthew Knepley }
5068e0b68406SMatthew Knepley 
5069e0b68406SMatthew Knepley /*@
5070e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5071e0b68406SMatthew Knepley 
507220f4b53cSBarry Smith   Not Collective
5073e0b68406SMatthew Knepley 
5074e0b68406SMatthew Knepley   Input Parameters:
5075bb7acecfSBarry Smith + dm - The `DM`
5076e0b68406SMatthew Knepley - f  - The field index
5077e0b68406SMatthew Knepley 
5078e0b68406SMatthew Knepley   Output Parameter:
5079e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5080e0b68406SMatthew Knepley 
5081e0b68406SMatthew Knepley   Level: intermediate
5082e0b68406SMatthew Knepley 
508360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5084e0b68406SMatthew Knepley @*/
5085d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5086d71ae5a4SJacob Faibussowitsch {
5087e0b68406SMatthew Knepley   PetscFunctionBegin;
508863a3b9bcSJacob 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);
5089e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
50903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5091e0b68406SMatthew Knepley }
5092e0b68406SMatthew Knepley 
5093e0b68406SMatthew Knepley /*@
5094bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5095e5e52638SMatthew G. Knepley 
509620f4b53cSBarry Smith   Collective
5097e5e52638SMatthew G. Knepley 
5098e5e52638SMatthew G. Knepley   Input Parameter:
5099bb7acecfSBarry Smith . dm - The `DM`
5100e5e52638SMatthew G. Knepley 
5101e5e52638SMatthew G. Knepley   Output Parameter:
5102bb7acecfSBarry Smith . newdm - The `DM`
5103e5e52638SMatthew G. Knepley 
5104e5e52638SMatthew G. Knepley   Level: advanced
5105e5e52638SMatthew G. Knepley 
51061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5107e5e52638SMatthew G. Knepley @*/
5108d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5109d71ae5a4SJacob Faibussowitsch {
5110e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5111e5e52638SMatthew G. Knepley 
5112e5e52638SMatthew G. Knepley   PetscFunctionBegin;
51133ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
51149566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
51159566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5116e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5117e5e52638SMatthew G. Knepley     DMLabel     label;
5118e5e52638SMatthew G. Knepley     PetscObject field;
511934aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5120e5e52638SMatthew G. Knepley 
51219566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
51229566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
51239566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
51249566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
512534aa8a36SMatthew G. Knepley   }
51263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
512734aa8a36SMatthew G. Knepley }
512834aa8a36SMatthew G. Knepley 
512934aa8a36SMatthew G. Knepley /*@
513034aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
513134aa8a36SMatthew G. Knepley 
513220f4b53cSBarry Smith   Not Collective
513334aa8a36SMatthew G. Knepley 
513434aa8a36SMatthew G. Knepley   Input Parameters:
513520f4b53cSBarry Smith + dm - The `DM` object
513620f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
513734aa8a36SMatthew G. Knepley 
5138d8d19677SJose E. Roman   Output Parameters:
513934aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
514034aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
514134aa8a36SMatthew G. Knepley 
514234aa8a36SMatthew G. Knepley   Level: developer
514334aa8a36SMatthew G. Knepley 
514420f4b53cSBarry Smith   Notes:
514520f4b53cSBarry Smith .vb
514620f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
514720f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
514820f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
514920f4b53cSBarry Smith .ve
515020f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
515120f4b53cSBarry Smith 
51521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
515334aa8a36SMatthew G. Knepley @*/
5154d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5155d71ae5a4SJacob Faibussowitsch {
515634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
515734aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51584f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
51594f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
516034aa8a36SMatthew G. Knepley   if (f < 0) {
516134aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
516234aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
516334aa8a36SMatthew G. Knepley   } else {
516434aa8a36SMatthew G. Knepley     PetscInt Nf;
516534aa8a36SMatthew G. Knepley 
51669566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51677a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
516834aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
516934aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
517034aa8a36SMatthew G. Knepley   }
51713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
517234aa8a36SMatthew G. Knepley }
517334aa8a36SMatthew G. Knepley 
517434aa8a36SMatthew G. Knepley /*@
517534aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
517634aa8a36SMatthew G. Knepley 
517720f4b53cSBarry Smith   Not Collective
517834aa8a36SMatthew G. Knepley 
517934aa8a36SMatthew G. Knepley   Input Parameters:
518020f4b53cSBarry Smith + dm         - The `DM` object
518134aa8a36SMatthew G. Knepley . f          - The field number
518234aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
518334aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
518434aa8a36SMatthew G. Knepley 
518534aa8a36SMatthew G. Knepley   Level: developer
518634aa8a36SMatthew G. Knepley 
518720f4b53cSBarry Smith   Notes:
518820f4b53cSBarry Smith .vb
518920f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
519020f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
519120f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
519220f4b53cSBarry Smith .ve
519320f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
519420f4b53cSBarry Smith 
51951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
519634aa8a36SMatthew G. Knepley @*/
5197d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5198d71ae5a4SJacob Faibussowitsch {
519934aa8a36SMatthew G. Knepley   PetscFunctionBegin;
520034aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
520134aa8a36SMatthew G. Knepley   if (f < 0) {
520234aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
520334aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
520434aa8a36SMatthew G. Knepley   } else {
520534aa8a36SMatthew G. Knepley     PetscInt Nf;
520634aa8a36SMatthew G. Knepley 
52079566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52087a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
520934aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
521034aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5211e5e52638SMatthew G. Knepley   }
52123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5213e5e52638SMatthew G. Knepley }
5214e5e52638SMatthew G. Knepley 
5215b0441da4SMatthew G. Knepley /*@
5216b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5217b0441da4SMatthew G. Knepley 
5218b0441da4SMatthew G. Knepley   Not collective
5219b0441da4SMatthew G. Knepley 
5220f899ff85SJose E. Roman   Input Parameter:
522120f4b53cSBarry Smith . dm - The `DM` object
5222b0441da4SMatthew G. Knepley 
5223d8d19677SJose E. Roman   Output Parameters:
5224b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5225b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5226b0441da4SMatthew G. Knepley 
5227b0441da4SMatthew G. Knepley   Level: developer
5228b0441da4SMatthew G. Knepley 
522920f4b53cSBarry Smith   Notes:
523020f4b53cSBarry Smith .vb
523120f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
523220f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
523320f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
523420f4b53cSBarry Smith .ve
523520f4b53cSBarry Smith 
52361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5237b0441da4SMatthew G. Knepley @*/
5238d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5239d71ae5a4SJacob Faibussowitsch {
5240b0441da4SMatthew G. Knepley   PetscInt Nf;
5241b0441da4SMatthew G. Knepley 
5242b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5243b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52444f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
52454f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
52469566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5247b0441da4SMatthew G. Knepley   if (!Nf) {
52489566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5249b0441da4SMatthew G. Knepley   } else {
52509566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5251b0441da4SMatthew G. Knepley   }
52523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5253b0441da4SMatthew G. Knepley }
5254b0441da4SMatthew G. Knepley 
5255b0441da4SMatthew G. Knepley /*@
5256b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5257b0441da4SMatthew G. Knepley 
525820f4b53cSBarry Smith   Not Collective
5259b0441da4SMatthew G. Knepley 
5260b0441da4SMatthew G. Knepley   Input Parameters:
526120f4b53cSBarry Smith + dm         - The `DM` object
5262b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5263b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5264b0441da4SMatthew G. Knepley 
5265b0441da4SMatthew G. Knepley   Level: developer
5266b0441da4SMatthew G. Knepley 
526720f4b53cSBarry Smith   Notes:
526820f4b53cSBarry Smith .vb
526920f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
527020f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
527120f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
527220f4b53cSBarry Smith .ve
527320f4b53cSBarry Smith 
52741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5275b0441da4SMatthew G. Knepley @*/
5276d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5277d71ae5a4SJacob Faibussowitsch {
5278b0441da4SMatthew G. Knepley   PetscInt Nf;
5279b0441da4SMatthew G. Knepley 
5280b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5281b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52829566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5283b0441da4SMatthew G. Knepley   if (!Nf) {
52849566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5285b0441da4SMatthew G. Knepley   } else {
52869566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5287e5e52638SMatthew G. Knepley   }
52883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5289e5e52638SMatthew G. Knepley }
5290e5e52638SMatthew G. Knepley 
5291d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5292d71ae5a4SJacob Faibussowitsch {
5293799db056SMatthew G. Knepley   DM           plex;
5294799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5295799db056SMatthew G. Knepley   const char **names;
5296799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5297799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5298799db056SMatthew G. Knepley   size_t       len;
5299799db056SMatthew G. Knepley   MPI_Comm     comm;
5300799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5301783e2ec8SMatthew G. Knepley 
5302783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5303799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5304799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5305799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5306799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5307799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5308799db056SMatthew G. Knepley     PetscDS  dsBC;
5309799db056SMatthew G. Knepley     PetscInt numBd;
5310799db056SMatthew G. Knepley 
531107218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5312799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5313799db056SMatthew G. Knepley     maxLabels += numBd;
5314799db056SMatthew G. Knepley   }
5315799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5316799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5317799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5318799db056SMatthew G. Knepley     PetscDS  dsBC;
5319799db056SMatthew G. Knepley     PetscInt numBd, bd;
5320799db056SMatthew G. Knepley 
532107218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5322799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5323799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5324799db056SMatthew G. Knepley       DMLabel      label;
5325799db056SMatthew G. Knepley       PetscInt     field;
5326799db056SMatthew G. Knepley       PetscObject  obj;
5327799db056SMatthew G. Knepley       PetscClassId id;
5328799db056SMatthew G. Knepley 
5329799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
53309566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
53319566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5332799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
53339371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
53349371c9d4SSatish Balay         if (labels[l] == label) break;
5335799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5336783e2ec8SMatthew G. Knepley     }
5337799db056SMatthew G. Knepley   }
5338799db056SMatthew G. Knepley   /* Get label names */
5339799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5340799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
53419371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
53429371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
53439371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
53449371c9d4SSatish Balay   }
5345799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5346712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5347799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5348c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5349799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5350799db056SMatthew G. Knepley   /* Put all names on all processes */
5351799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5352799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5353799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5354799db056SMatthew G. Knepley   gNl = displs[size];
53559371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
53569371c9d4SSatish Balay     counts[p] *= gmaxLen;
53579371c9d4SSatish Balay     displs[p] *= gmaxLen;
53589371c9d4SSatish Balay   }
5359799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5360799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5361799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5362799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5363799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5364799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5365799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
53669371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
53679371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
53689566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5369799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
53709566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5371799db056SMatthew G. Knepley     ++gl;
5372783e2ec8SMatthew G. Knepley   }
5373799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
53743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5375783e2ec8SMatthew G. Knepley }
5376783e2ec8SMatthew G. Knepley 
5377d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5378d71ae5a4SJacob Faibussowitsch {
5379e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5380e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5381e5e52638SMatthew G. Knepley 
5382e5e52638SMatthew G. Knepley   PetscFunctionBegin;
53833ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
53849566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5385e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
53869371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
53879371c9d4SSatish Balay     tmpd[s].ds     = NULL;
53889371c9d4SSatish Balay     tmpd[s].label  = NULL;
53899371c9d4SSatish Balay     tmpd[s].fields = NULL;
53909371c9d4SSatish Balay   }
53919566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5392e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5393e5e52638SMatthew G. Knepley   dm->probs = tmpd;
53943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5395e5e52638SMatthew G. Knepley }
5396e5e52638SMatthew G. Knepley 
5397e5e52638SMatthew G. Knepley /*@
539820f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5399e5e52638SMatthew G. Knepley 
540020f4b53cSBarry Smith   Not Collective
5401e5e52638SMatthew G. Knepley 
5402e5e52638SMatthew G. Knepley   Input Parameter:
540320f4b53cSBarry Smith . dm - The `DM`
5404e5e52638SMatthew G. Knepley 
5405e5e52638SMatthew G. Knepley   Output Parameter:
540620f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5407e5e52638SMatthew G. Knepley 
5408e5e52638SMatthew G. Knepley   Level: intermediate
5409e5e52638SMatthew G. Knepley 
54101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5411e5e52638SMatthew G. Knepley @*/
5412d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5413d71ae5a4SJacob Faibussowitsch {
5414e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5415e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54164f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5417e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
54183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5419e5e52638SMatthew G. Knepley }
5420e5e52638SMatthew G. Knepley 
5421e5e52638SMatthew G. Knepley /*@
542220f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5423e5e52638SMatthew G. Knepley 
542420f4b53cSBarry Smith   Logically Collective
5425e5e52638SMatthew G. Knepley 
5426e5e52638SMatthew G. Knepley   Input Parameter:
542720f4b53cSBarry Smith . dm - The `DM`
5428e5e52638SMatthew G. Knepley 
5429e5e52638SMatthew G. Knepley   Level: intermediate
5430e5e52638SMatthew G. Knepley 
54311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5432e5e52638SMatthew G. Knepley @*/
5433d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5434d71ae5a4SJacob Faibussowitsch {
5435e5e52638SMatthew G. Knepley   PetscInt s;
5436e5e52638SMatthew G. Knepley 
5437e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5438e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5439e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
54409566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
544107218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
54429566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
54439566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5444e5e52638SMatthew G. Knepley   }
54459566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5446e5e52638SMatthew G. Knepley   dm->probs = NULL;
5447e5e52638SMatthew G. Knepley   dm->Nds   = 0;
54483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5449e5e52638SMatthew G. Knepley }
5450e5e52638SMatthew G. Knepley 
5451e5e52638SMatthew G. Knepley /*@
545220f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5453e5e52638SMatthew G. Knepley 
545420f4b53cSBarry Smith   Not Collective
5455e5e52638SMatthew G. Knepley 
5456e5e52638SMatthew G. Knepley   Input Parameter:
545720f4b53cSBarry Smith . dm - The `DM`
5458e5e52638SMatthew G. Knepley 
5459e5e52638SMatthew G. Knepley   Output Parameter:
546007218a29SMatthew G. Knepley . ds - The default `PetscDS`
5461e5e52638SMatthew G. Knepley 
5462e5e52638SMatthew G. Knepley   Level: intermediate
5463e5e52638SMatthew G. Knepley 
54641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5465e5e52638SMatthew G. Knepley @*/
546607218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5467d71ae5a4SJacob Faibussowitsch {
5468e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5469e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54704f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
547107218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
547207218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
54733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5474e5e52638SMatthew G. Knepley }
5475e5e52638SMatthew G. Knepley 
5476e5e52638SMatthew G. Knepley /*@
547720f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5478e5e52638SMatthew G. Knepley 
547920f4b53cSBarry Smith   Not Collective
5480e5e52638SMatthew G. Knepley 
5481e5e52638SMatthew G. Knepley   Input Parameters:
548220f4b53cSBarry Smith + dm    - The `DM`
548320f4b53cSBarry Smith - point - Cell for the `PetscDS`
5484e5e52638SMatthew G. Knepley 
548507218a29SMatthew G. Knepley   Output Parameters:
548607218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
548707218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5488e5e52638SMatthew G. Knepley 
5489e5e52638SMatthew G. Knepley   Level: developer
5490e5e52638SMatthew G. Knepley 
54911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5492e5e52638SMatthew G. Knepley @*/
549307218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5494d71ae5a4SJacob Faibussowitsch {
549507218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5496e5e52638SMatthew G. Knepley   PetscInt s;
5497e5e52638SMatthew G. Knepley 
5498e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5499e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55004f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
55014f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
550263a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
550307218a29SMatthew G. Knepley   if (ds) *ds = NULL;
550407218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5505e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5506e5e52638SMatthew G. Knepley     PetscInt val;
5507e5e52638SMatthew G. Knepley 
55089371c9d4SSatish Balay     if (!dm->probs[s].label) {
550907218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
55109371c9d4SSatish Balay     } else {
55119566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
55129371c9d4SSatish Balay       if (val >= 0) {
551307218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
551407218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
55159371c9d4SSatish Balay         break;
55169371c9d4SSatish Balay       }
5517e5e52638SMatthew G. Knepley     }
5518e5e52638SMatthew G. Knepley   }
551907218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
55203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5521e5e52638SMatthew G. Knepley }
5522e5e52638SMatthew G. Knepley 
5523e5e52638SMatthew G. Knepley /*@
552420f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5525e5e52638SMatthew G. Knepley 
552620f4b53cSBarry Smith   Not Collective
5527e5e52638SMatthew G. Knepley 
5528e5e52638SMatthew G. Knepley   Input Parameters:
552920f4b53cSBarry Smith + dm    - The `DM`
553020f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5531e5e52638SMatthew G. Knepley 
5532b3cf3223SMatthew G. Knepley   Output Parameters:
553320f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
553407218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
553507218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5536e5e52638SMatthew G. Knepley 
5537e5e52638SMatthew G. Knepley   Level: advanced
5538e5e52638SMatthew G. Knepley 
553920f4b53cSBarry Smith   Note:
554020f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
554120f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
554207218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
554320f4b53cSBarry Smith 
55441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5545e5e52638SMatthew G. Knepley @*/
554607218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5547d71ae5a4SJacob Faibussowitsch {
5548e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5549e5e52638SMatthew G. Knepley 
5550e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5551e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5552e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
55539371c9d4SSatish Balay   if (fields) {
55544f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
55559371c9d4SSatish Balay     *fields = NULL;
55569371c9d4SSatish Balay   }
55579371c9d4SSatish Balay   if (ds) {
55584f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
55599371c9d4SSatish Balay     *ds = NULL;
55609371c9d4SSatish Balay   }
556107218a29SMatthew G. Knepley   if (dsIn) {
55624f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
556307218a29SMatthew G. Knepley     *dsIn = NULL;
556407218a29SMatthew G. Knepley   }
5565e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5566154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5567b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5568b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
556907218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
55703ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5571b3cf3223SMatthew G. Knepley     }
5572e5e52638SMatthew G. Knepley   }
55733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5574e5e52638SMatthew G. Knepley }
5575e5e52638SMatthew G. Knepley 
5576e5e52638SMatthew G. Knepley /*@
5577bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5578083401c6SMatthew G. Knepley 
557920f4b53cSBarry Smith   Collective
5580083401c6SMatthew G. Knepley 
5581083401c6SMatthew G. Knepley   Input Parameters:
5582bb7acecfSBarry Smith + dm     - The `DM`
558320f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
558407218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
558507218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
558607218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5587083401c6SMatthew G. Knepley 
558820f4b53cSBarry Smith   Level: advanced
558920f4b53cSBarry Smith 
5590bb7acecfSBarry Smith   Note:
5591bb7acecfSBarry 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,
5592083401c6SMatthew G. Knepley   the fields argument is ignored.
5593083401c6SMatthew G. Knepley 
55941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5595083401c6SMatthew G. Knepley @*/
559607218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5597d71ae5a4SJacob Faibussowitsch {
5598083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5599083401c6SMatthew G. Knepley 
5600083401c6SMatthew G. Knepley   PetscFunctionBegin;
5601083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5602083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
560307218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5604064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
560507218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5606083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5607083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
56089566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
560907218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5610083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
561107218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
56123ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5613083401c6SMatthew G. Knepley     }
5614083401c6SMatthew G. Knepley   }
56159566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
56169566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56179566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
56189566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
561907218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5620083401c6SMatthew G. Knepley   if (!label) {
5621083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5622083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5623083401c6SMatthew G. Knepley     Nds = 0;
5624083401c6SMatthew G. Knepley   }
5625083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5626083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5627083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
562807218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
56293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5630083401c6SMatthew G. Knepley }
5631083401c6SMatthew G. Knepley 
5632083401c6SMatthew G. Knepley /*@
563320f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5634e5e52638SMatthew G. Knepley 
563520f4b53cSBarry Smith   Not Collective
5636e5e52638SMatthew G. Knepley 
5637e5e52638SMatthew G. Knepley   Input Parameters:
563820f4b53cSBarry Smith + dm  - The `DM`
5639e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5640e5e52638SMatthew G. Knepley 
5641e5e52638SMatthew G. Knepley   Output Parameters:
564220f4b53cSBarry Smith + label  - The region label, or `NULL`
564320f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
564407218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
564507218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5646e5e52638SMatthew G. Knepley 
5647e5e52638SMatthew G. Knepley   Level: advanced
5648e5e52638SMatthew G. Knepley 
56491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5650e5e52638SMatthew G. Knepley @*/
565107218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5652d71ae5a4SJacob Faibussowitsch {
5653e5e52638SMatthew G. Knepley   PetscInt Nds;
5654e5e52638SMatthew G. Knepley 
5655e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5656e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56579566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
565863a3b9bcSJacob 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);
5659e5e52638SMatthew G. Knepley   if (label) {
56604f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5661e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5662e5e52638SMatthew G. Knepley   }
5663b3cf3223SMatthew G. Knepley   if (fields) {
56644f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5665b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5666b3cf3223SMatthew G. Knepley   }
5667e5e52638SMatthew G. Knepley   if (ds) {
56684f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5669e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5670e5e52638SMatthew G. Knepley   }
567107218a29SMatthew G. Knepley   if (dsIn) {
56724f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
567307218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
567407218a29SMatthew G. Knepley   }
56753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5676e5e52638SMatthew G. Knepley }
5677e5e52638SMatthew G. Knepley 
5678e5e52638SMatthew G. Knepley /*@
567920f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5680e5e52638SMatthew G. Knepley 
568120f4b53cSBarry Smith   Not Collective
5682e5e52638SMatthew G. Knepley 
5683e5e52638SMatthew G. Knepley   Input Parameters:
568420f4b53cSBarry Smith + dm     - The `DM`
5685083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
568620f4b53cSBarry Smith . label  - The region label, or `NULL`
568707218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
568807218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
568907218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5690e5e52638SMatthew G. Knepley 
5691e5e52638SMatthew G. Knepley   Level: advanced
5692e5e52638SMatthew G. Knepley 
56931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5694e5e52638SMatthew G. Knepley @*/
569507218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5696d71ae5a4SJacob Faibussowitsch {
5697083401c6SMatthew G. Knepley   PetscInt Nds;
5698e5e52638SMatthew G. Knepley 
5699e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5700e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5701ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
57029566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
570363a3b9bcSJacob 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);
57049566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57059566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5706083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5707083401c6SMatthew G. Knepley   if (fields) {
5708083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
57099566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
57109566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5711083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5712e5e52638SMatthew G. Knepley   }
5713083401c6SMatthew G. Knepley   if (ds) {
5714083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
57159566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
57169566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5717083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5718083401c6SMatthew G. Knepley   }
571907218a29SMatthew G. Knepley   if (dsIn) {
572007218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
572107218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
572207218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
572307218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
572407218a29SMatthew G. Knepley   }
57253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5726e5e52638SMatthew G. Knepley }
5727e5e52638SMatthew G. Knepley 
5728e5e52638SMatthew G. Knepley /*@
572920f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
57301d3af9e0SMatthew G. Knepley 
573120f4b53cSBarry Smith   Not Collective
57321d3af9e0SMatthew G. Knepley 
57331d3af9e0SMatthew G. Knepley   Input Parameters:
573420f4b53cSBarry Smith + dm - The `DM`
573520f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
57361d3af9e0SMatthew G. Knepley 
57371d3af9e0SMatthew G. Knepley   Output Parameter:
57381d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
57391d3af9e0SMatthew G. Knepley 
57401d3af9e0SMatthew G. Knepley   Level: advanced
57411d3af9e0SMatthew G. Knepley 
57421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
57431d3af9e0SMatthew G. Knepley @*/
5744d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5745d71ae5a4SJacob Faibussowitsch {
57461d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
57471d3af9e0SMatthew G. Knepley 
57481d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
57491d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57501d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
57514f572ea9SToby Isaac   PetscAssertPointer(num, 3);
57529566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
57539371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
57549371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
57551d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
57561d3af9e0SMatthew G. Knepley   else *num = n;
57573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57581d3af9e0SMatthew G. Knepley }
57591d3af9e0SMatthew G. Knepley 
57602df84da0SMatthew G. Knepley /*@C
5761bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
57622df84da0SMatthew G. Knepley 
576320f4b53cSBarry Smith   Not Collective
57642df84da0SMatthew G. Knepley 
5765f1a722f8SMatthew G. Knepley   Input Parameters:
5766bb7acecfSBarry Smith + dm     - The `DM`
57672df84da0SMatthew G. Knepley . Nc     - The number of components for the field
576820f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5769bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
57702df84da0SMatthew G. Knepley 
57712df84da0SMatthew G. Knepley   Output Parameter:
5772bb7acecfSBarry Smith . fem - The `PetscFE`
57732df84da0SMatthew G. Knepley 
577420f4b53cSBarry Smith   Level: intermediate
577520f4b53cSBarry Smith 
5776bb7acecfSBarry Smith   Note:
5777bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
57782df84da0SMatthew G. Knepley 
57791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
57802df84da0SMatthew G. Knepley @*/
5781d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5782d71ae5a4SJacob Faibussowitsch {
57832df84da0SMatthew G. Knepley   DMPolytopeType ct;
57842df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
57852df84da0SMatthew G. Knepley 
57862df84da0SMatthew G. Knepley   PetscFunctionBegin;
57872df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57882df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
57894f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
57902df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
57914f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
57929566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
57939566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
57949566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
57959566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
57963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57972df84da0SMatthew G. Knepley }
57982df84da0SMatthew G. Knepley 
57991d3af9e0SMatthew G. Knepley /*@
5800bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5801e5e52638SMatthew G. Knepley 
580220f4b53cSBarry Smith   Collective
5803e5e52638SMatthew G. Knepley 
5804e5e52638SMatthew G. Knepley   Input Parameter:
5805bb7acecfSBarry Smith . dm - The `DM`
5806e5e52638SMatthew G. Knepley 
580720f4b53cSBarry Smith   Options Database Key:
5808bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
580945480ffeSMatthew G. Knepley 
581020f4b53cSBarry Smith   Level: intermediate
581120f4b53cSBarry Smith 
58121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5813e5e52638SMatthew G. Knepley @*/
5814d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5815d71ae5a4SJacob Faibussowitsch {
5816e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5817083401c6SMatthew G. Knepley   PetscDS   dsDef;
5818083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5819f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5820f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5821e5e52638SMatthew G. Knepley 
5822e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5823e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58243ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
58259566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
58269566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5827083401c6SMatthew G. Knepley   /* Determine how many regions we have */
58289566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5829083401c6SMatthew G. Knepley   Nl   = 0;
5830083401c6SMatthew G. Knepley   Ndef = 0;
5831083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5832083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5833083401c6SMatthew G. Knepley     PetscInt l;
5834083401c6SMatthew G. Knepley 
5835f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5836f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5837f918ec44SMatthew G. Knepley     {
5838f918ec44SMatthew G. Knepley       PetscClassId id;
5839f918ec44SMatthew G. Knepley 
58409566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5841f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5842f918ec44SMatthew G. Knepley         Ceed ceed;
5843f918ec44SMatthew G. Knepley 
58449566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
58459566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5846f918ec44SMatthew G. Knepley       }
5847f918ec44SMatthew G. Knepley     }
5848f918ec44SMatthew G. Knepley #endif
58499371c9d4SSatish Balay     if (!label) {
58509371c9d4SSatish Balay       ++Ndef;
58519371c9d4SSatish Balay       continue;
58529371c9d4SSatish Balay     }
58539371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
58549371c9d4SSatish Balay       if (label == labelSet[l]) break;
5855083401c6SMatthew G. Knepley     if (l < Nl) continue;
5856083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5857083401c6SMatthew G. Knepley   }
5858083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
585907218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5860083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5861b3cf3223SMatthew G. Knepley     IS        fields;
5862b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5863b3cf3223SMatthew G. Knepley 
58649371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58659371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
58667a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
58679566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
58689371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58699371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
58709566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58719566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58729566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58739566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
587488f0c812SMatthew G. Knepley 
58759566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
587607218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
58779566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58789566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
58792df9ee95SMatthew G. Knepley   }
588007218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
58819566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5882083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5883083401c6SMatthew G. Knepley   if (Ndef && Nl) {
58840122748bSMatthew G. Knepley     DM              plex;
5885083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5886083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5887083401c6SMatthew G. Knepley     PetscInt       *fields;
5888083401c6SMatthew G. Knepley     const PetscInt *cells;
5889083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
58900122748bSMatthew G. Knepley 
58919566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
58929566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
58939566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
58949566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
58955fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5896083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5897083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5898083401c6SMatthew G. Knepley       IS      pointIS;
5899083401c6SMatthew G. Knepley 
59009566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
59019566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
59029566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
59039566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5904083401c6SMatthew G. Knepley     }
59059566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5906083401c6SMatthew G. Knepley 
59079566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
59089566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
59099566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
59109566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
59119566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
59129566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
59139566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5914083401c6SMatthew G. Knepley 
59159566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
59169371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
59179371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
59189566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
59199566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
59209566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
59219566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5922083401c6SMatthew G. Knepley 
59239566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
592407218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
59259566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
59269566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
59279566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59289566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
59299566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5930083401c6SMatthew G. Knepley   }
5931083401c6SMatthew G. Knepley   /* Create label DSes
5932083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5933083401c6SMatthew G. Knepley   */
5934083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5935083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5936083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
593707218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
5938083401c6SMatthew G. Knepley     IS        fields;
5939083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5940083401c6SMatthew G. Knepley 
59419566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
59429371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59439371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
59449566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59459371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59469371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
59479566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59489566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59499566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59509566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
59519566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5952083401c6SMatthew G. Knepley     {
5953083401c6SMatthew G. Knepley       DMPolytopeType ct;
5954083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
59555fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
59560122748bSMatthew G. Knepley 
59579566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5958665f567fSMatthew G. Knepley       if (lStart >= 0) {
59599566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5960412e9a14SMatthew G. Knepley         switch (ct) {
5961412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5962412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5963412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
5964d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5965d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
5966d71ae5a4SJacob Faibussowitsch           break;
5967d71ae5a4SJacob Faibussowitsch         default:
5968d71ae5a4SJacob Faibussowitsch           break;
5969412e9a14SMatthew G. Knepley         }
5970665f567fSMatthew G. Knepley       }
5971712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
597207218a29SMatthew G. Knepley       if (isCohesive) {
597307218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
597407218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
597507218a29SMatthew G. Knepley       }
59765fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
59775fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
59785fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
59799566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
59809566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
598107218a29SMatthew G. Knepley             if (dsIn) {
598207218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
598307218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
598407218a29SMatthew G. Knepley             }
59855fedec97SMatthew G. Knepley           }
59865fedec97SMatthew G. Knepley           ++nf;
59875fedec97SMatthew G. Knepley         }
59885fedec97SMatthew G. Knepley       }
5989e5e52638SMatthew G. Knepley     }
599007218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
599107218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
59929566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
599307218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
5994e5e52638SMatthew G. Knepley   }
59959566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5996e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5997083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5998083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
599907218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
6000083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
6001083401c6SMatthew G. Knepley     const PetscInt *fld;
60025fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
60035fedec97SMatthew G. Knepley     PetscBool       isCohesive;
6004e5e52638SMatthew G. Knepley 
60059566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
60069566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
60079566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
60089566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
6009083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
6010083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
60115fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
6012e5e52638SMatthew G. Knepley       PetscClassId id;
6013e5e52638SMatthew G. Knepley 
60145fedec97SMatthew G. Knepley       /* Handle DS with no fields */
60159566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
60165fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
601707218a29SMatthew G. Knepley       if (isCohesive) {
601807218a29SMatthew G. Knepley         if (!isCohesiveField) {
601907218a29SMatthew G. Knepley           PetscObject bdDisc;
602007218a29SMatthew G. Knepley 
602107218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
602207218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
602307218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
602407218a29SMatthew G. Knepley         } else {
60259566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
602607218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
602707218a29SMatthew G. Knepley         }
602807218a29SMatthew G. Knepley       } else {
602907218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
603007218a29SMatthew G. Knepley       }
6031083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
60329566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
6033e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
6034e5e52638SMatthew G. Knepley     }
60359566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6036e5e52638SMatthew G. Knepley   }
6037f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
60389566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6039f9244615SMatthew G. Knepley   if (flg) {
60403b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
60413b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
604207218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
60433b4aee56SMatthew G. Knepley       PetscInt Nf, f;
60443b4aee56SMatthew G. Knepley 
60459566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
604607218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
604707218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
604807218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
604907218a29SMatthew G. Knepley       }
60503b4aee56SMatthew G. Knepley     }
6051f9244615SMatthew G. Knepley   }
6052e5e52638SMatthew G. Knepley   /* Setup DSes */
6053e5e52638SMatthew G. Knepley   if (doSetup) {
605407218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
6055cf1363e4SMatthew G. Knepley       if (dm->setfromoptionscalled) {
6056cf1363e4SMatthew G. Knepley         PetscCall(PetscDSSetFromOptions(dm->probs[s].ds));
6057cf1363e4SMatthew G. Knepley         if (dm->probs[s].dsIn) PetscCall(PetscDSSetFromOptions(dm->probs[s].dsIn));
6058cf1363e4SMatthew G. Knepley       }
605907218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
606007218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
606107218a29SMatthew G. Knepley     }
6062e5e52638SMatthew G. Knepley   }
60633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6064e5e52638SMatthew G. Knepley }
6065e5e52638SMatthew G. Knepley 
6066e5e52638SMatthew G. Knepley /*@
6067d2b2dc1eSMatthew G. Knepley   DMUseTensorOrder - Use a tensor product closure ordering for the default section
6068d2b2dc1eSMatthew G. Knepley 
6069d2b2dc1eSMatthew G. Knepley   Input Parameters:
6070d2b2dc1eSMatthew G. Knepley + dm     - The DM
6071d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order
6072d2b2dc1eSMatthew G. Knepley 
6073d2b2dc1eSMatthew G. Knepley   Level: developer
6074d2b2dc1eSMatthew G. Knepley 
6075d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()`
6076d2b2dc1eSMatthew G. Knepley @*/
6077d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor)
6078d2b2dc1eSMatthew G. Knepley {
6079d2b2dc1eSMatthew G. Knepley   PetscInt  Nf;
6080d2b2dc1eSMatthew G. Knepley   PetscBool reorder = PETSC_TRUE, isPlex;
6081d2b2dc1eSMatthew G. Knepley 
6082d2b2dc1eSMatthew G. Knepley   PetscFunctionBegin;
6083d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex));
6084d2b2dc1eSMatthew G. Knepley   PetscCall(DMGetNumFields(dm, &Nf));
6085d2b2dc1eSMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
6086d2b2dc1eSMatthew G. Knepley     PetscObject  obj;
6087d2b2dc1eSMatthew G. Knepley     PetscClassId id;
6088d2b2dc1eSMatthew G. Knepley 
6089d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetField(dm, f, NULL, &obj));
6090d2b2dc1eSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
6091d2b2dc1eSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
6092d2b2dc1eSMatthew G. Knepley       PetscSpace sp;
6093d2b2dc1eSMatthew G. Knepley       PetscBool  tensor;
6094d2b2dc1eSMatthew G. Knepley 
6095d2b2dc1eSMatthew G. Knepley       PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp));
6096d2b2dc1eSMatthew G. Knepley       PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor));
6097d2b2dc1eSMatthew G. Knepley       reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE;
6098d2b2dc1eSMatthew G. Knepley     } else reorder = PETSC_FALSE;
6099d2b2dc1eSMatthew G. Knepley   }
6100d2b2dc1eSMatthew G. Knepley   if (tensor) {
6101d2b2dc1eSMatthew G. Knepley     if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
6102d2b2dc1eSMatthew G. Knepley   } else {
6103d2b2dc1eSMatthew G. Knepley     PetscSection s;
6104d2b2dc1eSMatthew G. Knepley 
6105d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetLocalSection(dm, &s));
6106d2b2dc1eSMatthew G. Knepley     if (s) PetscCall(PetscSectionResetClosurePermutation(s));
6107d2b2dc1eSMatthew G. Knepley   }
6108d2b2dc1eSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6109d2b2dc1eSMatthew G. Knepley }
6110d2b2dc1eSMatthew G. Knepley 
6111d2b2dc1eSMatthew G. Knepley /*@
6112bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
61137f96f943SMatthew G. Knepley 
611420f4b53cSBarry Smith   Collective
6115f2cacb80SMatthew G. Knepley 
61167f96f943SMatthew G. Knepley   Input Parameters:
6117bb7acecfSBarry Smith + dm   - The `DM`
61187f96f943SMatthew G. Knepley - time - The time
61197f96f943SMatthew G. Knepley 
61207f96f943SMatthew G. Knepley   Output Parameters:
612120f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
612220f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
612320f4b53cSBarry Smith 
612420f4b53cSBarry Smith   Level: developer
61257f96f943SMatthew G. Knepley 
6126bb7acecfSBarry Smith   Note:
6127bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
61287f96f943SMatthew G. Knepley 
61291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
61307f96f943SMatthew G. Knepley @*/
6131d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6132d71ae5a4SJacob Faibussowitsch {
61337f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
61347f96f943SMatthew G. Knepley   void   **ectxs;
6135f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
61367f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
61377f96f943SMatthew G. Knepley 
61387f96f943SMatthew G. Knepley   PetscFunctionBegin;
6139f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6140f60fa741SMatthew G. Knepley   if (u) {
6141f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6142f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6143f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6144f60fa741SMatthew G. Knepley   }
6145f60fa741SMatthew G. Knepley   if (u_t) {
6146f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6147f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6148f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6149f60fa741SMatthew G. Knepley   }
61509566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
61519566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
61529566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
61537f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
61547f96f943SMatthew G. Knepley     PetscDS         ds;
61557f96f943SMatthew G. Knepley     DMLabel         label;
61567f96f943SMatthew G. Knepley     IS              fieldIS;
61577f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
61587f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
61597f96f943SMatthew G. Knepley 
616007218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
61619566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
61629566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
61639566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
61649566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6165f2cacb80SMatthew G. Knepley     if (u) {
6166f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6167f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6168f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
61697f96f943SMatthew G. Knepley     }
6170f2cacb80SMatthew G. Knepley     if (u_t) {
61719566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
61729566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6173f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6174f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6175f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6176f2cacb80SMatthew G. Knepley     }
61779566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6178f2cacb80SMatthew G. Knepley   }
6179f2cacb80SMatthew G. Knepley   if (u) {
61809566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
61819566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6182f2cacb80SMatthew G. Knepley   }
6183f2cacb80SMatthew G. Knepley   if (u_t) {
61849566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
61859566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6186f2cacb80SMatthew G. Knepley   }
61879566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6188f60fa741SMatthew G. Knepley   if (u) {
6189f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6190f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6191f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6192f60fa741SMatthew G. Knepley   }
6193f60fa741SMatthew G. Knepley   if (u_t) {
6194f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6195f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6196f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6197f60fa741SMatthew G. Knepley   }
61983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
61997f96f943SMatthew G. Knepley }
62007f96f943SMatthew G. Knepley 
620107218a29SMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
6202d71ae5a4SJacob Faibussowitsch {
620307218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
620445480ffeSMatthew G. Knepley 
620545480ffeSMatthew G. Knepley   PetscFunctionBegin;
62069566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
620707218a29SMatthew G. Knepley   PetscCall(PetscDSCopy(ds, dm, dsNew));
620807218a29SMatthew G. Knepley   if (dsIn) {
620907218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
621007218a29SMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, dm, dsInNew));
621145480ffeSMatthew G. Knepley   }
621207218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
62139566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
621407218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
62153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
621645480ffeSMatthew G. Knepley }
621745480ffeSMatthew G. Knepley 
62187f96f943SMatthew G. Knepley /*@
6219bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6220e5e52638SMatthew G. Knepley 
622120f4b53cSBarry Smith   Collective
6222e5e52638SMatthew G. Knepley 
6223e5e52638SMatthew G. Knepley   Input Parameter:
6224bb7acecfSBarry Smith . dm - The `DM`
6225e5e52638SMatthew G. Knepley 
6226e5e52638SMatthew G. Knepley   Output Parameter:
6227bb7acecfSBarry Smith . newdm - The `DM`
6228e5e52638SMatthew G. Knepley 
6229e5e52638SMatthew G. Knepley   Level: advanced
6230e5e52638SMatthew G. Knepley 
62311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6232e5e52638SMatthew G. Knepley @*/
6233d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6234d71ae5a4SJacob Faibussowitsch {
6235e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6236e5e52638SMatthew G. Knepley 
6237e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62383ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
62399566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62409566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6241e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6242e5e52638SMatthew G. Knepley     DMLabel  label;
6243b3cf3223SMatthew G. Knepley     IS       fields;
624407218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6245783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6246e5e52638SMatthew G. Knepley 
624707218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6248b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
624907218a29SMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds, dsIn));
6250d5b43468SJose E. Roman     /* Complete new labels in the new DS */
625107218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
62529566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6253783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6254b8025e53SMatthew G. Knepley       PetscWeakForm wf;
625545480ffeSMatthew G. Knepley       DMLabel       label;
6256783e2ec8SMatthew G. Knepley       PetscInt      field;
6257783e2ec8SMatthew G. Knepley 
62589566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
62599566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6260783e2ec8SMatthew G. Knepley     }
6261e5e52638SMatthew G. Knepley   }
6262799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
62633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6264e5e52638SMatthew G. Knepley }
6265e5e52638SMatthew G. Knepley 
6266e5e52638SMatthew G. Knepley /*@
6267bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6268e5e52638SMatthew G. Knepley 
626920f4b53cSBarry Smith   Collective
6270e5e52638SMatthew G. Knepley 
6271e5e52638SMatthew G. Knepley   Input Parameter:
6272bb7acecfSBarry Smith . dm - The `DM`
6273e5e52638SMatthew G. Knepley 
6274e5e52638SMatthew G. Knepley   Output Parameter:
6275bb7acecfSBarry Smith . newdm - The `DM`
6276e5e52638SMatthew G. Knepley 
6277e5e52638SMatthew G. Knepley   Level: advanced
6278e5e52638SMatthew G. Knepley 
627960225df5SJacob Faibussowitsch   Developer Notes:
6280bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6281bb7acecfSBarry Smith 
62821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6283e5e52638SMatthew G. Knepley @*/
6284d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6285d71ae5a4SJacob Faibussowitsch {
6286e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62879566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
62889566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
62893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6290e5e52638SMatthew G. Knepley }
6291e5e52638SMatthew G. Knepley 
6292c73cfb54SMatthew G. Knepley /*@
6293bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6294c73cfb54SMatthew G. Knepley 
629520f4b53cSBarry Smith   Not Collective
6296c73cfb54SMatthew G. Knepley 
6297c73cfb54SMatthew G. Knepley   Input Parameter:
6298bb7acecfSBarry Smith . dm - The `DM`
6299c73cfb54SMatthew G. Knepley 
6300c73cfb54SMatthew G. Knepley   Output Parameter:
6301c73cfb54SMatthew G. Knepley . dim - The topological dimension
6302c73cfb54SMatthew G. Knepley 
6303c73cfb54SMatthew G. Knepley   Level: beginner
6304c73cfb54SMatthew G. Knepley 
63051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6306c73cfb54SMatthew G. Knepley @*/
6307d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6308d71ae5a4SJacob Faibussowitsch {
6309c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6310c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63114f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6312c73cfb54SMatthew G. Knepley   *dim = dm->dim;
63133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6314c73cfb54SMatthew G. Knepley }
6315c73cfb54SMatthew G. Knepley 
6316c73cfb54SMatthew G. Knepley /*@
6317bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6318c73cfb54SMatthew G. Knepley 
631920f4b53cSBarry Smith   Collective
6320c73cfb54SMatthew G. Knepley 
6321c73cfb54SMatthew G. Knepley   Input Parameters:
6322bb7acecfSBarry Smith + dm  - The `DM`
6323c73cfb54SMatthew G. Knepley - dim - The topological dimension
6324c73cfb54SMatthew G. Knepley 
6325c73cfb54SMatthew G. Knepley   Level: beginner
6326c73cfb54SMatthew G. Knepley 
63271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6328c73cfb54SMatthew G. Knepley @*/
6329d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6330d71ae5a4SJacob Faibussowitsch {
6331e5e52638SMatthew G. Knepley   PetscDS  ds;
633245480ffeSMatthew G. Knepley   PetscInt Nds, n;
6333f17e8794SMatthew G. Knepley 
6334c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6335c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6336c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6337c73cfb54SMatthew G. Knepley   dm->dim = dim;
6338d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
63399566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
634045480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
634107218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
63429566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
634345480ffeSMatthew G. Knepley     }
6344d17bd122SMatthew G. Knepley   }
63453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6346c73cfb54SMatthew G. Knepley }
6347c73cfb54SMatthew G. Knepley 
6348793f3fe5SMatthew G. Knepley /*@
6349793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6350793f3fe5SMatthew G. Knepley 
635120f4b53cSBarry Smith   Collective
6352793f3fe5SMatthew G. Knepley 
6353793f3fe5SMatthew G. Knepley   Input Parameters:
6354bb7acecfSBarry Smith + dm  - the `DM`
6355793f3fe5SMatthew G. Knepley - dim - the dimension
6356793f3fe5SMatthew G. Knepley 
6357793f3fe5SMatthew G. Knepley   Output Parameters:
6358793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6359aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6360793f3fe5SMatthew G. Knepley 
636120f4b53cSBarry Smith   Level: intermediate
636220f4b53cSBarry Smith 
6363793f3fe5SMatthew G. Knepley   Note:
6364793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6365a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6366793f3fe5SMatthew G. Knepley   then the interval is empty.
6367793f3fe5SMatthew G. Knepley 
63681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6369793f3fe5SMatthew G. Knepley @*/
6370d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6371d71ae5a4SJacob Faibussowitsch {
6372793f3fe5SMatthew G. Knepley   PetscInt d;
6373793f3fe5SMatthew G. Knepley 
6374793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6375793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63769566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
63777a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6378dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
63793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6380793f3fe5SMatthew G. Knepley }
6381793f3fe5SMatthew G. Knepley 
63826636e97aSMatthew G Knepley /*@
6383bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6384f4d763aaSMatthew G. Knepley 
638520f4b53cSBarry Smith   Collective
63868f700142SStefano Zampini 
6387f4d763aaSMatthew G. Knepley   Input Parameter:
6388bb7acecfSBarry Smith . dm - The original `DM`
6389f4d763aaSMatthew G. Knepley 
6390f4d763aaSMatthew G. Knepley   Output Parameter:
6391bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6392f4d763aaSMatthew G. Knepley 
6393f4d763aaSMatthew G. Knepley   Level: intermediate
6394f4d763aaSMatthew G. Knepley 
6395bb7acecfSBarry Smith   Note:
6396bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6397bb7acecfSBarry 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
6398bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6399bb7acecfSBarry Smith 
64001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6401f4d763aaSMatthew G. Knepley @*/
6402d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6403d71ae5a4SJacob Faibussowitsch {
6404c26acbdeSMatthew G. Knepley   PetscSection section;
64052d4e4a49SMatthew G. Knepley   PetscBool    hasConstraints, ghasConstraints;
640614f150ffSMatthew G. Knepley 
640714f150ffSMatthew G. Knepley   PetscFunctionBegin;
640814f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64094f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
64109566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
64119566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6412712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
64132d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6414c26acbdeSMatthew G. Knepley     *odm = dm;
64153ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6416c26acbdeSMatthew G. Knepley   }
641714f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6418c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
641914f150ffSMatthew G. Knepley     PetscSF      sf;
642014f150ffSMatthew G. Knepley 
64219566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
64229566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
64239566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
64249566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
64259566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
64269566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
64279566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
64289566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
64299566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
643014f150ffSMatthew G. Knepley   }
643114f150ffSMatthew G. Knepley   *odm = dm->dmBC;
64323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
643314f150ffSMatthew G. Knepley }
6434f4d763aaSMatthew G. Knepley 
6435f4d763aaSMatthew G. Knepley /*@
6436cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6437f4d763aaSMatthew G. Knepley 
6438f4d763aaSMatthew G. Knepley   Input Parameter:
6439bb7acecfSBarry Smith . dm - The original `DM`
6440f4d763aaSMatthew G. Knepley 
6441cdb7a50dSMatthew G. Knepley   Output Parameters:
6442cdb7a50dSMatthew G. Knepley + num - The output sequence number
6443cdb7a50dSMatthew G. Knepley - val - The output sequence value
6444f4d763aaSMatthew G. Knepley 
6445f4d763aaSMatthew G. Knepley   Level: intermediate
6446f4d763aaSMatthew G. Knepley 
6447bb7acecfSBarry Smith   Note:
6448bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6449bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6450bb7acecfSBarry Smith 
645160225df5SJacob Faibussowitsch   Developer Notes:
6452bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6453bb7acecfSBarry Smith   not directly related to the `DM`.
6454f4d763aaSMatthew G. Knepley 
64551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6456f4d763aaSMatthew G. Knepley @*/
6457d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6458d71ae5a4SJacob Faibussowitsch {
6459f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6460f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64619371c9d4SSatish Balay   if (num) {
64624f572ea9SToby Isaac     PetscAssertPointer(num, 2);
64639371c9d4SSatish Balay     *num = dm->outputSequenceNum;
64649371c9d4SSatish Balay   }
64659371c9d4SSatish Balay   if (val) {
64664f572ea9SToby Isaac     PetscAssertPointer(val, 3);
64679371c9d4SSatish Balay     *val = dm->outputSequenceVal;
64689371c9d4SSatish Balay   }
64693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6470f4d763aaSMatthew G. Knepley }
6471f4d763aaSMatthew G. Knepley 
6472f4d763aaSMatthew G. Knepley /*@
6473cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6474f4d763aaSMatthew G. Knepley 
6475f4d763aaSMatthew G. Knepley   Input Parameters:
6476bb7acecfSBarry Smith + dm  - The original `DM`
6477cdb7a50dSMatthew G. Knepley . num - The output sequence number
6478cdb7a50dSMatthew G. Knepley - val - The output sequence value
6479f4d763aaSMatthew G. Knepley 
6480f4d763aaSMatthew G. Knepley   Level: intermediate
6481f4d763aaSMatthew G. Knepley 
6482bb7acecfSBarry Smith   Note:
6483bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6484bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6485f4d763aaSMatthew G. Knepley 
64861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6487f4d763aaSMatthew G. Knepley @*/
6488d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6489d71ae5a4SJacob Faibussowitsch {
6490f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6491f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6492f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6493cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
64943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6495cdb7a50dSMatthew G. Knepley }
6496cdb7a50dSMatthew G. Knepley 
6497cdb7a50dSMatthew G. Knepley /*@C
6498bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6499cdb7a50dSMatthew G. Knepley 
6500cdb7a50dSMatthew G. Knepley   Input Parameters:
6501bb7acecfSBarry Smith + dm     - The original `DM`
6502a4e35b19SJacob Faibussowitsch . viewer - The viewer to get it from
6503cdb7a50dSMatthew G. Knepley . name   - The sequence name
6504cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6505cdb7a50dSMatthew G. Knepley 
6506cdb7a50dSMatthew G. Knepley   Output Parameter:
6507cdb7a50dSMatthew G. Knepley . val - The output sequence value
6508cdb7a50dSMatthew G. Knepley 
6509cdb7a50dSMatthew G. Knepley   Level: intermediate
6510cdb7a50dSMatthew G. Knepley 
6511bb7acecfSBarry Smith   Note:
6512bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6513bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6514bb7acecfSBarry Smith 
651560225df5SJacob Faibussowitsch   Developer Notes:
6516bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6517cdb7a50dSMatthew G. Knepley 
65181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6519cdb7a50dSMatthew G. Knepley @*/
6520d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6521d71ae5a4SJacob Faibussowitsch {
6522cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6523cdb7a50dSMatthew G. Knepley 
6524cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6525cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6526cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
65274f572ea9SToby Isaac   PetscAssertPointer(val, 5);
65289566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6529cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6530cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6531cdb7a50dSMatthew G. Knepley     PetscScalar value;
6532cdb7a50dSMatthew G. Knepley 
65339566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
65344aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6535cdb7a50dSMatthew G. Knepley #endif
6536cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
65373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6538f4d763aaSMatthew G. Knepley }
65398e4ac7eaSMatthew G. Knepley 
65408e4ac7eaSMatthew G. Knepley /*@
6541bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
65428e4ac7eaSMatthew G. Knepley 
654320f4b53cSBarry Smith   Not Collective
65448e4ac7eaSMatthew G. Knepley 
65458e4ac7eaSMatthew G. Knepley   Input Parameter:
6546bb7acecfSBarry Smith . dm - The `DM`
65478e4ac7eaSMatthew G. Knepley 
65488e4ac7eaSMatthew G. Knepley   Output Parameter:
6549bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
65508e4ac7eaSMatthew G. Knepley 
65518e4ac7eaSMatthew G. Knepley   Level: beginner
65528e4ac7eaSMatthew G. Knepley 
65531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
65548e4ac7eaSMatthew G. Knepley @*/
6555d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6556d71ae5a4SJacob Faibussowitsch {
65578e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
65588e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65594f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
65608e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
65613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65628e4ac7eaSMatthew G. Knepley }
65638e4ac7eaSMatthew G. Knepley 
65648e4ac7eaSMatthew G. Knepley /*@
6565bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
65668e4ac7eaSMatthew G. Knepley 
656720f4b53cSBarry Smith   Collective
65688e4ac7eaSMatthew G. Knepley 
65698e4ac7eaSMatthew G. Knepley   Input Parameters:
6570bb7acecfSBarry Smith + dm         - The `DM`
6571bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
65728e4ac7eaSMatthew G. Knepley 
6573bb7acecfSBarry Smith   Note:
6574bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
65755d3b26e6SMatthew G. Knepley 
65768e4ac7eaSMatthew G. Knepley   Level: beginner
65778e4ac7eaSMatthew G. Knepley 
65781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
65798e4ac7eaSMatthew G. Knepley @*/
6580d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6581d71ae5a4SJacob Faibussowitsch {
65828e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
65838e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65848833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
65858e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
65863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65878e4ac7eaSMatthew G. Knepley }
6588c58f1c22SToby Isaac 
6589c58f1c22SToby Isaac /*@C
6590bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6591c58f1c22SToby Isaac 
6592c58f1c22SToby Isaac   Not Collective
6593c58f1c22SToby Isaac 
6594c58f1c22SToby Isaac   Input Parameters:
6595bb7acecfSBarry Smith + dm   - The `DM` object
6596c58f1c22SToby Isaac - name - The label name
6597c58f1c22SToby Isaac 
6598c58f1c22SToby Isaac   Level: intermediate
6599c58f1c22SToby Isaac 
66001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6601c58f1c22SToby Isaac @*/
6602d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6603d71ae5a4SJacob Faibussowitsch {
66045d80c0bfSVaclav Hapla   PetscBool flg;
66055d80c0bfSVaclav Hapla   DMLabel   label;
6606c58f1c22SToby Isaac 
6607c58f1c22SToby Isaac   PetscFunctionBegin;
6608c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66094f572ea9SToby Isaac   PetscAssertPointer(name, 2);
66109566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6611c58f1c22SToby Isaac   if (!flg) {
66129566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
66139566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
66149566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6615c58f1c22SToby Isaac   }
66163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6617c58f1c22SToby Isaac }
6618c58f1c22SToby Isaac 
6619c58f1c22SToby Isaac /*@C
6620bb7acecfSBarry 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.
66210fdc7489SMatthew Knepley 
66220fdc7489SMatthew Knepley   Not Collective
66230fdc7489SMatthew Knepley 
66240fdc7489SMatthew Knepley   Input Parameters:
6625bb7acecfSBarry Smith + dm   - The `DM` object
66260fdc7489SMatthew Knepley . l    - The index for the label
66270fdc7489SMatthew Knepley - name - The label name
66280fdc7489SMatthew Knepley 
66290fdc7489SMatthew Knepley   Level: intermediate
66300fdc7489SMatthew Knepley 
66311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
66320fdc7489SMatthew Knepley @*/
6633d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6634d71ae5a4SJacob Faibussowitsch {
66350fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
66360fdc7489SMatthew Knepley   DMLabel     label;
66370fdc7489SMatthew Knepley   PetscInt    Nl, m;
66380fdc7489SMatthew Knepley   PetscBool   flg, match;
66390fdc7489SMatthew Knepley   const char *lname;
66400fdc7489SMatthew Knepley 
66410fdc7489SMatthew Knepley   PetscFunctionBegin;
66420fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66434f572ea9SToby Isaac   PetscAssertPointer(name, 3);
66449566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
66450fdc7489SMatthew Knepley   if (!flg) {
66469566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
66479566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
66489566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
66490fdc7489SMatthew Knepley   }
66509566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
665163a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
66520fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
66539566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
66549566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
66550fdc7489SMatthew Knepley     if (match) break;
66560fdc7489SMatthew Knepley   }
66573ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
66580fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
66590fdc7489SMatthew Knepley   else prev->next = orig->next;
66600fdc7489SMatthew Knepley   if (!l) {
66610fdc7489SMatthew Knepley     orig->next = dm->labels;
66620fdc7489SMatthew Knepley     dm->labels = orig;
66630fdc7489SMatthew Knepley   } else {
66649371c9d4SSatish Balay     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next)
66659371c9d4SSatish Balay       ;
66660fdc7489SMatthew Knepley     orig->next = prev->next;
66670fdc7489SMatthew Knepley     prev->next = orig;
66680fdc7489SMatthew Knepley   }
66693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66700fdc7489SMatthew Knepley }
66710fdc7489SMatthew Knepley 
66720fdc7489SMatthew Knepley /*@C
6673bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6674c58f1c22SToby Isaac 
6675c58f1c22SToby Isaac   Not Collective
6676c58f1c22SToby Isaac 
6677c58f1c22SToby Isaac   Input Parameters:
6678bb7acecfSBarry Smith + dm    - The `DM` object
6679c58f1c22SToby Isaac . name  - The label name
6680c58f1c22SToby Isaac - point - The mesh point
6681c58f1c22SToby Isaac 
6682c58f1c22SToby Isaac   Output Parameter:
6683c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6684c58f1c22SToby Isaac 
6685c58f1c22SToby Isaac   Level: beginner
6686c58f1c22SToby Isaac 
66871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6688c58f1c22SToby Isaac @*/
6689d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6690d71ae5a4SJacob Faibussowitsch {
6691c58f1c22SToby Isaac   DMLabel label;
6692c58f1c22SToby Isaac 
6693c58f1c22SToby Isaac   PetscFunctionBegin;
6694c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66954f572ea9SToby Isaac   PetscAssertPointer(name, 2);
66969566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
66977a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
66989566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
66993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6700c58f1c22SToby Isaac }
6701c58f1c22SToby Isaac 
6702c58f1c22SToby Isaac /*@C
6703bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6704c58f1c22SToby Isaac 
6705c58f1c22SToby Isaac   Not Collective
6706c58f1c22SToby Isaac 
6707c58f1c22SToby Isaac   Input Parameters:
6708bb7acecfSBarry Smith + dm    - The `DM` object
6709c58f1c22SToby Isaac . name  - The label name
6710c58f1c22SToby Isaac . point - The mesh point
6711c58f1c22SToby Isaac - value - The label value for this point
6712c58f1c22SToby Isaac 
6713c58f1c22SToby Isaac   Output Parameter:
6714c58f1c22SToby Isaac 
6715c58f1c22SToby Isaac   Level: beginner
6716c58f1c22SToby Isaac 
67171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6718c58f1c22SToby Isaac @*/
6719d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6720d71ae5a4SJacob Faibussowitsch {
6721c58f1c22SToby Isaac   DMLabel label;
6722c58f1c22SToby Isaac 
6723c58f1c22SToby Isaac   PetscFunctionBegin;
6724c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67254f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67269566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6727c58f1c22SToby Isaac   if (!label) {
67289566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
67299566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6730c58f1c22SToby Isaac   }
67319566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
67323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6733c58f1c22SToby Isaac }
6734c58f1c22SToby Isaac 
6735c58f1c22SToby Isaac /*@C
6736bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6737c58f1c22SToby Isaac 
6738c58f1c22SToby Isaac   Not Collective
6739c58f1c22SToby Isaac 
6740c58f1c22SToby Isaac   Input Parameters:
6741bb7acecfSBarry Smith + dm    - The `DM` object
6742c58f1c22SToby Isaac . name  - The label name
6743c58f1c22SToby Isaac . point - The mesh point
6744c58f1c22SToby Isaac - value - The label value for this point
6745c58f1c22SToby Isaac 
6746c58f1c22SToby Isaac   Level: beginner
6747c58f1c22SToby Isaac 
67481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6749c58f1c22SToby Isaac @*/
6750d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6751d71ae5a4SJacob Faibussowitsch {
6752c58f1c22SToby Isaac   DMLabel label;
6753c58f1c22SToby Isaac 
6754c58f1c22SToby Isaac   PetscFunctionBegin;
6755c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67564f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67579566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67583ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67599566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
67603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6761c58f1c22SToby Isaac }
6762c58f1c22SToby Isaac 
6763c58f1c22SToby Isaac /*@C
6764bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6765c58f1c22SToby Isaac 
6766c58f1c22SToby Isaac   Not Collective
6767c58f1c22SToby Isaac 
6768c58f1c22SToby Isaac   Input Parameters:
6769bb7acecfSBarry Smith + dm   - The `DM` object
6770c58f1c22SToby Isaac - name - The label name
6771c58f1c22SToby Isaac 
6772c58f1c22SToby Isaac   Output Parameter:
6773c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6774c58f1c22SToby Isaac 
6775c58f1c22SToby Isaac   Level: beginner
6776c58f1c22SToby Isaac 
677760225df5SJacob Faibussowitsch   Developer Notes:
6778bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6779bb7acecfSBarry Smith 
67801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6781c58f1c22SToby Isaac @*/
6782d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6783d71ae5a4SJacob Faibussowitsch {
6784c58f1c22SToby Isaac   DMLabel label;
6785c58f1c22SToby Isaac 
6786c58f1c22SToby Isaac   PetscFunctionBegin;
6787c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67884f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67894f572ea9SToby Isaac   PetscAssertPointer(size, 3);
67909566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6791c58f1c22SToby Isaac   *size = 0;
67923ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67939566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
67943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6795c58f1c22SToby Isaac }
6796c58f1c22SToby Isaac 
6797c58f1c22SToby Isaac /*@C
6798bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6799c58f1c22SToby Isaac 
6800c58f1c22SToby Isaac   Not Collective
6801c58f1c22SToby Isaac 
6802c58f1c22SToby Isaac   Input Parameters:
680360225df5SJacob Faibussowitsch + dm   - The `DM` object
6804c58f1c22SToby Isaac - name - The label name
6805c58f1c22SToby Isaac 
6806c58f1c22SToby Isaac   Output Parameter:
680720f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6808c58f1c22SToby Isaac 
6809c58f1c22SToby Isaac   Level: beginner
6810c58f1c22SToby Isaac 
68111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6812c58f1c22SToby Isaac @*/
6813d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6814d71ae5a4SJacob Faibussowitsch {
6815c58f1c22SToby Isaac   DMLabel label;
6816c58f1c22SToby Isaac 
6817c58f1c22SToby Isaac   PetscFunctionBegin;
6818c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68194f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68204f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
68219566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6822c58f1c22SToby Isaac   *ids = NULL;
6823dab2e251SBlaise Bourdin   if (label) {
68249566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6825dab2e251SBlaise Bourdin   } else {
6826dab2e251SBlaise Bourdin     /* returning an empty IS */
68279566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6828dab2e251SBlaise Bourdin   }
68293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6830c58f1c22SToby Isaac }
6831c58f1c22SToby Isaac 
6832c58f1c22SToby Isaac /*@C
6833c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6834c58f1c22SToby Isaac 
6835c58f1c22SToby Isaac   Not Collective
6836c58f1c22SToby Isaac 
6837c58f1c22SToby Isaac   Input Parameters:
6838bb7acecfSBarry Smith + dm    - The `DM` object
6839c58f1c22SToby Isaac . name  - The label name
6840c58f1c22SToby Isaac - value - The stratum value
6841c58f1c22SToby Isaac 
6842c58f1c22SToby Isaac   Output Parameter:
6843bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6844c58f1c22SToby Isaac 
6845c58f1c22SToby Isaac   Level: beginner
6846c58f1c22SToby Isaac 
68471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6848c58f1c22SToby Isaac @*/
6849d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6850d71ae5a4SJacob Faibussowitsch {
6851c58f1c22SToby Isaac   DMLabel label;
6852c58f1c22SToby Isaac 
6853c58f1c22SToby Isaac   PetscFunctionBegin;
6854c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68554f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68564f572ea9SToby Isaac   PetscAssertPointer(size, 4);
68579566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6858c58f1c22SToby Isaac   *size = 0;
68593ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68609566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
68613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6862c58f1c22SToby Isaac }
6863c58f1c22SToby Isaac 
6864c58f1c22SToby Isaac /*@C
6865c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6866c58f1c22SToby Isaac 
6867c58f1c22SToby Isaac   Not Collective
6868c58f1c22SToby Isaac 
6869c58f1c22SToby Isaac   Input Parameters:
6870bb7acecfSBarry Smith + dm    - The `DM` object
6871c58f1c22SToby Isaac . name  - The label name
6872c58f1c22SToby Isaac - value - The stratum value
6873c58f1c22SToby Isaac 
6874c58f1c22SToby Isaac   Output Parameter:
687520f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
6876c58f1c22SToby Isaac 
6877c58f1c22SToby Isaac   Level: beginner
6878c58f1c22SToby Isaac 
68791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6880c58f1c22SToby Isaac @*/
6881d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6882d71ae5a4SJacob Faibussowitsch {
6883c58f1c22SToby Isaac   DMLabel label;
6884c58f1c22SToby Isaac 
6885c58f1c22SToby Isaac   PetscFunctionBegin;
6886c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68874f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68884f572ea9SToby Isaac   PetscAssertPointer(points, 4);
68899566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6890c58f1c22SToby Isaac   *points = NULL;
68913ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68929566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
68933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6894c58f1c22SToby Isaac }
6895c58f1c22SToby Isaac 
68964de306b1SToby Isaac /*@C
68979044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
68984de306b1SToby Isaac 
68994de306b1SToby Isaac   Not Collective
69004de306b1SToby Isaac 
69014de306b1SToby Isaac   Input Parameters:
6902bb7acecfSBarry Smith + dm     - The `DM` object
69034de306b1SToby Isaac . name   - The label name
69044de306b1SToby Isaac . value  - The stratum value
69054de306b1SToby Isaac - points - The stratum points
69064de306b1SToby Isaac 
69074de306b1SToby Isaac   Level: beginner
69084de306b1SToby Isaac 
69091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
69104de306b1SToby Isaac @*/
6911d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6912d71ae5a4SJacob Faibussowitsch {
69134de306b1SToby Isaac   DMLabel label;
69144de306b1SToby Isaac 
69154de306b1SToby Isaac   PetscFunctionBegin;
69164de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69174f572ea9SToby Isaac   PetscAssertPointer(name, 2);
6918292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
69199566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
69203ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69219566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
69223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
69234de306b1SToby Isaac }
69244de306b1SToby Isaac 
6925c58f1c22SToby Isaac /*@C
6926bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6927c58f1c22SToby Isaac 
6928c58f1c22SToby Isaac   Not Collective
6929c58f1c22SToby Isaac 
6930c58f1c22SToby Isaac   Input Parameters:
6931bb7acecfSBarry Smith + dm    - The `DM` object
6932c58f1c22SToby Isaac . name  - The label name
6933c58f1c22SToby Isaac - value - The label value for this point
6934c58f1c22SToby Isaac 
6935c58f1c22SToby Isaac   Output Parameter:
6936c58f1c22SToby Isaac 
6937c58f1c22SToby Isaac   Level: beginner
6938c58f1c22SToby Isaac 
69391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6940c58f1c22SToby Isaac @*/
6941d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6942d71ae5a4SJacob Faibussowitsch {
6943c58f1c22SToby Isaac   DMLabel label;
6944c58f1c22SToby Isaac 
6945c58f1c22SToby Isaac   PetscFunctionBegin;
6946c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69474f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69489566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
69493ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69509566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
69513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6952c58f1c22SToby Isaac }
6953c58f1c22SToby Isaac 
6954c58f1c22SToby Isaac /*@
6955bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6956c58f1c22SToby Isaac 
6957c58f1c22SToby Isaac   Not Collective
6958c58f1c22SToby Isaac 
6959c58f1c22SToby Isaac   Input Parameter:
6960bb7acecfSBarry Smith . dm - The `DM` object
6961c58f1c22SToby Isaac 
6962c58f1c22SToby Isaac   Output Parameter:
6963c58f1c22SToby Isaac . numLabels - the number of Labels
6964c58f1c22SToby Isaac 
6965c58f1c22SToby Isaac   Level: intermediate
6966c58f1c22SToby Isaac 
69671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6968c58f1c22SToby Isaac @*/
6969d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6970d71ae5a4SJacob Faibussowitsch {
69715d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6972c58f1c22SToby Isaac   PetscInt    n    = 0;
6973c58f1c22SToby Isaac 
6974c58f1c22SToby Isaac   PetscFunctionBegin;
6975c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69764f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
69779371c9d4SSatish Balay   while (next) {
69789371c9d4SSatish Balay     ++n;
69799371c9d4SSatish Balay     next = next->next;
69809371c9d4SSatish Balay   }
6981c58f1c22SToby Isaac   *numLabels = n;
69823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6983c58f1c22SToby Isaac }
6984c58f1c22SToby Isaac 
6985c58f1c22SToby Isaac /*@C
6986c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6987c58f1c22SToby Isaac 
6988c58f1c22SToby Isaac   Not Collective
6989c58f1c22SToby Isaac 
6990c58f1c22SToby Isaac   Input Parameters:
6991bb7acecfSBarry Smith + dm - The `DM` object
6992c58f1c22SToby Isaac - n  - the label number
6993c58f1c22SToby Isaac 
6994c58f1c22SToby Isaac   Output Parameter:
6995c58f1c22SToby Isaac . name - the label name
6996c58f1c22SToby Isaac 
6997c58f1c22SToby Isaac   Level: intermediate
6998c58f1c22SToby Isaac 
699960225df5SJacob Faibussowitsch   Developer Notes:
7000bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
7001bb7acecfSBarry Smith 
70021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7003c58f1c22SToby Isaac @*/
7004d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7005d71ae5a4SJacob Faibussowitsch {
70065d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7007c58f1c22SToby Isaac   PetscInt    l    = 0;
7008c58f1c22SToby Isaac 
7009c58f1c22SToby Isaac   PetscFunctionBegin;
7010c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70114f572ea9SToby Isaac   PetscAssertPointer(name, 3);
7012c58f1c22SToby Isaac   while (next) {
7013c58f1c22SToby Isaac     if (l == n) {
70149566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
70153ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7016c58f1c22SToby Isaac     }
7017c58f1c22SToby Isaac     ++l;
7018c58f1c22SToby Isaac     next = next->next;
7019c58f1c22SToby Isaac   }
702063a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7021c58f1c22SToby Isaac }
7022c58f1c22SToby Isaac 
7023c58f1c22SToby Isaac /*@C
7024bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
7025c58f1c22SToby Isaac 
7026c58f1c22SToby Isaac   Not Collective
7027c58f1c22SToby Isaac 
7028c58f1c22SToby Isaac   Input Parameters:
7029bb7acecfSBarry Smith + dm   - The `DM` object
7030c58f1c22SToby Isaac - name - The label name
7031c58f1c22SToby Isaac 
7032c58f1c22SToby Isaac   Output Parameter:
7033bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
7034c58f1c22SToby Isaac 
7035c58f1c22SToby Isaac   Level: intermediate
7036c58f1c22SToby Isaac 
70371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7038c58f1c22SToby Isaac @*/
7039d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7040d71ae5a4SJacob Faibussowitsch {
70415d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7042d67d17b1SMatthew G. Knepley   const char *lname;
7043c58f1c22SToby Isaac 
7044c58f1c22SToby Isaac   PetscFunctionBegin;
7045c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70464f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70474f572ea9SToby Isaac   PetscAssertPointer(hasLabel, 3);
7048c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7049c58f1c22SToby Isaac   while (next) {
70509566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70519566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7052c58f1c22SToby Isaac     if (*hasLabel) break;
7053c58f1c22SToby Isaac     next = next->next;
7054c58f1c22SToby Isaac   }
70553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7056c58f1c22SToby Isaac }
7057c58f1c22SToby Isaac 
7058a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7059c58f1c22SToby Isaac /*@C
706020f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
7061c58f1c22SToby Isaac 
7062c58f1c22SToby Isaac   Not Collective
7063c58f1c22SToby Isaac 
7064c58f1c22SToby Isaac   Input Parameters:
7065bb7acecfSBarry Smith + dm   - The `DM` object
7066c58f1c22SToby Isaac - name - The label name
7067c58f1c22SToby Isaac 
7068c58f1c22SToby Isaac   Output Parameter:
706920f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
7070c58f1c22SToby Isaac 
7071bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7072bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7073bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7074bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7075bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7076bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7077bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
70786d7c9049SMatthew G. Knepley 
7079c58f1c22SToby Isaac   Level: intermediate
7080c58f1c22SToby Isaac 
708160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7082c58f1c22SToby Isaac @*/
7083d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7084d71ae5a4SJacob Faibussowitsch {
70855d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7086c58f1c22SToby Isaac   PetscBool   hasLabel;
7087d67d17b1SMatthew G. Knepley   const char *lname;
7088c58f1c22SToby Isaac 
7089c58f1c22SToby Isaac   PetscFunctionBegin;
7090c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70914f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70924f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7093c58f1c22SToby Isaac   *label = NULL;
7094c58f1c22SToby Isaac   while (next) {
70959566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70969566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7097c58f1c22SToby Isaac     if (hasLabel) {
7098c58f1c22SToby Isaac       *label = next->label;
7099c58f1c22SToby Isaac       break;
7100c58f1c22SToby Isaac     }
7101c58f1c22SToby Isaac     next = next->next;
7102c58f1c22SToby Isaac   }
71033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7104c58f1c22SToby Isaac }
7105c58f1c22SToby Isaac 
7106c58f1c22SToby Isaac /*@C
7107bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7108c58f1c22SToby Isaac 
7109c58f1c22SToby Isaac   Not Collective
7110c58f1c22SToby Isaac 
7111c58f1c22SToby Isaac   Input Parameters:
7112bb7acecfSBarry Smith + dm - The `DM` object
7113c58f1c22SToby Isaac - n  - the label number
7114c58f1c22SToby Isaac 
7115c58f1c22SToby Isaac   Output Parameter:
7116c58f1c22SToby Isaac . label - the label
7117c58f1c22SToby Isaac 
7118c58f1c22SToby Isaac   Level: intermediate
7119c58f1c22SToby Isaac 
71201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7121c58f1c22SToby Isaac @*/
7122d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7123d71ae5a4SJacob Faibussowitsch {
71245d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7125c58f1c22SToby Isaac   PetscInt    l    = 0;
7126c58f1c22SToby Isaac 
7127c58f1c22SToby Isaac   PetscFunctionBegin;
7128c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71294f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7130c58f1c22SToby Isaac   while (next) {
7131c58f1c22SToby Isaac     if (l == n) {
7132c58f1c22SToby Isaac       *label = next->label;
71333ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7134c58f1c22SToby Isaac     }
7135c58f1c22SToby Isaac     ++l;
7136c58f1c22SToby Isaac     next = next->next;
7137c58f1c22SToby Isaac   }
713863a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7139c58f1c22SToby Isaac }
7140c58f1c22SToby Isaac 
7141c58f1c22SToby Isaac /*@C
7142bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7143c58f1c22SToby Isaac 
7144c58f1c22SToby Isaac   Not Collective
7145c58f1c22SToby Isaac 
7146c58f1c22SToby Isaac   Input Parameters:
7147bb7acecfSBarry Smith + dm    - The `DM` object
7148bb7acecfSBarry Smith - label - The `DMLabel`
7149c58f1c22SToby Isaac 
7150c58f1c22SToby Isaac   Level: developer
7151c58f1c22SToby Isaac 
715260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7153c58f1c22SToby Isaac @*/
7154d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7155d71ae5a4SJacob Faibussowitsch {
71565d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7157c58f1c22SToby Isaac   PetscBool   hasLabel;
7158d67d17b1SMatthew G. Knepley   const char *lname;
71595d80c0bfSVaclav Hapla   PetscBool   flg;
7160c58f1c22SToby Isaac 
7161c58f1c22SToby Isaac   PetscFunctionBegin;
7162c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71639566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
71649566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
71657a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
71669566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7167c58f1c22SToby Isaac   tmpLabel->label  = label;
7168c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
71695d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
71705d80c0bfSVaclav Hapla   *p = tmpLabel;
71719566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
71729566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
71735d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
71749566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7175ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
71763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7177c58f1c22SToby Isaac }
7178c58f1c22SToby Isaac 
7179a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7180c58f1c22SToby Isaac /*@C
71814a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
71824a7ee7d0SMatthew G. Knepley 
71834a7ee7d0SMatthew G. Knepley   Not Collective
71844a7ee7d0SMatthew G. Knepley 
71854a7ee7d0SMatthew G. Knepley   Input Parameters:
7186bb7acecfSBarry Smith + dm    - The `DM` object
7187bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
71884a7ee7d0SMatthew G. Knepley 
7189bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7190bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7191bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7192bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7193bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7194bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7195bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
71964a7ee7d0SMatthew G. Knepley 
71974a7ee7d0SMatthew G. Knepley   Level: intermediate
71984a7ee7d0SMatthew G. Knepley 
71991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
72004a7ee7d0SMatthew G. Knepley @*/
7201d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7202d71ae5a4SJacob Faibussowitsch {
72034a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
72044a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
72054a7ee7d0SMatthew G. Knepley   const char *name, *lname;
72064a7ee7d0SMatthew G. Knepley 
72074a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
72084a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72094a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
72109566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
72114a7ee7d0SMatthew G. Knepley   while (next) {
72129566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72139566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
72144a7ee7d0SMatthew G. Knepley     if (hasLabel) {
72159566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
72169566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
72174a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
72189566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
72194a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
72209566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
72214a7ee7d0SMatthew G. Knepley       next->label = label;
72224a7ee7d0SMatthew G. Knepley       break;
72234a7ee7d0SMatthew G. Knepley     }
72244a7ee7d0SMatthew G. Knepley     next = next->next;
72254a7ee7d0SMatthew G. Knepley   }
72263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
72274a7ee7d0SMatthew G. Knepley }
72284a7ee7d0SMatthew G. Knepley 
72294a7ee7d0SMatthew G. Knepley /*@C
7230bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7231c58f1c22SToby Isaac 
7232c58f1c22SToby Isaac   Not Collective
7233c58f1c22SToby Isaac 
7234c58f1c22SToby Isaac   Input Parameters:
7235bb7acecfSBarry Smith + dm   - The `DM` object
7236c58f1c22SToby Isaac - name - The label name
7237c58f1c22SToby Isaac 
7238c58f1c22SToby Isaac   Output Parameter:
723920f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7240bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7241c58f1c22SToby Isaac 
7242c58f1c22SToby Isaac   Level: developer
7243c58f1c22SToby Isaac 
72441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7245c58f1c22SToby Isaac @*/
7246d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7247d71ae5a4SJacob Faibussowitsch {
724895d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7249c58f1c22SToby Isaac   PetscBool   hasLabel;
7250d67d17b1SMatthew G. Knepley   const char *lname;
7251c58f1c22SToby Isaac 
7252c58f1c22SToby Isaac   PetscFunctionBegin;
7253c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72544f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7255e5472504SVaclav Hapla   if (label) {
72564f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7257c58f1c22SToby Isaac     *label = NULL;
7258e5472504SVaclav Hapla   }
72595d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
72609566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
72619566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7262c58f1c22SToby Isaac     if (hasLabel) {
726395d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
72649566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
726595d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
72669566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7267ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
726895d578d6SVaclav Hapla       if (label) *label = link->label;
72699566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
72709566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7271c58f1c22SToby Isaac       break;
7272c58f1c22SToby Isaac     }
7273c58f1c22SToby Isaac   }
72743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7275c58f1c22SToby Isaac }
7276c58f1c22SToby Isaac 
7277306894acSVaclav Hapla /*@
7278bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7279306894acSVaclav Hapla 
7280306894acSVaclav Hapla   Not Collective
7281306894acSVaclav Hapla 
7282306894acSVaclav Hapla   Input Parameters:
7283bb7acecfSBarry Smith + dm           - The `DM` object
7284bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
728520f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7286306894acSVaclav Hapla 
7287306894acSVaclav Hapla   Level: developer
7288306894acSVaclav Hapla 
7289bb7acecfSBarry Smith   Note:
7290306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7291bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7292306894acSVaclav Hapla   *label nullified.
7293306894acSVaclav Hapla 
72941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7295306894acSVaclav Hapla @*/
7296d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7297d71ae5a4SJacob Faibussowitsch {
729843e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7299306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7300306894acSVaclav Hapla 
7301306894acSVaclav Hapla   PetscFunctionBegin;
7302306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73034f572ea9SToby Isaac   PetscAssertPointer(label, 2);
73043ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7305306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7306306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
73075d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
730843e45a93SVaclav Hapla     if (*label == link->label) {
7309306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
731043e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7311306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7312ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
731343e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
73149566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
73159566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7316306894acSVaclav Hapla       break;
7317306894acSVaclav Hapla     }
7318306894acSVaclav Hapla   }
73197a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
73203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7321306894acSVaclav Hapla }
7322306894acSVaclav Hapla 
7323c58f1c22SToby Isaac /*@C
7324c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7325c58f1c22SToby Isaac 
7326c58f1c22SToby Isaac   Not Collective
7327c58f1c22SToby Isaac 
7328c58f1c22SToby Isaac   Input Parameters:
7329bb7acecfSBarry Smith + dm   - The `DM` object
7330c58f1c22SToby Isaac - name - The label name
7331c58f1c22SToby Isaac 
7332c58f1c22SToby Isaac   Output Parameter:
7333c58f1c22SToby Isaac . output - The flag for output
7334c58f1c22SToby Isaac 
7335c58f1c22SToby Isaac   Level: developer
7336c58f1c22SToby Isaac 
73371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7338c58f1c22SToby Isaac @*/
7339d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7340d71ae5a4SJacob Faibussowitsch {
73415d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7342d67d17b1SMatthew G. Knepley   const char *lname;
7343c58f1c22SToby Isaac 
7344c58f1c22SToby Isaac   PetscFunctionBegin;
7345c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73464f572ea9SToby Isaac   PetscAssertPointer(name, 2);
73474f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7348c58f1c22SToby Isaac   while (next) {
7349c58f1c22SToby Isaac     PetscBool flg;
7350c58f1c22SToby Isaac 
73519566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73529566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
73539371c9d4SSatish Balay     if (flg) {
73549371c9d4SSatish Balay       *output = next->output;
73553ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
73569371c9d4SSatish Balay     }
7357c58f1c22SToby Isaac     next = next->next;
7358c58f1c22SToby Isaac   }
735998921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7360c58f1c22SToby Isaac }
7361c58f1c22SToby Isaac 
7362c58f1c22SToby Isaac /*@C
7363bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7364c58f1c22SToby Isaac 
7365c58f1c22SToby Isaac   Not Collective
7366c58f1c22SToby Isaac 
7367c58f1c22SToby Isaac   Input Parameters:
7368bb7acecfSBarry Smith + dm     - The `DM` object
7369c58f1c22SToby Isaac . name   - The label name
7370bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7371c58f1c22SToby Isaac 
7372c58f1c22SToby Isaac   Level: developer
7373c58f1c22SToby Isaac 
73741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7375c58f1c22SToby Isaac @*/
7376d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7377d71ae5a4SJacob Faibussowitsch {
73785d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7379d67d17b1SMatthew G. Knepley   const char *lname;
7380c58f1c22SToby Isaac 
7381c58f1c22SToby Isaac   PetscFunctionBegin;
7382c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73834f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7384c58f1c22SToby Isaac   while (next) {
7385c58f1c22SToby Isaac     PetscBool flg;
7386c58f1c22SToby Isaac 
73879566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73889566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
73899371c9d4SSatish Balay     if (flg) {
73909371c9d4SSatish Balay       next->output = output;
73913ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
73929371c9d4SSatish Balay     }
7393c58f1c22SToby Isaac     next = next->next;
7394c58f1c22SToby Isaac   }
739598921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7396c58f1c22SToby Isaac }
7397c58f1c22SToby Isaac 
7398c58f1c22SToby Isaac /*@
7399bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7400c58f1c22SToby Isaac 
740120f4b53cSBarry Smith   Collective
7402c58f1c22SToby Isaac 
7403d8d19677SJose E. Roman   Input Parameters:
7404bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7405bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7406bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7407bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7408bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7409c58f1c22SToby Isaac 
7410c58f1c22SToby Isaac   Level: intermediate
7411c58f1c22SToby Isaac 
7412bb7acecfSBarry Smith   Note:
74132cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7414c58f1c22SToby Isaac 
74151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7416c58f1c22SToby Isaac @*/
7417d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7418d71ae5a4SJacob Faibussowitsch {
74192cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7420c58f1c22SToby Isaac   const char *name;
7421c58f1c22SToby Isaac   PetscBool   flg;
74225d80c0bfSVaclav Hapla   DMLabelLink link;
7423c58f1c22SToby Isaac 
74245d80c0bfSVaclav Hapla   PetscFunctionBegin;
74255d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
74265d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
74275d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
74285d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
74297a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
74303ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
74315d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
74325d80c0bfSVaclav Hapla     label = link->label;
74339566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
74345d80c0bfSVaclav Hapla     if (!all) {
74359566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7436c58f1c22SToby Isaac       if (flg) continue;
74379566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
74387d5acc75SStefano Zampini       if (flg) continue;
74399566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7440ba2698f1SMatthew G. Knepley       if (flg) continue;
74415d80c0bfSVaclav Hapla     }
74429566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
74432cbb9b06SVaclav Hapla     if (labelOld) {
74442cbb9b06SVaclav Hapla       switch (emode) {
7445d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7446d71ae5a4SJacob Faibussowitsch         continue;
7447d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7448d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7449d71ae5a4SJacob Faibussowitsch         break;
7450d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7451d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7452d71ae5a4SJacob Faibussowitsch       default:
7453d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
74542cbb9b06SVaclav Hapla       }
74552cbb9b06SVaclav Hapla     }
74565d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
74579566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
74585d80c0bfSVaclav Hapla     } else {
74595d80c0bfSVaclav Hapla       labelNew = label;
74605d80c0bfSVaclav Hapla     }
74619566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
74629566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7463c58f1c22SToby Isaac   }
74643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7465c58f1c22SToby Isaac }
7466461a15a0SLisandro Dalcin 
7467609dae6eSVaclav Hapla /*@C
7468b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7469609dae6eSVaclav Hapla 
747020f4b53cSBarry Smith   Collective; No Fortran Support
7471609dae6eSVaclav Hapla 
7472609dae6eSVaclav Hapla   Input Parameters:
7473bb7acecfSBarry Smith + dm0 - First `DM` object
7474bb7acecfSBarry Smith - dm1 - Second `DM` object
7475609dae6eSVaclav Hapla 
7476a4e35b19SJacob Faibussowitsch   Output Parameters:
74775efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
747820f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7479609dae6eSVaclav Hapla 
7480609dae6eSVaclav Hapla   Level: intermediate
7481609dae6eSVaclav Hapla 
7482609dae6eSVaclav Hapla   Notes:
7483bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7484bb7acecfSBarry Smith 
748520f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7486bb7acecfSBarry Smith 
748720f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7488609dae6eSVaclav Hapla 
74895efe38ccSVaclav Hapla   The output message is set independently on each rank.
7490bb7acecfSBarry Smith 
7491bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7492bb7acecfSBarry Smith 
749320f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7494bb7acecfSBarry Smith 
749520f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7496609dae6eSVaclav Hapla 
7497609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7498bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7499609dae6eSVaclav Hapla 
75001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7501609dae6eSVaclav Hapla @*/
7502d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7503d71ae5a4SJacob Faibussowitsch {
75045efe38ccSVaclav Hapla   PetscInt    n, i;
7505609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
75065efe38ccSVaclav Hapla   PetscBool   eq;
7507609dae6eSVaclav Hapla   MPI_Comm    comm;
75085efe38ccSVaclav Hapla   PetscMPIInt rank;
7509609dae6eSVaclav Hapla 
7510609dae6eSVaclav Hapla   PetscFunctionBegin;
7511609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7512609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7513609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
75144f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
75154f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
75169566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
75179566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
75185efe38ccSVaclav Hapla   {
75195efe38ccSVaclav Hapla     PetscInt n1;
75205efe38ccSVaclav Hapla 
75219566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
75229566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
75235efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
752448a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7525712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
75265efe38ccSVaclav Hapla     if (!eq) goto finish;
75275efe38ccSVaclav Hapla   }
75285efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7529609dae6eSVaclav Hapla     DMLabel     l0, l1;
7530609dae6eSVaclav Hapla     const char *name;
7531609dae6eSVaclav Hapla     char       *msgInner;
7532609dae6eSVaclav Hapla 
7533609dae6eSVaclav Hapla     /* Ignore label order */
75349566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
75359566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
75369566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7537609dae6eSVaclav Hapla     if (!l1) {
753863a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
75395efe38ccSVaclav Hapla       eq = PETSC_FALSE;
75405efe38ccSVaclav Hapla       break;
7541609dae6eSVaclav Hapla     }
75429566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
75439566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
75449566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
75455efe38ccSVaclav Hapla     if (!eq) break;
7546609dae6eSVaclav Hapla   }
7547712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7548609dae6eSVaclav Hapla finish:
75495efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7550609dae6eSVaclav Hapla   if (message) {
7551609dae6eSVaclav Hapla     *message = NULL;
755248a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
75535efe38ccSVaclav Hapla   } else {
755448a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
75559566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
75565efe38ccSVaclav Hapla   }
75575efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
75585efe38ccSVaclav Hapla   if (equal) *equal = eq;
75597a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
75603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7561609dae6eSVaclav Hapla }
7562609dae6eSVaclav Hapla 
7563d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7564d71ae5a4SJacob Faibussowitsch {
7565461a15a0SLisandro Dalcin   PetscFunctionBegin;
75664f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7567461a15a0SLisandro Dalcin   if (!*label) {
75689566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
75699566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7570461a15a0SLisandro Dalcin   }
75719566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
75723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7573461a15a0SLisandro Dalcin }
7574461a15a0SLisandro Dalcin 
75750fdc7489SMatthew Knepley /*
75760fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
75770fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
75780fdc7489SMatthew Knepley   (label, id) pair in the DM.
75790fdc7489SMatthew Knepley 
75800fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
75810fdc7489SMatthew Knepley   each label.
75820fdc7489SMatthew Knepley */
7583d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7584d71ae5a4SJacob Faibussowitsch {
75850fdc7489SMatthew Knepley   DMUniversalLabel ul;
75860fdc7489SMatthew Knepley   PetscBool       *active;
75870fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
75880fdc7489SMatthew Knepley 
75890fdc7489SMatthew Knepley   PetscFunctionBegin;
75909566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
75919566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
75929566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
75939566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
75940fdc7489SMatthew Knepley   ul->Nl = 0;
75950fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
75960fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
75970fdc7489SMatthew Knepley     const char *name;
75980fdc7489SMatthew Knepley 
75999566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
76009566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
76019566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
76020fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
76030fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
76040fdc7489SMatthew Knepley   }
76059566063dSJacob 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));
76060fdc7489SMatthew Knepley   ul->Nv = 0;
76070fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
76080fdc7489SMatthew Knepley     DMLabel     label;
76090fdc7489SMatthew Knepley     PetscInt    nv;
76100fdc7489SMatthew Knepley     const char *name;
76110fdc7489SMatthew Knepley 
76120fdc7489SMatthew Knepley     if (!active[l]) continue;
76139566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
76149566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
76159566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
76169566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
76170fdc7489SMatthew Knepley     ul->indices[m] = l;
76180fdc7489SMatthew Knepley     ul->Nv += nv;
76190fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
76200fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
76210fdc7489SMatthew Knepley     ++m;
76220fdc7489SMatthew Knepley   }
76230fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
76240fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
76250fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
76260fdc7489SMatthew Knepley   }
76270fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
76280fdc7489SMatthew Knepley     PetscInt b;
76290fdc7489SMatthew Knepley 
76300fdc7489SMatthew Knepley     ul->masks[l] = 0;
76310fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
76320fdc7489SMatthew Knepley   }
76339566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
76340fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
76350fdc7489SMatthew Knepley     DMLabel         label;
76360fdc7489SMatthew Knepley     IS              valueIS;
76370fdc7489SMatthew Knepley     const PetscInt *varr;
76380fdc7489SMatthew Knepley     PetscInt        nv, v;
76390fdc7489SMatthew Knepley 
76400fdc7489SMatthew Knepley     if (!active[l]) continue;
76419566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
76429566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
76439566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
76449566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7645ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
76469566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
76479566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
76489566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
76490fdc7489SMatthew Knepley     ++m;
76500fdc7489SMatthew Knepley   }
76519566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
76520fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
76530fdc7489SMatthew Knepley     PetscInt  uval   = 0;
76540fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
76550fdc7489SMatthew Knepley 
76560fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
76570fdc7489SMatthew Knepley       DMLabel  label;
76580649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
76590fdc7489SMatthew Knepley 
76600fdc7489SMatthew Knepley       if (!active[l]) continue;
76619566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
76629566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
76639566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
76649371c9d4SSatish Balay       if (val == defval) {
76659371c9d4SSatish Balay         ++m;
76669371c9d4SSatish Balay         continue;
76679371c9d4SSatish Balay       }
76680649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
76690fdc7489SMatthew Knepley       marked = PETSC_TRUE;
76709566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
767163a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
76720fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
76730fdc7489SMatthew Knepley       ++m;
76740fdc7489SMatthew Knepley     }
76759566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
76760fdc7489SMatthew Knepley   }
76779566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
76780fdc7489SMatthew Knepley   *universal = ul;
76793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76800fdc7489SMatthew Knepley }
76810fdc7489SMatthew Knepley 
7682d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7683d71ae5a4SJacob Faibussowitsch {
76840fdc7489SMatthew Knepley   PetscInt l;
76850fdc7489SMatthew Knepley 
76860fdc7489SMatthew Knepley   PetscFunctionBegin;
76879566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
76889566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
76899566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
76909566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
76919566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
76920fdc7489SMatthew Knepley   *universal = NULL;
76933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76940fdc7489SMatthew Knepley }
76950fdc7489SMatthew Knepley 
7696d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7697d71ae5a4SJacob Faibussowitsch {
76980fdc7489SMatthew Knepley   PetscFunctionBegin;
76994f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
77000fdc7489SMatthew Knepley   *ulabel = ul->label;
77013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77020fdc7489SMatthew Knepley }
77030fdc7489SMatthew Knepley 
7704d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7705d71ae5a4SJacob Faibussowitsch {
77060fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
77070fdc7489SMatthew Knepley 
77080fdc7489SMatthew Knepley   PetscFunctionBegin;
7709064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
77100fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
77119566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
77129566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
77130fdc7489SMatthew Knepley   }
77140fdc7489SMatthew Knepley   if (preserveOrder) {
77150fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
77160fdc7489SMatthew Knepley       const char *name;
77170fdc7489SMatthew Knepley       PetscBool   match;
77180fdc7489SMatthew Knepley 
77199566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
77209566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
772163a3b9bcSJacob 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]);
77220fdc7489SMatthew Knepley     }
77230fdc7489SMatthew Knepley   }
77243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77250fdc7489SMatthew Knepley }
77260fdc7489SMatthew Knepley 
7727d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7728d71ae5a4SJacob Faibussowitsch {
77290fdc7489SMatthew Knepley   PetscInt l;
77300fdc7489SMatthew Knepley 
77310fdc7489SMatthew Knepley   PetscFunctionBegin;
77320fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
77330fdc7489SMatthew Knepley     DMLabel  label;
77340fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
77350fdc7489SMatthew Knepley 
77360fdc7489SMatthew Knepley     if (lval) {
77379566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
77389566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
77399566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
77400fdc7489SMatthew Knepley     }
77410fdc7489SMatthew Knepley   }
77423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77430fdc7489SMatthew Knepley }
7744a8fb8f29SToby Isaac 
7745a8fb8f29SToby Isaac /*@
7746bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7747bb7acecfSBarry Smith 
774820f4b53cSBarry Smith   Not Collective
7749a8fb8f29SToby Isaac 
7750a8fb8f29SToby Isaac   Input Parameter:
7751bb7acecfSBarry Smith . dm - The `DM` object
7752a8fb8f29SToby Isaac 
7753a8fb8f29SToby Isaac   Output Parameter:
7754bb7acecfSBarry Smith . cdm - The coarse `DM`
7755a8fb8f29SToby Isaac 
7756a8fb8f29SToby Isaac   Level: intermediate
7757a8fb8f29SToby Isaac 
77581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7759a8fb8f29SToby Isaac @*/
7760d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7761d71ae5a4SJacob Faibussowitsch {
7762a8fb8f29SToby Isaac   PetscFunctionBegin;
7763a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77644f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7765a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
77663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7767a8fb8f29SToby Isaac }
7768a8fb8f29SToby Isaac 
7769a8fb8f29SToby Isaac /*@
7770bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7771a8fb8f29SToby Isaac 
7772a8fb8f29SToby Isaac   Input Parameters:
7773bb7acecfSBarry Smith + dm  - The `DM` object
7774bb7acecfSBarry Smith - cdm - The coarse `DM`
7775a8fb8f29SToby Isaac 
7776a8fb8f29SToby Isaac   Level: intermediate
7777a8fb8f29SToby Isaac 
7778bb7acecfSBarry Smith   Note:
7779bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7780bb7acecfSBarry Smith 
77811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7782a8fb8f29SToby Isaac @*/
7783d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7784d71ae5a4SJacob Faibussowitsch {
7785a8fb8f29SToby Isaac   PetscFunctionBegin;
7786a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7787a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
778889d734beSBarry Smith   if (dm == cdm) cdm = NULL;
77899566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
77909566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7791a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
77923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7793a8fb8f29SToby Isaac }
7794a8fb8f29SToby Isaac 
779588bdff64SToby Isaac /*@
7796bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
779788bdff64SToby Isaac 
779888bdff64SToby Isaac   Input Parameter:
7799bb7acecfSBarry Smith . dm - The `DM` object
780088bdff64SToby Isaac 
780188bdff64SToby Isaac   Output Parameter:
7802bb7acecfSBarry Smith . fdm - The fine `DM`
780388bdff64SToby Isaac 
780488bdff64SToby Isaac   Level: intermediate
780588bdff64SToby Isaac 
78061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
780788bdff64SToby Isaac @*/
7808d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7809d71ae5a4SJacob Faibussowitsch {
781088bdff64SToby Isaac   PetscFunctionBegin;
781188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78124f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
781388bdff64SToby Isaac   *fdm = dm->fineMesh;
78143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
781588bdff64SToby Isaac }
781688bdff64SToby Isaac 
781788bdff64SToby Isaac /*@
7818bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
781988bdff64SToby Isaac 
782088bdff64SToby Isaac   Input Parameters:
7821bb7acecfSBarry Smith + dm  - The `DM` object
7822bb7acecfSBarry Smith - fdm - The fine `DM`
782388bdff64SToby Isaac 
7824bb7acecfSBarry Smith   Level: developer
782588bdff64SToby Isaac 
7826bb7acecfSBarry Smith   Note:
7827bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7828bb7acecfSBarry Smith 
78291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
783088bdff64SToby Isaac @*/
7831d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7832d71ae5a4SJacob Faibussowitsch {
783388bdff64SToby Isaac   PetscFunctionBegin;
783488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
783588bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
783689d734beSBarry Smith   if (dm == fdm) fdm = NULL;
78379566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
78389566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
783988bdff64SToby Isaac   dm->fineMesh = fdm;
78403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
784188bdff64SToby Isaac }
784288bdff64SToby Isaac 
7843a6ba4734SToby Isaac /*@C
7844bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7845a6ba4734SToby Isaac 
784620f4b53cSBarry Smith   Collective
7847783e2ec8SMatthew G. Knepley 
7848a6ba4734SToby Isaac   Input Parameters:
7849bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7850bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7851a6ba4734SToby Isaac . name     - The BC name
785245480ffeSMatthew G. Knepley . label    - The label defining constrained points
7853bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
785445480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7855a6ba4734SToby Isaac . field    - The field to constrain
785645480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7857a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7858a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
785956cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7860a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7861a6ba4734SToby Isaac 
786245480ffeSMatthew G. Knepley   Output Parameter:
786345480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
786445480ffeSMatthew G. Knepley 
7865a6ba4734SToby Isaac   Options Database Keys:
7866a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
7867a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7868a6ba4734SToby Isaac 
786920f4b53cSBarry Smith   Level: intermediate
787020f4b53cSBarry Smith 
7871bb7acecfSBarry Smith   Notes:
787237fdd005SBarry 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\:
787356cf3b9cSMatthew G. Knepley 
787420f4b53cSBarry Smith $ void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
787556cf3b9cSMatthew G. Knepley 
7876a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
787756cf3b9cSMatthew G. Knepley 
787820f4b53cSBarry Smith .vb
787920f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
788020f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
788120f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
788220f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
788320f4b53cSBarry Smith .ve
788456cf3b9cSMatthew G. Knepley + dim - the spatial dimension
788556cf3b9cSMatthew G. Knepley . Nf - the number of fields
788656cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
788756cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
788856cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
788956cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
789056cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
789156cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
789256cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
789356cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
789456cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
789556cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
789656cf3b9cSMatthew G. Knepley . t - current time
789756cf3b9cSMatthew G. Knepley . x - coordinates of the current point
789856cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
789956cf3b9cSMatthew G. Knepley . constants - constant parameters
790056cf3b9cSMatthew G. Knepley - bcval - output values at the current point
790156cf3b9cSMatthew G. Knepley 
79021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
7903a6ba4734SToby Isaac @*/
7904d71ae5a4SJacob 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)
7905d71ae5a4SJacob Faibussowitsch {
7906e5e52638SMatthew G. Knepley   PetscDS ds;
7907a6ba4734SToby Isaac 
7908a6ba4734SToby Isaac   PetscFunctionBegin;
7909a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7910783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
791145480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
791245480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
791345480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
791445480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
791501a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
79169566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7917799db056SMatthew G. Knepley   /* Complete label */
7918799db056SMatthew G. Knepley   if (label) {
7919799db056SMatthew G. Knepley     PetscObject  obj;
7920799db056SMatthew G. Knepley     PetscClassId id;
7921799db056SMatthew G. Knepley 
7922799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7923799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7924799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7925799db056SMatthew G. Knepley       DM plex;
7926799db056SMatthew G. Knepley 
7927799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7928799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7929799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7930799db056SMatthew G. Knepley     }
7931799db056SMatthew G. Knepley   }
79329566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
79333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7934a6ba4734SToby Isaac }
7935a6ba4734SToby Isaac 
793645480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7937d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
7938d71ae5a4SJacob Faibussowitsch {
7939e5e52638SMatthew G. Knepley   PetscDS     ds;
7940dff059c6SToby Isaac   DMBoundary *lastnext;
7941e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7942e6f8dbb6SToby Isaac 
7943e6f8dbb6SToby Isaac   PetscFunctionBegin;
79449566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7945e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
794647a1f5adSToby Isaac   if (dm->boundary) {
794747a1f5adSToby Isaac     DMBoundary next = dm->boundary;
794847a1f5adSToby Isaac 
794947a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
79503ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
795147a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
795247a1f5adSToby Isaac     while (next) {
795347a1f5adSToby Isaac       DMBoundary b = next;
795447a1f5adSToby Isaac 
795547a1f5adSToby Isaac       next = b->next;
79569566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7957a6ba4734SToby Isaac     }
795847a1f5adSToby Isaac     dm->boundary = NULL;
7959a6ba4734SToby Isaac   }
796047a1f5adSToby Isaac 
7961dff059c6SToby Isaac   lastnext = &(dm->boundary);
7962e6f8dbb6SToby Isaac   while (dsbound) {
7963e6f8dbb6SToby Isaac     DMBoundary dmbound;
7964e6f8dbb6SToby Isaac 
79659566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7966e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
796745480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
796847a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7969dff059c6SToby Isaac     *lastnext = dmbound;
7970dff059c6SToby Isaac     lastnext  = &(dmbound->next);
7971dff059c6SToby Isaac     dsbound   = dsbound->next;
7972a6ba4734SToby Isaac   }
79733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7974a6ba4734SToby Isaac }
7975a6ba4734SToby Isaac 
7976bb7acecfSBarry Smith /* TODO: missing manual page */
7977d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7978d71ae5a4SJacob Faibussowitsch {
7979b95f2879SToby Isaac   DMBoundary b;
7980a6ba4734SToby Isaac 
7981a6ba4734SToby Isaac   PetscFunctionBegin;
7982a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79834f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
7984a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
79859566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7986b95f2879SToby Isaac   b = dm->boundary;
7987a6ba4734SToby Isaac   while (b && !(*isBd)) {
7988e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7989e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7990a6ba4734SToby Isaac     PetscInt   i;
7991a6ba4734SToby Isaac 
799245480ffeSMatthew G. Knepley     if (label) {
79939566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7994a6ba4734SToby Isaac     }
7995a6ba4734SToby Isaac     b = b->next;
7996a6ba4734SToby Isaac   }
79973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7998a6ba4734SToby Isaac }
79994d6f44ffSToby Isaac 
80004d6f44ffSToby Isaac /*@C
8001bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
8002a6e0b375SMatthew G. Knepley 
800320f4b53cSBarry Smith   Collective
80044d6f44ffSToby Isaac 
80054d6f44ffSToby Isaac   Input Parameters:
8006bb7acecfSBarry Smith + dm    - The `DM`
80070709b2feSToby Isaac . time  - The time
80084d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
80094d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
80104d6f44ffSToby Isaac - mode  - The insertion mode for values
80114d6f44ffSToby Isaac 
80124d6f44ffSToby Isaac   Output Parameter:
80134d6f44ffSToby Isaac . X - vector
80144d6f44ffSToby Isaac 
801520f4b53cSBarry Smith   Calling sequence of `funcs`:
80164d6f44ffSToby Isaac + dim  - The spatial dimension
80178ec8862eSJed Brown . time - The time at which to sample
80184d6f44ffSToby Isaac . x    - The coordinates
801977b739a6SMatthew Knepley . Nc   - The number of components
80204d6f44ffSToby Isaac . u    - The output field values
80214d6f44ffSToby Isaac - ctx  - optional user-defined function context
80224d6f44ffSToby Isaac 
80234d6f44ffSToby Isaac   Level: developer
80244d6f44ffSToby Isaac 
8025bb7acecfSBarry Smith   Developer Notes:
8026bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8027bb7acecfSBarry Smith 
8028bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8029bb7acecfSBarry Smith 
80301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
80314d6f44ffSToby Isaac @*/
8032a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec X)
8033d71ae5a4SJacob Faibussowitsch {
80344d6f44ffSToby Isaac   Vec localX;
80354d6f44ffSToby Isaac 
80364d6f44ffSToby Isaac   PetscFunctionBegin;
80374d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8038708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
80399566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8040f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
80419566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
80429566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
80439566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
80449566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
8045708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
80463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80474d6f44ffSToby Isaac }
80484d6f44ffSToby Isaac 
8049a6e0b375SMatthew G. Knepley /*@C
8050bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
8051a6e0b375SMatthew G. Knepley 
805220f4b53cSBarry Smith   Not Collective
8053a6e0b375SMatthew G. Knepley 
8054a6e0b375SMatthew G. Knepley   Input Parameters:
8055bb7acecfSBarry Smith + dm    - The `DM`
8056a6e0b375SMatthew G. Knepley . time  - The time
8057a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
8058a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8059a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
8060a6e0b375SMatthew G. Knepley 
8061a6e0b375SMatthew G. Knepley   Output Parameter:
8062a6e0b375SMatthew G. Knepley . localX - vector
8063a6e0b375SMatthew G. Knepley 
806420f4b53cSBarry Smith   Calling sequence of `funcs`:
8065a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8066a4e35b19SJacob Faibussowitsch . time - The current timestep
8067a6e0b375SMatthew G. Knepley . x    - The coordinates
806877b739a6SMatthew Knepley . Nc   - The number of components
8069a6e0b375SMatthew G. Knepley . u    - The output field values
8070a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8071a6e0b375SMatthew G. Knepley 
8072a6e0b375SMatthew G. Knepley   Level: developer
8073a6e0b375SMatthew G. Knepley 
8074bb7acecfSBarry Smith   Developer Notes:
8075bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8076bb7acecfSBarry Smith 
8077bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8078bb7acecfSBarry Smith 
80791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8080a6e0b375SMatthew G. Knepley @*/
8081a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec localX)
8082d71ae5a4SJacob Faibussowitsch {
80834d6f44ffSToby Isaac   PetscFunctionBegin;
80844d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8085064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8086fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlocal, time, funcs, ctxs, mode, localX);
80873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80884d6f44ffSToby Isaac }
80894d6f44ffSToby Isaac 
8090a6e0b375SMatthew G. Knepley /*@C
8091bb7acecfSBarry 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.
8092a6e0b375SMatthew G. Knepley 
809320f4b53cSBarry Smith   Collective
8094a6e0b375SMatthew G. Knepley 
8095a6e0b375SMatthew G. Knepley   Input Parameters:
8096bb7acecfSBarry Smith + dm     - The `DM`
8097a6e0b375SMatthew G. Knepley . time   - The time
8098a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8099a4e35b19SJacob Faibussowitsch . ids    - The ids
8100a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8101a4e35b19SJacob Faibussowitsch . comps  - The components
8102bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8103a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8104bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8105a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8106a6e0b375SMatthew G. Knepley 
8107a6e0b375SMatthew G. Knepley   Output Parameter:
8108a6e0b375SMatthew G. Knepley . X - vector
8109a6e0b375SMatthew G. Knepley 
811020f4b53cSBarry Smith   Calling sequence of `funcs`:
8111a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8112a4e35b19SJacob Faibussowitsch . time - The current timestep
8113a6e0b375SMatthew G. Knepley . x    - The coordinates
811477b739a6SMatthew Knepley . Nc   - The number of components
8115a6e0b375SMatthew G. Knepley . u    - The output field values
8116a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8117a6e0b375SMatthew G. Knepley 
8118a6e0b375SMatthew G. Knepley   Level: developer
8119a6e0b375SMatthew G. Knepley 
8120bb7acecfSBarry Smith   Developer Notes:
8121bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8122bb7acecfSBarry Smith 
8123bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8124bb7acecfSBarry Smith 
81251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8126a6e0b375SMatthew G. Knepley @*/
8127a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec X)
8128d71ae5a4SJacob Faibussowitsch {
81292c53366bSMatthew G. Knepley   Vec localX;
81302c53366bSMatthew G. Knepley 
81312c53366bSMatthew G. Knepley   PetscFunctionBegin;
81322c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81339566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8134f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
81359566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
81369566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
81379566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
81389566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
81393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81402c53366bSMatthew G. Knepley }
81412c53366bSMatthew G. Knepley 
8142a6e0b375SMatthew G. Knepley /*@C
8143bb7acecfSBarry 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.
8144a6e0b375SMatthew G. Knepley 
814520f4b53cSBarry Smith   Not Collective
8146a6e0b375SMatthew G. Knepley 
8147a6e0b375SMatthew G. Knepley   Input Parameters:
8148bb7acecfSBarry Smith + dm     - The `DM`
8149a6e0b375SMatthew G. Knepley . time   - The time
8150bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8151a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8152a4e35b19SJacob Faibussowitsch . ids    - The ids
8153a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8154a4e35b19SJacob Faibussowitsch . comps  - The components
8155a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8156a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8157a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8158a6e0b375SMatthew G. Knepley 
8159a6e0b375SMatthew G. Knepley   Output Parameter:
8160a6e0b375SMatthew G. Knepley . localX - vector
8161a6e0b375SMatthew G. Knepley 
816220f4b53cSBarry Smith   Calling sequence of `funcs`:
8163a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8164a4e35b19SJacob Faibussowitsch . time - The current time
8165a6e0b375SMatthew G. Knepley . x    - The coordinates
816677b739a6SMatthew Knepley . Nc   - The number of components
8167a6e0b375SMatthew G. Knepley . u    - The output field values
8168a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8169a6e0b375SMatthew G. Knepley 
8170a6e0b375SMatthew G. Knepley   Level: developer
8171a6e0b375SMatthew G. Knepley 
8172bb7acecfSBarry Smith   Developer Notes:
8173bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8174bb7acecfSBarry Smith 
8175bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8176bb7acecfSBarry Smith 
81771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8178a6e0b375SMatthew G. Knepley @*/
8179a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec localX)
8180d71ae5a4SJacob Faibussowitsch {
81814d6f44ffSToby Isaac   PetscFunctionBegin;
81824d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8183064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8184fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlabellocal, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);
81853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81864d6f44ffSToby Isaac }
81872716604bSToby Isaac 
8188a6e0b375SMatthew G. Knepley /*@C
8189bb7acecfSBarry 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.
8190a6e0b375SMatthew G. Knepley 
819120f4b53cSBarry Smith   Not Collective
8192a6e0b375SMatthew G. Knepley 
8193a6e0b375SMatthew G. Knepley   Input Parameters:
8194bb7acecfSBarry Smith + dm     - The `DM`
8195a6e0b375SMatthew G. Knepley . time   - The time
819620f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8197a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8198a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8199a6e0b375SMatthew G. Knepley 
8200a6e0b375SMatthew G. Knepley   Output Parameter:
8201a6e0b375SMatthew G. Knepley . localX - The output vector
8202a6e0b375SMatthew G. Knepley 
820320f4b53cSBarry Smith   Calling sequence of `funcs`:
8204a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8205a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8206a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8207a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8208a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8209a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8210a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8211a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8212a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8213a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8214a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8215a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8216a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8217a6e0b375SMatthew G. Knepley . t            - The current time
8218a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8219a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8220a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8221a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8222a6e0b375SMatthew G. Knepley 
8223bb7acecfSBarry Smith   Note:
8224bb7acecfSBarry 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.
8225bb7acecfSBarry 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
8226bb7acecfSBarry 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
8227a6e0b375SMatthew 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.
8228a6e0b375SMatthew G. Knepley 
8229a6e0b375SMatthew G. Knepley   Level: intermediate
8230a6e0b375SMatthew G. Knepley 
8231bb7acecfSBarry Smith   Developer Notes:
8232bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8233bb7acecfSBarry Smith 
8234bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8235bb7acecfSBarry Smith 
8236a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8237a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8238a6e0b375SMatthew G. Knepley @*/
8239a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX)
8240d71ae5a4SJacob Faibussowitsch {
82418c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82428c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8243eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
82448c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8245fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlocal, time, localU, funcs, mode, localX);
82463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82478c6c5593SMatthew G. Knepley }
82488c6c5593SMatthew G. Knepley 
8249a6e0b375SMatthew G. Knepley /*@C
8250a6e0b375SMatthew 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.
8251a6e0b375SMatthew G. Knepley 
825220f4b53cSBarry Smith   Not Collective
8253a6e0b375SMatthew G. Knepley 
8254a6e0b375SMatthew G. Knepley   Input Parameters:
8255bb7acecfSBarry Smith + dm     - The `DM`
8256a6e0b375SMatthew G. Knepley . time   - The time
8257bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8258a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8259a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8260bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
826120f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8262a6e0b375SMatthew G. Knepley . localU - The input field vector
8263a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8264a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8265a6e0b375SMatthew G. Knepley 
8266a6e0b375SMatthew G. Knepley   Output Parameter:
8267a6e0b375SMatthew G. Knepley . localX - The output vector
8268a6e0b375SMatthew G. Knepley 
826920f4b53cSBarry Smith   Calling sequence of `funcs`:
8270a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8271a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8272a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8273a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8274a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8275a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8276a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8277a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8278a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8279a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8280a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8281a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8282a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8283a6e0b375SMatthew G. Knepley . t            - The current time
8284a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8285a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8286a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8287a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8288a6e0b375SMatthew G. Knepley 
8289bb7acecfSBarry Smith   Note:
8290bb7acecfSBarry 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.
8291bb7acecfSBarry 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
8292bb7acecfSBarry 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
8293a6e0b375SMatthew 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.
8294a6e0b375SMatthew G. Knepley 
8295a6e0b375SMatthew G. Knepley   Level: intermediate
8296a6e0b375SMatthew G. Knepley 
8297bb7acecfSBarry Smith   Developer Notes:
8298bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8299bb7acecfSBarry Smith 
8300bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8301bb7acecfSBarry Smith 
83021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8303a6e0b375SMatthew G. Knepley @*/
8304a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX)
8305d71ae5a4SJacob Faibussowitsch {
83068c6c5593SMatthew G. Knepley   PetscFunctionBegin;
83078c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8308064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8309064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8310fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
83113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83128c6c5593SMatthew G. Knepley }
83138c6c5593SMatthew G. Knepley 
83142716604bSToby Isaac /*@C
8315d29d7c6eSMatthew 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.
8316d29d7c6eSMatthew G. Knepley 
831720f4b53cSBarry Smith   Not Collective
8318d29d7c6eSMatthew G. Knepley 
8319d29d7c6eSMatthew G. Knepley   Input Parameters:
8320bb7acecfSBarry Smith + dm     - The `DM`
8321d29d7c6eSMatthew G. Knepley . time   - The time
8322bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8323d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8324d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8325bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
832620f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8327d29d7c6eSMatthew G. Knepley . U      - The input field vector
8328d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8329d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8330d29d7c6eSMatthew G. Knepley 
8331d29d7c6eSMatthew G. Knepley   Output Parameter:
8332d29d7c6eSMatthew G. Knepley . X - The output vector
8333d29d7c6eSMatthew G. Knepley 
833420f4b53cSBarry Smith   Calling sequence of `funcs`:
8335d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8336d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8337d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8338d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8339d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8340d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
8341d29d7c6eSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8342d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8343d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8344d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8345d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8346d29d7c6eSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8347d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8348d29d7c6eSMatthew G. Knepley . t            - The current time
8349d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8350d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8351d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8352d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8353d29d7c6eSMatthew G. Knepley 
8354bb7acecfSBarry Smith   Note:
8355bb7acecfSBarry 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.
8356bb7acecfSBarry 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
8357bb7acecfSBarry 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
8358d29d7c6eSMatthew 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.
8359d29d7c6eSMatthew G. Knepley 
8360d29d7c6eSMatthew G. Knepley   Level: intermediate
8361d29d7c6eSMatthew G. Knepley 
8362bb7acecfSBarry Smith   Developer Notes:
8363bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8364bb7acecfSBarry Smith 
8365bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8366bb7acecfSBarry Smith 
83671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8368d29d7c6eSMatthew G. Knepley @*/
8369a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec U, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec X)
8370d71ae5a4SJacob Faibussowitsch {
8371d29d7c6eSMatthew G. Knepley   DM  dmIn;
8372d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8373d29d7c6eSMatthew G. Knepley 
8374d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8375d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8376d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8377d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8378d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8379f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
838072fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
838172fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8382d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8383d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8384d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8385d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8386d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
83873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8388d29d7c6eSMatthew G. Knepley }
8389d29d7c6eSMatthew G. Knepley 
8390d29d7c6eSMatthew G. Knepley /*@C
8391ece3a9fcSMatthew 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.
8392ece3a9fcSMatthew G. Knepley 
839320f4b53cSBarry Smith   Not Collective
8394ece3a9fcSMatthew G. Knepley 
8395ece3a9fcSMatthew G. Knepley   Input Parameters:
8396bb7acecfSBarry Smith + dm     - The `DM`
8397ece3a9fcSMatthew G. Knepley . time   - The time
8398bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8399ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8400ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8401bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
840220f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8403ece3a9fcSMatthew G. Knepley . localU - The input field vector
8404ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8405ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8406ece3a9fcSMatthew G. Knepley 
8407ece3a9fcSMatthew G. Knepley   Output Parameter:
8408ece3a9fcSMatthew G. Knepley . localX - The output vector
8409ece3a9fcSMatthew G. Knepley 
841020f4b53cSBarry Smith   Calling sequence of `funcs`:
8411ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8412ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8413ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8414ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8415ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8416ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
8417ece3a9fcSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8418ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8419ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8420ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8421ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8422ece3a9fcSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8423ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8424ece3a9fcSMatthew G. Knepley . t            - The current time
8425ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8426ece3a9fcSMatthew G. Knepley . n            - The face normal
8427ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8428ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8429ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8430ece3a9fcSMatthew G. Knepley 
8431ece3a9fcSMatthew G. Knepley   Note:
8432bb7acecfSBarry 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.
8433bb7acecfSBarry 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
8434bb7acecfSBarry 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
8435ece3a9fcSMatthew 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.
8436ece3a9fcSMatthew G. Knepley 
8437ece3a9fcSMatthew G. Knepley   Level: intermediate
8438ece3a9fcSMatthew G. Knepley 
8439bb7acecfSBarry Smith   Developer Notes:
8440bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8441bb7acecfSBarry Smith 
8442bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8443bb7acecfSBarry Smith 
84441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8445ece3a9fcSMatthew G. Knepley @*/
8446a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], const PetscReal n[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX)
8447d71ae5a4SJacob Faibussowitsch {
8448ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8449ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8450064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8451064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8452fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectbdfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
84533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8454ece3a9fcSMatthew G. Knepley }
8455ece3a9fcSMatthew G. Knepley 
8456ece3a9fcSMatthew G. Knepley /*@C
84572716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
84582716604bSToby Isaac 
845920f4b53cSBarry Smith   Collective
8460bb7acecfSBarry Smith 
84612716604bSToby Isaac   Input Parameters:
8462bb7acecfSBarry Smith + dm    - The `DM`
84630709b2feSToby Isaac . time  - The time
84642716604bSToby Isaac . funcs - The functions to evaluate for each field component
84652716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8466574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84672716604bSToby Isaac 
84682716604bSToby Isaac   Output Parameter:
84692716604bSToby Isaac . diff - The diff ||u - u_h||_2
84702716604bSToby Isaac 
84712716604bSToby Isaac   Level: developer
84722716604bSToby Isaac 
8473bb7acecfSBarry Smith   Developer Notes:
8474bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8475bb7acecfSBarry Smith 
8476bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8477bb7acecfSBarry Smith 
84781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
84792716604bSToby Isaac @*/
8480d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8481d71ae5a4SJacob Faibussowitsch {
84822716604bSToby Isaac   PetscFunctionBegin;
84832716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8484b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8485fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2diff, time, funcs, ctxs, X, diff);
84863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84872716604bSToby Isaac }
8488b698f381SToby Isaac 
8489b698f381SToby Isaac /*@C
8490b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8491b698f381SToby Isaac 
849220f4b53cSBarry Smith   Collective
8493d083f849SBarry Smith 
8494b698f381SToby Isaac   Input Parameters:
8495bb7acecfSBarry Smith + dm    - The `DM`
8496a4e35b19SJacob Faibussowitsch . time  - The time
8497b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8498b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8499574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8500b698f381SToby Isaac - n     - The vector to project along
8501b698f381SToby Isaac 
8502b698f381SToby Isaac   Output Parameter:
8503b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8504b698f381SToby Isaac 
8505b698f381SToby Isaac   Level: developer
8506b698f381SToby Isaac 
8507bb7acecfSBarry Smith   Developer Notes:
8508bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8509bb7acecfSBarry Smith 
8510bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8511bb7acecfSBarry Smith 
85121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8513b698f381SToby Isaac @*/
8514d71ae5a4SJacob 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)
8515d71ae5a4SJacob Faibussowitsch {
8516b698f381SToby Isaac   PetscFunctionBegin;
8517b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8518b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8519fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2gradientdiff, time, funcs, ctxs, X, n, diff);
85203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8521b698f381SToby Isaac }
8522b698f381SToby Isaac 
85232a16baeaSToby Isaac /*@C
85242a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
85252a16baeaSToby Isaac 
852620f4b53cSBarry Smith   Collective
8527d083f849SBarry Smith 
85282a16baeaSToby Isaac   Input Parameters:
8529bb7acecfSBarry Smith + dm    - The `DM`
85302a16baeaSToby Isaac . time  - The time
85312a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
85322a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8533574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
85342a16baeaSToby Isaac 
85352a16baeaSToby Isaac   Output Parameter:
85362a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
85372a16baeaSToby Isaac 
85382a16baeaSToby Isaac   Level: developer
85392a16baeaSToby Isaac 
8540bb7acecfSBarry Smith   Developer Notes:
8541bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8542bb7acecfSBarry Smith 
8543bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8544bb7acecfSBarry Smith 
854542747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
85462a16baeaSToby Isaac @*/
8547d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8548d71ae5a4SJacob Faibussowitsch {
85492a16baeaSToby Isaac   PetscFunctionBegin;
85502a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85512a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8552fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2fielddiff, time, funcs, ctxs, X, diff);
85533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
85542a16baeaSToby Isaac }
85552a16baeaSToby Isaac 
8556df0b854cSToby Isaac /*@C
8557bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8558502a2867SDave May 
8559502a2867SDave May   Not Collective
8560502a2867SDave May 
8561502a2867SDave May   Input Parameter:
8562bb7acecfSBarry Smith . dm - The `DM`
8563502a2867SDave May 
85640a19bb7dSprj-   Output Parameters:
85650a19bb7dSprj- + nranks - the number of neighbours
85660a19bb7dSprj- - ranks  - the neighbors ranks
8567502a2867SDave May 
85689bdbcad8SBarry Smith   Level: beginner
85699bdbcad8SBarry Smith 
8570bb7acecfSBarry Smith   Note:
8571bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8572502a2867SDave May 
85731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8574502a2867SDave May @*/
8575d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8576d71ae5a4SJacob Faibussowitsch {
8577502a2867SDave May   PetscFunctionBegin;
8578502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8579fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, getneighbors, nranks, ranks);
85803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8581502a2867SDave May }
8582502a2867SDave May 
8583531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8584531c7667SBarry Smith 
8585531c7667SBarry Smith /*
8586531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
85872b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8588531c7667SBarry Smith */
858966976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8590d71ae5a4SJacob Faibussowitsch {
8591531c7667SBarry Smith   PetscFunctionBegin;
8592531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8593531c7667SBarry Smith     Vec x1local;
8594531c7667SBarry Smith     DM  dm;
85959566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
85967a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
85979566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
85989566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
85999566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8600531c7667SBarry Smith     x1 = x1local;
8601531c7667SBarry Smith   }
86029566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8603531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8604531c7667SBarry Smith     DM dm;
86059566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
86069566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8607531c7667SBarry Smith   }
86083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8609531c7667SBarry Smith }
8610531c7667SBarry Smith 
8611531c7667SBarry Smith /*@
8612bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8613531c7667SBarry Smith 
8614a4e35b19SJacob Faibussowitsch   Input Parameters:
8615a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8616a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8617531c7667SBarry Smith 
86189bdbcad8SBarry Smith   Level: advanced
86199bdbcad8SBarry Smith 
862060225df5SJacob Faibussowitsch   Developer Notes:
8621bb7acecfSBarry Smith   this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8622531c7667SBarry Smith 
86231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8624531c7667SBarry Smith @*/
8625d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8626d71ae5a4SJacob Faibussowitsch {
8627531c7667SBarry Smith   PetscFunctionBegin;
8628531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
86293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8630531c7667SBarry Smith }
86318320bc6fSPatrick Sanan 
86328320bc6fSPatrick Sanan /*@
8633bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
86348320bc6fSPatrick Sanan 
86358320bc6fSPatrick Sanan   Collective
86368320bc6fSPatrick Sanan 
86378320bc6fSPatrick Sanan   Input Parameters:
8638bb7acecfSBarry Smith + dm1 - the first `DM`
8639bb7acecfSBarry Smith - dm2 - the second `DM`
86408320bc6fSPatrick Sanan 
86418320bc6fSPatrick Sanan   Output Parameters:
8642bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8643bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
86448320bc6fSPatrick Sanan 
864520f4b53cSBarry Smith   Level: advanced
864620f4b53cSBarry Smith 
86478320bc6fSPatrick Sanan   Notes:
8648bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
86493d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
86508320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8651bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
86523d862458SPatrick Sanan   decomposition, but hold different data.
86538320bc6fSPatrick Sanan 
86548320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8655bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
86568320bc6fSPatrick Sanan 
8657bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
86588320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
86598320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8660bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
86618320bc6fSPatrick Sanan 
8662bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8663bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
86648320bc6fSPatrick Sanan .vb
86658320bc6fSPatrick Sanan   ...
86669566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
86678320bc6fSPatrick Sanan   if (set && compatible)  {
86689566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
86699566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
86709566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
86718320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
86728320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
86738320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
86748320bc6fSPatrick Sanan       }
86758320bc6fSPatrick Sanan     }
86769566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
86779566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
86788320bc6fSPatrick Sanan   } else {
86798320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
86808320bc6fSPatrick Sanan   }
86818320bc6fSPatrick Sanan   ...
86828320bc6fSPatrick Sanan .ve
86838320bc6fSPatrick Sanan 
8684bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
86858320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
86868320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
86878320bc6fSPatrick Sanan   always check the "set" output parameter.
86888320bc6fSPatrick Sanan 
8689bb7acecfSBarry Smith   A `DM` is always compatible with itself.
86908320bc6fSPatrick Sanan 
8691bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
86928320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
86938320bc6fSPatrick Sanan   incompatible.
86948320bc6fSPatrick Sanan 
86958320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8696bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
86978320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
86988320bc6fSPatrick Sanan 
869960225df5SJacob Faibussowitsch   Developer Notes:
8700bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
87013d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8702a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8703bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
87048320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
87053d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8706bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
87078320bc6fSPatrick Sanan 
87081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
87098320bc6fSPatrick Sanan @*/
8710d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8711d71ae5a4SJacob Faibussowitsch {
87128320bc6fSPatrick Sanan   PetscMPIInt compareResult;
87138320bc6fSPatrick Sanan   DMType      type, type2;
87148320bc6fSPatrick Sanan   PetscBool   sameType;
87158320bc6fSPatrick Sanan 
87168320bc6fSPatrick Sanan   PetscFunctionBegin;
8717a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
87188320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
87198320bc6fSPatrick Sanan 
87208320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8721a5bc1bf3SBarry Smith   if (dm1 == dm2) {
87228320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
87238320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
87243ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
87258320bc6fSPatrick Sanan   }
87268320bc6fSPatrick Sanan 
87278320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
87288320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
87298320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
87308320bc6fSPatrick Sanan      determined by the implementation-specific logic */
87319566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
87328320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
87338320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
87348320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
87353ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
87368320bc6fSPatrick Sanan   }
87378320bc6fSPatrick Sanan 
87388320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8739a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8740dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
87413ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
87428320bc6fSPatrick Sanan   }
87438320bc6fSPatrick Sanan 
8744a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
87458320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
87469566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
87479566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
87489566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
87498320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8750dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
87518320bc6fSPatrick Sanan   } else {
87528320bc6fSPatrick Sanan     *set = PETSC_FALSE;
87538320bc6fSPatrick Sanan   }
87543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
87558320bc6fSPatrick Sanan }
8756c0f0dcc3SMatthew G. Knepley 
8757c0f0dcc3SMatthew G. Knepley /*@C
8758bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8759c0f0dcc3SMatthew G. Knepley 
876020f4b53cSBarry Smith   Logically Collective
8761c0f0dcc3SMatthew G. Knepley 
8762c0f0dcc3SMatthew G. Knepley   Input Parameters:
876360225df5SJacob Faibussowitsch + dm             - the `DM`
8764c0f0dcc3SMatthew G. Knepley . f              - the monitor function
876520f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
876620f4b53cSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`)
8767c0f0dcc3SMatthew G. Knepley 
876820f4b53cSBarry Smith   Options Database Key:
876960225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8770c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8771c0f0dcc3SMatthew G. Knepley 
87729bdbcad8SBarry Smith   Level: intermediate
87739bdbcad8SBarry Smith 
8774bb7acecfSBarry Smith   Note:
8775c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8776bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8777c0f0dcc3SMatthew G. Knepley   order in which they were set.
8778c0f0dcc3SMatthew G. Knepley 
877960225df5SJacob Faibussowitsch   Fortran Notes:
8780bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8781bb7acecfSBarry Smith 
878260225df5SJacob Faibussowitsch   Developer Notes:
8783bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8784c0f0dcc3SMatthew G. Knepley 
87851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8786c0f0dcc3SMatthew G. Knepley @*/
8787d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8788d71ae5a4SJacob Faibussowitsch {
8789c0f0dcc3SMatthew G. Knepley   PetscInt m;
8790c0f0dcc3SMatthew G. Knepley 
8791c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8792c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8793c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8794c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8795c0f0dcc3SMatthew G. Knepley 
87969566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
87973ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8798c0f0dcc3SMatthew G. Knepley   }
87997a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8800c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8801c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8802c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
88033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8804c0f0dcc3SMatthew G. Knepley }
8805c0f0dcc3SMatthew G. Knepley 
8806c0f0dcc3SMatthew G. Knepley /*@
8807bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8808c0f0dcc3SMatthew G. Knepley 
880920f4b53cSBarry Smith   Logically Collective
8810c0f0dcc3SMatthew G. Knepley 
8811c0f0dcc3SMatthew G. Knepley   Input Parameter:
8812c0f0dcc3SMatthew G. Knepley . dm - the DM
8813c0f0dcc3SMatthew G. Knepley 
8814c0f0dcc3SMatthew G. Knepley   Options Database Key:
8815c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8816bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8817c0f0dcc3SMatthew G. Knepley   set via the options database
8818c0f0dcc3SMatthew G. Knepley 
88199bdbcad8SBarry Smith   Level: intermediate
88209bdbcad8SBarry Smith 
8821bb7acecfSBarry Smith   Note:
8822bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8823c0f0dcc3SMatthew G. Knepley 
88241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8825c0f0dcc3SMatthew G. Knepley @*/
8826d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8827d71ae5a4SJacob Faibussowitsch {
8828c0f0dcc3SMatthew G. Knepley   PetscInt m;
8829c0f0dcc3SMatthew G. Knepley 
8830c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8831c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8832c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
88339566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8834c0f0dcc3SMatthew G. Knepley   }
8835c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
88363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8837c0f0dcc3SMatthew G. Knepley }
8838c0f0dcc3SMatthew G. Knepley 
8839c0f0dcc3SMatthew G. Knepley /*@C
8840c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8841c0f0dcc3SMatthew G. Knepley 
884220f4b53cSBarry Smith   Collective
8843c0f0dcc3SMatthew G. Knepley 
8844c0f0dcc3SMatthew G. Knepley   Input Parameters:
8845bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
8846c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
8847c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
8848c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
8849c0f0dcc3SMatthew G. Knepley . monitor      - the monitor function
8850bb7acecfSBarry 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
8851c0f0dcc3SMatthew G. Knepley 
8852c0f0dcc3SMatthew G. Knepley   Output Parameter:
8853c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8854c0f0dcc3SMatthew G. Knepley 
8855c0f0dcc3SMatthew G. Knepley   Level: developer
8856c0f0dcc3SMatthew G. Knepley 
88571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8858db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
885960225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
8860db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8861c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8862db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8863bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8864c0f0dcc3SMatthew G. Knepley @*/
8865d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8866d71ae5a4SJacob Faibussowitsch {
8867c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8868c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8869c0f0dcc3SMatthew G. Knepley 
8870c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8871c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88729566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8873c0f0dcc3SMatthew G. Knepley   if (*flg) {
8874c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8875c0f0dcc3SMatthew G. Knepley 
88769566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
8877cd791dc2SBarry Smith     PetscCall(PetscOptionsRestoreViewer(&viewer));
88789566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
88799566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8880c0f0dcc3SMatthew G. Knepley   }
88813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8882c0f0dcc3SMatthew G. Knepley }
8883c0f0dcc3SMatthew G. Knepley 
8884c0f0dcc3SMatthew G. Knepley /*@
8885c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
8886c0f0dcc3SMatthew G. Knepley 
888720f4b53cSBarry Smith   Collective
8888c0f0dcc3SMatthew G. Knepley 
88892fe279fdSBarry Smith   Input Parameter:
8890bb7acecfSBarry Smith . dm - The `DM`
8891c0f0dcc3SMatthew G. Knepley 
8892c0f0dcc3SMatthew G. Knepley   Level: developer
8893c0f0dcc3SMatthew G. Knepley 
8894a4e35b19SJacob Faibussowitsch   Developer Notes:
8895a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
8896a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
8897a4e35b19SJacob Faibussowitsch   concept of discretization.
8898bb7acecfSBarry Smith 
88991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8900c0f0dcc3SMatthew G. Knepley @*/
8901d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8902d71ae5a4SJacob Faibussowitsch {
8903c0f0dcc3SMatthew G. Knepley   PetscInt m;
8904c0f0dcc3SMatthew G. Knepley 
8905c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
89063ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
8907c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
890848a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
89093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8910c0f0dcc3SMatthew G. Knepley }
89112e4af2aeSMatthew G. Knepley 
89122e4af2aeSMatthew G. Knepley /*@
8913bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
89142e4af2aeSMatthew G. Knepley 
891520f4b53cSBarry Smith   Collective
89162e4af2aeSMatthew G. Knepley 
89172e4af2aeSMatthew G. Knepley   Input Parameters:
8918bb7acecfSBarry Smith + dm  - The `DM`
89196b867d5aSJose E. Roman - sol - The solution vector
89202e4af2aeSMatthew G. Knepley 
89216b867d5aSJose E. Roman   Input/Output Parameter:
892220f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
89236b867d5aSJose E. Roman            contains the error in each field
89246b867d5aSJose E. Roman 
89256b867d5aSJose E. Roman   Output Parameter:
892620f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
892720f4b53cSBarry Smith 
892820f4b53cSBarry Smith   Level: developer
89292e4af2aeSMatthew G. Knepley 
8930bb7acecfSBarry Smith   Note:
8931bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
89322e4af2aeSMatthew G. Knepley 
89331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
89342e4af2aeSMatthew G. Knepley @*/
8935d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
8936d71ae5a4SJacob Faibussowitsch {
89372e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
89382e4af2aeSMatthew G. Knepley   void    **ctxs;
89392e4af2aeSMatthew G. Knepley   PetscReal time;
89402e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
89412e4af2aeSMatthew G. Knepley 
89422e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
89439566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
89449566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
89459566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
89462e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
89472e4af2aeSMatthew G. Knepley     PetscDS         ds;
89482e4af2aeSMatthew G. Knepley     DMLabel         label;
89492e4af2aeSMatthew G. Knepley     IS              fieldIS;
89502e4af2aeSMatthew G. Knepley     const PetscInt *fields;
89512e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
89522e4af2aeSMatthew G. Knepley 
895307218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
89549566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
89559566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
89562e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
89572e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
89589566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
89592e4af2aeSMatthew G. Knepley     }
89609566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
89612e4af2aeSMatthew G. Knepley   }
8962ad540459SPierre 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);
89639566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
89649566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
89652e4af2aeSMatthew G. Knepley   if (errorVec) {
89662e4af2aeSMatthew G. Knepley     DM             edm;
89672e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
89682e4af2aeSMatthew G. Knepley     PetscBool      simplex;
89692e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
89702e4af2aeSMatthew G. Knepley 
89719566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
89729566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
89739566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
89749566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
89752e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
89769566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
89772e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
89782e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
89792e4af2aeSMatthew G. Knepley       PetscQuadrature q;
89802e4af2aeSMatthew G. Knepley       const char     *name;
89812e4af2aeSMatthew G. Knepley 
89829566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
89839566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
89849566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
89859566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
89869566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
89879566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
89889566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
89899566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
89902e4af2aeSMatthew G. Knepley     }
89919566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
89922e4af2aeSMatthew G. Knepley 
89939566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
89949566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
89959566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
89969566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
89972e4af2aeSMatthew G. Knepley   }
89989566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
89993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90002e4af2aeSMatthew G. Knepley }
90019a2a23afSMatthew G. Knepley 
90029a2a23afSMatthew G. Knepley /*@
9003bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
90049a2a23afSMatthew G. Knepley 
900520f4b53cSBarry Smith   Not Collective
90069a2a23afSMatthew G. Knepley 
90079a2a23afSMatthew G. Knepley   Input Parameter:
9008bb7acecfSBarry Smith . dm - The `DM`
90099a2a23afSMatthew G. Knepley 
90109a2a23afSMatthew G. Knepley   Output Parameter:
9011a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
90129a2a23afSMatthew G. Knepley 
90139a2a23afSMatthew G. Knepley   Level: advanced
90149a2a23afSMatthew G. Knepley 
9015e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
90169a2a23afSMatthew G. Knepley @*/
9017d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
9018d71ae5a4SJacob Faibussowitsch {
90199a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90209a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90219566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
90223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90239a2a23afSMatthew G. Knepley }
90249a2a23afSMatthew G. Knepley 
90259a2a23afSMatthew G. Knepley /*@
9026ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
90279a2a23afSMatthew G. Knepley 
902820f4b53cSBarry Smith   Not Collective
90299a2a23afSMatthew G. Knepley 
90309a2a23afSMatthew G. Knepley   Input Parameters:
9031bb7acecfSBarry Smith + dm    - The `DM`
9032bb7acecfSBarry Smith . label - The `DMLabel`
9033ac17215fSMatthew G. Knepley . value - The label value indicating the region
9034ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
90359a2a23afSMatthew G. Knepley 
90369a2a23afSMatthew G. Knepley   Output Parameter:
9037bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
90389a2a23afSMatthew G. Knepley 
90399bdbcad8SBarry Smith   Level: advanced
90409bdbcad8SBarry Smith 
9041bb7acecfSBarry Smith   Note:
9042bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
904304c51a94SMatthew G. Knepley 
9044e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
90459a2a23afSMatthew G. Knepley @*/
9046d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
9047d71ae5a4SJacob Faibussowitsch {
9048ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
904904c51a94SMatthew G. Knepley   PetscBool       has;
90509a2a23afSMatthew G. Knepley 
90519a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90529a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90539a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
90549a2a23afSMatthew G. Knepley   key.label = label;
90559a2a23afSMatthew G. Knepley   key.value = value;
9056ac17215fSMatthew G. Knepley   key.part  = part;
90579566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
90589566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
90599566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
90603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90619a2a23afSMatthew G. Knepley }
90629a2a23afSMatthew G. Knepley 
90639a2a23afSMatthew G. Knepley /*@
9064bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
90659a2a23afSMatthew G. Knepley 
906620f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
90679a2a23afSMatthew G. Knepley 
90689a2a23afSMatthew G. Knepley   Input Parameters:
9069bb7acecfSBarry Smith + dm    - The `DM`
9070bb7acecfSBarry Smith . label - The `DMLabel`
90719a2a23afSMatthew G. Knepley . value - The label value indicating the region
9072ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
9073bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
90749a2a23afSMatthew G. Knepley 
90759a2a23afSMatthew G. Knepley   Level: advanced
90769a2a23afSMatthew G. Knepley 
9077e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
90789a2a23afSMatthew G. Knepley @*/
9079d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9080d71ae5a4SJacob Faibussowitsch {
90819a2a23afSMatthew G. Knepley   Vec             old;
90829a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
90839a2a23afSMatthew G. Knepley 
90849a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90859a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90869a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
90879a2a23afSMatthew G. Knepley   key.label = label;
90889a2a23afSMatthew G. Knepley   key.value = value;
9089ac17215fSMatthew G. Knepley   key.part  = part;
90909566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
90919566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
90929566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
90939566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
9094d705d0eaSStefano Zampini   PetscCall(VecDestroy(&old));
90953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90969a2a23afSMatthew G. Knepley }
90979a2a23afSMatthew G. Knepley 
90989a2a23afSMatthew G. Knepley /*@C
9099bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
91009a2a23afSMatthew G. Knepley 
910120f4b53cSBarry Smith   Not Collective
91029a2a23afSMatthew G. Knepley 
91039a2a23afSMatthew G. Knepley   Input Parameter:
9104bb7acecfSBarry Smith . dm - The `DM`
91059a2a23afSMatthew G. Knepley 
91069a2a23afSMatthew G. Knepley   Output Parameters:
9107bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9108bb7acecfSBarry Smith . values - The label values for each `Vec`
9109bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
91109a2a23afSMatthew G. Knepley 
91119bdbcad8SBarry Smith   Level: advanced
91129bdbcad8SBarry Smith 
9113bb7acecfSBarry Smith   Note:
9114bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
91159a2a23afSMatthew G. Knepley 
9116e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
91179a2a23afSMatthew G. Knepley @*/
9118d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9119d71ae5a4SJacob Faibussowitsch {
91209a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
91219a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
91229a2a23afSMatthew G. Knepley 
91239a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91249a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91254f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
91264f572ea9SToby Isaac   PetscAssertPointer(values, 3);
91274f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
91289566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
91299566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
91309566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
91319371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
91329371c9d4SSatish Balay     labels[i] = keys[i].label;
91339371c9d4SSatish Balay     values[i] = keys[i].value;
91349371c9d4SSatish Balay     parts[i]  = keys[i].part;
91359371c9d4SSatish Balay   }
91369566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
91373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91389a2a23afSMatthew G. Knepley }
91399a2a23afSMatthew G. Knepley 
91409a2a23afSMatthew G. Knepley /*@
9141bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
91429a2a23afSMatthew G. Knepley 
914320f4b53cSBarry Smith   Not Collective
91449a2a23afSMatthew G. Knepley 
91459a2a23afSMatthew G. Knepley   Input Parameter:
9146bb7acecfSBarry Smith . dm - The `DM`
91479a2a23afSMatthew G. Knepley 
91489a2a23afSMatthew G. Knepley   Output Parameter:
9149bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
91509a2a23afSMatthew G. Knepley 
91519a2a23afSMatthew G. Knepley   Level: advanced
91529a2a23afSMatthew G. Knepley 
9153bb7acecfSBarry Smith   Note:
9154bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9155bb7acecfSBarry Smith 
9156e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
91579a2a23afSMatthew G. Knepley @*/
9158d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9159d71ae5a4SJacob Faibussowitsch {
91609a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91619a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9162d705d0eaSStefano Zampini   PetscValidHeaderSpecific(dmNew, DM_CLASSID, 2);
9163d705d0eaSStefano Zampini   if (dm == dmNew) PetscFunctionReturn(PETSC_SUCCESS);
9164e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(dmNew));
9165e4d5475eSStefano Zampini 
9166e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
91679566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
9168d705d0eaSStefano Zampini   {
9169d705d0eaSStefano Zampini     Vec     *auxData;
9170d705d0eaSStefano Zampini     PetscInt n, i, off = 0;
9171d705d0eaSStefano Zampini 
9172d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetSize(dmNew->auxData, &n));
9173d705d0eaSStefano Zampini     PetscCall(PetscMalloc1(n, &auxData));
9174d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetVals(dmNew->auxData, &off, auxData));
9175d705d0eaSStefano Zampini     for (i = 0; i < n; ++i) PetscCall(PetscObjectReference((PetscObject)auxData[i]));
9176d705d0eaSStefano Zampini     PetscCall(PetscFree(auxData));
9177e4d5475eSStefano Zampini   }
9178e4d5475eSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
9179e4d5475eSStefano Zampini }
9180e4d5475eSStefano Zampini 
9181e4d5475eSStefano Zampini /*@
9182e4d5475eSStefano Zampini   DMClearAuxiliaryVec - Destroys the auxiliary vector information and creates a new empty one
9183e4d5475eSStefano Zampini 
9184e4d5475eSStefano Zampini   Not Collective
9185e4d5475eSStefano Zampini 
9186e4d5475eSStefano Zampini   Input Parameter:
9187e4d5475eSStefano Zampini . dm - The `DM`
9188e4d5475eSStefano Zampini 
9189e4d5475eSStefano Zampini   Level: advanced
9190e4d5475eSStefano Zampini 
9191e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMCopyAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
9192e4d5475eSStefano Zampini @*/
9193e4d5475eSStefano Zampini PetscErrorCode DMClearAuxiliaryVec(DM dm)
9194e4d5475eSStefano Zampini {
9195e4d5475eSStefano Zampini   Vec     *auxData;
9196e4d5475eSStefano Zampini   PetscInt n, i, off = 0;
9197e4d5475eSStefano Zampini 
9198e4d5475eSStefano Zampini   PetscFunctionBegin;
9199e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetSize(dm->auxData, &n));
9200d705d0eaSStefano Zampini   PetscCall(PetscMalloc1(n, &auxData));
9201e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetVals(dm->auxData, &off, auxData));
9202d705d0eaSStefano Zampini   for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
9203d705d0eaSStefano Zampini   PetscCall(PetscFree(auxData));
9204e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dm->auxData));
9205e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxCreate(&dm->auxData));
92063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92079a2a23afSMatthew G. Knepley }
9208b5a892a1SMatthew G. Knepley 
9209b5a892a1SMatthew G. Knepley /*@C
9210bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9211b5a892a1SMatthew G. Knepley 
921220f4b53cSBarry Smith   Not Collective
9213b5a892a1SMatthew G. Knepley 
9214b5a892a1SMatthew G. Knepley   Input Parameters:
9215bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9216b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9217b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9218b5a892a1SMatthew G. Knepley 
9219b5a892a1SMatthew G. Knepley   Output Parameters:
9220bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9221b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9222b5a892a1SMatthew G. Knepley 
9223b5a892a1SMatthew G. Knepley   Level: advanced
9224b5a892a1SMatthew G. Knepley 
9225bb7acecfSBarry Smith   Note:
9226bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9227bb7acecfSBarry Smith 
922885036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9229bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9230bb7acecfSBarry Smith 
9231bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9232bb7acecfSBarry Smith 
92331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9234b5a892a1SMatthew G. Knepley @*/
9235d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9236d71ae5a4SJacob Faibussowitsch {
9237b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
923885036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9239b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9240b5a892a1SMatthew G. Knepley 
9241b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92429371c9d4SSatish Balay   if (!nO) {
92439371c9d4SSatish Balay     *ornt  = 0;
92449371c9d4SSatish Balay     *found = PETSC_TRUE;
92453ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
92469371c9d4SSatish Balay   }
9247b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
924885036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
9249b5a892a1SMatthew G. Knepley 
92509371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
92519371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
92529371c9d4SSatish Balay     if (c == cS) {
92539371c9d4SSatish Balay       *ornt = o;
92549371c9d4SSatish Balay       break;
92559371c9d4SSatish Balay     }
9256b5a892a1SMatthew G. Knepley   }
9257b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
92583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9259b5a892a1SMatthew G. Knepley }
9260b5a892a1SMatthew G. Knepley 
9261b5a892a1SMatthew G. Knepley /*@C
9262bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9263b5a892a1SMatthew G. Knepley 
926420f4b53cSBarry Smith   Not Collective
9265b5a892a1SMatthew G. Knepley 
9266b5a892a1SMatthew G. Knepley   Input Parameters:
9267bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9268b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9269b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9270b5a892a1SMatthew G. Knepley 
92712fe279fdSBarry Smith   Output Parameter:
9272bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9273b5a892a1SMatthew G. Knepley 
9274b5a892a1SMatthew G. Knepley   Level: advanced
9275b5a892a1SMatthew G. Knepley 
9276bb7acecfSBarry Smith   Note:
9277bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9278bb7acecfSBarry Smith 
927960225df5SJacob Faibussowitsch   Developer Notes:
9280bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9281bb7acecfSBarry Smith 
92821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9283b5a892a1SMatthew G. Knepley @*/
9284d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9285d71ae5a4SJacob Faibussowitsch {
9286b5a892a1SMatthew G. Knepley   PetscBool found;
9287b5a892a1SMatthew G. Knepley 
9288b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92899566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
92907a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
92913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9292b5a892a1SMatthew G. Knepley }
9293b5a892a1SMatthew G. Knepley 
9294b5a892a1SMatthew G. Knepley /*@C
9295bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9296b5a892a1SMatthew G. Knepley 
929720f4b53cSBarry Smith   Not Collective
9298b5a892a1SMatthew G. Knepley 
9299b5a892a1SMatthew G. Knepley   Input Parameters:
9300bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9301b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9302b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9303b5a892a1SMatthew G. Knepley 
9304b5a892a1SMatthew G. Knepley   Output Parameters:
9305bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9306b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9307b5a892a1SMatthew G. Knepley 
9308b5a892a1SMatthew G. Knepley   Level: advanced
9309b5a892a1SMatthew G. Knepley 
9310bb7acecfSBarry Smith   Note:
9311bb7acecfSBarry Smith   An arrangement is a vertex order
9312bb7acecfSBarry Smith 
931385036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9314bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9315bb7acecfSBarry Smith 
9316bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9317bb7acecfSBarry Smith 
931885036b15SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangement()`
9319b5a892a1SMatthew G. Knepley @*/
9320d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9321d71ae5a4SJacob Faibussowitsch {
9322b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
932385036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9324b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9325b5a892a1SMatthew G. Knepley 
9326b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93279371c9d4SSatish Balay   if (!nO) {
93289371c9d4SSatish Balay     *ornt  = 0;
93299371c9d4SSatish Balay     *found = PETSC_TRUE;
93303ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
93319371c9d4SSatish Balay   }
9332b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
933385036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangement(ct, o);
9334b5a892a1SMatthew G. Knepley 
93359371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
93369371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
93379371c9d4SSatish Balay     if (c == cS) {
93389371c9d4SSatish Balay       *ornt = o;
93399371c9d4SSatish Balay       break;
93409371c9d4SSatish Balay     }
9341b5a892a1SMatthew G. Knepley   }
9342b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
93433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9344b5a892a1SMatthew G. Knepley }
9345b5a892a1SMatthew G. Knepley 
9346b5a892a1SMatthew G. Knepley /*@C
9347bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9348b5a892a1SMatthew G. Knepley 
934920f4b53cSBarry Smith   Not Collective
9350b5a892a1SMatthew G. Knepley 
9351b5a892a1SMatthew G. Knepley   Input Parameters:
9352bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9353b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9354b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9355b5a892a1SMatthew G. Knepley 
93562fe279fdSBarry Smith   Output Parameter:
9357bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9358b5a892a1SMatthew G. Knepley 
9359b5a892a1SMatthew G. Knepley   Level: advanced
9360b5a892a1SMatthew G. Knepley 
9361bb7acecfSBarry Smith   Note:
9362bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9363bb7acecfSBarry Smith 
936460225df5SJacob Faibussowitsch   Developer Notes:
9365bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9366bb7acecfSBarry Smith 
93671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9368b5a892a1SMatthew G. Knepley @*/
9369d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9370d71ae5a4SJacob Faibussowitsch {
9371b5a892a1SMatthew G. Knepley   PetscBool found;
9372b5a892a1SMatthew G. Knepley 
9373b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93749566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
93757a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
93763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9377b5a892a1SMatthew G. Knepley }
9378012bc364SMatthew G. Knepley 
9379012bc364SMatthew G. Knepley /*@C
9380012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9381012bc364SMatthew G. Knepley 
938220f4b53cSBarry Smith   Not Collective
9383012bc364SMatthew G. Knepley 
9384012bc364SMatthew G. Knepley   Input Parameters:
9385bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9386012bc364SMatthew G. Knepley - point - Coordinates of the point
9387012bc364SMatthew G. Knepley 
93882fe279fdSBarry Smith   Output Parameter:
9389012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9390012bc364SMatthew G. Knepley 
9391012bc364SMatthew G. Knepley   Level: advanced
9392012bc364SMatthew G. Knepley 
93931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9394012bc364SMatthew G. Knepley @*/
9395d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9396d71ae5a4SJacob Faibussowitsch {
9397012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9398012bc364SMatthew G. Knepley   PetscInt  d;
9399012bc364SMatthew G. Knepley 
9400012bc364SMatthew G. Knepley   PetscFunctionBegin;
9401012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9402012bc364SMatthew G. Knepley   switch (ct) {
9403012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9404012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9405012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
94069371c9d4SSatish Balay       if (point[d] < -1.0) {
94079371c9d4SSatish Balay         *inside = PETSC_FALSE;
94089371c9d4SSatish Balay         break;
94099371c9d4SSatish Balay       }
9410012bc364SMatthew G. Knepley       sum += point[d];
9411012bc364SMatthew G. Knepley     }
94129371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
94139371c9d4SSatish Balay       *inside = PETSC_FALSE;
94149371c9d4SSatish Balay       break;
94159371c9d4SSatish Balay     }
9416012bc364SMatthew G. Knepley     break;
9417012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9418012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9419012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
94209371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
94219371c9d4SSatish Balay         *inside = PETSC_FALSE;
9422012bc364SMatthew G. Knepley         break;
94239371c9d4SSatish Balay       }
94249371c9d4SSatish Balay     break;
9425d71ae5a4SJacob Faibussowitsch   default:
9426d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9427012bc364SMatthew G. Knepley   }
94283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9429012bc364SMatthew G. Knepley }
9430*adc21957SMatthew G. Knepley 
9431*adc21957SMatthew G. Knepley /*@
9432*adc21957SMatthew G. Knepley   DMReorderSectionSetDefault - Set flag indicating whether the local section should be reordered by default
9433*adc21957SMatthew G. Knepley 
9434*adc21957SMatthew G. Knepley   Logically collective
9435*adc21957SMatthew G. Knepley 
9436*adc21957SMatthew G. Knepley   Input Parameters:
9437*adc21957SMatthew G. Knepley + dm      - The DM
9438*adc21957SMatthew G. Knepley - reorder - Flag for reordering
9439*adc21957SMatthew G. Knepley 
9440*adc21957SMatthew G. Knepley   Level: intermediate
9441*adc21957SMatthew G. Knepley 
9442*adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetDefault()`
9443*adc21957SMatthew G. Knepley @*/
9444*adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetDefault(DM dm, DMReorderDefaultFlag reorder)
9445*adc21957SMatthew G. Knepley {
9446*adc21957SMatthew G. Knepley   PetscFunctionBegin;
9447*adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9448*adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetDefault_C", (DM, DMReorderDefaultFlag), (dm, reorder));
9449*adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9450*adc21957SMatthew G. Knepley }
9451*adc21957SMatthew G. Knepley 
9452*adc21957SMatthew G. Knepley /*@
9453*adc21957SMatthew G. Knepley   DMReorderSectionGetDefault - Get flag indicating whether the local section should be reordered by default
9454*adc21957SMatthew G. Knepley 
9455*adc21957SMatthew G. Knepley   Not collective
9456*adc21957SMatthew G. Knepley 
9457*adc21957SMatthew G. Knepley   Input Parameter:
9458*adc21957SMatthew G. Knepley . dm - The DM
9459*adc21957SMatthew G. Knepley 
9460*adc21957SMatthew G. Knepley   Output Parameter:
9461*adc21957SMatthew G. Knepley . reorder - Flag for reordering
9462*adc21957SMatthew G. Knepley 
9463*adc21957SMatthew G. Knepley   Level: intermediate
9464*adc21957SMatthew G. Knepley 
9465*adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`
9466*adc21957SMatthew G. Knepley @*/
9467*adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetDefault(DM dm, DMReorderDefaultFlag *reorder)
9468*adc21957SMatthew G. Knepley {
9469*adc21957SMatthew G. Knepley   PetscFunctionBegin;
9470*adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9471*adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9472*adc21957SMatthew G. Knepley   *reorder = DM_REORDER_DEFAULT_NOTSET;
9473*adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetDefault_C", (DM, DMReorderDefaultFlag *), (dm, reorder));
9474*adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9475*adc21957SMatthew G. Knepley }
9476*adc21957SMatthew G. Knepley 
9477*adc21957SMatthew G. Knepley /*@C
9478*adc21957SMatthew G. Knepley   DMReorderSectionSetType - Set the type of local section reordering
9479*adc21957SMatthew G. Knepley 
9480*adc21957SMatthew G. Knepley   Logically collective
9481*adc21957SMatthew G. Knepley 
9482*adc21957SMatthew G. Knepley   Input Parameters:
9483*adc21957SMatthew G. Knepley + dm      - The DM
9484*adc21957SMatthew G. Knepley - reorder - The reordering method
9485*adc21957SMatthew G. Knepley 
9486*adc21957SMatthew G. Knepley   Level: intermediate
9487*adc21957SMatthew G. Knepley 
9488*adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetType()`, `DMReorderSectionSetDefault()`
9489*adc21957SMatthew G. Knepley @*/
9490*adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetType(DM dm, MatOrderingType reorder)
9491*adc21957SMatthew G. Knepley {
9492*adc21957SMatthew G. Knepley   PetscFunctionBegin;
9493*adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9494*adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetType_C", (DM, MatOrderingType), (dm, reorder));
9495*adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9496*adc21957SMatthew G. Knepley }
9497*adc21957SMatthew G. Knepley 
9498*adc21957SMatthew G. Knepley /*@C
9499*adc21957SMatthew G. Knepley   DMReorderSectionGetType - Get the reordering type for the local section
9500*adc21957SMatthew G. Knepley 
9501*adc21957SMatthew G. Knepley   Not collective
9502*adc21957SMatthew G. Knepley 
9503*adc21957SMatthew G. Knepley   Input Parameter:
9504*adc21957SMatthew G. Knepley . dm - The DM
9505*adc21957SMatthew G. Knepley 
9506*adc21957SMatthew G. Knepley   Output Parameter:
9507*adc21957SMatthew G. Knepley . reorder - The reordering method
9508*adc21957SMatthew G. Knepley 
9509*adc21957SMatthew G. Knepley   Level: intermediate
9510*adc21957SMatthew G. Knepley 
9511*adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`, `DMReorderSectionGetDefault()`
9512*adc21957SMatthew G. Knepley @*/
9513*adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetType(DM dm, MatOrderingType *reorder)
9514*adc21957SMatthew G. Knepley {
9515*adc21957SMatthew G. Knepley   PetscFunctionBegin;
9516*adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9517*adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9518*adc21957SMatthew G. Knepley   *reorder = NULL;
9519*adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetType_C", (DM, MatOrderingType *), (dm, reorder));
9520*adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9521*adc21957SMatthew G. Knepley }
9522