xref: /petsc/src/dm/interface/dm.c (revision d71ae5a4db6382e7f06317b8d368875286fe9008)
1d0295fc0SJunchao Zhang #include <petscvec.h>
2af0996ceSBarry Smith #include <petsc/private/dmimpl.h>      /*I      "petscdm.h"          I*/
3c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h> /*I      "petscdmlabel.h"     I*/
4e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h> /*I      "petscds.h"     I*/
53e922f36SToby Isaac #include <petscdmplex.h>
6f19dbd58SToby Isaac #include <petscdmfield.h>
70c312b8eSJed Brown #include <petscsf.h>
82764a2aaSMatthew G. Knepley #include <petscds.h>
947c6ae99SBarry Smith 
10f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
11f918ec44SMatthew G. Knepley   #include <petscfeceed.h>
12f918ec44SMatthew G. Knepley #endif
13f918ec44SMatthew G. Knepley 
14cea3dcb8SSatish Balay #if !defined(PETSC_HAVE_WINDOWS_COMPILERS)
15cea3dcb8SSatish Balay   #include <petsc/private/valgrind/memcheck.h>
1600d952a4SJed Brown #endif
1700d952a4SJed Brown 
18732e2eb9SMatthew G Knepley PetscClassId DM_CLASSID;
19d67d17b1SMatthew G. Knepley PetscClassId DMLABEL_CLASSID;
205b8ffe73SMark Adams 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;
2167a56275SMatthew G Knepley 
22ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[]          = {"NONE", "GHOSTED", "MIRROR", "PERIODIC", "TWIST", "DMBoundaryType", "DM_BOUNDARY_", NULL};
23d1b3049bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID", "ESSENTIAL", "NATURAL", "INVALID", "INVALID", "ESSENTIAL_FIELD", "NATURAL_FIELD", "INVALID", "INVALID", "ESSENTIAL_BD_FIELD", "NATURAL_RIEMANN", "DMBoundaryConditionType", "DM_BC_", NULL};
249371c9d4SSatish Balay const char *const DMPolytopeTypes[]   = {"vertex",  "segment",       "tensor_segment",      "triangle", "quadrilateral", "tensor_quad",    "tetrahedron",  "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism",
259371c9d4SSatish Balay                                          "pyramid", "FV_ghost_cell", "interior_ghost_cell", "unknown",  "invalid",       "DMPolytopeType", "DM_POLYTOPE_", NULL};
262cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace", "keep", "fail", "DMCopyLabelsMode", "DM_COPY_LABELS_", NULL};
2760c22052SBarry Smith 
28a4121054SBarry Smith /*@
29bb7acecfSBarry Smith   DMCreate - Creates an empty `DM` object. `DM`s are the abstract objects in PETSc that mediate between meshes and discretizations and the
30bb7acecfSBarry Smith   algebraic solvers, time integrators, and optimization algorithms.
31a4121054SBarry Smith 
32d083f849SBarry Smith   Collective
33a4121054SBarry Smith 
34a4121054SBarry Smith   Input Parameter:
35bb7acecfSBarry Smith . comm - The communicator for the `DM` object
36a4121054SBarry Smith 
37a4121054SBarry Smith   Output Parameter:
38bb7acecfSBarry Smith . dm - The `DM` object
39a4121054SBarry Smith 
40a4121054SBarry Smith   Level: beginner
41a4121054SBarry Smith 
42bb7acecfSBarry Smith   Notes:
43bb7acecfSBarry Smith   See `DMType` for a brief summary of available `DM`.
44bb7acecfSBarry Smith 
45bb7acecfSBarry Smith   The type must then be set with `DMSetType()`. If you never call `DMSetType()` it will generate an
46bb7acecfSBarry Smith   error when you try to use the dm.
47bb7acecfSBarry Smith 
48bb7acecfSBarry Smith .seealso: `DMSetType()`, `DMType`, `DMDACreate()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK`
49a4121054SBarry Smith @*/
50*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreate(MPI_Comm comm, DM *dm)
51*d71ae5a4SJacob Faibussowitsch {
52a4121054SBarry Smith   DM      v;
53e5e52638SMatthew G. Knepley   PetscDS ds;
54a4121054SBarry Smith 
55a4121054SBarry Smith   PetscFunctionBegin;
561411c6eeSJed Brown   PetscValidPointer(dm, 2);
570298fd71SBarry Smith   *dm = NULL;
589566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
59a4121054SBarry Smith 
609566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
61e7c4fc90SDmitry Karpeev 
6249be4549SMatthew G. Knepley   v->setupcalled          = PETSC_FALSE;
6349be4549SMatthew G. Knepley   v->setfromoptionscalled = PETSC_FALSE;
640298fd71SBarry Smith   v->ltogmap              = NULL;
65a4ea9b21SRichard Tran Mills   v->bind_below           = 0;
661411c6eeSJed Brown   v->bs                   = 1;
67171400e9SBarry Smith   v->coloringtype         = IS_COLORING_GLOBAL;
689566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
699566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
70c58f1c22SToby Isaac   v->labels                    = NULL;
7134aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7234aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
73c58f1c22SToby Isaac   v->depthLabel                = NULL;
74ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
751bb6d2a8SBarry Smith   v->localSection              = NULL;
761bb6d2a8SBarry Smith   v->globalSection             = NULL;
773b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
783b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
7979769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
806858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
816858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
826858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8396173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
84435a35e8SMatthew G Knepley   {
85435a35e8SMatthew G Knepley     PetscInt i;
86435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
870298fd71SBarry Smith       v->nullspaceConstructors[i]     = NULL;
88f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
89435a35e8SMatthew G Knepley     }
90435a35e8SMatthew G Knepley   }
919566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
929566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(v, NULL, NULL, ds));
939566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
949566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
9514f150ffSMatthew G. Knepley   v->dmBC              = NULL;
96a8fb8f29SToby Isaac   v->coarseMesh        = NULL;
97f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
98cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
999566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v, VECSTANDARD));
1009566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v, MATAIJ));
1014a7a4c06SLawrence Mitchell 
1021411c6eeSJed Brown   *dm = v;
103a4121054SBarry Smith   PetscFunctionReturn(0);
104a4121054SBarry Smith }
105a4121054SBarry Smith 
10638221697SMatthew G. Knepley /*@
107bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
10838221697SMatthew G. Knepley 
109d083f849SBarry Smith   Collective
11038221697SMatthew G. Knepley 
11138221697SMatthew G. Knepley   Input Parameter:
112bb7acecfSBarry Smith . dm - The original `DM` object
11338221697SMatthew G. Knepley 
11438221697SMatthew G. Knepley   Output Parameter:
115bb7acecfSBarry Smith . newdm  - The new `DM` object
11638221697SMatthew G. Knepley 
11738221697SMatthew G. Knepley   Level: beginner
11838221697SMatthew G. Knepley 
1191cb8cacdSPatrick Sanan   Notes:
120bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
121bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
122bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1231bb6d2a8SBarry Smith 
124bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
12589706ed2SPatrick Sanan 
126bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
127bb7acecfSBarry Smith 
128bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMSetType()`, `DMConvert()`
1291bb6d2a8SBarry Smith 
13038221697SMatthew G. Knepley @*/
131*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm)
132*d71ae5a4SJacob Faibussowitsch {
13338221697SMatthew G. Knepley   PetscSF  sf;
13438221697SMatthew G. Knepley   Vec      coords;
13538221697SMatthew G. Knepley   void    *ctx;
1366858538eSMatthew G. Knepley   PetscInt dim, cdim, i;
13738221697SMatthew G. Knepley 
13838221697SMatthew G. Knepley   PetscFunctionBegin;
13938221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14038221697SMatthew G. Knepley   PetscValidPointer(newdm, 2);
1419566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1429566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
143ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
144ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
145c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
1469566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1479566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1489566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1499566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1509566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1519566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
152dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1533f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1549566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1559566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1569566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1579566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
1586858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1596858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
160be4c1c3eSMatthew G. Knepley       DM           ncdm;
161be4c1c3eSMatthew G. Knepley       PetscSection cs;
1625a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
163be4c1c3eSMatthew G. Knepley 
1646858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1659566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
1669566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1675a0206caSToby Isaac       if (pEndMax >= 0) {
1686858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1696858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1709566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1716858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1726858538eSMatthew G. Knepley         else PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1739566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
174be4c1c3eSMatthew G. Knepley       }
175be4c1c3eSMatthew G. Knepley     }
1766858538eSMatthew G. Knepley   }
1779566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1789566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1799566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
18038221697SMatthew G. Knepley   if (coords) {
1819566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
18238221697SMatthew G. Knepley   } else {
1839566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1849566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
18538221697SMatthew G. Knepley   }
1866858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
1876858538eSMatthew G. Knepley   if (coords) {
1886858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
1896858538eSMatthew G. Knepley   } else {
1906858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
1916858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
1926858538eSMatthew G. Knepley   }
19390b157c4SStefano Zampini   {
1944fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
1956858538eSMatthew G. Knepley 
1964fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
1974fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L));
198c6b900c6SMatthew G. Knepley   }
19934aa8a36SMatthew G. Knepley   {
20034aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
20134aa8a36SMatthew G. Knepley 
2029566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2039566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
20434aa8a36SMatthew G. Knepley   }
20538221697SMatthew G. Knepley   PetscFunctionReturn(0);
20638221697SMatthew G. Knepley }
20738221697SMatthew G. Knepley 
2089a42bb27SBarry Smith /*@C
209bb7acecfSBarry Smith        DMSetVecType - Sets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2109a42bb27SBarry Smith 
211d083f849SBarry Smith    Logically Collective on da
2129a42bb27SBarry Smith 
213147403d9SBarry Smith    Input Parameters:
2149a42bb27SBarry Smith +  da - initial distributed array
215bb7acecfSBarry Smith -  ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2169a42bb27SBarry Smith 
2179a42bb27SBarry Smith    Options Database:
218147403d9SBarry Smith .   -dm_vec_type ctype - the type of vector to create
2199a42bb27SBarry Smith 
2209a42bb27SBarry Smith    Level: intermediate
2219a42bb27SBarry Smith 
222bb7acecfSBarry Smith .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
223bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2249a42bb27SBarry Smith @*/
225*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVecType(DM da, VecType ctype)
226*d71ae5a4SJacob Faibussowitsch {
2279a42bb27SBarry Smith   PetscFunctionBegin;
2289a42bb27SBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
2299566063dSJacob Faibussowitsch   PetscCall(PetscFree(da->vectype));
2309566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&da->vectype));
2319a42bb27SBarry Smith   PetscFunctionReturn(0);
2329a42bb27SBarry Smith }
2339a42bb27SBarry Smith 
234c0dedaeaSBarry Smith /*@C
235bb7acecfSBarry Smith        DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
236c0dedaeaSBarry Smith 
237d083f849SBarry Smith    Logically Collective on da
238c0dedaeaSBarry Smith 
239c0dedaeaSBarry Smith    Input Parameter:
240c0dedaeaSBarry Smith .  da - initial distributed array
241c0dedaeaSBarry Smith 
242c0dedaeaSBarry Smith    Output Parameter:
243c0dedaeaSBarry Smith .  ctype - the vector type
244c0dedaeaSBarry Smith 
245c0dedaeaSBarry Smith    Level: intermediate
246c0dedaeaSBarry Smith 
247db781477SPatrick Sanan .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
248c0dedaeaSBarry Smith @*/
249*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
250*d71ae5a4SJacob Faibussowitsch {
251c0dedaeaSBarry Smith   PetscFunctionBegin;
252c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
253c0dedaeaSBarry Smith   *ctype = da->vectype;
254c0dedaeaSBarry Smith   PetscFunctionReturn(0);
255c0dedaeaSBarry Smith }
256c0dedaeaSBarry Smith 
2575f1ad066SMatthew G Knepley /*@
258bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2595f1ad066SMatthew G Knepley 
2605f1ad066SMatthew G Knepley   Not collective
2615f1ad066SMatthew G Knepley 
2625f1ad066SMatthew G Knepley   Input Parameter:
263bb7acecfSBarry Smith . v - The `Vec`
2645f1ad066SMatthew G Knepley 
2655f1ad066SMatthew G Knepley   Output Parameter:
266bb7acecfSBarry Smith . dm - The `DM`
2675f1ad066SMatthew G Knepley 
2685f1ad066SMatthew G Knepley   Level: intermediate
2695f1ad066SMatthew G Knepley 
270bb7acecfSBarry Smith   Note:
271bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
272bb7acecfSBarry Smith 
273bb7acecfSBarry Smith .seealso: `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2745f1ad066SMatthew G Knepley @*/
275*d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
276*d71ae5a4SJacob Faibussowitsch {
2775f1ad066SMatthew G Knepley   PetscFunctionBegin;
2785f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2795f1ad066SMatthew G Knepley   PetscValidPointer(dm, 2);
2809566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
2815f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2825f1ad066SMatthew G Knepley }
2835f1ad066SMatthew G Knepley 
2845f1ad066SMatthew G Knepley /*@
285bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
2865f1ad066SMatthew G Knepley 
2875f1ad066SMatthew G Knepley   Not collective
2885f1ad066SMatthew G Knepley 
2895f1ad066SMatthew G Knepley   Input Parameters:
290bb7acecfSBarry Smith + v - The `Vec`
291bb7acecfSBarry Smith - dm - The `DM`
2925f1ad066SMatthew G Knepley 
293bb7acecfSBarry Smith   Note:
294bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
295d9805387SMatthew G. Knepley 
296bb7acecfSBarry 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.
297bb7acecfSBarry Smith 
298bb7acecfSBarry Smith   Level: developer
2995f1ad066SMatthew G Knepley 
300db781477SPatrick Sanan .seealso: `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3015f1ad066SMatthew G Knepley @*/
302*d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
303*d71ae5a4SJacob Faibussowitsch {
3045f1ad066SMatthew G Knepley   PetscFunctionBegin;
3055f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
306d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3079566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3085f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
3095f1ad066SMatthew G Knepley }
3105f1ad066SMatthew G Knepley 
311521d9a4cSLisandro Dalcin /*@C
312bb7acecfSBarry Smith        DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3138f1509bcSBarry Smith 
314d083f849SBarry Smith    Logically Collective on dm
3158f1509bcSBarry Smith 
3168f1509bcSBarry Smith    Input Parameters:
317bb7acecfSBarry Smith +  dm - the `DM` context
3188f1509bcSBarry Smith -  ctype - the matrix type
3198f1509bcSBarry Smith 
3208f1509bcSBarry Smith    Options Database:
3218f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3228f1509bcSBarry Smith 
3238f1509bcSBarry Smith    Level: intermediate
3248f1509bcSBarry Smith 
325db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
326bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3278f1509bcSBarry Smith @*/
328*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
329*d71ae5a4SJacob Faibussowitsch {
3308f1509bcSBarry Smith   PetscFunctionBegin;
3318f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3328f1509bcSBarry Smith   dm->coloringtype = ctype;
3338f1509bcSBarry Smith   PetscFunctionReturn(0);
3348f1509bcSBarry Smith }
3358f1509bcSBarry Smith 
3368f1509bcSBarry Smith /*@C
337bb7acecfSBarry Smith        DMGetISColoringType - Gets the type of coloring,  `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
338521d9a4cSLisandro Dalcin 
339d083f849SBarry Smith    Logically Collective on dm
340521d9a4cSLisandro Dalcin 
341521d9a4cSLisandro Dalcin    Input Parameter:
342bb7acecfSBarry Smith .  dm - the `DM` context
3438f1509bcSBarry Smith 
3448f1509bcSBarry Smith    Output Parameter:
3458f1509bcSBarry Smith .  ctype - the matrix type
3468f1509bcSBarry Smith 
3478f1509bcSBarry Smith    Options Database:
3488f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3498f1509bcSBarry Smith 
3508f1509bcSBarry Smith    Level: intermediate
3518f1509bcSBarry Smith 
352db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
353bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3548f1509bcSBarry Smith @*/
355*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
356*d71ae5a4SJacob Faibussowitsch {
3578f1509bcSBarry Smith   PetscFunctionBegin;
3588f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3598f1509bcSBarry Smith   *ctype = dm->coloringtype;
3608f1509bcSBarry Smith   PetscFunctionReturn(0);
3618f1509bcSBarry Smith }
3628f1509bcSBarry Smith 
3638f1509bcSBarry Smith /*@C
364bb7acecfSBarry Smith        DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3658f1509bcSBarry Smith 
366d083f849SBarry Smith    Logically Collective on dm
3678f1509bcSBarry Smith 
3688f1509bcSBarry Smith    Input Parameters:
369bb7acecfSBarry Smith +  dm - the `DM` context
370bb7acecfSBarry Smith -  ctype - the matrix type, for example `MATMPIAIJ`
371521d9a4cSLisandro Dalcin 
372521d9a4cSLisandro Dalcin    Options Database:
373bb7acecfSBarry Smith .   -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
374521d9a4cSLisandro Dalcin 
375521d9a4cSLisandro Dalcin    Level: intermediate
376521d9a4cSLisandro Dalcin 
377bb7acecfSBarry Smith .seealso: `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`, `DMSetMatType()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
378521d9a4cSLisandro Dalcin @*/
379*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
380*d71ae5a4SJacob Faibussowitsch {
381521d9a4cSLisandro Dalcin   PetscFunctionBegin;
382521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3839566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->mattype));
3849566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
385521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
386521d9a4cSLisandro Dalcin }
387521d9a4cSLisandro Dalcin 
388c0dedaeaSBarry Smith /*@C
389bb7acecfSBarry Smith        DMGetMatType - Gets the type of matrix created with `DMCreateMatrix()`
390c0dedaeaSBarry Smith 
391d083f849SBarry Smith    Logically Collective on dm
392c0dedaeaSBarry Smith 
393c0dedaeaSBarry Smith    Input Parameter:
394bb7acecfSBarry Smith .  dm - the `DM` context
395c0dedaeaSBarry Smith 
396c0dedaeaSBarry Smith    Output Parameter:
397c0dedaeaSBarry Smith .  ctype - the matrix type
398c0dedaeaSBarry Smith 
399c0dedaeaSBarry Smith    Level: intermediate
400c0dedaeaSBarry Smith 
401db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`, `DMSetMatType()`, `DMGetMatType()`
402c0dedaeaSBarry Smith @*/
403*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
404*d71ae5a4SJacob Faibussowitsch {
405c0dedaeaSBarry Smith   PetscFunctionBegin;
406c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
407c0dedaeaSBarry Smith   *ctype = dm->mattype;
408c0dedaeaSBarry Smith   PetscFunctionReturn(0);
409c0dedaeaSBarry Smith }
410c0dedaeaSBarry Smith 
411c688c046SMatthew G Knepley /*@
412bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
413c688c046SMatthew G Knepley 
414c688c046SMatthew G Knepley   Not collective
415c688c046SMatthew G Knepley 
416c688c046SMatthew G Knepley   Input Parameter:
417bb7acecfSBarry Smith . A - The `Mat`
418c688c046SMatthew G Knepley 
419c688c046SMatthew G Knepley   Output Parameter:
420bb7acecfSBarry Smith . dm - The `DM`
421c688c046SMatthew G Knepley 
422c688c046SMatthew G Knepley   Level: intermediate
423c688c046SMatthew G Knepley 
424bb7acecfSBarry Smith   Note:
425bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
426bb7acecfSBarry Smith 
427bb7acecfSBarry Smith   Developer Note:
428bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4298f1509bcSBarry Smith 
430db781477SPatrick Sanan .seealso: `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
431c688c046SMatthew G Knepley @*/
432*d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
433*d71ae5a4SJacob Faibussowitsch {
434c688c046SMatthew G Knepley   PetscFunctionBegin;
435c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
436c688c046SMatthew G Knepley   PetscValidPointer(dm, 2);
4379566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
438c688c046SMatthew G Knepley   PetscFunctionReturn(0);
439c688c046SMatthew G Knepley }
440c688c046SMatthew G Knepley 
441c688c046SMatthew G Knepley /*@
442bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
443c688c046SMatthew G Knepley 
444c688c046SMatthew G Knepley   Not collective
445c688c046SMatthew G Knepley 
446c688c046SMatthew G Knepley   Input Parameters:
447c688c046SMatthew G Knepley + A - The Mat
448c688c046SMatthew G Knepley - dm - The DM
449c688c046SMatthew G Knepley 
450bb7acecfSBarry Smith   Level: developer
451c688c046SMatthew G Knepley 
452bb7acecfSBarry Smith   Note:
453bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
454bb7acecfSBarry Smith 
455bb7acecfSBarry Smith   Developer Note:
456bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
457bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4588f1509bcSBarry Smith 
459db781477SPatrick Sanan .seealso: `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
460c688c046SMatthew G Knepley @*/
461*d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
462*d71ae5a4SJacob Faibussowitsch {
463c688c046SMatthew G Knepley   PetscFunctionBegin;
464c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4658865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4669566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
467c688c046SMatthew G Knepley   PetscFunctionReturn(0);
468c688c046SMatthew G Knepley }
469c688c046SMatthew G Knepley 
4709a42bb27SBarry Smith /*@C
471bb7acecfSBarry Smith    DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4729a42bb27SBarry Smith 
473d083f849SBarry Smith    Logically Collective on dm
4749a42bb27SBarry Smith 
475d8d19677SJose E. Roman    Input Parameters:
476bb7acecfSBarry Smith +  da - the `DM` context
477bb7acecfSBarry Smith -  prefix - the prefix to prepend
4789a42bb27SBarry Smith 
4799a42bb27SBarry Smith    Notes:
4809a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4819a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4829a42bb27SBarry Smith 
4839a42bb27SBarry Smith    Level: advanced
4849a42bb27SBarry Smith 
485bb7acecfSBarry Smith .seealso: `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
4869a42bb27SBarry Smith @*/
487*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
488*d71ae5a4SJacob Faibussowitsch {
4899a42bb27SBarry Smith   PetscFunctionBegin;
4909a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4919566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
4921baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
4931baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
4949a42bb27SBarry Smith   PetscFunctionReturn(0);
4959a42bb27SBarry Smith }
4969a42bb27SBarry Smith 
49731697293SDave May /*@C
498bb7acecfSBarry Smith    DMAppendOptionsPrefix - Appends an additional string to an already exising prefix used for searching for
499bb7acecfSBarry Smith    `DM` options in the options database.
50031697293SDave May 
501d083f849SBarry Smith    Logically Collective on dm
50231697293SDave May 
50331697293SDave May    Input Parameters:
504bb7acecfSBarry Smith +  dm - the `DM` context
505bb7acecfSBarry Smith -  prefix - the string to append to the current prefix
50631697293SDave May 
50731697293SDave May    Notes:
508bb7acecfSBarry 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.
50931697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
51031697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
51131697293SDave May 
51231697293SDave May    Level: advanced
51331697293SDave May 
514bb7acecfSBarry Smith .seealso: `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
51531697293SDave May @*/
516*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
517*d71ae5a4SJacob Faibussowitsch {
51831697293SDave May   PetscFunctionBegin;
51931697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5209566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
52131697293SDave May   PetscFunctionReturn(0);
52231697293SDave May }
52331697293SDave May 
52431697293SDave May /*@C
52531697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
526bb7acecfSBarry Smith    DM options in the options database.
52731697293SDave May 
52831697293SDave May    Not Collective
52931697293SDave May 
53031697293SDave May    Input Parameters:
531bb7acecfSBarry Smith .  dm - the `DM` context
53231697293SDave May 
53331697293SDave May    Output Parameters:
53431697293SDave May .  prefix - pointer to the prefix string used is returned
53531697293SDave May 
536bb7acecfSBarry Smith    Fortran Note:
53795452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
53831697293SDave May    sufficient length to hold the prefix.
53931697293SDave May 
54031697293SDave May    Level: advanced
54131697293SDave May 
542bb7acecfSBarry Smith .seealso: `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
54331697293SDave May @*/
544*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
545*d71ae5a4SJacob Faibussowitsch {
54631697293SDave May   PetscFunctionBegin;
54731697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5489566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
54931697293SDave May   PetscFunctionReturn(0);
55031697293SDave May }
55131697293SDave May 
552*d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
553*d71ae5a4SJacob Faibussowitsch {
5546eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
55588bdff64SToby Isaac 
55688bdff64SToby Isaac   PetscFunctionBegin;
557aab5bcd8SJed Brown   *ncrefct = 0;
55888bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55988bdff64SToby Isaac     refct--;
56088bdff64SToby Isaac     if (recurseCoarse) {
56188bdff64SToby Isaac       PetscInt coarseCount;
56288bdff64SToby Isaac 
5639566063dSJacob Faibussowitsch       PetscCall(DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
56488bdff64SToby Isaac       refct += coarseCount;
56588bdff64SToby Isaac     }
56688bdff64SToby Isaac   }
56788bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56888bdff64SToby Isaac     refct--;
56988bdff64SToby Isaac     if (recurseFine) {
57088bdff64SToby Isaac       PetscInt fineCount;
57188bdff64SToby Isaac 
5729566063dSJacob Faibussowitsch       PetscCall(DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
57388bdff64SToby Isaac       refct += fineCount;
57488bdff64SToby Isaac     }
57588bdff64SToby Isaac   }
57688bdff64SToby Isaac   *ncrefct = refct;
57788bdff64SToby Isaac   PetscFunctionReturn(0);
57888bdff64SToby Isaac }
57988bdff64SToby Isaac 
580*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
581*d71ae5a4SJacob Faibussowitsch {
5825d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
583354557abSToby Isaac 
584354557abSToby Isaac   PetscFunctionBegin;
585354557abSToby Isaac   /* destroy the labels */
586354557abSToby Isaac   while (next) {
587354557abSToby Isaac     DMLabelLink tmp = next->next;
588354557abSToby Isaac 
5895d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
590ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
5919566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
5929566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
593354557abSToby Isaac     next = tmp;
594354557abSToby Isaac   }
5955d80c0bfSVaclav Hapla   dm->labels = NULL;
596354557abSToby Isaac   PetscFunctionReturn(0);
597354557abSToby Isaac }
598354557abSToby Isaac 
599*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
600*d71ae5a4SJacob Faibussowitsch {
6016858538eSMatthew G. Knepley   PetscFunctionBegin;
6026858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6036858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6046858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6056858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6066858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6076858538eSMatthew G. Knepley   PetscFunctionReturn(0);
6086858538eSMatthew G. Knepley }
6096858538eSMatthew G. Knepley 
6101fb7b255SJunchao Zhang /*@C
611bb7acecfSBarry Smith     DMDestroy - Destroys a `DM`.
61247c6ae99SBarry Smith 
613d083f849SBarry Smith     Collective on dm
61447c6ae99SBarry Smith 
61547c6ae99SBarry Smith     Input Parameter:
616bb7acecfSBarry Smith .   dm - the `DM` object to destroy
61747c6ae99SBarry Smith 
61847c6ae99SBarry Smith     Level: developer
61947c6ae99SBarry Smith 
620bb7acecfSBarry Smith .seealso: `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
62147c6ae99SBarry Smith 
62247c6ae99SBarry Smith @*/
623*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
624*d71ae5a4SJacob Faibussowitsch {
6256eb26441SStefano Zampini   PetscInt       cnt;
626dfe15315SJed Brown   DMNamedVecLink nlink, nnext;
62747c6ae99SBarry Smith 
62847c6ae99SBarry Smith   PetscFunctionBegin;
6296bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6306bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm), DM_CLASSID, 1);
63187e657c6SBarry Smith 
63288bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
6339566063dSJacob Faibussowitsch   PetscCall(DMCountNonCyclicReferences(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
63488bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
6359371c9d4SSatish Balay   if (--cnt > 0) {
6369371c9d4SSatish Balay     *dm = NULL;
6379371c9d4SSatish Balay     PetscFunctionReturn(0);
6389371c9d4SSatish Balay   }
6396bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6406bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6416eb26441SStefano Zampini 
6429566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6439566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
6446eb26441SStefano Zampini 
645f490541aSPeter Brune   nnext              = (*dm)->namedglobal;
6460298fd71SBarry Smith   (*dm)->namedglobal = NULL;
647f490541aSPeter Brune   for (nlink = nnext; nlink; nlink = nnext) { /* Destroy the named vectors */
6482348bcf4SPeter Brune     nnext = nlink->next;
6497a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN, ((PetscObject)*dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM still has Vec named '%s' checked out", nlink->name);
6509566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6519566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6529566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
6532348bcf4SPeter Brune   }
654f490541aSPeter Brune   nnext             = (*dm)->namedlocal;
6550298fd71SBarry Smith   (*dm)->namedlocal = NULL;
656f490541aSPeter Brune   for (nlink = nnext; nlink; nlink = nnext) { /* Destroy the named local vectors */
657f490541aSPeter Brune     nnext = nlink->next;
6587a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN, ((PetscObject)*dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM still has Vec named '%s' checked out", nlink->name);
6599566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6609566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6619566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
662f490541aSPeter Brune   }
6632348bcf4SPeter Brune 
664b17ce1afSJed Brown   /* Destroy the list of hooks */
665c833c3b5SJed Brown   {
666c833c3b5SJed Brown     DMCoarsenHookLink link, next;
667b17ce1afSJed Brown     for (link = (*dm)->coarsenhook; link; link = next) {
668b17ce1afSJed Brown       next = link->next;
6699566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
670b17ce1afSJed Brown     }
6710298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
672c833c3b5SJed Brown   }
673c833c3b5SJed Brown   {
674c833c3b5SJed Brown     DMRefineHookLink link, next;
675c833c3b5SJed Brown     for (link = (*dm)->refinehook; link; link = next) {
676c833c3b5SJed Brown       next = link->next;
6779566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
678c833c3b5SJed Brown     }
6790298fd71SBarry Smith     (*dm)->refinehook = NULL;
680c833c3b5SJed Brown   }
681be081cd6SPeter Brune   {
682be081cd6SPeter Brune     DMSubDomainHookLink link, next;
683be081cd6SPeter Brune     for (link = (*dm)->subdomainhook; link; link = next) {
684be081cd6SPeter Brune       next = link->next;
6859566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
686be081cd6SPeter Brune     }
6870298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
688be081cd6SPeter Brune   }
689baf369e7SPeter Brune   {
690baf369e7SPeter Brune     DMGlobalToLocalHookLink link, next;
691baf369e7SPeter Brune     for (link = (*dm)->gtolhook; link; link = next) {
692baf369e7SPeter Brune       next = link->next;
6939566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
694baf369e7SPeter Brune     }
6950298fd71SBarry Smith     (*dm)->gtolhook = NULL;
696baf369e7SPeter Brune   }
697d4d07f1eSToby Isaac   {
698d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link, next;
699d4d07f1eSToby Isaac     for (link = (*dm)->ltoghook; link; link = next) {
700d4d07f1eSToby Isaac       next = link->next;
7019566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
702d4d07f1eSToby Isaac     }
703d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
704d4d07f1eSToby Isaac   }
705aa1993deSMatthew G Knepley   /* Destroy the work arrays */
706aa1993deSMatthew G Knepley   {
707aa1993deSMatthew G Knepley     DMWorkLink link, next;
7087a8be351SBarry Smith     PetscCheck(!(*dm)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out");
709aa1993deSMatthew G Knepley     for (link = (*dm)->workin; link; link = next) {
710aa1993deSMatthew G Knepley       next = link->next;
7119566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
7129566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
713aa1993deSMatthew G Knepley     }
7140298fd71SBarry Smith     (*dm)->workin = NULL;
715aa1993deSMatthew G Knepley   }
716c58f1c22SToby Isaac   /* destroy the labels */
7179566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
718f4cdcedcSVaclav Hapla   /* destroy the fields */
7199566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
720f4cdcedcSVaclav Hapla   /* destroy the boundaries */
721e6f8dbb6SToby Isaac   {
722e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
723e6f8dbb6SToby Isaac     while (next) {
724e6f8dbb6SToby Isaac       DMBoundary b = next;
725e6f8dbb6SToby Isaac 
726e6f8dbb6SToby Isaac       next = b->next;
7279566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
728e6f8dbb6SToby Isaac     }
729e6f8dbb6SToby Isaac   }
730b17ce1afSJed Brown 
7319566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7329566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7339566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
73452536dc3SBarry Smith 
73548a46eb9SPierre Jolivet   if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7369566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7379566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7389566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7399566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
74088ed4aceSMatthew G Knepley 
7419566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7429566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
7439566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7449566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7459566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7469566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7479566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
748736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
74948a46eb9SPierre Jolivet     if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7509566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
751736995cdSBlaise Bourdin   }
7529a2a23afSMatthew G. Knepley   {
7539a2a23afSMatthew G. Knepley     Vec     *auxData;
7549a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7559a2a23afSMatthew G. Knepley 
7569566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetSize((*dm)->auxData, &n));
7579566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &auxData));
7589566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetVals((*dm)->auxData, &off, auxData));
7599566063dSJacob Faibussowitsch     for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
7609566063dSJacob Faibussowitsch     PetscCall(PetscFree(auxData));
7619566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
7629a2a23afSMatthew G. Knepley   }
76348a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7646eb26441SStefano Zampini 
7659566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
76648a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7679566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7684fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7699566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7709566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7716858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7726858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7739566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7749566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7759566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7766636e97aSMatthew G Knepley 
7779566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7789566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
779e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7809566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
781732e2eb9SMatthew G Knepley 
78248a46eb9SPierre Jolivet   if ((*dm)->ops->destroy) PetscCall((*(*dm)->ops->destroy)(*dm));
7839566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
784f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7859566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7869566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
787f918ec44SMatthew G. Knepley #endif
788435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7899566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
79047c6ae99SBarry Smith   PetscFunctionReturn(0);
79147c6ae99SBarry Smith }
79247c6ae99SBarry Smith 
793d7bf68aeSBarry Smith /*@
794bb7acecfSBarry Smith     DMSetUp - sets up the data structures inside a `DM` object
795d7bf68aeSBarry Smith 
796d083f849SBarry Smith     Collective on dm
797d7bf68aeSBarry Smith 
798d7bf68aeSBarry Smith     Input Parameter:
799bb7acecfSBarry Smith .   dm - the `DM` object to setup
800d7bf68aeSBarry Smith 
801bb7acecfSBarry Smith     Level: intermediate
802d7bf68aeSBarry Smith 
803bb7acecfSBarry Smith     Note:
804bb7acecfSBarry Smith     This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
805bb7acecfSBarry Smith 
806bb7acecfSBarry Smith .seealso: `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
807d7bf68aeSBarry Smith 
808d7bf68aeSBarry Smith @*/
809*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
810*d71ae5a4SJacob Faibussowitsch {
811d7bf68aeSBarry Smith   PetscFunctionBegin;
812171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8138387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
814dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8158387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
816d7bf68aeSBarry Smith   PetscFunctionReturn(0);
817d7bf68aeSBarry Smith }
818d7bf68aeSBarry Smith 
819d7bf68aeSBarry Smith /*@
820bb7acecfSBarry Smith     DMSetFromOptions - sets parameters in a `DM` from the options database
821d7bf68aeSBarry Smith 
822d083f849SBarry Smith     Collective on dm
823d7bf68aeSBarry Smith 
824d7bf68aeSBarry Smith     Input Parameter:
825bb7acecfSBarry Smith .   dm - the `DM` object to set options for
826d7bf68aeSBarry Smith 
827732e2eb9SMatthew G Knepley     Options Database:
828bb7acecfSBarry Smith +   -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
829bb7acecfSBarry Smith .   -dm_vec_type <type>  - type of vector to create inside `DM`
830bb7acecfSBarry Smith .   -dm_mat_type <type>  - type of matrix to create inside `DM`
831a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
832bb7acecfSBarry 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`
833732e2eb9SMatthew G Knepley 
8349318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8359318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8369318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
837cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
838bb7acecfSBarry Smith . -dm_plex_shape <shape>            - The domain shape, such as `DM_SHAPE_BOX`, `DM_SHAPE_SPHERE`, etc.
8399318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8409318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8419318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
842bb7acecfSBarry Smith . -dm_plex_simplex <bool>           - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
843bb7acecfSBarry Smith . -dm_plex_interpolate <bool>       - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8449318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8459318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8469318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8479318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
848bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>        - Specify the `DMBoundaryType `for each direction
8499318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8509318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8519318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8529318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
853bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8549318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8559318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8569318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8579318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
858bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
859d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
860d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
861d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
862d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
863d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
864909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
865909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8669318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8679318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8689318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8699318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8709318fe57SMatthew G. Knepley 
871384a6580SVaclav Hapla     DMPLEX Specific Checks
872bb7acecfSBarry Smith +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
873bb7acecfSBarry Smith .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
874bb7acecfSBarry 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()`
875bb7acecfSBarry Smith .   -dm_plex_check_geometry        - Check that cells have positive volume - `DMPlexCheckGeometry()`
876bb7acecfSBarry Smith .   -dm_plex_check_pointsf         - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
877bb7acecfSBarry Smith .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
878384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
879d7bf68aeSBarry Smith 
88095eb5ee5SVaclav Hapla     Level: intermediate
88195eb5ee5SVaclav Hapla 
882bb7acecfSBarry Smith .seealso: `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
883bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
884bb7acecfSBarry Smith          `DMSetOptionsPrefix()`, `DM`, `DMType`, `DMPLEX`, `DMDA`
885d7bf68aeSBarry Smith 
886d7bf68aeSBarry Smith @*/
887*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
888*d71ae5a4SJacob Faibussowitsch {
8897781c08eSBarry Smith   char      typeName[256];
890ca266f36SBarry Smith   PetscBool flg;
891d7bf68aeSBarry Smith 
892d7bf68aeSBarry Smith   PetscFunctionBegin;
893171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89449be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
8959566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
8969566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
897d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
8989566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
8999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9001baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9021baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
9039566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9049566063dSJacob 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));
905dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
906f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
907dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
908d0609cedSBarry Smith   PetscOptionsEnd();
909d7bf68aeSBarry Smith   PetscFunctionReturn(0);
910d7bf68aeSBarry Smith }
911d7bf68aeSBarry Smith 
912fc9bc008SSatish Balay /*@C
913bb7acecfSBarry Smith    DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
914fe2efc57SMark 
915bb7acecfSBarry Smith    Collective on dm
916fe2efc57SMark 
917fe2efc57SMark    Input Parameters:
918bb7acecfSBarry Smith +  dm - the `DM` object
919bb7acecfSBarry Smith .  obj - optional object that provides the prefix for the options database (if NULL then the prefix in obj is used)
920bb7acecfSBarry Smith -  optionname - option string that is used to activate viewing
921fe2efc57SMark 
922fe2efc57SMark    Level: intermediate
923bb7acecfSBarry Smith 
924bb7acecfSBarry Smith    Note:
925bb7acecfSBarry Smith    See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
926bb7acecfSBarry Smith 
927bb7acecfSBarry Smith .seealso: `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`, `PetscObjectViewFromOptions()`
928fe2efc57SMark @*/
929*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
930*d71ae5a4SJacob Faibussowitsch {
931fe2efc57SMark   PetscFunctionBegin;
932fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9339566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
934fe2efc57SMark   PetscFunctionReturn(0);
935fe2efc57SMark }
936fe2efc57SMark 
937fe2efc57SMark /*@C
938bb7acecfSBarry 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
939bb7acecfSBarry Smith     save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
94047c6ae99SBarry Smith 
941d083f849SBarry Smith     Collective on dm
94247c6ae99SBarry Smith 
943d8d19677SJose E. Roman     Input Parameters:
944bb7acecfSBarry Smith +   dm - the `DM` object to view
94547c6ae99SBarry Smith -   v - the viewer
94647c6ae99SBarry Smith 
947cd7e8a5eSksagiyam     Notes:
948bb7acecfSBarry Smith     Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
949bb7acecfSBarry Smith     meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
950bb7acecfSBarry Smith     before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
951cd7e8a5eSksagiyam 
952224748a4SBarry Smith     Level: beginner
95347c6ae99SBarry Smith 
954bb7acecfSBarry Smith .seealso: `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat`(), `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
95547c6ae99SBarry Smith 
95647c6ae99SBarry Smith @*/
957*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
958*d71ae5a4SJacob Faibussowitsch {
95932c0f0efSBarry Smith   PetscBool         isbinary;
96076a8abe0SBarry Smith   PetscMPIInt       size;
96176a8abe0SBarry Smith   PetscViewerFormat format;
96247c6ae99SBarry Smith 
96347c6ae99SBarry Smith   PetscFunctionBegin;
964171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
96548a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
966b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
96774903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
96874903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
96974903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
97074903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
97174903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
97274903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
97374903a4fSStefano Zampini      in an error here */
97474903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9759566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
976b1b135c8SBarry Smith 
9779566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9789566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
97976a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
9809566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9819566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
98232c0f0efSBarry Smith   if (isbinary) {
98355849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
98432c0f0efSBarry Smith     char     type[256];
98532c0f0efSBarry Smith 
9869566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
9879566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, 256));
9889566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
98932c0f0efSBarry Smith   }
990dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
99147c6ae99SBarry Smith   PetscFunctionReturn(0);
99247c6ae99SBarry Smith }
99347c6ae99SBarry Smith 
99447c6ae99SBarry Smith /*@
995bb7acecfSBarry 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,
996bb7acecfSBarry Smith     that is it has no ghost locations.
99747c6ae99SBarry Smith 
998d083f849SBarry Smith     Collective on dm
99947c6ae99SBarry Smith 
100047c6ae99SBarry Smith     Input Parameter:
1001bb7acecfSBarry Smith .   dm - the `DM` object
100247c6ae99SBarry Smith 
100347c6ae99SBarry Smith     Output Parameter:
100447c6ae99SBarry Smith .   vec - the global vector
100547c6ae99SBarry Smith 
1006073dac72SJed Brown     Level: beginner
100747c6ae99SBarry Smith 
1008bb7acecfSBarry Smith .seealso: `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1009bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith @*/
1012*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1013*d71ae5a4SJacob Faibussowitsch {
101447c6ae99SBarry Smith   PetscFunctionBegin;
1015171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1016b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1017dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
101876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1019c6b011d8SStefano Zampini     DM vdm;
1020c6b011d8SStefano Zampini 
10219566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10227a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1023c6b011d8SStefano Zampini   }
102447c6ae99SBarry Smith   PetscFunctionReturn(0);
102547c6ae99SBarry Smith }
102647c6ae99SBarry Smith 
102747c6ae99SBarry Smith /*@
1028bb7acecfSBarry Smith     DMCreateLocalVector - Creates a local vector from a `DM` object.
102947c6ae99SBarry Smith 
103047c6ae99SBarry Smith     Not Collective
103147c6ae99SBarry Smith 
103247c6ae99SBarry Smith     Input Parameter:
1033bb7acecfSBarry Smith .   dm - the `DM` object
103447c6ae99SBarry Smith 
103547c6ae99SBarry Smith     Output Parameter:
103647c6ae99SBarry Smith .   vec - the local vector
103747c6ae99SBarry Smith 
1038073dac72SJed Brown     Level: beginner
103947c6ae99SBarry Smith 
1040bb7acecfSBarry Smith     Notes:
1041bb7acecfSBarry 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.
1042bb7acecfSBarry Smith 
1043bb7acecfSBarry Smith  .seealso: `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1044bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
104547c6ae99SBarry Smith 
104647c6ae99SBarry Smith @*/
1047*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1048*d71ae5a4SJacob Faibussowitsch {
104947c6ae99SBarry Smith   PetscFunctionBegin;
1050171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1051b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1052dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
105376bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1054c6b011d8SStefano Zampini     DM vdm;
1055c6b011d8SStefano Zampini 
10569566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10577a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1058c6b011d8SStefano Zampini   }
105947c6ae99SBarry Smith   PetscFunctionReturn(0);
106047c6ae99SBarry Smith }
106147c6ae99SBarry Smith 
10621411c6eeSJed Brown /*@
1063bb7acecfSBarry Smith    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10641411c6eeSJed Brown 
1065d083f849SBarry Smith    Collective on dm
10661411c6eeSJed Brown 
10671411c6eeSJed Brown    Input Parameter:
1068bb7acecfSBarry Smith .  dm - the `DM` that provides the mapping
10691411c6eeSJed Brown 
10701411c6eeSJed Brown    Output Parameter:
10711411c6eeSJed Brown .  ltog - the mapping
10721411c6eeSJed Brown 
1073bb7acecfSBarry Smith    Level: advanced
10741411c6eeSJed Brown 
10751411c6eeSJed Brown    Notes:
1076bb7acecfSBarry Smith    The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10771411c6eeSJed Brown 
1078bb7acecfSBarry Smith    Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1079bb7acecfSBarry Smith    need to use this function with those objects.
1080bb7acecfSBarry Smith 
1081bb7acecfSBarry Smith    This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1082bb7acecfSBarry Smith 
1083bb7acecfSBarry Smith .seealso: `DMCreateLocalVector()`,  `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1084bb7acecfSBarry Smith           `DMCreateMatrix()`
10851411c6eeSJed Brown @*/
1086*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1087*d71ae5a4SJacob Faibussowitsch {
10880be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10891411c6eeSJed Brown 
10901411c6eeSJed Brown   PetscFunctionBegin;
10911411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10921411c6eeSJed Brown   PetscValidPointer(ltog, 2);
10931411c6eeSJed Brown   if (!dm->ltogmap) {
109437d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
109537d0c07bSMatthew G Knepley 
10969566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
109737d0c07bSMatthew G Knepley     if (section) {
1098a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
109937d0c07bSMatthew G Knepley       PetscInt       *ltog;
1100ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
110137d0c07bSMatthew G Knepley 
11029566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11039566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11049566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11059566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
110637d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1107e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
110837d0c07bSMatthew G Knepley 
110937d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11109566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11119566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11129566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11139566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11141a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11151a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1116ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11175227eafbSStefano Zampini 
1118e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11195227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1120e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1121e6befd46SJed Brown             cind++;
1122e6befd46SJed Brown           } else {
11235227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1124e6befd46SJed Brown           }
112537d0c07bSMatthew G Knepley         }
112637d0c07bSMatthew G Knepley       }
1127bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11289371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11299371c9d4SSatish Balay       bsLocal[1] = bs;
11309566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11319371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11329371c9d4SSatish Balay         bs = 1;
11339371c9d4SSatish Balay       } else {
11349371c9d4SSatish Balay         bs = bsMinMax[0];
11359371c9d4SSatish Balay       }
11367591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11377591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1138ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1139ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1140ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1141ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1142ca469d19SJed Brown         }
1143ccf3bd66SMatthew G. Knepley         n /= bs;
1144ccf3bd66SMatthew G. Knepley       }
11459566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1146dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
114737d0c07bSMatthew G Knepley   }
11481411c6eeSJed Brown   *ltog = dm->ltogmap;
11491411c6eeSJed Brown   PetscFunctionReturn(0);
11501411c6eeSJed Brown }
11511411c6eeSJed Brown 
11521411c6eeSJed Brown /*@
1153bb7acecfSBarry Smith    DMGetBlockSize - Gets the inherent block size associated with a `DM`
11541411c6eeSJed Brown 
11551411c6eeSJed Brown    Not Collective
11561411c6eeSJed Brown 
11571411c6eeSJed Brown    Input Parameter:
1158bb7acecfSBarry Smith .  dm - the `DM` with block structure
11591411c6eeSJed Brown 
11601411c6eeSJed Brown    Output Parameter:
11611411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11621411c6eeSJed Brown 
11631411c6eeSJed Brown    Level: intermediate
11641411c6eeSJed Brown 
1165bb7acecfSBarry Smith    Note:
1166bb7acecfSBarry Smith    This might be the number of degrees of freedom at each grid point for a structured grid.
1167bb7acecfSBarry Smith 
1168bb7acecfSBarry Smith    Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1169bb7acecfSBarry Smith    rather different locations in the vectors may have a different block size.
1170bb7acecfSBarry Smith 
1171db781477SPatrick Sanan .seealso: `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11721411c6eeSJed Brown @*/
1173*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1174*d71ae5a4SJacob Faibussowitsch {
11751411c6eeSJed Brown   PetscFunctionBegin;
11761411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1177534a8f05SLisandro Dalcin   PetscValidIntPointer(bs, 2);
11787a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11791411c6eeSJed Brown   *bs = dm->bs;
11801411c6eeSJed Brown   PetscFunctionReturn(0);
11811411c6eeSJed Brown }
11821411c6eeSJed Brown 
118348eeb7c8SBarry Smith /*@C
1184bb7acecfSBarry Smith     DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1185bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
118647c6ae99SBarry Smith 
1187a5bc1bf3SBarry Smith     Collective on dmc
118847c6ae99SBarry Smith 
1189d8d19677SJose E. Roman     Input Parameters:
1190bb7acecfSBarry Smith +   dmc - the `DM` object
1191bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
119247c6ae99SBarry Smith 
1193d8d19677SJose E. Roman     Output Parameters:
119447c6ae99SBarry Smith +  mat - the interpolation
1195bb7acecfSBarry Smith -  vec - the scaling (optional), see `DMCreateInterpolationScale()`
119647c6ae99SBarry Smith 
119747c6ae99SBarry Smith     Level: developer
119847c6ae99SBarry Smith 
119995452b02SPatrick Sanan     Notes:
1200bb7acecfSBarry 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
1201bb7acecfSBarry Smith     DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1202d52bd9f3SBarry Smith 
1203bb7acecfSBarry Smith     For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1204bb7acecfSBarry Smith     vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
120585afcc9aSBarry Smith 
1206bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
120747c6ae99SBarry Smith 
120847c6ae99SBarry Smith @*/
1209*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1210*d71ae5a4SJacob Faibussowitsch {
121147c6ae99SBarry Smith   PetscFunctionBegin;
1212a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1213a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
1214c7d20fa0SStefano Zampini   PetscValidPointer(mat, 3);
12159566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1216dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12179566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
121847c6ae99SBarry Smith   PetscFunctionReturn(0);
121947c6ae99SBarry Smith }
122047c6ae99SBarry Smith 
12213ad4599aSBarry Smith /*@
1222bb7acecfSBarry Smith     DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is the transpose of the interpolation between the `DM`.
1223bb7acecfSBarry Smith     xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual) restriction. In other words xcoarse is the coarse
1224bb7acecfSBarry Smith     representation of xfine.
12252ed6491fSPatrick Sanan 
12262ed6491fSPatrick Sanan   Input Parameters:
1227bb7acecfSBarry Smith +      dac - `DM` that defines a coarse mesh
1228bb7acecfSBarry Smith .      daf - `DM` that defines a fine mesh
12292ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12302ed6491fSPatrick Sanan 
12312ed6491fSPatrick Sanan   Output Parameter:
12322ed6491fSPatrick Sanan .    scale - the scaled vector
12332ed6491fSPatrick Sanan 
1234bb7acecfSBarry Smith   Level: advanced
12352ed6491fSPatrick Sanan 
1236e9c74fd6SRichard Tran Mills   Developer Notes:
1237bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1238e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1239e9c74fd6SRichard Tran Mills 
1240bb7acecfSBarry Smith .seealso: `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, DMCreateRestriction()`, `DMCreateGlobalVector()`
12412ed6491fSPatrick Sanan 
12422ed6491fSPatrick Sanan @*/
1243*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1244*d71ae5a4SJacob Faibussowitsch {
12452ed6491fSPatrick Sanan   Vec         fine;
12462ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12479704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1248e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12499704db99SRichard Tran Mills #endif
12502ed6491fSPatrick Sanan 
12512ed6491fSPatrick Sanan   PetscFunctionBegin;
12529566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12539566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12549566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12559704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12569704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12579704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12589704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12599566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1260e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12619566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12629566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12639566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
126483aa49f4SRichard Tran Mills   }
12659704db99SRichard Tran Mills #endif
12669566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12679566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12689566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12692ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12702ed6491fSPatrick Sanan }
12712ed6491fSPatrick Sanan 
12722ed6491fSPatrick Sanan /*@
1273bb7acecfSBarry Smith     DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1274bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12753ad4599aSBarry Smith 
1276a5bc1bf3SBarry Smith     Collective on dmc
12773ad4599aSBarry Smith 
1278d8d19677SJose E. Roman     Input Parameters:
1279bb7acecfSBarry Smith +   dmc - the `DM` object
1280bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
12813ad4599aSBarry Smith 
12823ad4599aSBarry Smith     Output Parameter:
12833ad4599aSBarry Smith .  mat - the restriction
12843ad4599aSBarry Smith 
12853ad4599aSBarry Smith     Level: developer
12863ad4599aSBarry Smith 
1287bb7acecfSBarry Smith     Note:
1288bb7acecfSBarry Smith     This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1289bb7acecfSBarry Smith     matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12903ad4599aSBarry Smith 
1291bb7acecfSBarry Smith .seealso: `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12923ad4599aSBarry Smith 
12933ad4599aSBarry Smith @*/
1294*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1295*d71ae5a4SJacob Faibussowitsch {
12963ad4599aSBarry Smith   PetscFunctionBegin;
1297a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1298a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12995a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13009566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1301dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13029566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13033ad4599aSBarry Smith   PetscFunctionReturn(0);
13043ad4599aSBarry Smith }
13053ad4599aSBarry Smith 
130647c6ae99SBarry Smith /*@
1307bb7acecfSBarry Smith     DMCreateInjection - Gets injection matrix between two `DM` objects. This is an operator that applied to a vector obtained with
1308bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting the values
1309bb7acecfSBarry Smith     on the coarse grid points. This compares to the operator obtained by `DMCreateRestriction()` or the transpose of the operator obtained
1310bb7acecfSBarry Smith     by `DMCreateInterpolation()` that uses a "local weighted average" of the values around the coarse grid point as the coarse grid value.
131147c6ae99SBarry Smith 
1312a5bc1bf3SBarry Smith     Collective on dac
131347c6ae99SBarry Smith 
1314d8d19677SJose E. Roman     Input Parameters:
1315bb7acecfSBarry Smith +   dac - the `DM` object
1316bb7acecfSBarry Smith -   daf - the second, finer `DM` object
131747c6ae99SBarry Smith 
131847c6ae99SBarry Smith     Output Parameter:
13196dbf9973SLawrence Mitchell .   mat - the injection
132047c6ae99SBarry Smith 
132147c6ae99SBarry Smith     Level: developer
132247c6ae99SBarry Smith 
1323bb7acecfSBarry Smith    Note:
1324bb7acecfSBarry 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
1325bb7acecfSBarry Smith         `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
132685afcc9aSBarry Smith 
1327bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1328bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
132947c6ae99SBarry Smith 
133047c6ae99SBarry Smith @*/
1331*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1332*d71ae5a4SJacob Faibussowitsch {
133347c6ae99SBarry Smith   PetscFunctionBegin;
1334a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1335a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13365a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13379566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1338dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13399566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
134047c6ae99SBarry Smith   PetscFunctionReturn(0);
134147c6ae99SBarry Smith }
134247c6ae99SBarry Smith 
1343b412c318SBarry Smith /*@
1344bb7acecfSBarry 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
1345bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1346bd041c0cSMatthew G. Knepley 
1347a5bc1bf3SBarry Smith   Collective on dac
1348bd041c0cSMatthew G. Knepley 
1349d8d19677SJose E. Roman   Input Parameters:
1350bb7acecfSBarry Smith + dmc - the target `DM` object
1351bb7acecfSBarry Smith - dmf - the source `DM` object
1352bd041c0cSMatthew G. Knepley 
1353bd041c0cSMatthew G. Knepley   Output Parameter:
1354b4937a87SMatthew G. Knepley . mat - the mass matrix
1355bd041c0cSMatthew G. Knepley 
1356bd041c0cSMatthew G. Knepley   Level: developer
1357bd041c0cSMatthew G. Knepley 
1358bb7acecfSBarry Smith   Notes:
1359bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1360bb7acecfSBarry Smith 
1361bb7acecfSBarry 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()`
1362bb7acecfSBarry Smith 
1363bb7acecfSBarry Smith .seealso: `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1364bd041c0cSMatthew G. Knepley @*/
1365*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1366*d71ae5a4SJacob Faibussowitsch {
1367bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1368b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1369b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13705a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13715b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1372dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13735b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
1374b4937a87SMatthew G. Knepley   PetscFunctionReturn(0);
1375b4937a87SMatthew G. Knepley }
1376b4937a87SMatthew G. Knepley 
1377b4937a87SMatthew G. Knepley /*@
1378bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1379b4937a87SMatthew G. Knepley 
1380b4937a87SMatthew G. Knepley   Collective on dm
1381b4937a87SMatthew G. Knepley 
1382b4937a87SMatthew G. Knepley   Input Parameter:
1383bb7acecfSBarry Smith . dm - the `DM` object
1384b4937a87SMatthew G. Knepley 
1385b4937a87SMatthew G. Knepley   Output Parameter:
1386bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1387b4937a87SMatthew G. Knepley 
1388b4937a87SMatthew G. Knepley   Level: developer
1389b4937a87SMatthew G. Knepley 
1390bb7acecfSBarry Smith   Note:
1391bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1392bb7acecfSBarry Smith 
1393bb7acecfSBarry Smith .seealso: `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1394b4937a87SMatthew G. Knepley @*/
1395*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1396*d71ae5a4SJacob Faibussowitsch {
1397b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1398b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1399b4937a87SMatthew G. Knepley   PetscValidPointer(lm, 2);
1400dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
1401bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1402bd041c0cSMatthew G. Knepley }
1403bd041c0cSMatthew G. Knepley 
1404bd041c0cSMatthew G. Knepley /*@
1405bb7acecfSBarry Smith     DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1406bb7acecfSBarry Smith     of a PDE on the `DM`.
140747c6ae99SBarry Smith 
1408d083f849SBarry Smith     Collective on dm
140947c6ae99SBarry Smith 
1410d8d19677SJose E. Roman     Input Parameters:
1411bb7acecfSBarry Smith +   dm - the `DM` object
1412bb7acecfSBarry Smith -   ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
141347c6ae99SBarry Smith 
141447c6ae99SBarry Smith     Output Parameter:
141547c6ae99SBarry Smith .   coloring - the coloring
141647c6ae99SBarry Smith 
1417ec5066bdSBarry Smith     Notes:
1418bb7acecfSBarry 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
1419bb7acecfSBarry Smith     matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1420ec5066bdSBarry Smith 
1421bb7acecfSBarry Smith     This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
1422ec5066bdSBarry Smith 
142347c6ae99SBarry Smith     Level: developer
142447c6ae99SBarry Smith 
1425bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
142647c6ae99SBarry Smith 
1427aab9d709SJed Brown @*/
1428*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1429*d71ae5a4SJacob Faibussowitsch {
143047c6ae99SBarry Smith   PetscFunctionBegin;
1431171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14325a84ad33SLisandro Dalcin   PetscValidPointer(coloring, 3);
1433dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
143447c6ae99SBarry Smith   PetscFunctionReturn(0);
143547c6ae99SBarry Smith }
143647c6ae99SBarry Smith 
1437b412c318SBarry Smith /*@
1438bb7acecfSBarry Smith     DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
143947c6ae99SBarry Smith 
1440d083f849SBarry Smith     Collective on dm
144147c6ae99SBarry Smith 
144247c6ae99SBarry Smith     Input Parameter:
1443bb7acecfSBarry Smith .   dm - the `DM` object
144447c6ae99SBarry Smith 
144547c6ae99SBarry Smith     Output Parameter:
144647c6ae99SBarry Smith .   mat - the empty Jacobian
144747c6ae99SBarry Smith 
1448073dac72SJed Brown     Level: beginner
144947c6ae99SBarry Smith 
1450f27dd7c6SMatthew G. Knepley     Options Database Keys:
1451bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1452f27dd7c6SMatthew G. Knepley 
145395452b02SPatrick Sanan     Notes:
145495452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
145594013140SBarry Smith     do not need to do it yourself.
145694013140SBarry Smith 
145794013140SBarry Smith     By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1458bb7acecfSBarry Smith     the nonzero pattern call `DMSetMatrixPreallocateOnly()`
145994013140SBarry Smith 
1460bb7acecfSBarry Smith     For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
146194013140SBarry Smith     internally by PETSc.
146294013140SBarry Smith 
1463bb7acecfSBarry Smith     For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1464bb7acecfSBarry Smith     `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
146594013140SBarry Smith 
1466bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
146747c6ae99SBarry Smith 
1468aab9d709SJed Brown @*/
1469*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1470*d71ae5a4SJacob Faibussowitsch {
147147c6ae99SBarry Smith   PetscFunctionBegin;
1472171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1473064a246eSJacob Faibussowitsch   PetscValidPointer(mat, 2);
14749566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14759566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1476dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
147776bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1478c6b011d8SStefano Zampini     DM mdm;
1479c6b011d8SStefano Zampini 
14809566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14817a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1482c6b011d8SStefano Zampini   }
1483e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1484e5e52638SMatthew G. Knepley   if (dm->Nf) {
1485e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1486649ef022SMatthew Knepley     PetscInt     Nf, f;
1487e571a35bSMatthew G. Knepley 
14889566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1489649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1490649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14919566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14929566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14939566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1494649ef022SMatthew Knepley         break;
1495e571a35bSMatthew G. Knepley       }
1496649ef022SMatthew Knepley     }
1497649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1498649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
14999566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15009566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15019566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1502e571a35bSMatthew G. Knepley       }
1503e571a35bSMatthew G. Knepley     }
1504e571a35bSMatthew G. Knepley   }
15059566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
150647c6ae99SBarry Smith   PetscFunctionReturn(0);
150747c6ae99SBarry Smith }
150847c6ae99SBarry Smith 
1509732e2eb9SMatthew G Knepley /*@
1510bb7acecfSBarry Smith   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and `ISLocalToGlobalMapping` will be
1511bb7acecfSBarry Smith   properly set, but the data structures to store values in the matrices will not be preallocated. This is most useful to reduce initialization costs when
1512bb7acecfSBarry Smith   `MatSetPreallocationCOO()` and `MatSetValuesCOO()` will be used.
1513aa0f6e3cSJed Brown 
1514aa0f6e3cSJed Brown   Logically Collective on dm
1515aa0f6e3cSJed Brown 
1516aa0f6e3cSJed Brown   Input Parameters:
1517bb7acecfSBarry Smith + dm - the `DM`
1518bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1519aa0f6e3cSJed Brown 
1520aa0f6e3cSJed Brown   Level: developer
1521aa0f6e3cSJed Brown 
1522bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1523aa0f6e3cSJed Brown @*/
1524*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1525*d71ae5a4SJacob Faibussowitsch {
1526aa0f6e3cSJed Brown   PetscFunctionBegin;
1527aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1528aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
1529aa0f6e3cSJed Brown   PetscFunctionReturn(0);
1530aa0f6e3cSJed Brown }
1531aa0f6e3cSJed Brown 
1532aa0f6e3cSJed Brown /*@
1533bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1534732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1535732e2eb9SMatthew G Knepley 
1536d083f849SBarry Smith   Logically Collective on dm
1537732e2eb9SMatthew G Knepley 
1538d8d19677SJose E. Roman   Input Parameters:
1539bb7acecfSBarry Smith + dm - the `DM`
1540bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1541732e2eb9SMatthew G Knepley 
1542732e2eb9SMatthew G Knepley   Level: developer
1543f27dd7c6SMatthew G. Knepley 
1544f27dd7c6SMatthew G. Knepley   Options Database Keys:
1545bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1546f27dd7c6SMatthew G. Knepley 
1547bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1548732e2eb9SMatthew G Knepley @*/
1549*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1550*d71ae5a4SJacob Faibussowitsch {
1551732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1552732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1553732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1554732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1555732e2eb9SMatthew G Knepley }
1556732e2eb9SMatthew G Knepley 
1557b06ff27eSHong Zhang /*@
1558bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1559bb7acecfSBarry Smith     but the array for numerical values will not be allocated.
1560b06ff27eSHong Zhang 
1561d083f849SBarry Smith   Logically Collective on dm
1562b06ff27eSHong Zhang 
1563d8d19677SJose E. Roman   Input Parameters:
1564bb7acecfSBarry Smith + dm - the `DM`
1565bb7acecfSBarry Smith - only - `PETSC_TRUE` if you only want matrix stucture
1566b06ff27eSHong Zhang 
1567b06ff27eSHong Zhang   Level: developer
1568bb7acecfSBarry Smith 
1569bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1570b06ff27eSHong Zhang @*/
1571*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1572*d71ae5a4SJacob Faibussowitsch {
1573b06ff27eSHong Zhang   PetscFunctionBegin;
1574b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1575b06ff27eSHong Zhang   dm->structure_only = only;
1576b06ff27eSHong Zhang   PetscFunctionReturn(0);
1577b06ff27eSHong Zhang }
1578b06ff27eSHong Zhang 
1579a89ea682SMatthew G Knepley /*@C
1580bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1581a89ea682SMatthew G Knepley 
1582a89ea682SMatthew G Knepley   Not Collective
1583a89ea682SMatthew G Knepley 
1584a89ea682SMatthew G Knepley   Input Parameters:
1585bb7acecfSBarry Smith + dm - the `DM` object
1586a5b23f4aSJose E. Roman . count - The minimum size
1587bb7acecfSBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, or MPIU_INT)
1588a89ea682SMatthew G Knepley 
1589a89ea682SMatthew G Knepley   Output Parameter:
1590a89ea682SMatthew G Knepley . array - the work array
1591a89ea682SMatthew G Knepley 
1592a89ea682SMatthew G Knepley   Level: developer
1593a89ea682SMatthew G Knepley 
1594bb7acecfSBarry Smith   Note:
1595bb7acecfSBarry Smith   A `DM` may stash the array between instantations so using this routine may be more efficient than calling `PetscMalloc()`
1596bb7acecfSBarry Smith 
1597bb7acecfSBarry Smith   The array may contain nonzero values
1598bb7acecfSBarry Smith 
1599bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1600a89ea682SMatthew G Knepley @*/
1601*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1602*d71ae5a4SJacob Faibussowitsch {
1603aa1993deSMatthew G Knepley   DMWorkLink  link;
160469291d52SBarry Smith   PetscMPIInt dsize;
1605a89ea682SMatthew G Knepley 
1606a89ea682SMatthew G Knepley   PetscFunctionBegin;
1607a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1608aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1609aa1993deSMatthew G Knepley   if (dm->workin) {
1610aa1993deSMatthew G Knepley     link       = dm->workin;
1611aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1612aa1993deSMatthew G Knepley   } else {
16134dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1614a89ea682SMatthew G Knepley   }
16159566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dtype, &dsize));
16165056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16179566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16189566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1619854ce69bSBarry Smith     link->bytes = dsize * count;
1620aa1993deSMatthew G Knepley   }
1621aa1993deSMatthew G Knepley   link->next  = dm->workout;
1622aa1993deSMatthew G Knepley   dm->workout = link;
1623cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
162400d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
162500d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
162600d952a4SJed Brown #endif
1627aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
1628a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1629a89ea682SMatthew G Knepley }
1630a89ea682SMatthew G Knepley 
1631aa1993deSMatthew G Knepley /*@C
1632bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1633aa1993deSMatthew G Knepley 
1634aa1993deSMatthew G Knepley   Not Collective
1635aa1993deSMatthew G Knepley 
1636aa1993deSMatthew G Knepley   Input Parameters:
1637bb7acecfSBarry Smith + dm - the `DM` object
1638a5b23f4aSJose E. Roman . count - The minimum size
163969291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1640aa1993deSMatthew G Knepley 
1641aa1993deSMatthew G Knepley   Output Parameter:
1642aa1993deSMatthew G Knepley . array - the work array
1643aa1993deSMatthew G Knepley 
1644aa1993deSMatthew G Knepley   Level: developer
1645aa1993deSMatthew G Knepley 
164695452b02SPatrick Sanan   Developer Notes:
1647bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1648147403d9SBarry Smith 
1649bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1650aa1993deSMatthew G Knepley @*/
1651*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1652*d71ae5a4SJacob Faibussowitsch {
1653aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1654aa1993deSMatthew G Knepley 
1655aa1993deSMatthew G Knepley   PetscFunctionBegin;
1656aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1657aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1658aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1659aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1660aa1993deSMatthew G Knepley       *p            = link->next;
1661aa1993deSMatthew G Knepley       link->next    = dm->workin;
1662aa1993deSMatthew G Knepley       dm->workin    = link;
16630298fd71SBarry Smith       *(void **)mem = NULL;
1664aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1665aa1993deSMatthew G Knepley     }
1666aa1993deSMatthew G Knepley   }
1667aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1668aa1993deSMatthew G Knepley }
1669e7c4fc90SDmitry Karpeev 
16708cda7954SMatthew G. Knepley /*@C
1671bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1672bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
16738cda7954SMatthew G. Knepley 
1674bb7acecfSBarry Smith   Logically collective on dm
16758cda7954SMatthew G. Knepley 
16768cda7954SMatthew G. Knepley   Input Parameters:
1677bb7acecfSBarry Smith + dm     - The `DM`
16788cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16798cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
16808cda7954SMatthew G. Knepley 
1681147403d9SBarry Smith   Calling sequence of nullsp:
1682147403d9SBarry Smith .vb
1683147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1684147403d9SBarry Smith .ve
1685bb7acecfSBarry Smith +  dm        - The present `DM`
1686bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1687147403d9SBarry Smith .  field     - The field number in dm
1688147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16898cda7954SMatthew G. Knepley 
169049762cbcSSatish Balay   Level: intermediate
169149762cbcSSatish Balay 
1692bb7acecfSBarry Smith   Fortran Notes:
1693bb7acecfSBarry Smith   This function is not available from Fortran.
1694bb7acecfSBarry Smith 
1695bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1696147403d9SBarry Smith @*/
1697*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1698*d71ae5a4SJacob Faibussowitsch {
1699435a35e8SMatthew G Knepley   PetscFunctionBegin;
1700435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17017a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1702435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1703435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1704435a35e8SMatthew G Knepley }
1705435a35e8SMatthew G Knepley 
17068cda7954SMatthew G. Knepley /*@C
1707bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17088cda7954SMatthew G. Knepley 
17098cda7954SMatthew G. Knepley   Not collective
17108cda7954SMatthew G. Knepley 
17118cda7954SMatthew G. Knepley   Input Parameters:
1712bb7acecfSBarry Smith + dm     - The `DM`
17138cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17148cda7954SMatthew G. Knepley 
17158cda7954SMatthew G. Knepley   Output Parameter:
17168cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17178cda7954SMatthew G. Knepley 
1718147403d9SBarry Smith   Calling sequence of nullsp:
1719147403d9SBarry Smith .vb
1720147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1721147403d9SBarry Smith .ve
1722147403d9SBarry Smith +  dm        - The present DM
1723147403d9SBarry Smith .  origField - The field number given above, in the original DM
1724147403d9SBarry Smith .  field     - The field number in dm
1725147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17268cda7954SMatthew G. Knepley 
1727bb7acecfSBarry Smith   Fortran Note:
1728bb7acecfSBarry Smith   This function is not available from Fortran.
17298cda7954SMatthew G. Knepley 
173049762cbcSSatish Balay    Level: intermediate
173149762cbcSSatish Balay 
1732bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1733147403d9SBarry Smith @*/
1734*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1735*d71ae5a4SJacob Faibussowitsch {
17360a50eb56SMatthew G. Knepley   PetscFunctionBegin;
17370a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1738f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
17397a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
17400a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
17410a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
17420a50eb56SMatthew G. Knepley }
17430a50eb56SMatthew G. Knepley 
17448cda7954SMatthew G. Knepley /*@C
1745bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
17468cda7954SMatthew G. Knepley 
1747bb7acecfSBarry Smith   Logically collective on dm
17488cda7954SMatthew G. Knepley 
17498cda7954SMatthew G. Knepley   Input Parameters:
1750bb7acecfSBarry Smith + dm     - The `DM`
17518cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17528cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
17538cda7954SMatthew G. Knepley 
1754147403d9SBarry Smith   Calling sequence of nullsp:
1755147403d9SBarry Smith .vb
1756147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1757147403d9SBarry Smith .ve
1758bb7acecfSBarry Smith +  dm        - The present `DM`
1759bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1760147403d9SBarry Smith .  field     - The field number in dm
1761147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17628cda7954SMatthew G. Knepley 
1763bb7acecfSBarry Smith   Fortran Note:
1764bb7acecfSBarry Smith   This function is not available from Fortran.
17658cda7954SMatthew G. Knepley 
176649762cbcSSatish Balay    Level: intermediate
176749762cbcSSatish Balay 
1768bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1769bb7acecfSBarry Smith           `MatNullSpace`
1770147403d9SBarry Smith @*/
1771*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1772*d71ae5a4SJacob Faibussowitsch {
1773f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1774f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17757a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1776f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1777f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1778f9d4088aSMatthew G. Knepley }
1779f9d4088aSMatthew G. Knepley 
17808cda7954SMatthew G. Knepley /*@C
1781bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
17828cda7954SMatthew G. Knepley 
17838cda7954SMatthew G. Knepley   Not collective
17848cda7954SMatthew G. Knepley 
17858cda7954SMatthew G. Knepley   Input Parameters:
1786bb7acecfSBarry Smith + dm     - The `DM`
17878cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17888cda7954SMatthew G. Knepley 
17898cda7954SMatthew G. Knepley   Output Parameter:
17908cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
17918cda7954SMatthew G. Knepley 
1792147403d9SBarry Smith   Calling sequence of nullsp:
1793147403d9SBarry Smith .vb
1794147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1795147403d9SBarry Smith .ve
1796bb7acecfSBarry Smith +  dm        - The present `DM`
1797bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1798147403d9SBarry Smith .  field     - The field number in dm
1799147403d9SBarry Smith -  nullSpace - The nullspace for the given field
18008cda7954SMatthew G. Knepley 
1801bb7acecfSBarry Smith   Fortran Note:
1802bb7acecfSBarry Smith   This function is not available from Fortran.
18038cda7954SMatthew G. Knepley 
180449762cbcSSatish Balay    Level: intermediate
180549762cbcSSatish Balay 
1806bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1807bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1808147403d9SBarry Smith @*/
1809*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1810*d71ae5a4SJacob Faibussowitsch {
1811f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1812f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1813f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
18147a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1815f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1816f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1817f9d4088aSMatthew G. Knepley }
1818f9d4088aSMatthew G. Knepley 
18194f3b5142SJed Brown /*@C
1820bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18214d343eeaSMatthew G Knepley 
18224d343eeaSMatthew G Knepley   Not collective
18234d343eeaSMatthew G Knepley 
18244d343eeaSMatthew G Knepley   Input Parameter:
1825bb7acecfSBarry Smith . dm - the `DM` object
18264d343eeaSMatthew G Knepley 
18274d343eeaSMatthew G Knepley   Output Parameters:
18280298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
1829bb7acecfSBarry Smith . fieldNames - The number of each field (or NULL if not requested)
18300298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
18314d343eeaSMatthew G Knepley 
18324d343eeaSMatthew G Knepley   Level: intermediate
18334d343eeaSMatthew G Knepley 
1834bb7acecfSBarry Smith   Note:
183521c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1836bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1837bb7acecfSBarry Smith   `PetscFree()`.
183821c9b008SJed Brown 
1839bb7acecfSBarry Smith   Fortran Note:
1840bb7acecfSBarry Smith   Not available in Fortran.
1841bb7acecfSBarry Smith 
1842bb7acecfSBarry Smith   Developer Note:
1843bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1844bb7acecfSBarry Smith   likely be removed.
1845bb7acecfSBarry Smith 
1846bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1847bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18484d343eeaSMatthew G Knepley @*/
1849*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1850*d71ae5a4SJacob Faibussowitsch {
185137d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
18524d343eeaSMatthew G Knepley 
18534d343eeaSMatthew G Knepley   PetscFunctionBegin;
18544d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
185569ca1f37SDmitry Karpeev   if (numFields) {
1856534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields, 2);
185769ca1f37SDmitry Karpeev     *numFields = 0;
185869ca1f37SDmitry Karpeev   }
185937d0c07bSMatthew G Knepley   if (fieldNames) {
186037d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames, 3);
18610298fd71SBarry Smith     *fieldNames = NULL;
186269ca1f37SDmitry Karpeev   }
186369ca1f37SDmitry Karpeev   if (fields) {
186469ca1f37SDmitry Karpeev     PetscValidPointer(fields, 4);
18650298fd71SBarry Smith     *fields = NULL;
186669ca1f37SDmitry Karpeev   }
18679566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
186837d0c07bSMatthew G Knepley   if (section) {
18693a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
187037d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
187137d0c07bSMatthew G Knepley 
18729566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
18739566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
18749566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
18759566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
187637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
187737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
18789566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
187937d0c07bSMatthew G Knepley     }
188037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
188137d0c07bSMatthew G Knepley       PetscInt gdof;
188237d0c07bSMatthew G Knepley 
18839566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
188437d0c07bSMatthew G Knepley       if (gdof > 0) {
188537d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
18863a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
188737d0c07bSMatthew G Knepley 
18889566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
18899566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
18903a544194SStefano Zampini           fpdof = fdof - fcdof;
18913a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
18923a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
18933a544194SStefano Zampini             fieldNc[f] = 1;
18943a544194SStefano Zampini           }
18953a544194SStefano Zampini           fieldSizes[f] += fpdof;
189637d0c07bSMatthew G Knepley         }
189737d0c07bSMatthew G Knepley       }
189837d0c07bSMatthew G Knepley     }
189937d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19009566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
190137d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
190237d0c07bSMatthew G Knepley     }
190337d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
190437d0c07bSMatthew G Knepley       PetscInt gdof, goff;
190537d0c07bSMatthew G Knepley 
19069566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
190737d0c07bSMatthew G Knepley       if (gdof > 0) {
19089566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
190937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
191037d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
191137d0c07bSMatthew G Knepley 
19129566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19139566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1914ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
191537d0c07bSMatthew G Knepley         }
191637d0c07bSMatthew G Knepley       }
191737d0c07bSMatthew G Knepley     }
19188865f1eaSKarl Rupp     if (numFields) *numFields = nF;
191937d0c07bSMatthew G Knepley     if (fieldNames) {
19209566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
192137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
192237d0c07bSMatthew G Knepley         const char *fieldName;
192337d0c07bSMatthew G Knepley 
19249566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19259566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
192637d0c07bSMatthew G Knepley       }
192737d0c07bSMatthew G Knepley     }
192837d0c07bSMatthew G Knepley     if (fields) {
19299566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
193037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19313a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19323a544194SStefano Zampini 
19339566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19343a544194SStefano Zampini         in[0] = -fieldNc[f];
19353a544194SStefano Zampini         in[1] = fieldNc[f];
19361c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19373a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19389566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
193937d0c07bSMatthew G Knepley       }
194037d0c07bSMatthew G Knepley     }
19419566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1942dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19434d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
19444d343eeaSMatthew G Knepley }
19454d343eeaSMatthew G Knepley 
194616621825SDmitry Karpeev /*@C
1947bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1948bb7acecfSBarry Smith                           corresponding to different fields: each `IS` contains the global indices of the dofs of the
1949bb7acecfSBarry Smith                           corresponding field, defined by `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
1950bb7acecfSBarry Smith                           The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
1951e7c4fc90SDmitry Karpeev 
1952e7c4fc90SDmitry Karpeev   Not collective
1953e7c4fc90SDmitry Karpeev 
1954e7c4fc90SDmitry Karpeev   Input Parameter:
1955bb7acecfSBarry Smith . dm - the `DM` object
1956e7c4fc90SDmitry Karpeev 
1957e7c4fc90SDmitry Karpeev   Output Parameters:
1958bb7acecfSBarry Smith + len       - The number of fields (or NULL if not requested)
19590298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
19600298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
1961bb7acecfSBarry Smith - dmlist    - The `DM`s for each field subproblem (or NULL, if not requested; if NULL is returned, no `DM`s are defined)
1962e7c4fc90SDmitry Karpeev 
1963e7c4fc90SDmitry Karpeev   Level: intermediate
1964e7c4fc90SDmitry Karpeev 
1965bb7acecfSBarry Smith   Note:
1966e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1967bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
1968bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
1969e7c4fc90SDmitry Karpeev 
1970bb7acecfSBarry Smith   Fortran Note:
1971bb7acecfSBarry Smith   Not available in Fortran.
1972bb7acecfSBarry Smith 
1973bb7acecfSBarry Smith   Developer Note:
1974bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
1975bb7acecfSBarry Smith 
1976bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
1977e7c4fc90SDmitry Karpeev @*/
1978*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1979*d71ae5a4SJacob Faibussowitsch {
1980e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1981e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19828865f1eaSKarl Rupp   if (len) {
1983534a8f05SLisandro Dalcin     PetscValidIntPointer(len, 2);
19848865f1eaSKarl Rupp     *len = 0;
19858865f1eaSKarl Rupp   }
19868865f1eaSKarl Rupp   if (namelist) {
19878865f1eaSKarl Rupp     PetscValidPointer(namelist, 3);
1988ea78f98cSLisandro Dalcin     *namelist = NULL;
19898865f1eaSKarl Rupp   }
19908865f1eaSKarl Rupp   if (islist) {
19918865f1eaSKarl Rupp     PetscValidPointer(islist, 4);
1992ea78f98cSLisandro Dalcin     *islist = NULL;
19938865f1eaSKarl Rupp   }
19948865f1eaSKarl Rupp   if (dmlist) {
19958865f1eaSKarl Rupp     PetscValidPointer(dmlist, 5);
1996ea78f98cSLisandro Dalcin     *dmlist = NULL;
19978865f1eaSKarl Rupp   }
1998f3f0edfdSDmitry Karpeev   /*
1999f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2000f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2001f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2002f3f0edfdSDmitry Karpeev    */
20037a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
200416621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2005435a35e8SMatthew G Knepley     PetscSection section;
2006435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2007435a35e8SMatthew G Knepley 
20089566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20099566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2010435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2011f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20129566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20139566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20149566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2015435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2016435a35e8SMatthew G Knepley         const char *fieldName;
2017435a35e8SMatthew G Knepley 
20189566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
201903dc3394SMatthew G. Knepley         if (namelist) {
20209566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20219566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2022435a35e8SMatthew G Knepley         }
202303dc3394SMatthew G. Knepley       }
2024435a35e8SMatthew G Knepley     } else {
20259566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2026e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20270298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2028e7c4fc90SDmitry Karpeev     }
2029dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
203016621825SDmitry Karpeev   PetscFunctionReturn(0);
203116621825SDmitry Karpeev }
203216621825SDmitry Karpeev 
2033412a4547SAlexis Marboeuf /*@C
2034412a4547SAlexis Marboeuf   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
2035412a4547SAlexis Marboeuf                   The fields are defined by DMCreateFieldIS().
2036435a35e8SMatthew G Knepley 
2037435a35e8SMatthew G Knepley   Not collective
2038435a35e8SMatthew G Knepley 
2039435a35e8SMatthew G Knepley   Input Parameters:
2040bb7acecfSBarry Smith + dm        - The `DM `object
2041bb7acecfSBarry Smith . numFields - The number of fields to select
20422adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2043435a35e8SMatthew G Knepley 
2044435a35e8SMatthew G Knepley   Output Parameters:
2045bb7acecfSBarry Smith + is - The global indices for all the degrees of freedom in the new sub `DM`
2046bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2047435a35e8SMatthew G Knepley 
2048bb7acecfSBarry Smith   Note:
2049bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20505d3b26e6SMatthew G. Knepley 
2051435a35e8SMatthew G Knepley   Level: intermediate
2052435a35e8SMatthew G Knepley 
2053bb7acecfSBarry Smith .seealso: `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `DM`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2054435a35e8SMatthew G Knepley @*/
2055*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2056*d71ae5a4SJacob Faibussowitsch {
2057435a35e8SMatthew G Knepley   PetscFunctionBegin;
2058435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2059dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields, 3);
20608865f1eaSKarl Rupp   if (is) PetscValidPointer(is, 4);
20618865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm, 5);
2062dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
2063435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
2064435a35e8SMatthew G Knepley }
2065435a35e8SMatthew G Knepley 
20662adcc780SMatthew G. Knepley /*@C
2067bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
20682adcc780SMatthew G. Knepley 
20692adcc780SMatthew G. Knepley   Not collective
20702adcc780SMatthew G. Knepley 
2071d8d19677SJose E. Roman   Input Parameters:
2072bb7acecfSBarry Smith + dms - The `DM` objects
2073bb7acecfSBarry Smith - n - The number of `DM`s
20742adcc780SMatthew G. Knepley 
20752adcc780SMatthew G. Knepley   Output Parameters:
2076bb7acecfSBarry Smith + is - The global indices for each of subproblem within the super `DM`, or NULL
2077bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
20782adcc780SMatthew G. Knepley 
2079bb7acecfSBarry Smith   Note:
2080bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20815d3b26e6SMatthew G. Knepley 
20822adcc780SMatthew G. Knepley   Level: intermediate
20832adcc780SMatthew G. Knepley 
2084bb7acecfSBarry Smith .seealso: `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
20852adcc780SMatthew G. Knepley @*/
2086*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
2087*d71ae5a4SJacob Faibussowitsch {
20882adcc780SMatthew G. Knepley   PetscInt i;
20892adcc780SMatthew G. Knepley 
20902adcc780SMatthew G. Knepley   PetscFunctionBegin;
20912adcc780SMatthew G. Knepley   PetscValidPointer(dms, 1);
2092ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
20932adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is, 3);
2094a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm, 4);
2095bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2096bb7acecfSBarry Smith   if (n) {
2097b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2098dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
20992adcc780SMatthew G. Knepley   }
21002adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
21012adcc780SMatthew G. Knepley }
21022adcc780SMatthew G. Knepley 
210316621825SDmitry Karpeev /*@C
2104bb7acecfSBarry Smith   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a problem into subproblems
2105bb7acecfSBarry Smith                           corresponding to restrictions to pairs of nested subdomains: each `IS` contains the global
2106bb7acecfSBarry Smith                           indices of the dofs of the corresponding subdomains with in the dofs of the original `DM`.
2107bb7acecfSBarry Smith                           The inner subdomains conceptually define a nonoverlapping covering, while outer subdomains can overlap.
2108bb7acecfSBarry Smith                           The optional list of `DM`s define a `DM` for each subproblem.
210916621825SDmitry Karpeev 
211016621825SDmitry Karpeev   Not collective
211116621825SDmitry Karpeev 
211216621825SDmitry Karpeev   Input Parameter:
2113bb7acecfSBarry Smith . dm - the `DM` object
211416621825SDmitry Karpeev 
211516621825SDmitry Karpeev   Output Parameters:
2116bb7acecfSBarry Smith + n            - The number of subproblems in the domain decomposition (or NULL if not requested)
21170298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
21180298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
21190298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
2120bb7acecfSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no `DM`s are defined)
212116621825SDmitry Karpeev 
212216621825SDmitry Karpeev   Level: intermediate
212316621825SDmitry Karpeev 
2124bb7acecfSBarry Smith   Note:
212516621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2126bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2127bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
212816621825SDmitry Karpeev 
2129bb7acecfSBarry Smith   Questions:
2130bb7acecfSBarry Smith   The dmlist is for the inner subdomains or the outer subdomains or all subdomains?
2131bb7acecfSBarry Smith 
2132bb7acecfSBarry Smith .seealso: `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldDecomposition()`
213316621825SDmitry Karpeev @*/
2134*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2135*d71ae5a4SJacob Faibussowitsch {
2136be081cd6SPeter Brune   DMSubDomainHookLink link;
2137be081cd6SPeter Brune   PetscInt            i, l;
213816621825SDmitry Karpeev 
213916621825SDmitry Karpeev   PetscFunctionBegin;
214016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21419371c9d4SSatish Balay   if (n) {
21429371c9d4SSatish Balay     PetscValidIntPointer(n, 2);
21439371c9d4SSatish Balay     *n = 0;
21449371c9d4SSatish Balay   }
21459371c9d4SSatish Balay   if (namelist) {
21469371c9d4SSatish Balay     PetscValidPointer(namelist, 3);
21479371c9d4SSatish Balay     *namelist = NULL;
21489371c9d4SSatish Balay   }
21499371c9d4SSatish Balay   if (innerislist) {
21509371c9d4SSatish Balay     PetscValidPointer(innerislist, 4);
21519371c9d4SSatish Balay     *innerislist = NULL;
21529371c9d4SSatish Balay   }
21539371c9d4SSatish Balay   if (outerislist) {
21549371c9d4SSatish Balay     PetscValidPointer(outerislist, 5);
21559371c9d4SSatish Balay     *outerislist = NULL;
21569371c9d4SSatish Balay   }
21579371c9d4SSatish Balay   if (dmlist) {
21589371c9d4SSatish Balay     PetscValidPointer(dmlist, 6);
21599371c9d4SSatish Balay     *dmlist = NULL;
21609371c9d4SSatish Balay   }
2161f3f0edfdSDmitry Karpeev   /*
2162f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2163f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2164f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2165f3f0edfdSDmitry Karpeev    */
21667a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
216716621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2168dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
216914a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2170f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2171be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2172be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
21739566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2174be081cd6SPeter Brune         }
2175648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2176e7c4fc90SDmitry Karpeev       }
217714a18fd3SPeter Brune     }
2178bb7acecfSBarry Smith     if (n) *n = l;
217914a18fd3SPeter Brune   }
2180e30e807fSPeter Brune   PetscFunctionReturn(0);
2181e30e807fSPeter Brune }
2182e30e807fSPeter Brune 
2183e30e807fSPeter Brune /*@C
2184e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2185e30e807fSPeter Brune 
2186e30e807fSPeter Brune   Not collective
2187e30e807fSPeter Brune 
2188e30e807fSPeter Brune   Input Parameters:
2189bb7acecfSBarry Smith + dm - the `DM` object
2190e30e807fSPeter Brune . n  - the number of subdomain scatters
2191e30e807fSPeter Brune - subdms - the local subdomains
2192e30e807fSPeter Brune 
2193e30e807fSPeter Brune   Output Parameters:
21946b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2195e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2196e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2197e30e807fSPeter Brune 
2198bb7acecfSBarry Smith   Note:
2199bb7acecfSBarry Smith     This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2200e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2201e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2202e30e807fSPeter Brune   solution and residual data.
2203e30e807fSPeter Brune 
2204bb7acecfSBarry Smith   Questions:
2205bb7acecfSBarry Smith   Can the subdms input be anything or are they exactly the `DM` obtained from `DMCreateDomainDecomposition()`?
2206bb7acecfSBarry Smith 
2207e30e807fSPeter Brune   Level: developer
2208e30e807fSPeter Brune 
2209bb7acecfSBarry Smith .seealso: `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2210e30e807fSPeter Brune @*/
2211*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat)
2212*d71ae5a4SJacob Faibussowitsch {
2213e30e807fSPeter Brune   PetscFunctionBegin;
2214e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2215e30e807fSPeter Brune   PetscValidPointer(subdms, 3);
2216dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
2217e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2218e7c4fc90SDmitry Karpeev }
2219e7c4fc90SDmitry Karpeev 
222047c6ae99SBarry Smith /*@
2221bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
222247c6ae99SBarry Smith 
2223d083f849SBarry Smith   Collective on dm
222447c6ae99SBarry Smith 
2225d8d19677SJose E. Roman   Input Parameters:
2226bb7acecfSBarry Smith + dm   - the `DM` object
2227bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
222847c6ae99SBarry Smith 
222947c6ae99SBarry Smith   Output Parameter:
2230bb7acecfSBarry Smith . dmf - the refined `D`M, or NULL
2231ae0a1c52SMatthew G Knepley 
2232f27dd7c6SMatthew G. Knepley   Options Database Keys:
2233412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2234412e9a14SMatthew G. Knepley 
2235bb7acecfSBarry Smith   Note:
2236bb7acecfSBarry Smith   If no refinement was done, the return value is NULL
223747c6ae99SBarry Smith 
223847c6ae99SBarry Smith   Level: developer
223947c6ae99SBarry Smith 
2240bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
224147c6ae99SBarry Smith @*/
2242*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2243*d71ae5a4SJacob Faibussowitsch {
2244c833c3b5SJed Brown   DMRefineHookLink link;
224547c6ae99SBarry Smith 
224647c6ae99SBarry Smith   PetscFunctionBegin;
2247732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22489566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2249dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
22504057135bSMatthew G Knepley   if (*dmf) {
225143842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
22528865f1eaSKarl Rupp 
22539566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
22548865f1eaSKarl Rupp 
2255644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
22560598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2257656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
22588865f1eaSKarl Rupp 
22599566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2260c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
22611baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2262c833c3b5SJed Brown     }
2263c833c3b5SJed Brown   }
22649566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
2265c833c3b5SJed Brown   PetscFunctionReturn(0);
2266c833c3b5SJed Brown }
2267c833c3b5SJed Brown 
2268bb9467b5SJed Brown /*@C
2269c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2270c833c3b5SJed Brown 
2271bb7acecfSBarry Smith    Logically Collective on coarse
2272c833c3b5SJed Brown 
22734165533cSJose E. Roman    Input Parameters:
2274bb7acecfSBarry Smith +  coarse - `DM` on which to run a hook when interpolating to a finer level
2275bb7acecfSBarry Smith .  refinehook - function to run when setting up the finer level
2276bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels (once per `SNESSolve`())
22770298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2278c833c3b5SJed Brown 
2279c833c3b5SJed Brown    Calling sequence of refinehook:
2280c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2281c833c3b5SJed Brown 
2282bb7acecfSBarry Smith +  coarse - coarse level `DM`
2283bb7acecfSBarry Smith .  fine - fine level `DM` to interpolate problem to
2284c833c3b5SJed Brown -  ctx - optional user-defined function context
2285c833c3b5SJed Brown 
2286c833c3b5SJed Brown    Calling sequence for interphook:
2287c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2288c833c3b5SJed Brown 
2289bb7acecfSBarry Smith +  coarse - coarse level `DM`
2290c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2291bb7acecfSBarry Smith .  fine - fine level `DM` to update
2292c833c3b5SJed Brown -  ctx - optional user-defined function context
2293c833c3b5SJed Brown 
2294c833c3b5SJed Brown    Level: advanced
2295c833c3b5SJed Brown 
2296c833c3b5SJed Brown    Notes:
2297bb7acecfSBarry Smith    This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2298bb7acecfSBarry Smith    passed to fine grids while grid sequencing.
2299bb7acecfSBarry Smith 
2300bb7acecfSBarry Smith    The actual interpolation is done when `DMInterpolate()` is called.
2301c833c3b5SJed Brown 
2302c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2303c833c3b5SJed Brown 
2304bb7acecfSBarry Smith    Fortran Note:
2305bb7acecfSBarry Smith    This function is not available from Fortran.
2306bb9467b5SJed Brown 
2307bb7acecfSBarry Smith .seealso: `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2308c833c3b5SJed Brown @*/
2309*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2310*d71ae5a4SJacob Faibussowitsch {
2311c833c3b5SJed Brown   DMRefineHookLink link, *p;
2312c833c3b5SJed Brown 
2313c833c3b5SJed Brown   PetscFunctionBegin;
2314c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23153d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23163d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
23173d8e3701SJed Brown   }
23189566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2319c833c3b5SJed Brown   link->refinehook = refinehook;
2320c833c3b5SJed Brown   link->interphook = interphook;
2321c833c3b5SJed Brown   link->ctx        = ctx;
23220298fd71SBarry Smith   link->next       = NULL;
2323c833c3b5SJed Brown   *p               = link;
2324c833c3b5SJed Brown   PetscFunctionReturn(0);
2325c833c3b5SJed Brown }
2326c833c3b5SJed Brown 
23273d8e3701SJed Brown /*@C
2328bb7acecfSBarry Smith    DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2329bb7acecfSBarry Smith     a nonlinear problem to a finer grid
23303d8e3701SJed Brown 
2331bb7acecfSBarry Smith    Logically Collective on coarse
23323d8e3701SJed Brown 
23334165533cSJose E. Roman    Input Parameters:
2334bb7acecfSBarry Smith +  coarse - the `DM` on which to run a hook when restricting to a coarser level
2335bb7acecfSBarry Smith .  refinehook - function to run when setting up a finer level
2336bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels
23373d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
23383d8e3701SJed Brown 
23393d8e3701SJed Brown    Level: advanced
23403d8e3701SJed Brown 
2341bb7acecfSBarry Smith    Note:
23423d8e3701SJed Brown    This function does nothing if the hook is not in the list.
23433d8e3701SJed Brown 
2344bb7acecfSBarry Smith    Fortran Note:
2345bb7acecfSBarry Smith    This function is not available from Fortran.
23463d8e3701SJed Brown 
2347bb7acecfSBarry Smith .seealso: `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
23483d8e3701SJed Brown @*/
2349*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2350*d71ae5a4SJacob Faibussowitsch {
23513d8e3701SJed Brown   DMRefineHookLink link, *p;
23523d8e3701SJed Brown 
23533d8e3701SJed Brown   PetscFunctionBegin;
23543d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23553d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
23563d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
23573d8e3701SJed Brown       link = *p;
23583d8e3701SJed Brown       *p   = link->next;
23599566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
23603d8e3701SJed Brown       break;
23613d8e3701SJed Brown     }
23623d8e3701SJed Brown   }
23633d8e3701SJed Brown   PetscFunctionReturn(0);
23643d8e3701SJed Brown }
23653d8e3701SJed Brown 
2366c833c3b5SJed Brown /*@
2367bb7acecfSBarry Smith    DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2368c833c3b5SJed Brown 
2369c833c3b5SJed Brown    Collective if any hooks are
2370c833c3b5SJed Brown 
23714165533cSJose E. Roman    Input Parameters:
2372bb7acecfSBarry Smith +  coarse - coarser `DM` to use as a base
2373bb7acecfSBarry Smith .  interp - interpolation matrix, apply using `MatInterpolate()`
2374bb7acecfSBarry Smith -  fine - finer `DM` to update
2375c833c3b5SJed Brown 
2376c833c3b5SJed Brown    Level: developer
2377c833c3b5SJed Brown 
2378bb7acecfSBarry Smith    Developer Note:
2379bb7acecfSBarry Smith    This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2380bb7acecfSBarry Smith    an API with consistent terminology.
2381bb7acecfSBarry Smith 
2382bb7acecfSBarry Smith .seealso: `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2383c833c3b5SJed Brown @*/
2384*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2385*d71ae5a4SJacob Faibussowitsch {
2386c833c3b5SJed Brown   DMRefineHookLink link;
2387c833c3b5SJed Brown 
2388c833c3b5SJed Brown   PetscFunctionBegin;
2389c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
23901baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
23914057135bSMatthew G Knepley   }
239247c6ae99SBarry Smith   PetscFunctionReturn(0);
239347c6ae99SBarry Smith }
239447c6ae99SBarry Smith 
2395eb3f98d2SBarry Smith /*@
23961f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
23971f3379b2SToby Isaac 
2398bb7acecfSBarry Smith    Collective on dm
23991f3379b2SToby Isaac 
24004165533cSJose E. Roman    Input Parameters:
2401bb7acecfSBarry Smith +  coarse - coarse `DM`
2402bb7acecfSBarry Smith .  fine   - fine `DM`
2403bb7acecfSBarry Smith .  interp - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2404bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2405bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24061f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
24071f3379b2SToby Isaac 
24084165533cSJose E. Roman    Output Parameter:
24091f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
24101f3379b2SToby Isaac 
24111f3379b2SToby Isaac    Level: developer
24121f3379b2SToby Isaac 
2413bb7acecfSBarry Smith    Note:
2414bb7acecfSBarry Smith    This function exists because the interpolation of a solution vector between meshes is not always a linear
24151f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24161f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24171f3379b2SToby Isaac    slope-limiting reconstruction.
24181f3379b2SToby Isaac 
2419bb7acecfSBarry Smith    Developer Note:
2420bb7acecfSBarry Smith    This doesn't just interpolate "solutions" so its API name is questionable.
2421bb7acecfSBarry Smith 
2422bb7acecfSBarry Smith .seealso: `DMInterpolate()`, `DMCreateInterpolation()`
24231f3379b2SToby Isaac @*/
2424*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2425*d71ae5a4SJacob Faibussowitsch {
24261f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24271f3379b2SToby Isaac 
24281f3379b2SToby Isaac   PetscFunctionBegin;
24291f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24301f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24311f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24321f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24331f3379b2SToby Isaac 
24349566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24351f3379b2SToby Isaac   if (interpsol) {
24369566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24371f3379b2SToby Isaac   } else if (interp) {
24389566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
243998921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24401f3379b2SToby Isaac   PetscFunctionReturn(0);
24411f3379b2SToby Isaac }
24421f3379b2SToby Isaac 
24431f3379b2SToby Isaac /*@
2444bb7acecfSBarry Smith     DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2445eb3f98d2SBarry Smith 
2446eb3f98d2SBarry Smith     Not Collective
2447eb3f98d2SBarry Smith 
2448eb3f98d2SBarry Smith     Input Parameter:
2449bb7acecfSBarry Smith .   dm - the `DM` object
2450eb3f98d2SBarry Smith 
2451eb3f98d2SBarry Smith     Output Parameter:
2452eb3f98d2SBarry Smith .   level - number of refinements
2453eb3f98d2SBarry Smith 
2454eb3f98d2SBarry Smith     Level: developer
2455eb3f98d2SBarry Smith 
2456bb7acecfSBarry Smith     Note:
2457bb7acecfSBarry Smith     This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2458bb7acecfSBarry Smith 
2459bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2460eb3f98d2SBarry Smith 
2461eb3f98d2SBarry Smith @*/
2462*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2463*d71ae5a4SJacob Faibussowitsch {
2464eb3f98d2SBarry Smith   PetscFunctionBegin;
2465eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2466eb3f98d2SBarry Smith   *level = dm->levelup;
2467eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2468eb3f98d2SBarry Smith }
2469eb3f98d2SBarry Smith 
2470fef3a512SBarry Smith /*@
2471bb7acecfSBarry Smith     DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2472fef3a512SBarry Smith 
2473fef3a512SBarry Smith     Not Collective
2474fef3a512SBarry Smith 
2475d8d19677SJose E. Roman     Input Parameters:
2476bb7acecfSBarry Smith +   dm - the `DM` object
2477fef3a512SBarry Smith -   level - number of refinements
2478fef3a512SBarry Smith 
2479fef3a512SBarry Smith     Level: advanced
2480fef3a512SBarry Smith 
248195452b02SPatrick Sanan     Notes:
2482bb7acecfSBarry Smith     This value is used by `PCMG` to determine how many multigrid levels to use
2483fef3a512SBarry Smith 
2484bb7acecfSBarry Smith     The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2485bb7acecfSBarry Smith 
2486bb7acecfSBarry Smith .seealso: `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2487fef3a512SBarry Smith 
2488fef3a512SBarry Smith @*/
2489*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2490*d71ae5a4SJacob Faibussowitsch {
2491fef3a512SBarry Smith   PetscFunctionBegin;
2492fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2493fef3a512SBarry Smith   dm->levelup = level;
2494fef3a512SBarry Smith   PetscFunctionReturn(0);
2495fef3a512SBarry Smith }
2496fef3a512SBarry Smith 
2497d410b0cfSMatthew G. Knepley /*@
2498bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2499d410b0cfSMatthew G. Knepley 
2500d410b0cfSMatthew G. Knepley   Collective on dm
2501d410b0cfSMatthew G. Knepley 
2502f1a722f8SMatthew G. Knepley   Input Parameters:
2503bb7acecfSBarry Smith + dm     - the `DM` object
2504d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2505d410b0cfSMatthew G. Knepley 
2506d410b0cfSMatthew G. Knepley   Output Parameter:
2507bb7acecfSBarry Smith . dme - the extruded `DM`, or NULL
2508d410b0cfSMatthew G. Knepley 
2509bb7acecfSBarry Smith   Note:
2510bb7acecfSBarry Smith   If no extrusion was done, the return value is NULL
2511d410b0cfSMatthew G. Knepley 
2512d410b0cfSMatthew G. Knepley   Level: developer
2513d410b0cfSMatthew G. Knepley 
2514bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2515d410b0cfSMatthew G. Knepley @*/
2516*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2517*d71ae5a4SJacob Faibussowitsch {
2518d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2519d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2520dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2521d410b0cfSMatthew G. Knepley   if (*dme) {
2522d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25239566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2524d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25259566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2526d410b0cfSMatthew G. Knepley   }
2527d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2528d410b0cfSMatthew G. Knepley }
2529d410b0cfSMatthew G. Knepley 
2530*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2531*d71ae5a4SJacob Faibussowitsch {
2532ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2533ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2534ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2535ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2536ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2537ca3d3a14SMatthew G. Knepley }
2538ca3d3a14SMatthew G. Knepley 
2539*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2540*d71ae5a4SJacob Faibussowitsch {
2541ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2542ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2543ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2544ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2545ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2546ca3d3a14SMatthew G. Knepley }
2547ca3d3a14SMatthew G. Knepley 
2548ca3d3a14SMatthew G. Knepley /*@
2549bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2550ca3d3a14SMatthew G. Knepley 
2551ca3d3a14SMatthew G. Knepley   Input Parameter:
2552ca3d3a14SMatthew G. Knepley . dm - The DM
2553ca3d3a14SMatthew G. Knepley 
2554ca3d3a14SMatthew G. Knepley   Output Parameter:
2555ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2556ca3d3a14SMatthew G. Knepley 
2557ca3d3a14SMatthew G. Knepley   Level: developer
2558ca3d3a14SMatthew G. Knepley 
2559bb7acecfSBarry Smith .seealso: `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2560ca3d3a14SMatthew G. Knepley @*/
2561*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2562*d71ae5a4SJacob Faibussowitsch {
2563ca3d3a14SMatthew G. Knepley   Vec tv;
2564ca3d3a14SMatthew G. Knepley 
2565ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2566ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2567534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
25689566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2569ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2570ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2571ca3d3a14SMatthew G. Knepley }
2572ca3d3a14SMatthew G. Knepley 
2573*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2574*d71ae5a4SJacob Faibussowitsch {
2575ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2576ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2577ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2578ca3d3a14SMatthew G. Knepley 
2579ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
25809566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
25819566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
25829566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
25839566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
25849566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
25859566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
25869566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
25879566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2588ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
25899566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2590ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2591ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2592ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
25939566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2594ca3d3a14SMatthew G. Knepley       if (!dof) continue;
25959566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
25969566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2597ca3d3a14SMatthew G. Knepley     }
2598ca3d3a14SMatthew G. Knepley   }
25999566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26009566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26019566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2602ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2603ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26049566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2605ca3d3a14SMatthew G. Knepley       if (dof) {
2606ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2607ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2608ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2609ca3d3a14SMatthew G. Knepley 
2610ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26119566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26129566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26139566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2614ca3d3a14SMatthew G. Knepley       }
2615ca3d3a14SMatthew G. Knepley     }
2616ca3d3a14SMatthew G. Knepley   }
26179566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
2618ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2619ca3d3a14SMatthew G. Knepley }
2620ca3d3a14SMatthew G. Knepley 
2621*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2622*d71ae5a4SJacob Faibussowitsch {
2623ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2624ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2625ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2626ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2627ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2628ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2629ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26309566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
2631ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2632ca3d3a14SMatthew G. Knepley }
2633ca3d3a14SMatthew G. Knepley 
2634bb9467b5SJed Brown /*@C
2635bb7acecfSBarry Smith    DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2636baf369e7SPeter Brune 
2637bb7acecfSBarry Smith    Logically Collective on dm
2638baf369e7SPeter Brune 
26394165533cSJose E. Roman    Input Parameters:
2640bb7acecfSBarry Smith +  dm - the `DM`
2641bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2642bb7acecfSBarry Smith .  endhook - function to run after `DMGlobalToLocalEnd()` has completed
26430298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2644baf369e7SPeter Brune 
2645baf369e7SPeter Brune    Calling sequence for beginhook:
2646baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2647baf369e7SPeter Brune 
2648baf369e7SPeter Brune +  dm - global DM
2649baf369e7SPeter Brune .  g - global vector
2650baf369e7SPeter Brune .  mode - mode
2651baf369e7SPeter Brune .  l - local vector
2652baf369e7SPeter Brune -  ctx - optional user-defined function context
2653baf369e7SPeter Brune 
2654baf369e7SPeter Brune    Calling sequence for endhook:
2655ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2656baf369e7SPeter Brune 
2657baf369e7SPeter Brune +  global - global DM
2658baf369e7SPeter Brune -  ctx - optional user-defined function context
2659baf369e7SPeter Brune 
2660baf369e7SPeter Brune    Level: advanced
2661baf369e7SPeter Brune 
2662bb7acecfSBarry Smith    Note:
2663bb7acecfSBarry 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.
2664bb7acecfSBarry Smith 
2665bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2666baf369e7SPeter Brune @*/
2667*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx)
2668*d71ae5a4SJacob Faibussowitsch {
2669baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2670baf369e7SPeter Brune 
2671baf369e7SPeter Brune   PetscFunctionBegin;
2672baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2673baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
26749566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2675baf369e7SPeter Brune   link->beginhook = beginhook;
2676baf369e7SPeter Brune   link->endhook   = endhook;
2677baf369e7SPeter Brune   link->ctx       = ctx;
26780298fd71SBarry Smith   link->next      = NULL;
2679baf369e7SPeter Brune   *p              = link;
2680baf369e7SPeter Brune   PetscFunctionReturn(0);
2681baf369e7SPeter Brune }
2682baf369e7SPeter Brune 
2683*d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2684*d71ae5a4SJacob Faibussowitsch {
26854c274da1SToby Isaac   Mat          cMat;
268679769bd5SJed Brown   Vec          cVec, cBias;
26874c274da1SToby Isaac   PetscSection section, cSec;
26884c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
26894c274da1SToby Isaac 
26904c274da1SToby Isaac   PetscFunctionBegin;
26914c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26929566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
26934c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
26945db9a05bSToby Isaac     PetscInt nRows;
26955db9a05bSToby Isaac 
26969566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
26975db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
26989566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
26999566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27009566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27019566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27029566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27034c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27049566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27054c274da1SToby Isaac       if (dof) {
27064c274da1SToby Isaac         PetscScalar *vals;
27079566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27089566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27094c274da1SToby Isaac       }
27104c274da1SToby Isaac     }
27119566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27124c274da1SToby Isaac   }
27134c274da1SToby Isaac   PetscFunctionReturn(0);
27144c274da1SToby Isaac }
27154c274da1SToby Isaac 
271647c6ae99SBarry Smith /*@
271701729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
271801729b5cSPatrick Sanan 
2719d083f849SBarry Smith     Neighbor-wise Collective on dm
272001729b5cSPatrick Sanan 
272101729b5cSPatrick Sanan     Input Parameters:
2722bb7acecfSBarry Smith +   dm - the `DM` object
272301729b5cSPatrick Sanan .   g - the global vector
2724bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
272501729b5cSPatrick Sanan -   l - the local vector
272601729b5cSPatrick Sanan 
272701729b5cSPatrick Sanan     Notes:
2728bb7acecfSBarry Smith     The communication involved in this update can be overlapped with computation by instead using
2729bb7acecfSBarry Smith     `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2730bb7acecfSBarry Smith 
2731bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
273201729b5cSPatrick Sanan 
273301729b5cSPatrick Sanan     Level: beginner
273401729b5cSPatrick Sanan 
2735bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
2736bb7acecfSBarry Smith           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`,
2737bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
273801729b5cSPatrick Sanan 
273901729b5cSPatrick Sanan @*/
2740*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2741*d71ae5a4SJacob Faibussowitsch {
274201729b5cSPatrick Sanan   PetscFunctionBegin;
27439566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27449566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
274501729b5cSPatrick Sanan   PetscFunctionReturn(0);
274601729b5cSPatrick Sanan }
274701729b5cSPatrick Sanan 
274801729b5cSPatrick Sanan /*@
274947c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
275047c6ae99SBarry Smith 
2751d083f849SBarry Smith     Neighbor-wise Collective on dm
275247c6ae99SBarry Smith 
275347c6ae99SBarry Smith     Input Parameters:
2754bb7acecfSBarry Smith +   dm - the `DM` object
275547c6ae99SBarry Smith .   g - the global vector
2756bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
275747c6ae99SBarry Smith -   l - the local vector
275847c6ae99SBarry Smith 
275901729b5cSPatrick Sanan     Level: intermediate
276047c6ae99SBarry Smith 
2761bb7acecfSBarry Smith     Notes:
2762bb7acecfSBarry Smith     The operation is completed with `DMGlobalToLocalEnd()`
2763bb7acecfSBarry Smith 
2764bb7acecfSBarry Smith     One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2765bb7acecfSBarry Smith 
2766bb7acecfSBarry Smith     `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2767bb7acecfSBarry Smith 
2768bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2769bb7acecfSBarry Smith 
2770bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
277147c6ae99SBarry Smith 
277247c6ae99SBarry Smith @*/
2773*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2774*d71ae5a4SJacob Faibussowitsch {
27757128ae9fSMatthew G Knepley   PetscSF                 sf;
2776baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
277747c6ae99SBarry Smith 
277847c6ae99SBarry Smith   PetscFunctionBegin;
2779171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2780baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
27811baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2782baf369e7SPeter Brune   }
27839566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
27847128ae9fSMatthew G Knepley   if (sf) {
2785ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2786ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2787d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
27887128ae9fSMatthew G Knepley 
27897a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
27909566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
27919566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
27929566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
27939566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
27949566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
27957128ae9fSMatthew G Knepley   } else {
27969566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
27977128ae9fSMatthew G Knepley   }
279847c6ae99SBarry Smith   PetscFunctionReturn(0);
279947c6ae99SBarry Smith }
280047c6ae99SBarry Smith 
280147c6ae99SBarry Smith /*@
280247c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
280347c6ae99SBarry Smith 
2804d083f849SBarry Smith     Neighbor-wise Collective on dm
280547c6ae99SBarry Smith 
280647c6ae99SBarry Smith     Input Parameters:
2807bb7acecfSBarry Smith +   dm - the `DM` object
280847c6ae99SBarry Smith .   g - the global vector
2809bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
281047c6ae99SBarry Smith -   l - the local vector
281147c6ae99SBarry Smith 
281201729b5cSPatrick Sanan     Level: intermediate
281347c6ae99SBarry Smith 
2814bb7acecfSBarry Smith     Note:
2815bb7acecfSBarry Smith     See `DMGlobalToLocalBegin()` for details.
2816bb7acecfSBarry Smith 
2817bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
281847c6ae99SBarry Smith 
281947c6ae99SBarry Smith @*/
2820*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2821*d71ae5a4SJacob Faibussowitsch {
28227128ae9fSMatthew G Knepley   PetscSF                 sf;
2823ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2824ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2825ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2826baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2827d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
282847c6ae99SBarry Smith 
282947c6ae99SBarry Smith   PetscFunctionBegin;
2830171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28319566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28329566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28337128ae9fSMatthew G Knepley   if (sf) {
28347a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28357128ae9fSMatthew G Knepley 
28369566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28379566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28389566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28399566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28409566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28419566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28427128ae9fSMatthew G Knepley   } else {
28439566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28447128ae9fSMatthew G Knepley   }
28459566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2846baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28479566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2848baf369e7SPeter Brune   }
284947c6ae99SBarry Smith   PetscFunctionReturn(0);
285047c6ae99SBarry Smith }
285147c6ae99SBarry Smith 
2852d4d07f1eSToby Isaac /*@C
2853d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2854d4d07f1eSToby Isaac 
2855bb7acecfSBarry Smith    Logically Collective on dm
2856d4d07f1eSToby Isaac 
28574165533cSJose E. Roman    Input Parameters:
2858bb7acecfSBarry Smith +  dm - the `DM`
2859bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2860bb7acecfSBarry Smith .  endhook - function to run after `DMLocalToGlobalEnd()` has completed
2861d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2862d4d07f1eSToby Isaac 
2863d4d07f1eSToby Isaac    Calling sequence for beginhook:
2864d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2865d4d07f1eSToby Isaac 
2866bb7acecfSBarry Smith +  dm - global `DM`
2867d4d07f1eSToby Isaac .  l - local vector
2868d4d07f1eSToby Isaac .  mode - mode
2869d4d07f1eSToby Isaac .  g - global vector
2870d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2871d4d07f1eSToby Isaac 
2872d4d07f1eSToby Isaac    Calling sequence for endhook:
2873d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2874d4d07f1eSToby Isaac 
2875bb7acecfSBarry Smith +  global - global `DM`
2876d4d07f1eSToby Isaac .  l - local vector
2877d4d07f1eSToby Isaac .  mode - mode
2878d4d07f1eSToby Isaac .  g - global vector
2879d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2880d4d07f1eSToby Isaac 
2881d4d07f1eSToby Isaac    Level: advanced
2882d4d07f1eSToby Isaac 
2883bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2884d4d07f1eSToby Isaac @*/
2885*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx)
2886*d71ae5a4SJacob Faibussowitsch {
2887d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2888d4d07f1eSToby Isaac 
2889d4d07f1eSToby Isaac   PetscFunctionBegin;
2890d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2891d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
28929566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2893d4d07f1eSToby Isaac   link->beginhook = beginhook;
2894d4d07f1eSToby Isaac   link->endhook   = endhook;
2895d4d07f1eSToby Isaac   link->ctx       = ctx;
2896d4d07f1eSToby Isaac   link->next      = NULL;
2897d4d07f1eSToby Isaac   *p              = link;
2898d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2899d4d07f1eSToby Isaac }
2900d4d07f1eSToby Isaac 
2901*d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2902*d71ae5a4SJacob Faibussowitsch {
29034c274da1SToby Isaac   Mat          cMat;
29044c274da1SToby Isaac   Vec          cVec;
29054c274da1SToby Isaac   PetscSection section, cSec;
29064c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
29074c274da1SToby Isaac 
29084c274da1SToby Isaac   PetscFunctionBegin;
29094c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29109566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29114c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
29125db9a05bSToby Isaac     PetscInt nRows;
29135db9a05bSToby Isaac 
29149566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29155db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
29169566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29179566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29189566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29194c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29209566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29214c274da1SToby Isaac       if (dof) {
29224c274da1SToby Isaac         PetscInt     d;
29234c274da1SToby Isaac         PetscScalar *vals;
29249566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29259566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29264c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29274c274da1SToby Isaac          * we just extracted */
2928ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29294c274da1SToby Isaac       }
29304c274da1SToby Isaac     }
29319566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29329566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29334c274da1SToby Isaac   }
29344c274da1SToby Isaac   PetscFunctionReturn(0);
29354c274da1SToby Isaac }
293601729b5cSPatrick Sanan /*@
293701729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
293801729b5cSPatrick Sanan 
2939d083f849SBarry Smith     Neighbor-wise Collective on dm
294001729b5cSPatrick Sanan 
294101729b5cSPatrick Sanan     Input Parameters:
2942bb7acecfSBarry Smith +   dm - the `DM` object
294301729b5cSPatrick Sanan .   l - the local vector
2944bb7acecfSBarry 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.
294501729b5cSPatrick Sanan -   g - the global vector
294601729b5cSPatrick Sanan 
294701729b5cSPatrick Sanan     Notes:
294801729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
2949bb7acecfSBarry Smith     `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
295001729b5cSPatrick Sanan 
2951bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2952bb7acecfSBarry Smith 
2953bb7acecfSBarry 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.
2954bb7acecfSBarry Smith 
2955bb7acecfSBarry Smith     Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
295601729b5cSPatrick Sanan 
295701729b5cSPatrick Sanan     Level: beginner
295801729b5cSPatrick Sanan 
2959bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`,  `DMLocalToGlobalHookAdd()`,  `DMGlobaToLocallHookAdd()`
296001729b5cSPatrick Sanan 
296101729b5cSPatrick Sanan @*/
2962*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
2963*d71ae5a4SJacob Faibussowitsch {
296401729b5cSPatrick Sanan   PetscFunctionBegin;
29659566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
29669566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
296701729b5cSPatrick Sanan   PetscFunctionReturn(0);
296801729b5cSPatrick Sanan }
29694c274da1SToby Isaac 
297047c6ae99SBarry Smith /*@
297101729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
29729a42bb27SBarry Smith 
2973d083f849SBarry Smith     Neighbor-wise Collective on dm
29749a42bb27SBarry Smith 
29759a42bb27SBarry Smith     Input Parameters:
2976bb7acecfSBarry Smith +   dm - the `DM` object
2977f6813fd5SJed Brown .   l - the local vector
2978bb7acecfSBarry 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.
29791eb28f2eSBarry Smith -   g - the global vector
29809a42bb27SBarry Smith 
298195452b02SPatrick Sanan     Notes:
2982bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2983bb7acecfSBarry Smith 
2984bb7acecfSBarry 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.
2985bb7acecfSBarry Smith 
2986bb7acecfSBarry Smith     Use `DMLocalToGlobalEnd()` to complete the communication process.
2987bb7acecfSBarry Smith 
2988bb7acecfSBarry Smith     `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
2989bb7acecfSBarry Smith 
2990bb7acecfSBarry Smith     `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
29919a42bb27SBarry Smith 
299201729b5cSPatrick Sanan     Level: intermediate
29939a42bb27SBarry Smith 
2994bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
29959a42bb27SBarry Smith 
29969a42bb27SBarry Smith @*/
2997*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
2998*d71ae5a4SJacob Faibussowitsch {
29997128ae9fSMatthew G Knepley   PetscSF                 sf;
300084330215SMatthew G. Knepley   PetscSection            s, gs;
3001d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3002ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3003ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3004ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3005fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3006d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30079a42bb27SBarry Smith 
30089a42bb27SBarry Smith   PetscFunctionBegin;
3009171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3010d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30111baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3012d4d07f1eSToby Isaac   }
30139566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30149566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30159566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30167128ae9fSMatthew G Knepley   switch (mode) {
30177128ae9fSMatthew G Knepley   case INSERT_VALUES:
30187128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3019*d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3020*d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3021*d71ae5a4SJacob Faibussowitsch     break;
30227128ae9fSMatthew G Knepley   case ADD_VALUES:
30237128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3024*d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3025*d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3026*d71ae5a4SJacob Faibussowitsch     break;
3027*d71ae5a4SJacob Faibussowitsch   default:
3028*d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30297128ae9fSMatthew G Knepley   }
3030ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30319566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3032ca3d3a14SMatthew G. Knepley     if (transform) {
30339566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30349566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30359566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30369566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3037fa88e482SJed Brown     } else if (isInsert) {
30389566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3039fa88e482SJed Brown     } else {
30409566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3041fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3042ca3d3a14SMatthew G. Knepley     }
3043fa88e482SJed Brown     if (s && isInsert) {
30449566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3045fa88e482SJed Brown     } else {
30469566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3047fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3048fa88e482SJed Brown     }
3049ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30509566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
305184330215SMatthew G. Knepley     } else if (s && isInsert) {
305284330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
305384330215SMatthew G. Knepley 
30549566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
30559566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
30569566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
305784330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3058b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
305984330215SMatthew G. Knepley 
30609566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
30619566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
30629566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
30639566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
30649566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
30659566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3066b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
306703442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
30687a8be351SBarry 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);
3069b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3070b3b16f48SMatthew G. Knepley         if (!gcdof) {
307184330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3072b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3073b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
307484330215SMatthew G. Knepley           const PetscInt *cdofs;
307584330215SMatthew G. Knepley           PetscInt        cind = 0;
307684330215SMatthew G. Knepley 
30779566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3078b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
30799371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
30809371c9d4SSatish Balay               ++cind;
30819371c9d4SSatish Balay               continue;
30829371c9d4SSatish Balay             }
3083b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
308484330215SMatthew G. Knepley           }
30857a8be351SBarry 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);
308684330215SMatthew G. Knepley       }
3087ca3d3a14SMatthew G. Knepley     }
3088fa88e482SJed Brown     if (g_inplace) {
30899566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3090fa88e482SJed Brown     } else {
30919566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3092fa88e482SJed Brown     }
3093ca3d3a14SMatthew G. Knepley     if (transform) {
30949566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
30959566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3096fa88e482SJed Brown     } else if (l_inplace) {
30979566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3098ca3d3a14SMatthew G. Knepley     } else {
30999566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3100ca3d3a14SMatthew G. Knepley     }
31017128ae9fSMatthew G Knepley   } else {
31029566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
31037128ae9fSMatthew G Knepley   }
31049a42bb27SBarry Smith   PetscFunctionReturn(0);
31059a42bb27SBarry Smith }
31069a42bb27SBarry Smith 
31079a42bb27SBarry Smith /*@
31089a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
310947c6ae99SBarry Smith 
3110d083f849SBarry Smith     Neighbor-wise Collective on dm
311147c6ae99SBarry Smith 
311247c6ae99SBarry Smith     Input Parameters:
3113bb7acecfSBarry Smith +   dm - the `DM` object
3114f6813fd5SJed Brown .   l - the local vector
3115bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
3116f6813fd5SJed Brown -   g - the global vector
311747c6ae99SBarry Smith 
311801729b5cSPatrick Sanan     Level: intermediate
311947c6ae99SBarry Smith 
3120bb7acecfSBarry Smith     Note:
3121bb7acecfSBarry Smith     See `DMLocalToGlobalBegin()` for full details
3122bb7acecfSBarry Smith 
3123bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalEnd()`
312447c6ae99SBarry Smith 
312547c6ae99SBarry Smith @*/
3126*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3127*d71ae5a4SJacob Faibussowitsch {
31287128ae9fSMatthew G Knepley   PetscSF                 sf;
312984330215SMatthew G. Knepley   PetscSection            s;
3130d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3131ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
313247c6ae99SBarry Smith 
313347c6ae99SBarry Smith   PetscFunctionBegin;
3134171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31359566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31369566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31377128ae9fSMatthew G Knepley   switch (mode) {
31387128ae9fSMatthew G Knepley   case INSERT_VALUES:
3139*d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3140*d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3141*d71ae5a4SJacob Faibussowitsch     break;
31427128ae9fSMatthew G Knepley   case ADD_VALUES:
3143*d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3144*d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3145*d71ae5a4SJacob Faibussowitsch     break;
3146*d71ae5a4SJacob Faibussowitsch   default:
3147*d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31487128ae9fSMatthew G Knepley   }
314984330215SMatthew G. Knepley   if (sf && !isInsert) {
3150ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3151ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3152ca3d3a14SMatthew G. Knepley     Vec                tmpl;
315384330215SMatthew G. Knepley 
31549566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3155ca3d3a14SMatthew G. Knepley     if (transform) {
31569566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31579566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3158ca3d3a14SMatthew G. Knepley     } else {
31599566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3160ca3d3a14SMatthew G. Knepley     }
31619566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
31629566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3163ca3d3a14SMatthew G. Knepley     if (transform) {
31649566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31659566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3166ca3d3a14SMatthew G. Knepley     } else {
31679566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3168ca3d3a14SMatthew G. Knepley     }
31699566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
317084330215SMatthew G. Knepley   } else if (s && isInsert) {
31717128ae9fSMatthew G Knepley   } else {
31729566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
31737128ae9fSMatthew G Knepley   }
3174d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
31759566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3176d4d07f1eSToby Isaac   }
317747c6ae99SBarry Smith   PetscFunctionReturn(0);
317847c6ae99SBarry Smith }
317947c6ae99SBarry Smith 
3180f089877aSRichard Tran Mills /*@
3181bb7acecfSBarry Smith    DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include ghost points
3182bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3183bb7acecfSBarry Smith    points in the second are set correctly from values on other MPI ranks. Must be followed by `DMLocalToLocalEnd()`.
3184f089877aSRichard Tran Mills 
3185d083f849SBarry Smith    Neighbor-wise Collective on dm
3186f089877aSRichard Tran Mills 
3187f089877aSRichard Tran Mills    Input Parameters:
3188bb7acecfSBarry Smith +  dm - the `DM` object
3189bc0a1609SRichard Tran Mills .  g - the original local vector
3190bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3191f089877aSRichard Tran Mills 
3192bc0a1609SRichard Tran Mills    Output Parameter:
3193bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3194f089877aSRichard Tran Mills 
3195f089877aSRichard Tran Mills    Level: intermediate
3196f089877aSRichard Tran Mills 
3197bb7acecfSBarry Smith .seealso: `DMLocalToLocalEnd(), `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalEnd()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3198f089877aSRichard Tran Mills 
3199f089877aSRichard Tran Mills @*/
3200*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3201*d71ae5a4SJacob Faibussowitsch {
3202f089877aSRichard Tran Mills   PetscFunctionBegin;
3203f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32049566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
3205f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3206f089877aSRichard Tran Mills }
3207f089877aSRichard Tran Mills 
3208f089877aSRichard Tran Mills /*@
3209bb7acecfSBarry Smith    DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3210bb7acecfSBarry Smith    points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3211f089877aSRichard Tran Mills 
3212d083f849SBarry Smith    Neighbor-wise Collective on dm
3213f089877aSRichard Tran Mills 
3214f089877aSRichard Tran Mills    Input Parameters:
3215bb7acecfSBarry Smith +  da - the `DM` object
3216bc0a1609SRichard Tran Mills .  g - the original local vector
3217bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3218f089877aSRichard Tran Mills 
3219bc0a1609SRichard Tran Mills    Output Parameter:
3220bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3221f089877aSRichard Tran Mills 
3222f089877aSRichard Tran Mills    Level: intermediate
3223f089877aSRichard Tran Mills 
3224bb7acecfSBarry Smith .seealso: `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalBegin()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3225f089877aSRichard Tran Mills 
3226f089877aSRichard Tran Mills @*/
3227*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3228*d71ae5a4SJacob Faibussowitsch {
3229f089877aSRichard Tran Mills   PetscFunctionBegin;
3230f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32319566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
3232f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3233f089877aSRichard Tran Mills }
3234f089877aSRichard Tran Mills 
323547c6ae99SBarry Smith /*@
3236bb7acecfSBarry Smith     DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
323747c6ae99SBarry Smith 
3238d083f849SBarry Smith     Collective on dm
323947c6ae99SBarry Smith 
3240d8d19677SJose E. Roman     Input Parameters:
3241bb7acecfSBarry Smith +   dm - the `DM` object
3242bb7acecfSBarry Smith -   comm - the communicator to contain the new `DM` object (or MPI_COMM_NULL)
324347c6ae99SBarry Smith 
324447c6ae99SBarry Smith     Output Parameter:
3245bb7acecfSBarry Smith .   dmc - the coarsened `DM`
324647c6ae99SBarry Smith 
324747c6ae99SBarry Smith     Level: developer
324847c6ae99SBarry Smith 
3249bb7acecfSBarry Smith .seealso: `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
325047c6ae99SBarry Smith 
325147c6ae99SBarry Smith @*/
3252*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3253*d71ae5a4SJacob Faibussowitsch {
3254b17ce1afSJed Brown   DMCoarsenHookLink link;
325547c6ae99SBarry Smith 
325647c6ae99SBarry Smith   PetscFunctionBegin;
3257171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32589566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3259dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3260b9d85ea2SLisandro Dalcin   if (*dmc) {
3261a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
32629566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
326343842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
32649566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3265644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
32660598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3267656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
32689566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3269b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
32709566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3271b17ce1afSJed Brown     }
3272b9d85ea2SLisandro Dalcin   }
32739566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
32747a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3275b17ce1afSJed Brown   PetscFunctionReturn(0);
3276b17ce1afSJed Brown }
3277b17ce1afSJed Brown 
3278bb9467b5SJed Brown /*@C
3279b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3280b17ce1afSJed Brown 
3281bb7acecfSBarry Smith    Logically Collective on fine
3282b17ce1afSJed Brown 
32834165533cSJose E. Roman    Input Parameters:
3284bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3285b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3286bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
32870298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3288b17ce1afSJed Brown 
3289b17ce1afSJed Brown    Calling sequence of coarsenhook:
3290b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3291b17ce1afSJed Brown 
3292bb7acecfSBarry Smith +  fine - fine level `DM`
3293bb7acecfSBarry Smith .  coarse - coarse level `DM` to restrict problem to
3294b17ce1afSJed Brown -  ctx - optional user-defined function context
3295b17ce1afSJed Brown 
3296b17ce1afSJed Brown    Calling sequence for restricthook:
3297c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3298bb7acecfSBarry Smith $
3299bb7acecfSBarry Smith +  fine - fine level `DM`
3300bb7acecfSBarry Smith .  mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3301c833c3b5SJed Brown .  rscale - scaling vector for restriction
3302c833c3b5SJed Brown .  inject - matrix restricting by injection
3303b17ce1afSJed Brown .  coarse - coarse level DM to update
3304b17ce1afSJed Brown -  ctx - optional user-defined function context
3305b17ce1afSJed Brown 
3306b17ce1afSJed Brown    Level: advanced
3307b17ce1afSJed Brown 
3308b17ce1afSJed Brown    Notes:
3309bb7acecfSBarry 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`.
3310b17ce1afSJed Brown 
3311b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3312b17ce1afSJed Brown 
3313b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3314bb7acecfSBarry Smith    extract the finest level information from its context (instead of from the `SNES`).
3315b17ce1afSJed Brown 
3316bb7acecfSBarry Smith    The hooks are automatically called by `DMRestrict()`
3317bb7acecfSBarry Smith 
3318bb7acecfSBarry Smith    Fortran Note:
3319bb7acecfSBarry Smith    This function is not available from Fortran.
3320bb9467b5SJed Brown 
3321db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3322b17ce1afSJed Brown @*/
3323*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3324*d71ae5a4SJacob Faibussowitsch {
3325b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3326b17ce1afSJed Brown 
3327b17ce1afSJed Brown   PetscFunctionBegin;
3328b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33291e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33301e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
33311e3d8eccSJed Brown   }
33329566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3333b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3334b17ce1afSJed Brown   link->restricthook = restricthook;
3335b17ce1afSJed Brown   link->ctx          = ctx;
33360298fd71SBarry Smith   link->next         = NULL;
3337b17ce1afSJed Brown   *p                 = link;
3338b17ce1afSJed Brown   PetscFunctionReturn(0);
3339b17ce1afSJed Brown }
3340b17ce1afSJed Brown 
3341dc822a44SJed Brown /*@C
3342bb7acecfSBarry Smith    DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3343dc822a44SJed Brown 
3344bb7acecfSBarry Smith    Logically Collective on fine
3345dc822a44SJed Brown 
33464165533cSJose E. Roman    Input Parameters:
3347bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3348dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3349bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels
3350dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3351dc822a44SJed Brown 
3352dc822a44SJed Brown    Level: advanced
3353dc822a44SJed Brown 
3354bb7acecfSBarry Smith    Note:
3355dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3356dc822a44SJed Brown 
3357bb7acecfSBarry Smith    Fortran Note:
3358bb7acecfSBarry Smith    This function is not available from Fortran.
3359dc822a44SJed Brown 
3360db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3361dc822a44SJed Brown @*/
3362*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3363*d71ae5a4SJacob Faibussowitsch {
3364dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3365dc822a44SJed Brown 
3366dc822a44SJed Brown   PetscFunctionBegin;
3367dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3368dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3369dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3370dc822a44SJed Brown       link = *p;
3371dc822a44SJed Brown       *p   = link->next;
33729566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3373dc822a44SJed Brown       break;
3374dc822a44SJed Brown     }
3375dc822a44SJed Brown   }
3376dc822a44SJed Brown   PetscFunctionReturn(0);
3377dc822a44SJed Brown }
3378dc822a44SJed Brown 
3379b17ce1afSJed Brown /*@
3380bb7acecfSBarry Smith    DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3381b17ce1afSJed Brown 
3382b17ce1afSJed Brown    Collective if any hooks are
3383b17ce1afSJed Brown 
33844165533cSJose E. Roman    Input Parameters:
3385bb7acecfSBarry Smith +  fine - finer `DM` from which the data is obtained
3386bb7acecfSBarry Smith .  restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3387e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3388bb7acecfSBarry Smith .  inject - injection matrix, also use `MatRestrict()`
3389e91eccc2SStefano Zampini -  coarse - coarser DM to update
3390b17ce1afSJed Brown 
3391b17ce1afSJed Brown    Level: developer
3392b17ce1afSJed Brown 
3393bb7acecfSBarry Smith    Developer Note:
3394bb7acecfSBarry Smith    Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3395bb7acecfSBarry Smith 
3396bb7acecfSBarry Smith .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3397b17ce1afSJed Brown @*/
3398*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3399*d71ae5a4SJacob Faibussowitsch {
3400b17ce1afSJed Brown   DMCoarsenHookLink link;
3401b17ce1afSJed Brown 
3402b17ce1afSJed Brown   PetscFunctionBegin;
3403b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34041baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3405b17ce1afSJed Brown   }
340647c6ae99SBarry Smith   PetscFunctionReturn(0);
340747c6ae99SBarry Smith }
340847c6ae99SBarry Smith 
3409bb9467b5SJed Brown /*@C
3410be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
34115dbd56e3SPeter Brune 
3412d083f849SBarry Smith    Logically Collective on global
34135dbd56e3SPeter Brune 
34144165533cSJose E. Roman    Input Parameters:
3415bb7acecfSBarry Smith +  global - global `DM`
3416bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
34175dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
34180298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
34195dbd56e3SPeter Brune 
3420ec4806b8SPeter Brune    Calling sequence for ddhook:
3421ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3422ec4806b8SPeter Brune 
3423bb7acecfSBarry Smith +  global - global `DM`
3424bb7acecfSBarry Smith .  block  - block `DM`
3425ec4806b8SPeter Brune -  ctx - optional user-defined function context
3426ec4806b8SPeter Brune 
34275dbd56e3SPeter Brune    Calling sequence for restricthook:
3428ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
34295dbd56e3SPeter Brune 
3430bb7acecfSBarry Smith +  global - global `DM`
34315dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
34325dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3433bb7acecfSBarry Smith .  block  - block `DM`
34345dbd56e3SPeter Brune -  ctx - optional user-defined function context
34355dbd56e3SPeter Brune 
34365dbd56e3SPeter Brune    Level: advanced
34375dbd56e3SPeter Brune 
34385dbd56e3SPeter Brune    Notes:
3439bb7acecfSBarry Smith    This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
34405dbd56e3SPeter Brune 
34415dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
34425dbd56e3SPeter Brune 
34435dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3444bb7acecfSBarry Smith    extract the global information from its context (instead of from the `SNES`).
34455dbd56e3SPeter Brune 
3446bb7acecfSBarry Smith    Fortran Note:
3447bb7acecfSBarry Smith    This function is not available from Fortran.
3448bb9467b5SJed Brown 
3449bb7acecfSBarry Smith .seealso: `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
34505dbd56e3SPeter Brune @*/
3451*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3452*d71ae5a4SJacob Faibussowitsch {
3453be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34545dbd56e3SPeter Brune 
34555dbd56e3SPeter Brune   PetscFunctionBegin;
34565dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3457b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
3458b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3459b3a6b972SJed Brown   }
34609566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
34615dbd56e3SPeter Brune   link->restricthook = restricthook;
3462be081cd6SPeter Brune   link->ddhook       = ddhook;
34635dbd56e3SPeter Brune   link->ctx          = ctx;
34640298fd71SBarry Smith   link->next         = NULL;
34655dbd56e3SPeter Brune   *p                 = link;
34665dbd56e3SPeter Brune   PetscFunctionReturn(0);
34675dbd56e3SPeter Brune }
34685dbd56e3SPeter Brune 
3469b3a6b972SJed Brown /*@C
3470b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3471b3a6b972SJed Brown 
3472bb7acecfSBarry Smith    Logically Collective on global
3473b3a6b972SJed Brown 
34744165533cSJose E. Roman    Input Parameters:
3475bb7acecfSBarry Smith +  global - global `DM`
3476bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
3477b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3478b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3479b3a6b972SJed Brown 
3480b3a6b972SJed Brown    Level: advanced
3481b3a6b972SJed Brown 
3482bb7acecfSBarry Smith    Fortran Note:
3483bb7acecfSBarry Smith    This function is not available from Fortran.
3484b3a6b972SJed Brown 
3485db781477SPatrick Sanan .seealso: `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3486b3a6b972SJed Brown @*/
3487*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3488*d71ae5a4SJacob Faibussowitsch {
3489b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3490b3a6b972SJed Brown 
3491b3a6b972SJed Brown   PetscFunctionBegin;
3492b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3493b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3494b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3495b3a6b972SJed Brown       link = *p;
3496b3a6b972SJed Brown       *p   = link->next;
34979566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3498b3a6b972SJed Brown       break;
3499b3a6b972SJed Brown     }
3500b3a6b972SJed Brown   }
3501b3a6b972SJed Brown   PetscFunctionReturn(0);
3502b3a6b972SJed Brown }
3503b3a6b972SJed Brown 
35045dbd56e3SPeter Brune /*@
3505bb7acecfSBarry Smith    DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
35065dbd56e3SPeter Brune 
35075dbd56e3SPeter Brune    Collective if any hooks are
35085dbd56e3SPeter Brune 
35094165533cSJose E. Roman    Input Parameters:
3510bb7acecfSBarry Smith +  fine - finer `DM` to use as a base
3511be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3512be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
3513bb7acecfSBarry Smith -  coarse - coarser `DM` to update
35145dbd56e3SPeter Brune 
35155dbd56e3SPeter Brune    Level: developer
35165dbd56e3SPeter Brune 
3517db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
35185dbd56e3SPeter Brune @*/
3519*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3520*d71ae5a4SJacob Faibussowitsch {
3521be081cd6SPeter Brune   DMSubDomainHookLink link;
35225dbd56e3SPeter Brune 
35235dbd56e3SPeter Brune   PetscFunctionBegin;
3524be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35251baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35265dbd56e3SPeter Brune   }
35275dbd56e3SPeter Brune   PetscFunctionReturn(0);
35285dbd56e3SPeter Brune }
35295dbd56e3SPeter Brune 
35305fe1f584SPeter Brune /*@
3531bb7acecfSBarry Smith     DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35325fe1f584SPeter Brune 
35335fe1f584SPeter Brune     Not Collective
35345fe1f584SPeter Brune 
35355fe1f584SPeter Brune     Input Parameter:
3536bb7acecfSBarry Smith .   dm - the `DM` object
35375fe1f584SPeter Brune 
35385fe1f584SPeter Brune     Output Parameter:
35396a7d9d85SPeter Brune .   level - number of coarsenings
35405fe1f584SPeter Brune 
35415fe1f584SPeter Brune     Level: developer
35425fe1f584SPeter Brune 
3543bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35445fe1f584SPeter Brune 
35455fe1f584SPeter Brune @*/
3546*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3547*d71ae5a4SJacob Faibussowitsch {
35485fe1f584SPeter Brune   PetscFunctionBegin;
35495fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3550b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level, 2);
35515fe1f584SPeter Brune   *level = dm->leveldown;
35525fe1f584SPeter Brune   PetscFunctionReturn(0);
35535fe1f584SPeter Brune }
35545fe1f584SPeter Brune 
35559a64c4a8SMatthew G. Knepley /*@
3556bb7acecfSBarry Smith     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
35579a64c4a8SMatthew G. Knepley 
3558bb7acecfSBarry Smith     Collective on dm
35599a64c4a8SMatthew G. Knepley 
35609a64c4a8SMatthew G. Knepley     Input Parameters:
3561bb7acecfSBarry Smith +   dm - the `DM` object
35629a64c4a8SMatthew G. Knepley -   level - number of coarsenings
35639a64c4a8SMatthew G. Knepley 
35649a64c4a8SMatthew G. Knepley     Level: developer
35659a64c4a8SMatthew G. Knepley 
3566bb7acecfSBarry Smith     Note:
3567bb7acecfSBarry Smith     This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3568bb7acecfSBarry Smith 
3569bb7acecfSBarry Smith .seealso: `DMSetCoarsenLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35709a64c4a8SMatthew G. Knepley @*/
3571*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3572*d71ae5a4SJacob Faibussowitsch {
35739a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
35749a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35759a64c4a8SMatthew G. Knepley   dm->leveldown = level;
35769a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
35779a64c4a8SMatthew G. Knepley }
35789a64c4a8SMatthew G. Knepley 
357947c6ae99SBarry Smith /*@C
3580bb7acecfSBarry Smith     DMRefineHierarchy - Refines a `DM` object, all levels at once
358147c6ae99SBarry Smith 
3582d083f849SBarry Smith     Collective on dm
358347c6ae99SBarry Smith 
3584d8d19677SJose E. Roman     Input Parameters:
3585bb7acecfSBarry Smith +   dm - the `DM` object
358647c6ae99SBarry Smith -   nlevels - the number of levels of refinement
358747c6ae99SBarry Smith 
358847c6ae99SBarry Smith     Output Parameter:
3589bb7acecfSBarry Smith .   dmf - the refined `DM` hierarchy
359047c6ae99SBarry Smith 
359147c6ae99SBarry Smith     Level: developer
359247c6ae99SBarry Smith 
3593bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
359447c6ae99SBarry Smith 
359547c6ae99SBarry Smith @*/
3596*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3597*d71ae5a4SJacob Faibussowitsch {
359847c6ae99SBarry Smith   PetscFunctionBegin;
3599171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36007a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
360147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3602b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf, 3);
360347c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
3604dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
360547c6ae99SBarry Smith   } else if (dm->ops->refine) {
360647c6ae99SBarry Smith     PetscInt i;
360747c6ae99SBarry Smith 
36089566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
360948a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3610ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No RefineHierarchy for this DM yet");
361147c6ae99SBarry Smith   PetscFunctionReturn(0);
361247c6ae99SBarry Smith }
361347c6ae99SBarry Smith 
361447c6ae99SBarry Smith /*@C
3615bb7acecfSBarry Smith     DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
361647c6ae99SBarry Smith 
3617d083f849SBarry Smith     Collective on dm
361847c6ae99SBarry Smith 
3619d8d19677SJose E. Roman     Input Parameters:
3620bb7acecfSBarry Smith +   dm - the `DM` object
362147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
362247c6ae99SBarry Smith 
362347c6ae99SBarry Smith     Output Parameter:
3624bb7acecfSBarry Smith .   dmc - the coarsened `DM` hierarchy
362547c6ae99SBarry Smith 
362647c6ae99SBarry Smith     Level: developer
362747c6ae99SBarry Smith 
3628bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
362947c6ae99SBarry Smith 
363047c6ae99SBarry Smith @*/
3631*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3632*d71ae5a4SJacob Faibussowitsch {
363347c6ae99SBarry Smith   PetscFunctionBegin;
3634171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36357a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
363647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
363747c6ae99SBarry Smith   PetscValidPointer(dmc, 3);
363847c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
3639dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
364047c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
364147c6ae99SBarry Smith     PetscInt i;
364247c6ae99SBarry Smith 
36439566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
364448a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3645ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No CoarsenHierarchy for this DM yet");
364647c6ae99SBarry Smith   PetscFunctionReturn(0);
364747c6ae99SBarry Smith }
364847c6ae99SBarry Smith 
36491a266240SBarry Smith /*@C
3650bb7acecfSBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36511a266240SBarry Smith 
3652bb7acecfSBarry Smith     Logically Collective if the function is collective
36531a266240SBarry Smith 
36541a266240SBarry Smith     Input Parameters:
3655bb7acecfSBarry Smith +   dm - the `DM` object
36561a266240SBarry Smith -   destroy - the destroy function
36571a266240SBarry Smith 
36581a266240SBarry Smith     Level: intermediate
36591a266240SBarry Smith 
3660bb7acecfSBarry Smith .seealso: `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
36611a266240SBarry Smith 
3662f07f9ceaSJed Brown @*/
3663*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3664*d71ae5a4SJacob Faibussowitsch {
36651a266240SBarry Smith   PetscFunctionBegin;
3666171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36671a266240SBarry Smith   dm->ctxdestroy = destroy;
36681a266240SBarry Smith   PetscFunctionReturn(0);
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:
3683bb7acecfSBarry Smith     A user context is a way to pass problem specific information that is accessable whenever the `DM` is available
3684bb7acecfSBarry Smith 
3685bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
368647c6ae99SBarry Smith 
368747c6ae99SBarry Smith @*/
3688*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3689*d71ae5a4SJacob Faibussowitsch {
369047c6ae99SBarry Smith   PetscFunctionBegin;
3691171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
369247c6ae99SBarry Smith   dm->ctx = ctx;
369347c6ae99SBarry Smith   PetscFunctionReturn(0);
369447c6ae99SBarry Smith }
369547c6ae99SBarry Smith 
369647c6ae99SBarry Smith /*@
3697bb7acecfSBarry Smith     DMGetApplicationContext - Gets a user context from a `DM` object
369847c6ae99SBarry Smith 
369947c6ae99SBarry Smith     Not Collective
370047c6ae99SBarry Smith 
370147c6ae99SBarry Smith     Input Parameter:
3702bb7acecfSBarry Smith .   dm - the `DM` object
370347c6ae99SBarry Smith 
370447c6ae99SBarry Smith     Output Parameter:
370547c6ae99SBarry Smith .   ctx - the user context
370647c6ae99SBarry Smith 
370747c6ae99SBarry Smith     Level: intermediate
370847c6ae99SBarry Smith 
3709bb7acecfSBarry Smith     Note:
3710bb7acecfSBarry Smith     A user context is a way to pass problem specific information that is accessable whenever the `DM` is available
3711bb7acecfSBarry Smith 
3712bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
371347c6ae99SBarry Smith 
371447c6ae99SBarry Smith @*/
3715*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3716*d71ae5a4SJacob Faibussowitsch {
371747c6ae99SBarry Smith   PetscFunctionBegin;
3718171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37191b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
372047c6ae99SBarry Smith   PetscFunctionReturn(0);
372147c6ae99SBarry Smith }
372247c6ae99SBarry Smith 
372308da532bSDmitry Karpeev /*@C
3724bb7acecfSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
372508da532bSDmitry Karpeev 
3726d083f849SBarry Smith     Logically Collective on dm
372708da532bSDmitry Karpeev 
3728d8d19677SJose E. Roman     Input Parameters:
372908da532bSDmitry Karpeev +   dm - the DM object
37300298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
373108da532bSDmitry Karpeev 
373208da532bSDmitry Karpeev     Level: intermediate
373308da532bSDmitry Karpeev 
3734bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3735db781477SPatrick Sanan          `DMSetJacobian()`
373608da532bSDmitry Karpeev 
373708da532bSDmitry Karpeev @*/
3738*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3739*d71ae5a4SJacob Faibussowitsch {
374008da532bSDmitry Karpeev   PetscFunctionBegin;
37415a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
374208da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
374308da532bSDmitry Karpeev   PetscFunctionReturn(0);
374408da532bSDmitry Karpeev }
374508da532bSDmitry Karpeev 
374608da532bSDmitry Karpeev /*@
3747bb7acecfSBarry Smith     DMHasVariableBounds - does the `DM` object have a variable bounds function?
374808da532bSDmitry Karpeev 
374908da532bSDmitry Karpeev     Not Collective
375008da532bSDmitry Karpeev 
375108da532bSDmitry Karpeev     Input Parameter:
3752bb7acecfSBarry Smith .   dm - the `DM` object to destroy
375308da532bSDmitry Karpeev 
375408da532bSDmitry Karpeev     Output Parameter:
3755bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the variable bounds function exists
375608da532bSDmitry Karpeev 
375708da532bSDmitry Karpeev     Level: developer
375808da532bSDmitry Karpeev 
3759bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
376008da532bSDmitry Karpeev 
376108da532bSDmitry Karpeev @*/
3762*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3763*d71ae5a4SJacob Faibussowitsch {
376408da532bSDmitry Karpeev   PetscFunctionBegin;
37655a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3766534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
376708da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
376808da532bSDmitry Karpeev   PetscFunctionReturn(0);
376908da532bSDmitry Karpeev }
377008da532bSDmitry Karpeev 
377108da532bSDmitry Karpeev /*@C
3772bb7acecfSBarry Smith     DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
377308da532bSDmitry Karpeev 
3774d083f849SBarry Smith     Logically Collective on dm
377508da532bSDmitry Karpeev 
3776f899ff85SJose E. Roman     Input Parameter:
3777bb7acecfSBarry Smith .   dm - the `DM` object
377808da532bSDmitry Karpeev 
377908da532bSDmitry Karpeev     Output parameters:
378008da532bSDmitry Karpeev +   xl - lower bound
378108da532bSDmitry Karpeev -   xu - upper bound
378208da532bSDmitry Karpeev 
3783907376e6SBarry Smith     Level: advanced
3784907376e6SBarry Smith 
378595452b02SPatrick Sanan     Notes:
378695452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
378708da532bSDmitry Karpeev 
3788bb7acecfSBarry Smith .seealso: `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
378908da532bSDmitry Karpeev 
379008da532bSDmitry Karpeev @*/
3791*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3792*d71ae5a4SJacob Faibussowitsch {
379308da532bSDmitry Karpeev   PetscFunctionBegin;
37945a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
379508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
37965a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3797dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
379808da532bSDmitry Karpeev   PetscFunctionReturn(0);
379908da532bSDmitry Karpeev }
380008da532bSDmitry Karpeev 
3801b0ae01b7SPeter Brune /*@
3802bb7acecfSBarry Smith     DMHasColoring - does the `DM` object have a method of providing a coloring?
3803b0ae01b7SPeter Brune 
3804b0ae01b7SPeter Brune     Not Collective
3805b0ae01b7SPeter Brune 
3806b0ae01b7SPeter Brune     Input Parameter:
3807b0ae01b7SPeter Brune .   dm - the DM object
3808b0ae01b7SPeter Brune 
3809b0ae01b7SPeter Brune     Output Parameter:
3810bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3811b0ae01b7SPeter Brune 
3812b0ae01b7SPeter Brune     Level: developer
3813b0ae01b7SPeter Brune 
3814bb7acecfSBarry Smith .seealso: `DMCreateColoring()`
3815b0ae01b7SPeter Brune 
3816b0ae01b7SPeter Brune @*/
3817*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3818*d71ae5a4SJacob Faibussowitsch {
3819b0ae01b7SPeter Brune   PetscFunctionBegin;
38205a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3821534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3822b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3823b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3824b0ae01b7SPeter Brune }
3825b0ae01b7SPeter Brune 
38263ad4599aSBarry Smith /*@
3827bb7acecfSBarry Smith     DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38283ad4599aSBarry Smith 
38293ad4599aSBarry Smith     Not Collective
38303ad4599aSBarry Smith 
38313ad4599aSBarry Smith     Input Parameter:
3832bb7acecfSBarry Smith .   dm - the `DM` object
38333ad4599aSBarry Smith 
38343ad4599aSBarry Smith     Output Parameter:
3835bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38363ad4599aSBarry Smith 
38373ad4599aSBarry Smith     Level: developer
38383ad4599aSBarry Smith 
3839bb7acecfSBarry Smith .seealso: `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38403ad4599aSBarry Smith 
38413ad4599aSBarry Smith @*/
3842*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3843*d71ae5a4SJacob Faibussowitsch {
38443ad4599aSBarry Smith   PetscFunctionBegin;
38455a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3846534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
38473ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38483ad4599aSBarry Smith   PetscFunctionReturn(0);
38493ad4599aSBarry Smith }
38503ad4599aSBarry Smith 
3851a7058e45SLawrence Mitchell /*@
3852bb7acecfSBarry Smith     DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3853a7058e45SLawrence Mitchell 
3854a7058e45SLawrence Mitchell     Not Collective
3855a7058e45SLawrence Mitchell 
3856a7058e45SLawrence Mitchell     Input Parameter:
3857bb7acecfSBarry Smith .   dm - the `DM` object
3858a7058e45SLawrence Mitchell 
3859a7058e45SLawrence Mitchell     Output Parameter:
3860bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3861a7058e45SLawrence Mitchell 
3862a7058e45SLawrence Mitchell     Level: developer
3863a7058e45SLawrence Mitchell 
3864bb7acecfSBarry Smith .seealso: `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3865a7058e45SLawrence Mitchell 
3866a7058e45SLawrence Mitchell @*/
3867*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3868*d71ae5a4SJacob Faibussowitsch {
3869a7058e45SLawrence Mitchell   PetscFunctionBegin;
38705a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3871534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3872dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3873ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
3874a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3875a7058e45SLawrence Mitchell }
3876a7058e45SLawrence Mitchell 
38770298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3878264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3879264ace61SBarry Smith 
3880264ace61SBarry Smith /*@C
3881bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3882264ace61SBarry Smith 
3883d083f849SBarry Smith   Collective on dm
3884264ace61SBarry Smith 
3885264ace61SBarry Smith   Input Parameters:
3886bb7acecfSBarry Smith + dm     - The `DM` object
3887bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3888264ace61SBarry Smith 
3889264ace61SBarry Smith   Options Database Key:
3890bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3891264ace61SBarry Smith 
3892264ace61SBarry Smith   Level: intermediate
3893264ace61SBarry Smith 
3894bb7acecfSBarry Smith   Note:
3895bb7acecfSBarry Smith   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPLEXCreateBoxMesh()`
3896bb7acecfSBarry Smith 
3897bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3898264ace61SBarry Smith @*/
3899*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3900*d71ae5a4SJacob Faibussowitsch {
3901264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3902264ace61SBarry Smith   PetscBool match;
3903264ace61SBarry Smith 
3904264ace61SBarry Smith   PetscFunctionBegin;
3905264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39069566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
3907264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3908264ace61SBarry Smith 
39099566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39109566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39117a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3912264ace61SBarry Smith 
3913dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39149566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39159566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39169566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
3917264ace61SBarry Smith   PetscFunctionReturn(0);
3918264ace61SBarry Smith }
3919264ace61SBarry Smith 
3920264ace61SBarry Smith /*@C
3921bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3922264ace61SBarry Smith 
3923264ace61SBarry Smith   Not Collective
3924264ace61SBarry Smith 
3925264ace61SBarry Smith   Input Parameter:
3926bb7acecfSBarry Smith . dm  - The `DM`
3927264ace61SBarry Smith 
3928264ace61SBarry Smith   Output Parameter:
3929bb7acecfSBarry Smith . type - The `DMType` name
3930264ace61SBarry Smith 
3931264ace61SBarry Smith   Level: intermediate
3932264ace61SBarry Smith 
3933bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3934264ace61SBarry Smith @*/
3935*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3936*d71ae5a4SJacob Faibussowitsch {
3937264ace61SBarry Smith   PetscFunctionBegin;
3938264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3939c959eef4SJed Brown   PetscValidPointer(type, 2);
39409566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3941264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3942264ace61SBarry Smith   PetscFunctionReturn(0);
3943264ace61SBarry Smith }
3944264ace61SBarry Smith 
394567a56275SMatthew G Knepley /*@C
3946bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
394767a56275SMatthew G Knepley 
3948d083f849SBarry Smith   Collective on dm
394967a56275SMatthew G Knepley 
395067a56275SMatthew G Knepley   Input Parameters:
3951bb7acecfSBarry Smith + dm - the `DM`
3952bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
395367a56275SMatthew G Knepley 
395467a56275SMatthew G Knepley   Output Parameter:
3955bb7acecfSBarry Smith . M - pointer to new `DM`
395667a56275SMatthew G Knepley 
395767a56275SMatthew G Knepley   Notes:
3958bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3959bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3960bb7acecfSBarry Smith   of the input `DM`.
396167a56275SMatthew G Knepley 
396267a56275SMatthew G Knepley   Level: intermediate
396367a56275SMatthew G Knepley 
3964bb7acecfSBarry Smith .seealso: `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
396567a56275SMatthew G Knepley @*/
3966*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3967*d71ae5a4SJacob Faibussowitsch {
396867a56275SMatthew G Knepley   DM        B;
396967a56275SMatthew G Knepley   char      convname[256];
3970c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
397167a56275SMatthew G Knepley 
397267a56275SMatthew G Knepley   PetscFunctionBegin;
397367a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
397467a56275SMatthew G Knepley   PetscValidType(dm, 1);
397567a56275SMatthew G Knepley   PetscValidPointer(M, 3);
39769566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
39779566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3978c067b6caSMatthew G. Knepley   if (sametype) {
3979c067b6caSMatthew G. Knepley     *M = dm;
39809566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
3981c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3982c067b6caSMatthew G. Knepley   } else {
39830298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
398467a56275SMatthew G Knepley 
398567a56275SMatthew G Knepley     /*
398667a56275SMatthew G Knepley        Order of precedence:
398767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
398867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
398967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
399067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
399167a56275SMatthew G Knepley        5) Use a really basic converter.
399267a56275SMatthew G Knepley     */
399367a56275SMatthew G Knepley 
399467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
39959566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39969566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
39979566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
39989566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
39999566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40009566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
400167a56275SMatthew G Knepley     if (conv) goto foundconv;
400267a56275SMatthew G Knepley 
400367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40049566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40059566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40069566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40079566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40089566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40099566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40109566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40119566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
401267a56275SMatthew G Knepley     if (conv) {
40139566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
401467a56275SMatthew G Knepley       goto foundconv;
401567a56275SMatthew G Knepley     }
401667a56275SMatthew G Knepley 
401767a56275SMatthew G Knepley #if 0
401867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
401967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40209566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
402167a56275SMatthew G Knepley     if (conv) goto foundconv;
402267a56275SMatthew G Knepley 
402367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
402467a56275SMatthew G Knepley     if (dm->ops->convert) {
402567a56275SMatthew G Knepley       conv = dm->ops->convert;
402667a56275SMatthew G Knepley     }
402767a56275SMatthew G Knepley     if (conv) goto foundconv;
402867a56275SMatthew G Knepley #endif
402967a56275SMatthew G Knepley 
403067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
403198921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
403267a56275SMatthew G Knepley 
403367a56275SMatthew G Knepley   foundconv:
40349566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40359566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
403612fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
403790b157c4SStefano Zampini     {
40384fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40396858538eSMatthew G. Knepley 
40404fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40414fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4042c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40439566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40449566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40459566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40469566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
404712fa691eSMatthew G. Knepley     }
40489566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
404967a56275SMatthew G Knepley   }
40509566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
405167a56275SMatthew G Knepley   PetscFunctionReturn(0);
405267a56275SMatthew G Knepley }
4053264ace61SBarry Smith 
4054264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4055264ace61SBarry Smith 
4056264ace61SBarry Smith /*@C
4057bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40581c84c290SBarry Smith 
40591c84c290SBarry Smith   Not Collective
40601c84c290SBarry Smith 
40611c84c290SBarry Smith   Input Parameters:
40621c84c290SBarry Smith + name        - The name of a new user-defined creation routine
40631c84c290SBarry Smith - create_func - The creation routine itself
40641c84c290SBarry Smith 
40651c84c290SBarry Smith   Notes:
4066bb7acecfSBarry Smith   DMRegister() may be called multiple times to add several user-defined `DM`s
40671c84c290SBarry Smith 
40681c84c290SBarry Smith   Sample usage:
40691c84c290SBarry Smith .vb
4070bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40711c84c290SBarry Smith .ve
40721c84c290SBarry Smith 
40731c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
40741c84c290SBarry Smith .vb
40751c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
40761c84c290SBarry Smith     DMSetType(DM,"my_da");
40771c84c290SBarry Smith .ve
40781c84c290SBarry Smith    or at runtime via the option
40791c84c290SBarry Smith .vb
40801c84c290SBarry Smith     -da_type my_da
40811c84c290SBarry Smith .ve
4082264ace61SBarry Smith 
4083264ace61SBarry Smith   Level: advanced
40841c84c290SBarry Smith 
4085bb7acecfSBarry Smith .seealso: `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
40861c84c290SBarry Smith 
4087264ace61SBarry Smith @*/
4088*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4089*d71ae5a4SJacob Faibussowitsch {
4090264ace61SBarry Smith   PetscFunctionBegin;
40919566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
40929566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
4093264ace61SBarry Smith   PetscFunctionReturn(0);
4094264ace61SBarry Smith }
4095264ace61SBarry Smith 
4096b859378eSBarry Smith /*@C
4097bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4098b859378eSBarry Smith 
4099d083f849SBarry Smith   Collective on viewer
4100b859378eSBarry Smith 
4101b859378eSBarry Smith   Input Parameters:
4102bb7acecfSBarry Smith + newdm - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4103bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4104bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4105bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4106b859378eSBarry Smith 
4107b859378eSBarry Smith    Level: intermediate
4108b859378eSBarry Smith 
4109b859378eSBarry Smith   Notes:
411055849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4111b859378eSBarry Smith 
4112bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4113bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4114bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4115cd7e8a5eSksagiyam 
4116b859378eSBarry Smith   Notes for advanced users:
4117b859378eSBarry Smith   Most users should not need to know the details of the binary storage
4118bb7acecfSBarry Smith   format, since `DMLoad()` and `DMView()` completely hide these details.
4119b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
4120b859378eSBarry Smith   format is
4121b859378eSBarry Smith .vb
4122b859378eSBarry Smith      has not yet been determined
4123b859378eSBarry Smith .ve
4124b859378eSBarry Smith 
4125db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4126b859378eSBarry Smith @*/
4127*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4128*d71ae5a4SJacob Faibussowitsch {
41299331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4130b859378eSBarry Smith 
4131b859378eSBarry Smith   PetscFunctionBegin;
4132b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4133b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41349566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41359566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41369566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41379566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41389331c7a4SMatthew G. Knepley   if (isbinary) {
41399331c7a4SMatthew G. Knepley     PetscInt classid;
41409331c7a4SMatthew G. Knepley     char     type[256];
4141b859378eSBarry Smith 
41429566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41437a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41449566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41459566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4146dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41479331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4148dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41499331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41509566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
4151b859378eSBarry Smith   PetscFunctionReturn(0);
4152b859378eSBarry Smith }
4153b859378eSBarry Smith 
41547da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41557da65231SMatthew G Knepley 
4156*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4157*d71ae5a4SJacob Faibussowitsch {
41581d47ebbbSSatish Balay   PetscInt f;
41591b30c384SMatthew G Knepley 
41607da65231SMatthew G Knepley   PetscFunctionBegin;
416163a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
416248a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41637da65231SMatthew G Knepley   PetscFunctionReturn(0);
41647da65231SMatthew G Knepley }
41657da65231SMatthew G Knepley 
4166*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4167*d71ae5a4SJacob Faibussowitsch {
41681b30c384SMatthew G Knepley   PetscInt f, g;
41697da65231SMatthew G Knepley 
41707da65231SMatthew G Knepley   PetscFunctionBegin;
417163a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41721d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
41739566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
417448a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
41759566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
41767da65231SMatthew G Knepley   }
41777da65231SMatthew G Knepley   PetscFunctionReturn(0);
41787da65231SMatthew G Knepley }
4179e7c4fc90SDmitry Karpeev 
4180*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4181*d71ae5a4SJacob Faibussowitsch {
41820c5b8624SToby Isaac   PetscInt           localSize, bs;
41830c5b8624SToby Isaac   PetscMPIInt        size;
41840c5b8624SToby Isaac   Vec                x, xglob;
41850c5b8624SToby Isaac   const PetscScalar *xarray;
4186e759306cSMatthew G. Knepley 
4187e759306cSMatthew G. Knepley   PetscFunctionBegin;
41889566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
41899566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
41909566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
41919566063dSJacob Faibussowitsch   PetscCall(VecChop(x, tol));
41929566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
41930c5b8624SToby Isaac   if (size > 1) {
41949566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
41959566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
41969566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
41979566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
41980c5b8624SToby Isaac   } else {
41990c5b8624SToby Isaac     xglob = x;
42000c5b8624SToby Isaac   }
42019566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42020c5b8624SToby Isaac   if (size > 1) {
42039566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42049566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42050c5b8624SToby Isaac   }
42069566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
4207e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4208e759306cSMatthew G. Knepley }
4209e759306cSMatthew G. Knepley 
421088ed4aceSMatthew G Knepley /*@
4211bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4212061576a5SJed Brown 
4213061576a5SJed Brown   Input Parameter:
4214bb7acecfSBarry Smith . dm - The `DM`
4215061576a5SJed Brown 
4216061576a5SJed Brown   Output Parameter:
4217bb7acecfSBarry Smith . section - The `PetscSection`
4218061576a5SJed Brown 
4219061576a5SJed Brown   Options Database Keys:
4220bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4221061576a5SJed Brown 
4222061576a5SJed Brown   Level: advanced
4223061576a5SJed Brown 
4224061576a5SJed Brown   Notes:
4225bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4226061576a5SJed Brown 
4227bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4228061576a5SJed Brown 
4229db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4230061576a5SJed Brown @*/
4231*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4232*d71ae5a4SJacob Faibussowitsch {
4233061576a5SJed Brown   PetscFunctionBegin;
42349566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
4235061576a5SJed Brown   PetscFunctionReturn(0);
4236061576a5SJed Brown }
4237061576a5SJed Brown 
4238061576a5SJed Brown /*@
4239bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
424088ed4aceSMatthew G Knepley 
424188ed4aceSMatthew G Knepley   Input Parameter:
4242bb7acecfSBarry Smith . dm - The `DM`
424388ed4aceSMatthew G Knepley 
424488ed4aceSMatthew G Knepley   Output Parameter:
4245bb7acecfSBarry Smith . section - The `PetscSection`
424688ed4aceSMatthew G Knepley 
4247e5893cccSMatthew G. Knepley   Options Database Keys:
4248bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4249e5893cccSMatthew G. Knepley 
425088ed4aceSMatthew G Knepley   Level: intermediate
425188ed4aceSMatthew G Knepley 
4252bb7acecfSBarry Smith   Note:
4253bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
425488ed4aceSMatthew G Knepley 
4255db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetGlobalSection()`
425688ed4aceSMatthew G Knepley @*/
4257*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4258*d71ae5a4SJacob Faibussowitsch {
425988ed4aceSMatthew G Knepley   PetscFunctionBegin;
426088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
426188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42621bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4263e5e52638SMatthew G. Knepley     PetscInt d;
4264e5e52638SMatthew G. Knepley 
426545480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
426645480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
426745480ffeSMatthew G. Knepley       PetscViewer       viewer;
426845480ffeSMatthew G. Knepley       PetscViewerFormat format;
426945480ffeSMatthew G. Knepley       PetscBool         flg;
427045480ffeSMatthew G. Knepley 
42719566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
42729566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
427345480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
42749566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
42759566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
427645480ffeSMatthew G. Knepley       }
427745480ffeSMatthew G. Knepley       if (flg) {
42789566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
42799566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
42809566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
428145480ffeSMatthew G. Knepley       }
428245480ffeSMatthew G. Knepley     }
4283dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
42849566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
42852f0f8703SMatthew G. Knepley   }
42861bb6d2a8SBarry Smith   *section = dm->localSection;
428788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
428888ed4aceSMatthew G Knepley }
428988ed4aceSMatthew G Knepley 
429088ed4aceSMatthew G Knepley /*@
4291bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4292061576a5SJed Brown 
4293061576a5SJed Brown   Input Parameters:
4294bb7acecfSBarry Smith + dm - The `DM`
4295bb7acecfSBarry Smith - section - The `PetscSection`
4296061576a5SJed Brown 
4297061576a5SJed Brown   Level: advanced
4298061576a5SJed Brown 
4299061576a5SJed Brown   Notes:
4300bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4301061576a5SJed Brown 
4302bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4303061576a5SJed Brown 
4304db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4305061576a5SJed Brown @*/
4306*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4307*d71ae5a4SJacob Faibussowitsch {
4308061576a5SJed Brown   PetscFunctionBegin;
43099566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
4310061576a5SJed Brown   PetscFunctionReturn(0);
4311061576a5SJed Brown }
4312061576a5SJed Brown 
4313061576a5SJed Brown /*@
4314bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
431588ed4aceSMatthew G Knepley 
431688ed4aceSMatthew G Knepley   Input Parameters:
4317bb7acecfSBarry Smith + dm - The `DM`
4318bb7acecfSBarry Smith - section - The `PetscSection`
431988ed4aceSMatthew G Knepley 
432088ed4aceSMatthew G Knepley   Level: intermediate
432188ed4aceSMatthew G Knepley 
4322bb7acecfSBarry Smith   Note:
4323bb7acecfSBarry Smith   Any existing Section will be destroyed
432488ed4aceSMatthew G Knepley 
4325bb7acecfSBarry Smith .seealso: `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
432688ed4aceSMatthew G Knepley @*/
4327*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4328*d71ae5a4SJacob Faibussowitsch {
4329c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4330af122d2aSMatthew G Knepley   PetscInt f;
433188ed4aceSMatthew G Knepley 
433288ed4aceSMatthew G Knepley   PetscFunctionBegin;
433388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4334b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43359566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43369566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43371bb6d2a8SBarry Smith   dm->localSection = section;
43389566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4339af122d2aSMatthew G Knepley   if (numFields) {
43409566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4341af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43420f21e855SMatthew G. Knepley       PetscObject disc;
4343af122d2aSMatthew G Knepley       const char *name;
4344af122d2aSMatthew G Knepley 
43459566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43469566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43479566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4348af122d2aSMatthew G Knepley     }
4349af122d2aSMatthew G Knepley   }
4350e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43519566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
435288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
435388ed4aceSMatthew G Knepley }
435488ed4aceSMatthew G Knepley 
43559435951eSToby Isaac /*@
4356bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
43579435951eSToby Isaac 
4358e228b242SToby Isaac   not collective
4359e228b242SToby Isaac 
43609435951eSToby Isaac   Input Parameter:
4361bb7acecfSBarry Smith . dm - The `DM`
43629435951eSToby Isaac 
4363d8d19677SJose E. Roman   Output Parameters:
4364bb7acecfSBarry 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.
4365bb7acecfSBarry 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.
436679769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
43679435951eSToby Isaac 
43689435951eSToby Isaac   Level: advanced
43699435951eSToby Isaac 
4370bb7acecfSBarry Smith   Note:
4371bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
43729435951eSToby Isaac 
4373db781477SPatrick Sanan .seealso: `DMSetDefaultConstraints()`
43749435951eSToby Isaac @*/
4375*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4376*d71ae5a4SJacob Faibussowitsch {
43779435951eSToby Isaac   PetscFunctionBegin;
43789435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4379dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
43803b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
43813b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
438279769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
43839435951eSToby Isaac   PetscFunctionReturn(0);
43849435951eSToby Isaac }
43859435951eSToby Isaac 
43869435951eSToby Isaac /*@
4387bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
43889435951eSToby Isaac 
4389bb7acecfSBarry 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()`.
43909435951eSToby Isaac 
4391bb7acecfSBarry 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.
43929435951eSToby Isaac 
4393e228b242SToby Isaac   collective on dm
4394e228b242SToby Isaac 
43959435951eSToby Isaac   Input Parameters:
4396bb7acecfSBarry Smith + dm - The `DM`
4397bb7acecfSBarry 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).
4398bb7acecfSBarry 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).
4399bb7acecfSBarry 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).
44009435951eSToby Isaac 
44019435951eSToby Isaac   Level: advanced
44029435951eSToby Isaac 
4403bb7acecfSBarry Smith   Note:
4404bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44059435951eSToby Isaac 
4406db781477SPatrick Sanan .seealso: `DMGetDefaultConstraints()`
44079435951eSToby Isaac @*/
4408*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4409*d71ae5a4SJacob Faibussowitsch {
4410e228b242SToby Isaac   PetscMPIInt result;
44119435951eSToby Isaac 
44129435951eSToby Isaac   PetscFunctionBegin;
44139435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4414e228b242SToby Isaac   if (section) {
4415e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44169566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44177a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4418e228b242SToby Isaac   }
4419e228b242SToby Isaac   if (mat) {
4420e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44219566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44227a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4423e228b242SToby Isaac   }
442479769bd5SJed Brown   if (bias) {
442579769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44269566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
442779769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
442879769bd5SJed Brown   }
44299566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44309566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44313b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44329566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44339566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44343b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44359566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44369566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
443779769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44389435951eSToby Isaac   PetscFunctionReturn(0);
44399435951eSToby Isaac }
44409435951eSToby Isaac 
4441497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4442507e4973SMatthew G. Knepley /*
4443bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4444507e4973SMatthew G. Knepley 
4445507e4973SMatthew G. Knepley   Input Parameters:
4446bb7acecfSBarry Smith + dm - The `DM`
4447bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4448bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4449507e4973SMatthew G. Knepley 
4450507e4973SMatthew G. Knepley   Level: intermediate
4451507e4973SMatthew G. Knepley 
4452db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`
4453507e4973SMatthew G. Knepley */
4454*d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4455*d71ae5a4SJacob Faibussowitsch {
4456507e4973SMatthew G. Knepley   MPI_Comm        comm;
4457507e4973SMatthew G. Knepley   PetscLayout     layout;
4458507e4973SMatthew G. Knepley   const PetscInt *ranges;
4459507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4460507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4461507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4462507e4973SMatthew G. Knepley 
4463507e4973SMatthew G. Knepley   PetscFunctionBegin;
44649566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4465507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44669566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
44679566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44689566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
44699566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
44709566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
44719566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
44729566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
44739566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
44749566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4475507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4476f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4477507e4973SMatthew G. Knepley 
44789566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
44799566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
44809566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
44819566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
44829566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
44839566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4484507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
44859371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
44869371c9d4SSatish 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));
44879371c9d4SSatish Balay       valid = PETSC_FALSE;
44889371c9d4SSatish Balay     }
44899371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
44909371c9d4SSatish 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));
44919371c9d4SSatish Balay       valid = PETSC_FALSE;
44929371c9d4SSatish Balay     }
4493507e4973SMatthew G. Knepley     if (gdof < 0) {
4494507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4495507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4496507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4497507e4973SMatthew G. Knepley 
44989566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4499507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45009371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45019371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45029371c9d4SSatish Balay           valid = PETSC_FALSE;
45039371c9d4SSatish Balay           break;
45049371c9d4SSatish Balay         }
4505507e4973SMatthew G. Knepley       }
4506507e4973SMatthew G. Knepley     }
4507507e4973SMatthew G. Knepley   }
45089566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45099566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
45101c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4511507e4973SMatthew G. Knepley   if (!gvalid) {
45129566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4513507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4514507e4973SMatthew G. Knepley   }
4515507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4516507e4973SMatthew G. Knepley }
4517f741bcd2SMatthew G. Knepley #endif
4518507e4973SMatthew G. Knepley 
451988ed4aceSMatthew G Knepley /*@
4520bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
452188ed4aceSMatthew G Knepley 
4522d083f849SBarry Smith   Collective on dm
45238b1ab98fSJed Brown 
452488ed4aceSMatthew G Knepley   Input Parameter:
4525bb7acecfSBarry Smith . dm - The `DM`
452688ed4aceSMatthew G Knepley 
452788ed4aceSMatthew G Knepley   Output Parameter:
4528bb7acecfSBarry Smith . section - The `PetscSection`
452988ed4aceSMatthew G Knepley 
453088ed4aceSMatthew G Knepley   Level: intermediate
453188ed4aceSMatthew G Knepley 
4532bb7acecfSBarry Smith   Note:
4533bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
453488ed4aceSMatthew G Knepley 
4535db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`
453688ed4aceSMatthew G Knepley @*/
4537*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4538*d71ae5a4SJacob Faibussowitsch {
453988ed4aceSMatthew G Knepley   PetscFunctionBegin;
454088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
454188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
45421bb6d2a8SBarry Smith   if (!dm->globalSection) {
4543fd59a867SMatthew G. Knepley     PetscSection s;
4544fd59a867SMatthew G. Knepley 
45459566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
45467a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
45477a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45489566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
45499566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
45509566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
45519566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
455288ed4aceSMatthew G Knepley   }
45531bb6d2a8SBarry Smith   *section = dm->globalSection;
455488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
455588ed4aceSMatthew G Knepley }
455688ed4aceSMatthew G Knepley 
4557b21d0597SMatthew G Knepley /*@
4558bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4559b21d0597SMatthew G Knepley 
4560b21d0597SMatthew G Knepley   Input Parameters:
4561bb7acecfSBarry Smith + dm - The `DM`
45625080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4563b21d0597SMatthew G Knepley 
4564b21d0597SMatthew G Knepley   Level: intermediate
4565b21d0597SMatthew G Knepley 
4566bb7acecfSBarry Smith   Note:
4567bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4568b21d0597SMatthew G Knepley 
4569db781477SPatrick Sanan .seealso: `DMGetGlobalSection()`, `DMSetLocalSection()`
4570b21d0597SMatthew G Knepley @*/
4571*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4572*d71ae5a4SJacob Faibussowitsch {
4573b21d0597SMatthew G Knepley   PetscFunctionBegin;
4574b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45755080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
45769566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45779566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
45781bb6d2a8SBarry Smith   dm->globalSection = section;
4579497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
45809566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4581507e4973SMatthew G. Knepley #endif
4582b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4583b21d0597SMatthew G Knepley }
4584b21d0597SMatthew G Knepley 
458588ed4aceSMatthew G Knepley /*@
4586bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4587bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
458888ed4aceSMatthew G Knepley 
458988ed4aceSMatthew G Knepley   Input Parameter:
4590bb7acecfSBarry Smith . dm - The `DM`
459188ed4aceSMatthew G Knepley 
459288ed4aceSMatthew G Knepley   Output Parameter:
4593bb7acecfSBarry Smith . sf - The `PetscSF`
459488ed4aceSMatthew G Knepley 
459588ed4aceSMatthew G Knepley   Level: intermediate
459688ed4aceSMatthew G Knepley 
4597bb7acecfSBarry Smith   Note:
4598bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
459988ed4aceSMatthew G Knepley 
4600db781477SPatrick Sanan .seealso: `DMSetSectionSF()`, `DMCreateSectionSF()`
460188ed4aceSMatthew G Knepley @*/
4602*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4603*d71ae5a4SJacob Faibussowitsch {
460488ed4aceSMatthew G Knepley   PetscInt nroots;
460588ed4aceSMatthew G Knepley 
460688ed4aceSMatthew G Knepley   PetscFunctionBegin;
460788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
460888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
460948a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
46109566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
461188ed4aceSMatthew G Knepley   if (nroots < 0) {
461288ed4aceSMatthew G Knepley     PetscSection section, gSection;
461388ed4aceSMatthew G Knepley 
46149566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
461531ea6d37SMatthew G Knepley     if (section) {
46169566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
46179566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
461831ea6d37SMatthew G Knepley     } else {
46190298fd71SBarry Smith       *sf = NULL;
462031ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
462131ea6d37SMatthew G Knepley     }
462288ed4aceSMatthew G Knepley   }
46231bb6d2a8SBarry Smith   *sf = dm->sectionSF;
462488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
462588ed4aceSMatthew G Knepley }
462688ed4aceSMatthew G Knepley 
462788ed4aceSMatthew G Knepley /*@
4628bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
462988ed4aceSMatthew G Knepley 
463088ed4aceSMatthew G Knepley   Input Parameters:
4631bb7acecfSBarry Smith + dm - The `DM`
4632bb7acecfSBarry Smith - sf - The `PetscSF`
463388ed4aceSMatthew G Knepley 
463488ed4aceSMatthew G Knepley   Level: intermediate
463588ed4aceSMatthew G Knepley 
4636bb7acecfSBarry Smith   Note:
4637bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
463888ed4aceSMatthew G Knepley 
4639db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMCreateSectionSF()`
464088ed4aceSMatthew G Knepley @*/
4641*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4642*d71ae5a4SJacob Faibussowitsch {
464388ed4aceSMatthew G Knepley   PetscFunctionBegin;
464488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4645b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46469566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46479566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
46481bb6d2a8SBarry Smith   dm->sectionSF = sf;
464988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
465088ed4aceSMatthew G Knepley }
465188ed4aceSMatthew G Knepley 
465288ed4aceSMatthew G Knepley /*@C
4653bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
465488ed4aceSMatthew G Knepley   describing the data layout.
465588ed4aceSMatthew G Knepley 
465688ed4aceSMatthew G Knepley   Input Parameters:
4657bb7acecfSBarry Smith + dm - The `DM`
4658bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4659bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
466088ed4aceSMatthew G Knepley 
46611bb6d2a8SBarry Smith   Level: developer
46621bb6d2a8SBarry Smith 
4663bb7acecfSBarry Smith   Note:
4664bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4665bb7acecfSBarry Smith 
4666bb7acecfSBarry Smith   Developer Note:
4667bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4668bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4669bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
46701bb6d2a8SBarry Smith 
4671db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
467288ed4aceSMatthew G Knepley @*/
4673*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4674*d71ae5a4SJacob Faibussowitsch {
467588ed4aceSMatthew G Knepley   PetscFunctionBegin;
467688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46779566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
467888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
467988ed4aceSMatthew G Knepley }
4680af122d2aSMatthew G Knepley 
4681b21d0597SMatthew G Knepley /*@
4682bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4683bb7acecfSBarry Smith 
4684bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4685b21d0597SMatthew G Knepley 
4686b21d0597SMatthew G Knepley   Input Parameter:
4687bb7acecfSBarry Smith . dm - The `DM`
4688b21d0597SMatthew G Knepley 
4689b21d0597SMatthew G Knepley   Output Parameter:
4690bb7acecfSBarry Smith . sf - The `PetscSF`
4691b21d0597SMatthew G Knepley 
4692b21d0597SMatthew G Knepley   Level: intermediate
4693b21d0597SMatthew G Knepley 
4694bb7acecfSBarry Smith   Note:
4695bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4696b21d0597SMatthew G Knepley 
4697db781477SPatrick Sanan .seealso: `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4698b21d0597SMatthew G Knepley @*/
4699*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4700*d71ae5a4SJacob Faibussowitsch {
4701b21d0597SMatthew G Knepley   PetscFunctionBegin;
4702b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4703b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4704b21d0597SMatthew G Knepley   *sf = dm->sf;
4705b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4706b21d0597SMatthew G Knepley }
4707b21d0597SMatthew G Knepley 
4708057b4bcdSMatthew G Knepley /*@
4709bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4710bb7acecfSBarry Smith 
4711bb7acecfSBarry Smith   Collective on dm
4712057b4bcdSMatthew G Knepley 
4713057b4bcdSMatthew G Knepley   Input Parameters:
4714bb7acecfSBarry Smith + dm - The `DM`
4715bb7acecfSBarry Smith - sf - The` PetscSF`
4716057b4bcdSMatthew G Knepley 
4717057b4bcdSMatthew G Knepley   Level: intermediate
4718057b4bcdSMatthew G Knepley 
4719db781477SPatrick Sanan .seealso: `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4720057b4bcdSMatthew G Knepley @*/
4721*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4722*d71ae5a4SJacob Faibussowitsch {
4723057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4724057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4725b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47269566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47279566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4728057b4bcdSMatthew G Knepley   dm->sf = sf;
4729057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4730057b4bcdSMatthew G Knepley }
4731057b4bcdSMatthew G Knepley 
47324f37162bSMatthew G. Knepley /*@
4733bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
47344f37162bSMatthew G. Knepley 
47354f37162bSMatthew G. Knepley   Input Parameter:
4736bb7acecfSBarry Smith . dm - The `DM`
47374f37162bSMatthew G. Knepley 
47384f37162bSMatthew G. Knepley   Output Parameter:
4739bb7acecfSBarry Smith . sf - The `PetscSF`
47404f37162bSMatthew G. Knepley 
47414f37162bSMatthew G. Knepley   Level: intermediate
47424f37162bSMatthew G. Knepley 
4743bb7acecfSBarry Smith   Note:
4744bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
47454f37162bSMatthew G. Knepley 
4746db781477SPatrick Sanan .seealso: `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47474f37162bSMatthew G. Knepley @*/
4748*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4749*d71ae5a4SJacob Faibussowitsch {
47504f37162bSMatthew G. Knepley   PetscFunctionBegin;
47514f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47524f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
47534f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
47544f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
47554f37162bSMatthew G. Knepley }
47564f37162bSMatthew G. Knepley 
47574f37162bSMatthew G. Knepley /*@
47584f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
47594f37162bSMatthew G. Knepley 
47604f37162bSMatthew G. Knepley   Input Parameters:
47614f37162bSMatthew G. Knepley + dm - The DM
47624f37162bSMatthew G. Knepley - sf - The PetscSF
47634f37162bSMatthew G. Knepley 
47644f37162bSMatthew G. Knepley   Level: intermediate
47654f37162bSMatthew G. Knepley 
4766db781477SPatrick Sanan .seealso: `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47674f37162bSMatthew G. Knepley @*/
4768*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4769*d71ae5a4SJacob Faibussowitsch {
47704f37162bSMatthew G. Knepley   PetscFunctionBegin;
47714f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47724f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47739566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47749566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
47754f37162bSMatthew G. Knepley   dm->sfNatural = sf;
47764f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
47774f37162bSMatthew G. Knepley }
47784f37162bSMatthew G. Knepley 
4779*d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4780*d71ae5a4SJacob Faibussowitsch {
478134aa8a36SMatthew G. Knepley   PetscClassId id;
478234aa8a36SMatthew G. Knepley 
478334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
47849566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
478534aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
47869566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
478734aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
47889566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
478917c1d62eSMatthew G. Knepley   } else {
47909566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
479134aa8a36SMatthew G. Knepley   }
479234aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
479334aa8a36SMatthew G. Knepley }
479434aa8a36SMatthew G. Knepley 
4795*d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4796*d71ae5a4SJacob Faibussowitsch {
479744a7f3ddSMatthew G. Knepley   RegionField *tmpr;
479844a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
479944a7f3ddSMatthew G. Knepley 
480044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
480144a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
48029566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
480344a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
48049371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
48059371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48069371c9d4SSatish Balay     tmpr[f].label       = NULL;
48079371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
48089371c9d4SSatish Balay   }
48099566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
481044a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
481144a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
481244a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
481344a7f3ddSMatthew G. Knepley }
481444a7f3ddSMatthew G. Knepley 
481544a7f3ddSMatthew G. Knepley /*@
481644a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
481744a7f3ddSMatthew G. Knepley 
4818d083f849SBarry Smith   Logically collective on dm
481944a7f3ddSMatthew G. Knepley 
482044a7f3ddSMatthew G. Knepley   Input Parameter:
482144a7f3ddSMatthew G. Knepley . dm - The DM
482244a7f3ddSMatthew G. Knepley 
482344a7f3ddSMatthew G. Knepley   Level: intermediate
482444a7f3ddSMatthew G. Knepley 
4825db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
482644a7f3ddSMatthew G. Knepley @*/
4827*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4828*d71ae5a4SJacob Faibussowitsch {
482944a7f3ddSMatthew G. Knepley   PetscInt f;
483044a7f3ddSMatthew G. Knepley 
483144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
483244a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
483344a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
48349566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
48359566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
483644a7f3ddSMatthew G. Knepley   }
48379566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
483844a7f3ddSMatthew G. Knepley   dm->fields = NULL;
483944a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
484044a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
484144a7f3ddSMatthew G. Knepley }
484244a7f3ddSMatthew G. Knepley 
4843689b5837SMatthew G. Knepley /*@
4844689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4845689b5837SMatthew G. Knepley 
4846689b5837SMatthew G. Knepley   Not collective
4847689b5837SMatthew G. Knepley 
4848689b5837SMatthew G. Knepley   Input Parameter:
4849689b5837SMatthew G. Knepley . dm - The DM
4850689b5837SMatthew G. Knepley 
4851689b5837SMatthew G. Knepley   Output Parameter:
4852689b5837SMatthew G. Knepley . Nf - The number of fields
4853689b5837SMatthew G. Knepley 
4854689b5837SMatthew G. Knepley   Level: intermediate
4855689b5837SMatthew G. Knepley 
4856db781477SPatrick Sanan .seealso: `DMSetNumFields()`, `DMSetField()`
4857689b5837SMatthew G. Knepley @*/
4858*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4859*d71ae5a4SJacob Faibussowitsch {
48600f21e855SMatthew G. Knepley   PetscFunctionBegin;
48610f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4862534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
486344a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4864af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4865af122d2aSMatthew G Knepley }
4866af122d2aSMatthew G Knepley 
4867689b5837SMatthew G. Knepley /*@
4868689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4869689b5837SMatthew G. Knepley 
4870d083f849SBarry Smith   Logically collective on dm
4871689b5837SMatthew G. Knepley 
4872689b5837SMatthew G. Knepley   Input Parameters:
4873689b5837SMatthew G. Knepley + dm - The DM
4874689b5837SMatthew G. Knepley - Nf - The number of fields
4875689b5837SMatthew G. Knepley 
4876689b5837SMatthew G. Knepley   Level: intermediate
4877689b5837SMatthew G. Knepley 
4878db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetField()`
4879689b5837SMatthew G. Knepley @*/
4880*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4881*d71ae5a4SJacob Faibussowitsch {
48820f21e855SMatthew G. Knepley   PetscInt Nf, f;
4883af122d2aSMatthew G Knepley 
4884af122d2aSMatthew G Knepley   PetscFunctionBegin;
4885af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48869566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
48870f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
48880f21e855SMatthew G. Knepley     PetscContainer obj;
48890f21e855SMatthew G. Knepley 
48909566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
48919566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
48929566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4893af122d2aSMatthew G Knepley   }
4894af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4895af122d2aSMatthew G Knepley }
4896af122d2aSMatthew G Knepley 
4897c1929be8SMatthew G. Knepley /*@
4898bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4899c1929be8SMatthew G. Knepley 
4900c1929be8SMatthew G. Knepley   Not collective
4901c1929be8SMatthew G. Knepley 
4902c1929be8SMatthew G. Knepley   Input Parameters:
4903bb7acecfSBarry Smith + dm - The `DM`
4904c1929be8SMatthew G. Knepley - f  - The field number
4905c1929be8SMatthew G. Knepley 
490644a7f3ddSMatthew G. Knepley   Output Parameters:
4907bb7acecfSBarry Smith + label - The label indicating the support of the field, or NULL for the entire mesh (pass in NULL if not needed)
4908bb7acecfSBarry Smith - disc - The discretization object (pass in NULL if not needed)
4909c1929be8SMatthew G. Knepley 
491044a7f3ddSMatthew G. Knepley   Level: intermediate
4911c1929be8SMatthew G. Knepley 
4912db781477SPatrick Sanan .seealso: `DMAddField()`, `DMSetField()`
4913c1929be8SMatthew G. Knepley @*/
4914*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4915*d71ae5a4SJacob Faibussowitsch {
4916af122d2aSMatthew G Knepley   PetscFunctionBegin;
4917af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4918bb7acecfSBarry Smith   PetscValidPointer(disc, 4);
49197a8be351SBarry 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);
492044a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4921bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
4922decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4923decb47aaSMatthew G. Knepley }
4924decb47aaSMatthew G. Knepley 
4925083401c6SMatthew G. Knepley /* Does not clear the DS */
4926*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4927*d71ae5a4SJacob Faibussowitsch {
4928083401c6SMatthew G. Knepley   PetscFunctionBegin;
49299566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
49309566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
49319566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4932083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4933bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
49349566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4935bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
4936083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4937083401c6SMatthew G. Knepley }
4938083401c6SMatthew G. Knepley 
4939c1929be8SMatthew G. Knepley /*@
4940bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
4941bb7acecfSBarry Smith   the field numbering.
4942c1929be8SMatthew G. Knepley 
4943d083f849SBarry Smith   Logically collective on dm
4944c1929be8SMatthew G. Knepley 
4945c1929be8SMatthew G. Knepley   Input Parameters:
4946bb7acecfSBarry Smith + dm    - The `DM`
4947c1929be8SMatthew G. Knepley . f     - The field number
494844a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4949bb7acecfSBarry Smith - disc - The discretization object
4950c1929be8SMatthew G. Knepley 
495144a7f3ddSMatthew G. Knepley   Level: intermediate
4952c1929be8SMatthew G. Knepley 
4953db781477SPatrick Sanan .seealso: `DMAddField()`, `DMGetField()`
4954c1929be8SMatthew G. Knepley @*/
4955*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4956*d71ae5a4SJacob Faibussowitsch {
4957decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4958decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4959e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
4960bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
49617a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
4962bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
4963bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
49649566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
496544a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
496644a7f3ddSMatthew G. Knepley }
496744a7f3ddSMatthew G. Knepley 
496844a7f3ddSMatthew G. Knepley /*@
4969bb7acecfSBarry 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)
4970bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
497144a7f3ddSMatthew G. Knepley 
4972d083f849SBarry Smith   Logically collective on dm
497344a7f3ddSMatthew G. Knepley 
497444a7f3ddSMatthew G. Knepley   Input Parameters:
4975bb7acecfSBarry Smith + dm    - The `DM`
497644a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4977bb7acecfSBarry Smith - disc - The discretization object
497844a7f3ddSMatthew G. Knepley 
497944a7f3ddSMatthew G. Knepley   Level: intermediate
498044a7f3ddSMatthew G. Knepley 
4981bb7acecfSBarry Smith   Notes:
4982bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
4983bb7acecfSBarry Smith 
4984bb7acecfSBarry Smith   For example, a piecewise continous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
4985bb7acecfSBarry 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
4986bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
4987bb7acecfSBarry Smith 
4988bb7acecfSBarry Smith .seealso: `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
498944a7f3ddSMatthew G. Knepley @*/
4990*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
4991*d71ae5a4SJacob Faibussowitsch {
499244a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
499344a7f3ddSMatthew G. Knepley 
499444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
499544a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4996064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4997bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
49989566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
499944a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5000bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50019566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5002bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5003bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50049566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
5005af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
5006af122d2aSMatthew G Knepley }
50076636e97aSMatthew G Knepley 
5008e5e52638SMatthew G. Knepley /*@
5009e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5010e0b68406SMatthew Knepley 
5011e0b68406SMatthew Knepley   Logically collective on dm
5012e0b68406SMatthew Knepley 
5013e0b68406SMatthew Knepley   Input Parameters:
5014bb7acecfSBarry Smith + dm          - The `DM`
5015e0b68406SMatthew Knepley . f           - The field index
5016bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5017e0b68406SMatthew Knepley 
5018e0b68406SMatthew Knepley   Level: intermediate
5019e0b68406SMatthew Knepley 
5020db781477SPatrick Sanan .seealso: `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5021e0b68406SMatthew Knepley @*/
5022*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5023*d71ae5a4SJacob Faibussowitsch {
5024e0b68406SMatthew Knepley   PetscFunctionBegin;
502563a3b9bcSJacob 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);
5026e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
5027e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5028e0b68406SMatthew Knepley }
5029e0b68406SMatthew Knepley 
5030e0b68406SMatthew Knepley /*@
5031e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5032e0b68406SMatthew Knepley 
5033bb7acecfSBarry Smith   Not collective
5034e0b68406SMatthew Knepley 
5035e0b68406SMatthew Knepley   Input Parameters:
5036bb7acecfSBarry Smith + dm          - The `DM`
5037e0b68406SMatthew Knepley - f           - The field index
5038e0b68406SMatthew Knepley 
5039e0b68406SMatthew Knepley   Output Parameter:
5040e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5041e0b68406SMatthew Knepley 
5042e0b68406SMatthew Knepley   Level: intermediate
5043e0b68406SMatthew Knepley 
5044bb7acecfSBarry Smith  .seealso: `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5045e0b68406SMatthew Knepley @*/
5046*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5047*d71ae5a4SJacob Faibussowitsch {
5048e0b68406SMatthew Knepley   PetscFunctionBegin;
504963a3b9bcSJacob 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);
5050e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
5051e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5052e0b68406SMatthew Knepley }
5053e0b68406SMatthew Knepley 
5054e0b68406SMatthew Knepley /*@
5055bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5056e5e52638SMatthew G. Knepley 
5057d083f849SBarry Smith   Collective on dm
5058e5e52638SMatthew G. Knepley 
5059e5e52638SMatthew G. Knepley   Input Parameter:
5060bb7acecfSBarry Smith . dm - The `DM`
5061e5e52638SMatthew G. Knepley 
5062e5e52638SMatthew G. Knepley   Output Parameter:
5063bb7acecfSBarry Smith . newdm - The `DM`
5064e5e52638SMatthew G. Knepley 
5065e5e52638SMatthew G. Knepley   Level: advanced
5066e5e52638SMatthew G. Knepley 
5067db781477SPatrick Sanan .seealso: `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5068e5e52638SMatthew G. Knepley @*/
5069*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5070*d71ae5a4SJacob Faibussowitsch {
5071e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5072e5e52638SMatthew G. Knepley 
5073e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5074e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
50759566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
50769566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5077e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5078e5e52638SMatthew G. Knepley     DMLabel     label;
5079e5e52638SMatthew G. Knepley     PetscObject field;
508034aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5081e5e52638SMatthew G. Knepley 
50829566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
50839566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
50849566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
50859566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
508634aa8a36SMatthew G. Knepley   }
508734aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
508834aa8a36SMatthew G. Knepley }
508934aa8a36SMatthew G. Knepley 
509034aa8a36SMatthew G. Knepley /*@
509134aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
509234aa8a36SMatthew G. Knepley 
509334aa8a36SMatthew G. Knepley   Not collective
509434aa8a36SMatthew G. Knepley 
509534aa8a36SMatthew G. Knepley   Input Parameters:
509634aa8a36SMatthew G. Knepley + dm - The DM object
509734aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
509834aa8a36SMatthew G. Knepley 
5099d8d19677SJose E. Roman   Output Parameters:
510034aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
510134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
510234aa8a36SMatthew G. Knepley 
510334aa8a36SMatthew G. Knepley   Notes:
510434aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
510534aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
510634aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5107979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
510834aa8a36SMatthew G. Knepley 
510934aa8a36SMatthew G. Knepley   Level: developer
511034aa8a36SMatthew G. Knepley 
5111db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
511234aa8a36SMatthew G. Knepley @*/
5113*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5114*d71ae5a4SJacob Faibussowitsch {
511534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
511634aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5117534a8f05SLisandro Dalcin   if (useCone) PetscValidBoolPointer(useCone, 3);
5118534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
511934aa8a36SMatthew G. Knepley   if (f < 0) {
512034aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
512134aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
512234aa8a36SMatthew G. Knepley   } else {
512334aa8a36SMatthew G. Knepley     PetscInt Nf;
512434aa8a36SMatthew G. Knepley 
51259566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51267a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
512734aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
512834aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
512934aa8a36SMatthew G. Knepley   }
513034aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
513134aa8a36SMatthew G. Knepley }
513234aa8a36SMatthew G. Knepley 
513334aa8a36SMatthew G. Knepley /*@
513434aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
513534aa8a36SMatthew G. Knepley 
513634aa8a36SMatthew G. Knepley   Not collective
513734aa8a36SMatthew G. Knepley 
513834aa8a36SMatthew G. Knepley   Input Parameters:
513934aa8a36SMatthew G. Knepley + dm         - The DM object
514034aa8a36SMatthew G. Knepley . f          - The field number
514134aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
514234aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
514334aa8a36SMatthew G. Knepley 
514434aa8a36SMatthew G. Knepley   Notes:
514534aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
514634aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
514734aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5148979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
514934aa8a36SMatthew G. Knepley 
515034aa8a36SMatthew G. Knepley   Level: developer
515134aa8a36SMatthew G. Knepley 
5152db781477SPatrick Sanan .seealso: `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
515334aa8a36SMatthew G. Knepley @*/
5154*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5155*d71ae5a4SJacob Faibussowitsch {
515634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
515734aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
515834aa8a36SMatthew G. Knepley   if (f < 0) {
515934aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
516034aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
516134aa8a36SMatthew G. Knepley   } else {
516234aa8a36SMatthew G. Knepley     PetscInt Nf;
516334aa8a36SMatthew G. Knepley 
51649566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51657a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
516634aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
516734aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5168e5e52638SMatthew G. Knepley   }
5169e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5170e5e52638SMatthew G. Knepley }
5171e5e52638SMatthew G. Knepley 
5172b0441da4SMatthew G. Knepley /*@
5173b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5174b0441da4SMatthew G. Knepley 
5175b0441da4SMatthew G. Knepley   Not collective
5176b0441da4SMatthew G. Knepley 
5177f899ff85SJose E. Roman   Input Parameter:
5178b0441da4SMatthew G. Knepley . dm - The DM object
5179b0441da4SMatthew G. Knepley 
5180d8d19677SJose E. Roman   Output Parameters:
5181b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5182b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5183b0441da4SMatthew G. Knepley 
5184b0441da4SMatthew G. Knepley   Notes:
5185b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5186b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5187b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5188b0441da4SMatthew G. Knepley 
5189b0441da4SMatthew G. Knepley   Level: developer
5190b0441da4SMatthew G. Knepley 
5191db781477SPatrick Sanan .seealso: `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5192b0441da4SMatthew G. Knepley @*/
5193*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5194*d71ae5a4SJacob Faibussowitsch {
5195b0441da4SMatthew G. Knepley   PetscInt Nf;
5196b0441da4SMatthew G. Knepley 
5197b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5198b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5199064a246eSJacob Faibussowitsch   if (useCone) PetscValidBoolPointer(useCone, 2);
5200064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
52019566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5202b0441da4SMatthew G. Knepley   if (!Nf) {
52039566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5204b0441da4SMatthew G. Knepley   } else {
52059566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5206b0441da4SMatthew G. Knepley   }
5207b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5208b0441da4SMatthew G. Knepley }
5209b0441da4SMatthew G. Knepley 
5210b0441da4SMatthew G. Knepley /*@
5211b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5212b0441da4SMatthew G. Knepley 
5213b0441da4SMatthew G. Knepley   Not collective
5214b0441da4SMatthew G. Knepley 
5215b0441da4SMatthew G. Knepley   Input Parameters:
5216b0441da4SMatthew G. Knepley + dm         - The DM object
5217b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5218b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5219b0441da4SMatthew G. Knepley 
5220b0441da4SMatthew G. Knepley   Notes:
5221b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5222b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5223b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5224b0441da4SMatthew G. Knepley 
5225b0441da4SMatthew G. Knepley   Level: developer
5226b0441da4SMatthew G. Knepley 
5227db781477SPatrick Sanan .seealso: `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5228b0441da4SMatthew G. Knepley @*/
5229*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5230*d71ae5a4SJacob Faibussowitsch {
5231b0441da4SMatthew G. Knepley   PetscInt Nf;
5232b0441da4SMatthew G. Knepley 
5233b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5234b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52359566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5236b0441da4SMatthew G. Knepley   if (!Nf) {
52379566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5238b0441da4SMatthew G. Knepley   } else {
52399566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5240e5e52638SMatthew G. Knepley   }
5241e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5242e5e52638SMatthew G. Knepley }
5243e5e52638SMatthew G. Knepley 
5244*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5245*d71ae5a4SJacob Faibussowitsch {
5246799db056SMatthew G. Knepley   DM           plex;
5247799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5248799db056SMatthew G. Knepley   const char **names;
5249799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5250799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5251799db056SMatthew G. Knepley   size_t       len;
5252799db056SMatthew G. Knepley   MPI_Comm     comm;
5253799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5254783e2ec8SMatthew G. Knepley 
5255783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5256799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5257799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5258799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5259799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5260799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5261799db056SMatthew G. Knepley     PetscDS  dsBC;
5262799db056SMatthew G. Knepley     PetscInt numBd;
5263799db056SMatthew G. Knepley 
5264799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5265799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5266799db056SMatthew G. Knepley     maxLabels += numBd;
5267799db056SMatthew G. Knepley   }
5268799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5269799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5270799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5271799db056SMatthew G. Knepley     PetscDS  dsBC;
5272799db056SMatthew G. Knepley     PetscInt numBd, bd;
5273799db056SMatthew G. Knepley 
5274799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5275799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5276799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5277799db056SMatthew G. Knepley       DMLabel      label;
5278799db056SMatthew G. Knepley       PetscInt     field;
5279799db056SMatthew G. Knepley       PetscObject  obj;
5280799db056SMatthew G. Knepley       PetscClassId id;
5281799db056SMatthew G. Knepley 
5282799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
52839566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
52849566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5285799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
52869371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
52879371c9d4SSatish Balay         if (labels[l] == label) break;
5288799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5289783e2ec8SMatthew G. Knepley     }
5290799db056SMatthew G. Knepley   }
5291799db056SMatthew G. Knepley   /* Get label names */
5292799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5293799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
52949371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
52959371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
52969371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
52979371c9d4SSatish Balay   }
5298799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5299799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5300799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5301799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscStrcpy(&sendNames[gmaxLen * l], names[l]));
5302799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5303799db056SMatthew G. Knepley   /* Put all names on all processes */
5304799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5305799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5306799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5307799db056SMatthew G. Knepley   gNl = displs[size];
53089371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
53099371c9d4SSatish Balay     counts[p] *= gmaxLen;
53109371c9d4SSatish Balay     displs[p] *= gmaxLen;
53119371c9d4SSatish Balay   }
5312799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5313799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5314799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5315799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5316799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5317799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5318799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
53199371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
53209371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
53219566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5322799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
53239566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5324799db056SMatthew G. Knepley     ++gl;
5325783e2ec8SMatthew G. Knepley   }
5326799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
5327783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5328783e2ec8SMatthew G. Knepley }
5329783e2ec8SMatthew G. Knepley 
5330*d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5331*d71ae5a4SJacob Faibussowitsch {
5332e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5333e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5334e5e52638SMatthew G. Knepley 
5335e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5336e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
53379566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5338e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
53399371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
53409371c9d4SSatish Balay     tmpd[s].ds     = NULL;
53419371c9d4SSatish Balay     tmpd[s].label  = NULL;
53429371c9d4SSatish Balay     tmpd[s].fields = NULL;
53439371c9d4SSatish Balay   }
53449566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5345e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5346e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5347e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5348e5e52638SMatthew G. Knepley }
5349e5e52638SMatthew G. Knepley 
5350e5e52638SMatthew G. Knepley /*@
5351e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5352e5e52638SMatthew G. Knepley 
5353e5e52638SMatthew G. Knepley   Not collective
5354e5e52638SMatthew G. Knepley 
5355e5e52638SMatthew G. Knepley   Input Parameter:
5356e5e52638SMatthew G. Knepley . dm - The DM
5357e5e52638SMatthew G. Knepley 
5358e5e52638SMatthew G. Knepley   Output Parameter:
5359e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5360e5e52638SMatthew G. Knepley 
5361e5e52638SMatthew G. Knepley   Level: intermediate
5362e5e52638SMatthew G. Knepley 
5363db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMGetCellDS()`
5364e5e52638SMatthew G. Knepley @*/
5365*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5366*d71ae5a4SJacob Faibussowitsch {
5367e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5368e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5369534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5370e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5371e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5372e5e52638SMatthew G. Knepley }
5373e5e52638SMatthew G. Knepley 
5374e5e52638SMatthew G. Knepley /*@
5375e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5376e5e52638SMatthew G. Knepley 
5377d083f849SBarry Smith   Logically collective on dm
5378e5e52638SMatthew G. Knepley 
5379e5e52638SMatthew G. Knepley   Input Parameter:
5380e5e52638SMatthew G. Knepley . dm - The DM
5381e5e52638SMatthew G. Knepley 
5382e5e52638SMatthew G. Knepley   Level: intermediate
5383e5e52638SMatthew G. Knepley 
5384db781477SPatrick Sanan .seealso: `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5385e5e52638SMatthew G. Knepley @*/
5386*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5387*d71ae5a4SJacob Faibussowitsch {
5388e5e52638SMatthew G. Knepley   PetscInt s;
5389e5e52638SMatthew G. Knepley 
5390e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5391e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5392e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
53939566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
53949566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
53959566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5396e5e52638SMatthew G. Knepley   }
53979566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5398e5e52638SMatthew G. Knepley   dm->probs = NULL;
5399e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5400e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5401e5e52638SMatthew G. Knepley }
5402e5e52638SMatthew G. Knepley 
5403e5e52638SMatthew G. Knepley /*@
5404e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5405e5e52638SMatthew G. Knepley 
5406e5e52638SMatthew G. Knepley   Not collective
5407e5e52638SMatthew G. Knepley 
5408e5e52638SMatthew G. Knepley   Input Parameter:
5409e5e52638SMatthew G. Knepley . dm    - The DM
5410e5e52638SMatthew G. Knepley 
5411e5e52638SMatthew G. Knepley   Output Parameter:
5412e5e52638SMatthew G. Knepley . prob - The default PetscDS
5413e5e52638SMatthew G. Knepley 
5414e5e52638SMatthew G. Knepley   Level: intermediate
5415e5e52638SMatthew G. Knepley 
5416db781477SPatrick Sanan .seealso: `DMGetCellDS()`, `DMGetRegionDS()`
5417e5e52638SMatthew G. Knepley @*/
5418*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5419*d71ae5a4SJacob Faibussowitsch {
5420e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5421e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5422e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5423b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5424b0143b4dSMatthew G. Knepley     PetscDS ds;
5425b0143b4dSMatthew G. Knepley 
54269566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
54279566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, NULL, ds));
54289566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5429b0143b4dSMatthew G. Knepley   }
5430b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5431e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5432e5e52638SMatthew G. Knepley }
5433e5e52638SMatthew G. Knepley 
5434e5e52638SMatthew G. Knepley /*@
5435e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5436e5e52638SMatthew G. Knepley 
5437e5e52638SMatthew G. Knepley   Not collective
5438e5e52638SMatthew G. Knepley 
5439e5e52638SMatthew G. Knepley   Input Parameters:
5440e5e52638SMatthew G. Knepley + dm    - The DM
5441e5e52638SMatthew G. Knepley - point - Cell for the DS
5442e5e52638SMatthew G. Knepley 
5443e5e52638SMatthew G. Knepley   Output Parameter:
5444e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5445e5e52638SMatthew G. Knepley 
5446e5e52638SMatthew G. Knepley   Level: developer
5447e5e52638SMatthew G. Knepley 
5448db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMSetRegionDS()`
5449e5e52638SMatthew G. Knepley @*/
5450*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5451*d71ae5a4SJacob Faibussowitsch {
5452e5e52638SMatthew G. Knepley   PetscDS  probDef = NULL;
5453e5e52638SMatthew G. Knepley   PetscInt s;
5454e5e52638SMatthew G. Knepley 
5455e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5456e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5457e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
545863a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
5459e5e52638SMatthew G. Knepley   *prob = NULL;
5460e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5461e5e52638SMatthew G. Knepley     PetscInt val;
5462e5e52638SMatthew G. Knepley 
54639371c9d4SSatish Balay     if (!dm->probs[s].label) {
54649371c9d4SSatish Balay       probDef = dm->probs[s].ds;
54659371c9d4SSatish Balay     } else {
54669566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
54679371c9d4SSatish Balay       if (val >= 0) {
54689371c9d4SSatish Balay         *prob = dm->probs[s].ds;
54699371c9d4SSatish Balay         break;
54709371c9d4SSatish Balay       }
5471e5e52638SMatthew G. Knepley     }
5472e5e52638SMatthew G. Knepley   }
5473e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5474e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5475e5e52638SMatthew G. Knepley }
5476e5e52638SMatthew G. Knepley 
5477e5e52638SMatthew G. Knepley /*@
5478e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5479e5e52638SMatthew G. Knepley 
5480e5e52638SMatthew G. Knepley   Not collective
5481e5e52638SMatthew G. Knepley 
5482e5e52638SMatthew G. Knepley   Input Parameters:
5483e5e52638SMatthew G. Knepley + dm    - The DM
5484e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5485e5e52638SMatthew G. Knepley 
5486b3cf3223SMatthew G. Knepley   Output Parameters:
5487b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5488b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5489e5e52638SMatthew G. Knepley 
5490154ca461SJed Brown   Note:
5491154ca461SJed Brown   If a non-NULL label is given, but there is no PetscDS on that specific label,
5492154ca461SJed Brown   the PetscDS for the full domain (if present) is returned. Returns with
5493154ca461SJed Brown   fields=NULL and prob=NULL if there is no PetscDS for the full domain.
5494e5e52638SMatthew G. Knepley 
5495e5e52638SMatthew G. Knepley   Level: advanced
5496e5e52638SMatthew G. Knepley 
5497db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5498e5e52638SMatthew G. Knepley @*/
5499*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5500*d71ae5a4SJacob Faibussowitsch {
5501e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5502e5e52638SMatthew G. Knepley 
5503e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5504e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5505e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
55069371c9d4SSatish Balay   if (fields) {
55079371c9d4SSatish Balay     PetscValidPointer(fields, 3);
55089371c9d4SSatish Balay     *fields = NULL;
55099371c9d4SSatish Balay   }
55109371c9d4SSatish Balay   if (ds) {
55119371c9d4SSatish Balay     PetscValidPointer(ds, 4);
55129371c9d4SSatish Balay     *ds = NULL;
55139371c9d4SSatish Balay   }
5514e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5515154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5516b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5517b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
5518154ca461SJed Brown       if (dm->probs[s].label) PetscFunctionReturn(0);
5519b3cf3223SMatthew G. Knepley     }
5520e5e52638SMatthew G. Knepley   }
55212df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5522e5e52638SMatthew G. Knepley }
5523e5e52638SMatthew G. Knepley 
5524e5e52638SMatthew G. Knepley /*@
5525bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5526083401c6SMatthew G. Knepley 
5527083401c6SMatthew G. Knepley   Collective on dm
5528083401c6SMatthew G. Knepley 
5529083401c6SMatthew G. Knepley   Input Parameters:
5530bb7acecfSBarry Smith + dm     - The `DM`
5531bb7acecfSBarry Smith . label  - The `DMLabel` defining the mesh region, or NULL for the entire mesh
5532bb7acecfSBarry Smith . fields - The IS containing the `DM` field numbers for the fields in this `PetscDS`, or NULL for all fields
5533bb7acecfSBarry Smith - prob   - The `PetscDS` defined on the given region
5534083401c6SMatthew G. Knepley 
5535bb7acecfSBarry Smith   Note:
5536bb7acecfSBarry 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,
5537083401c6SMatthew G. Knepley   the fields argument is ignored.
5538083401c6SMatthew G. Knepley 
5539083401c6SMatthew G. Knepley   Level: advanced
5540083401c6SMatthew G. Knepley 
5541db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5542083401c6SMatthew G. Knepley @*/
5543*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5544*d71ae5a4SJacob Faibussowitsch {
5545083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5546083401c6SMatthew G. Knepley 
5547083401c6SMatthew G. Knepley   PetscFunctionBegin;
5548083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5549083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5550064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5551083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5552083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
55539566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
5554083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5555083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5556083401c6SMatthew G. Knepley     }
5557083401c6SMatthew G. Knepley   }
55589566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
55599566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
55609566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
55619566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
5562083401c6SMatthew G. Knepley   if (!label) {
5563083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5564083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5565083401c6SMatthew G. Knepley     Nds = 0;
5566083401c6SMatthew G. Knepley   }
5567083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5568083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5569083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5570083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5571083401c6SMatthew G. Knepley }
5572083401c6SMatthew G. Knepley 
5573083401c6SMatthew G. Knepley /*@
5574e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5575e5e52638SMatthew G. Knepley 
5576e5e52638SMatthew G. Knepley   Not collective
5577e5e52638SMatthew G. Knepley 
5578e5e52638SMatthew G. Knepley   Input Parameters:
5579e5e52638SMatthew G. Knepley + dm  - The DM
5580e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5581e5e52638SMatthew G. Knepley 
5582e5e52638SMatthew G. Knepley   Output Parameters:
5583b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5584b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5585083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5586e5e52638SMatthew G. Knepley 
5587e5e52638SMatthew G. Knepley   Level: advanced
5588e5e52638SMatthew G. Knepley 
5589db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5590e5e52638SMatthew G. Knepley @*/
5591*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5592*d71ae5a4SJacob Faibussowitsch {
5593e5e52638SMatthew G. Knepley   PetscInt Nds;
5594e5e52638SMatthew G. Knepley 
5595e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5596e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55979566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
559863a3b9bcSJacob 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);
5599e5e52638SMatthew G. Knepley   if (label) {
5600e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5601e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5602e5e52638SMatthew G. Knepley   }
5603b3cf3223SMatthew G. Knepley   if (fields) {
5604b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5605b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5606b3cf3223SMatthew G. Knepley   }
5607e5e52638SMatthew G. Knepley   if (ds) {
5608b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5609e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5610e5e52638SMatthew G. Knepley   }
5611e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5612e5e52638SMatthew G. Knepley }
5613e5e52638SMatthew G. Knepley 
5614e5e52638SMatthew G. Knepley /*@
5615083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5616e5e52638SMatthew G. Knepley 
5617083401c6SMatthew G. Knepley   Not collective
5618e5e52638SMatthew G. Knepley 
5619e5e52638SMatthew G. Knepley   Input Parameters:
5620e5e52638SMatthew G. Knepley + dm     - The DM
5621083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5622083401c6SMatthew G. Knepley . label  - The region label, or NULL
5623083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5624083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5625e5e52638SMatthew G. Knepley 
5626e5e52638SMatthew G. Knepley   Level: advanced
5627e5e52638SMatthew G. Knepley 
5628db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5629e5e52638SMatthew G. Knepley @*/
5630*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5631*d71ae5a4SJacob Faibussowitsch {
5632083401c6SMatthew G. Knepley   PetscInt Nds;
5633e5e52638SMatthew G. Knepley 
5634e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5635e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5636ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
56379566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
563863a3b9bcSJacob 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);
56399566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56409566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5641083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5642083401c6SMatthew G. Knepley   if (fields) {
5643083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
56449566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
56459566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5646083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5647e5e52638SMatthew G. Knepley   }
5648083401c6SMatthew G. Knepley   if (ds) {
5649083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
56509566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
56519566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5652083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5653083401c6SMatthew G. Knepley   }
5654e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5655e5e52638SMatthew G. Knepley }
5656e5e52638SMatthew G. Knepley 
5657e5e52638SMatthew G. Knepley /*@
56581d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
56591d3af9e0SMatthew G. Knepley 
56601d3af9e0SMatthew G. Knepley   Not collective
56611d3af9e0SMatthew G. Knepley 
56621d3af9e0SMatthew G. Knepley   Input Parameters:
56631d3af9e0SMatthew G. Knepley + dm  - The DM
56641d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
56651d3af9e0SMatthew G. Knepley 
56661d3af9e0SMatthew G. Knepley   Output Parameter:
56671d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
56681d3af9e0SMatthew G. Knepley 
56691d3af9e0SMatthew G. Knepley   Level: advanced
56701d3af9e0SMatthew G. Knepley 
5671db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
56721d3af9e0SMatthew G. Knepley @*/
5673*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5674*d71ae5a4SJacob Faibussowitsch {
56751d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
56761d3af9e0SMatthew G. Knepley 
56771d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
56781d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56791d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
5680dadcf809SJacob Faibussowitsch   PetscValidIntPointer(num, 3);
56819566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
56829371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
56839371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
56841d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
56851d3af9e0SMatthew G. Knepley   else *num = n;
56861d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
56871d3af9e0SMatthew G. Knepley }
56881d3af9e0SMatthew G. Knepley 
56892df84da0SMatthew G. Knepley /*@C
5690bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
56912df84da0SMatthew G. Knepley 
56922df84da0SMatthew G. Knepley   Not collective
56932df84da0SMatthew G. Knepley 
5694f1a722f8SMatthew G. Knepley   Input Parameters:
5695bb7acecfSBarry Smith + dm     - The `DM`
56962df84da0SMatthew G. Knepley . Nc     - The number of components for the field
5697bb7acecfSBarry Smith . prefix - The options prefix for the output `PetscFE`, or NULL
5698bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
56992df84da0SMatthew G. Knepley 
57002df84da0SMatthew G. Knepley   Output Parameter:
5701bb7acecfSBarry Smith . fem - The `PetscFE`
57022df84da0SMatthew G. Knepley 
5703bb7acecfSBarry Smith   Note:
5704bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
57052df84da0SMatthew G. Knepley 
57062df84da0SMatthew G. Knepley   Level: intermediate
57072df84da0SMatthew G. Knepley 
5708db781477SPatrick Sanan .seealso: `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
57092df84da0SMatthew G. Knepley @*/
5710*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5711*d71ae5a4SJacob Faibussowitsch {
57122df84da0SMatthew G. Knepley   DMPolytopeType ct;
57132df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
57142df84da0SMatthew G. Knepley 
57152df84da0SMatthew G. Knepley   PetscFunctionBegin;
57162df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57172df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
57182df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
57192df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
57202df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
57219566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
57229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
57239566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
57249566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
57252df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
57262df84da0SMatthew G. Knepley }
57272df84da0SMatthew G. Knepley 
57281d3af9e0SMatthew G. Knepley /*@
5729bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5730e5e52638SMatthew G. Knepley 
5731d083f849SBarry Smith   Collective on dm
5732e5e52638SMatthew G. Knepley 
5733e5e52638SMatthew G. Knepley   Input Parameter:
5734bb7acecfSBarry Smith . dm - The `DM`
5735e5e52638SMatthew G. Knepley 
573645480ffeSMatthew G. Knepley   Options Database Keys:
5737bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
573845480ffeSMatthew G. Knepley 
5739bb7acecfSBarry Smith   Note:
5740bb7acecfSBarry Smith   If the label has a `PetscDS` defined, it will be replaced. Otherwise, it will be added to the `DM`.
5741e5e52638SMatthew G. Knepley 
5742e5e52638SMatthew G. Knepley   Level: intermediate
5743e5e52638SMatthew G. Knepley 
5744db781477SPatrick Sanan .seealso: `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5745e5e52638SMatthew G. Knepley @*/
5746*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5747*d71ae5a4SJacob Faibussowitsch {
5748e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5749083401c6SMatthew G. Knepley   PetscDS   dsDef;
5750083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5751f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5752f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5753e5e52638SMatthew G. Knepley 
5754e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5755e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5756e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
57579566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
57589566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5759083401c6SMatthew G. Knepley   /* Determine how many regions we have */
57609566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5761083401c6SMatthew G. Knepley   Nl   = 0;
5762083401c6SMatthew G. Knepley   Ndef = 0;
5763083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5764083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5765083401c6SMatthew G. Knepley     PetscInt l;
5766083401c6SMatthew G. Knepley 
5767f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5768f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5769f918ec44SMatthew G. Knepley     {
5770f918ec44SMatthew G. Knepley       PetscClassId id;
5771f918ec44SMatthew G. Knepley 
57729566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5773f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5774f918ec44SMatthew G. Knepley         Ceed ceed;
5775f918ec44SMatthew G. Knepley 
57769566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
57779566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5778f918ec44SMatthew G. Knepley       }
5779f918ec44SMatthew G. Knepley     }
5780f918ec44SMatthew G. Knepley #endif
57819371c9d4SSatish Balay     if (!label) {
57829371c9d4SSatish Balay       ++Ndef;
57839371c9d4SSatish Balay       continue;
57849371c9d4SSatish Balay     }
57859371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
57869371c9d4SSatish Balay       if (label == labelSet[l]) break;
5787083401c6SMatthew G. Knepley     if (l < Nl) continue;
5788083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5789083401c6SMatthew G. Knepley   }
5790083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
57919566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
5792083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5793b3cf3223SMatthew G. Knepley     IS        fields;
5794b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5795b3cf3223SMatthew G. Knepley 
57969371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
57979371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
57987a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
57999566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
58009371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58019371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
58029566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58039566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58049566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58059566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
580688f0c812SMatthew G. Knepley 
58079566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
58089566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef));
58099566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58109566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
58112df9ee95SMatthew G. Knepley   }
58129566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
58139566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5814083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5815083401c6SMatthew G. Knepley   if (Ndef && Nl) {
58160122748bSMatthew G. Knepley     DM              plex;
5817083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5818083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5819083401c6SMatthew G. Knepley     PetscInt       *fields;
5820083401c6SMatthew G. Knepley     const PetscInt *cells;
5821083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
58220122748bSMatthew G. Knepley 
58239566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
58249566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
58259566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
58269566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
58275fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5828083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5829083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5830083401c6SMatthew G. Knepley       IS      pointIS;
5831083401c6SMatthew G. Knepley 
58329566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
58339566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
58349566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
58359566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5836083401c6SMatthew G. Knepley     }
58379566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5838083401c6SMatthew G. Knepley 
58399566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
58409566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
58419566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
58429566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
58439566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
58449566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
58459566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5846083401c6SMatthew G. Knepley 
58479566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
58489371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
58499371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
58509566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
58519566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
58529566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
58539566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5854083401c6SMatthew G. Knepley 
58559566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
58569566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef));
58579566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
58589566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
58599566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58609566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
58619566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5862083401c6SMatthew G. Knepley   }
5863083401c6SMatthew G. Knepley   /* Create label DSes
5864083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5865083401c6SMatthew G. Knepley   */
5866083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5867083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5868083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5869083401c6SMatthew G. Knepley     PetscDS   ds;
5870083401c6SMatthew G. Knepley     IS        fields;
5871083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5872083401c6SMatthew G. Knepley 
58739566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
58749371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58759371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
58769566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
58779371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58789371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
58799566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58809566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58819566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58829566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
58839566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, label, fields, ds));
58849566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
58859566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5886083401c6SMatthew G. Knepley     {
5887083401c6SMatthew G. Knepley       DMPolytopeType ct;
5888083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
58895fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
58900122748bSMatthew G. Knepley 
58919566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5892665f567fSMatthew G. Knepley       if (lStart >= 0) {
58939566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5894412e9a14SMatthew G. Knepley         switch (ct) {
5895412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5896412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5897412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
5898*d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5899*d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
5900*d71ae5a4SJacob Faibussowitsch           break;
5901*d71ae5a4SJacob Faibussowitsch         default:
5902*d71ae5a4SJacob Faibussowitsch           break;
5903412e9a14SMatthew G. Knepley         }
5904665f567fSMatthew G. Knepley       }
59059566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
59065fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
59075fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
59085fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
59099566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
59109566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
59115fedec97SMatthew G. Knepley           }
59125fedec97SMatthew G. Knepley           ++nf;
59135fedec97SMatthew G. Knepley         }
59145fedec97SMatthew G. Knepley       }
5915e5e52638SMatthew G. Knepley     }
59169566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5917e5e52638SMatthew G. Knepley   }
59189566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5919e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5920083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5921083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5922083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5923083401c6SMatthew G. Knepley     const PetscInt *fld;
59245fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
59255fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5926e5e52638SMatthew G. Knepley 
59279566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
59289566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
59299566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
59309566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5931083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5932083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
59335fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5934e5e52638SMatthew G. Knepley       PetscClassId id;
5935e5e52638SMatthew G. Knepley 
59365fedec97SMatthew G. Knepley       /* Handle DS with no fields */
59379566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
59385fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
59399566063dSJacob Faibussowitsch       if (isCohesive && !isCohesiveField) PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&disc));
59409566063dSJacob Faibussowitsch       PetscCall(PetscDSSetDiscretization(ds, f, disc));
5941083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
59429566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5943e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5944e5e52638SMatthew G. Knepley     }
59459566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
5946e5e52638SMatthew G. Knepley   }
5947f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
59489566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
5949f9244615SMatthew G. Knepley   if (flg) {
59503b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
59513b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
59523b4aee56SMatthew G. Knepley       PetscInt Nf, f;
59533b4aee56SMatthew G. Knepley 
59549566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
59559566063dSJacob Faibussowitsch       for (f = 0; f < Nf; ++f) PetscCall(PetscDSSetJetDegree(ds, f, k));
59563b4aee56SMatthew G. Knepley     }
5957f9244615SMatthew G. Knepley   }
5958e5e52638SMatthew G. Knepley   /* Setup DSes */
5959e5e52638SMatthew G. Knepley   if (doSetup) {
59609566063dSJacob Faibussowitsch     for (s = 0; s < dm->Nds; ++s) PetscCall(PetscDSSetUp(dm->probs[s].ds));
5961e5e52638SMatthew G. Knepley   }
5962e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5963e5e52638SMatthew G. Knepley }
5964e5e52638SMatthew G. Knepley 
5965e5e52638SMatthew G. Knepley /*@
5966bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
59677f96f943SMatthew G. Knepley 
5968bb7acecfSBarry Smith   Collective on `DM`
5969f2cacb80SMatthew G. Knepley 
59707f96f943SMatthew G. Knepley   Input Parameters:
5971bb7acecfSBarry Smith + dm   - The `DM`
59727f96f943SMatthew G. Knepley - time - The time
59737f96f943SMatthew G. Knepley 
59747f96f943SMatthew G. Knepley   Output Parameters:
5975f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5976f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
59777f96f943SMatthew G. Knepley 
5978bb7acecfSBarry Smith   Note:
5979bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
59807f96f943SMatthew G. Knepley 
59817f96f943SMatthew G. Knepley   Level: developer
59827f96f943SMatthew G. Knepley 
5983db781477SPatrick Sanan .seealso: `PetscDSSetExactSolution()`
59847f96f943SMatthew G. Knepley @*/
5985*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
5986*d71ae5a4SJacob Faibussowitsch {
59877f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
59887f96f943SMatthew G. Knepley   void   **ectxs;
59897f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
59907f96f943SMatthew G. Knepley 
59917f96f943SMatthew G. Knepley   PetscFunctionBegin;
5992f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5993f2cacb80SMatthew G. Knepley   if (u) PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5994f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
59959566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
59969566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
59979566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
59987f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
59997f96f943SMatthew G. Knepley     PetscDS         ds;
60007f96f943SMatthew G. Knepley     DMLabel         label;
60017f96f943SMatthew G. Knepley     IS              fieldIS;
60027f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
60037f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
60047f96f943SMatthew G. Knepley 
60059566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
60069566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
60079566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
60089566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
60099566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6010f2cacb80SMatthew G. Knepley     if (u) {
60117f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
60127f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
60139566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]));
60147f96f943SMatthew G. Knepley       }
60159566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
60167f96f943SMatthew G. Knepley       if (label) {
60179566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u));
60187f96f943SMatthew G. Knepley       } else {
60199566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u));
60207f96f943SMatthew G. Knepley       }
60217f96f943SMatthew G. Knepley     }
6022f2cacb80SMatthew G. Knepley     if (u_t) {
60239566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
60249566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6025f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
6026f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
60279566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]));
6028f2cacb80SMatthew G. Knepley       }
60299566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
6030f2cacb80SMatthew G. Knepley       if (label) {
60319566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t));
6032f2cacb80SMatthew G. Knepley       } else {
60339566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t));
6034f2cacb80SMatthew G. Knepley       }
6035f2cacb80SMatthew G. Knepley     }
6036f2cacb80SMatthew G. Knepley   }
6037f2cacb80SMatthew G. Knepley   if (u) {
60389566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
60399566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6040f2cacb80SMatthew G. Knepley   }
6041f2cacb80SMatthew G. Knepley   if (u_t) {
60429566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
60439566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6044f2cacb80SMatthew G. Knepley   }
60459566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
60467f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
60477f96f943SMatthew G. Knepley }
60487f96f943SMatthew G. Knepley 
6049*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
6050*d71ae5a4SJacob Faibussowitsch {
605145480ffeSMatthew G. Knepley   PetscDS    dsNew;
605245480ffeSMatthew G. Knepley   DSBoundary b;
60536a02485aSMatthew G. Knepley   PetscInt   cdim, Nf, f, d;
60545fedec97SMatthew G. Knepley   PetscBool  isCohesive;
605545480ffeSMatthew G. Knepley   void      *ctx;
605645480ffeSMatthew G. Knepley 
605745480ffeSMatthew G. Knepley   PetscFunctionBegin;
60589566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
60599566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyConstants(ds, dsNew));
60609566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyExactSolutions(ds, dsNew));
60619566063dSJacob Faibussowitsch   PetscCall(PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew));
60629566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyEquations(ds, dsNew));
60639566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
606445480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
60659566063dSJacob Faibussowitsch     PetscCall(PetscDSGetContext(ds, f, &ctx));
60669566063dSJacob Faibussowitsch     PetscCall(PetscDSSetContext(dsNew, f, ctx));
60679566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCohesive(ds, f, &isCohesive));
60689566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCohesive(dsNew, f, isCohesive));
60696a02485aSMatthew G. Knepley     PetscCall(PetscDSGetJetDegree(ds, f, &d));
60706a02485aSMatthew G. Knepley     PetscCall(PetscDSSetJetDegree(dsNew, f, d));
607145480ffeSMatthew G. Knepley   }
607245480ffeSMatthew G. Knepley   if (Nf) {
60739566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCoordinateDimension(ds, &cdim));
60749566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsNew, cdim));
607545480ffeSMatthew G. Knepley   }
60769566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew));
607745480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
60789566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, b->lname, &b->label));
607945480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
60807a8be351SBarry Smith     //PetscCheck(b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
608145480ffeSMatthew G. Knepley   }
608245480ffeSMatthew G. Knepley 
60839566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(dm, label, fields, dsNew));
60849566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
608545480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
608645480ffeSMatthew G. Knepley }
608745480ffeSMatthew G. Knepley 
60887f96f943SMatthew G. Knepley /*@
6089bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6090e5e52638SMatthew G. Knepley 
6091d083f849SBarry Smith   Collective on dm
6092e5e52638SMatthew G. Knepley 
6093e5e52638SMatthew G. Knepley   Input Parameter:
6094bb7acecfSBarry Smith . dm - The `DM`
6095e5e52638SMatthew G. Knepley 
6096e5e52638SMatthew G. Knepley   Output Parameter:
6097bb7acecfSBarry Smith . newdm - The `DM`
6098e5e52638SMatthew G. Knepley 
6099e5e52638SMatthew G. Knepley   Level: advanced
6100e5e52638SMatthew G. Knepley 
6101db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6102e5e52638SMatthew G. Knepley @*/
6103*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6104*d71ae5a4SJacob Faibussowitsch {
6105e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6106e5e52638SMatthew G. Knepley 
6107e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6108e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
61099566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
61109566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6111e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6112e5e52638SMatthew G. Knepley     DMLabel  label;
6113b3cf3223SMatthew G. Knepley     IS       fields;
611445480ffeSMatthew G. Knepley     PetscDS  ds, newds;
6115783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6116e5e52638SMatthew G. Knepley 
61179566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds));
6118b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
61199566063dSJacob Faibussowitsch     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds));
612045480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
61219566063dSJacob Faibussowitsch     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds));
61229566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6123783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6124b8025e53SMatthew G. Knepley       PetscWeakForm wf;
612545480ffeSMatthew G. Knepley       DMLabel       label;
6126783e2ec8SMatthew G. Knepley       PetscInt      field;
6127783e2ec8SMatthew G. Knepley 
61289566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
61299566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6130783e2ec8SMatthew G. Knepley     }
6131e5e52638SMatthew G. Knepley   }
6132799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
6133e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6134e5e52638SMatthew G. Knepley }
6135e5e52638SMatthew G. Knepley 
6136e5e52638SMatthew G. Knepley /*@
6137bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6138e5e52638SMatthew G. Knepley 
6139d083f849SBarry Smith   Collective on dm
6140e5e52638SMatthew G. Knepley 
6141e5e52638SMatthew G. Knepley   Input Parameter:
6142bb7acecfSBarry Smith . dm - The `DM`
6143e5e52638SMatthew G. Knepley 
6144e5e52638SMatthew G. Knepley   Output Parameter:
6145bb7acecfSBarry Smith . newdm - The `DM`
6146e5e52638SMatthew G. Knepley 
6147e5e52638SMatthew G. Knepley   Level: advanced
6148e5e52638SMatthew G. Knepley 
6149bb7acecfSBarry Smith   Developer Note:
6150bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6151bb7acecfSBarry Smith 
6152db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMCopyDS()`
6153e5e52638SMatthew G. Knepley @*/
6154*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6155*d71ae5a4SJacob Faibussowitsch {
6156e5e52638SMatthew G. Knepley   PetscFunctionBegin;
61579566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
61589566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
6159e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6160e5e52638SMatthew G. Knepley }
6161e5e52638SMatthew G. Knepley 
6162c73cfb54SMatthew G. Knepley /*@
6163bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6164c73cfb54SMatthew G. Knepley 
6165c73cfb54SMatthew G. Knepley   Not collective
6166c73cfb54SMatthew G. Knepley 
6167c73cfb54SMatthew G. Knepley   Input Parameter:
6168bb7acecfSBarry Smith . dm - The `DM`
6169c73cfb54SMatthew G. Knepley 
6170c73cfb54SMatthew G. Knepley   Output Parameter:
6171c73cfb54SMatthew G. Knepley . dim - The topological dimension
6172c73cfb54SMatthew G. Knepley 
6173c73cfb54SMatthew G. Knepley   Level: beginner
6174c73cfb54SMatthew G. Knepley 
6175db781477SPatrick Sanan .seealso: `DMSetDimension()`, `DMCreate()`
6176c73cfb54SMatthew G. Knepley @*/
6177*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6178*d71ae5a4SJacob Faibussowitsch {
6179c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6180c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6181534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6182c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6183c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6184c73cfb54SMatthew G. Knepley }
6185c73cfb54SMatthew G. Knepley 
6186c73cfb54SMatthew G. Knepley /*@
6187bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6188c73cfb54SMatthew G. Knepley 
6189c73cfb54SMatthew G. Knepley   Collective on dm
6190c73cfb54SMatthew G. Knepley 
6191c73cfb54SMatthew G. Knepley   Input Parameters:
6192bb7acecfSBarry Smith + dm - The `DM`
6193c73cfb54SMatthew G. Knepley - dim - The topological dimension
6194c73cfb54SMatthew G. Knepley 
6195c73cfb54SMatthew G. Knepley   Level: beginner
6196c73cfb54SMatthew G. Knepley 
6197db781477SPatrick Sanan .seealso: `DMGetDimension()`, `DMCreate()`
6198c73cfb54SMatthew G. Knepley @*/
6199*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6200*d71ae5a4SJacob Faibussowitsch {
6201e5e52638SMatthew G. Knepley   PetscDS  ds;
620245480ffeSMatthew G. Knepley   PetscInt Nds, n;
6203f17e8794SMatthew G. Knepley 
6204c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6205c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6206c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6207c73cfb54SMatthew G. Knepley   dm->dim = dim;
6208d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
62099566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
621045480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
62119566063dSJacob Faibussowitsch       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
62129566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
621345480ffeSMatthew G. Knepley     }
6214d17bd122SMatthew G. Knepley   }
6215c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6216c73cfb54SMatthew G. Knepley }
6217c73cfb54SMatthew G. Knepley 
6218793f3fe5SMatthew G. Knepley /*@
6219793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6220793f3fe5SMatthew G. Knepley 
6221d083f849SBarry Smith   Collective on dm
6222793f3fe5SMatthew G. Knepley 
6223793f3fe5SMatthew G. Knepley   Input Parameters:
6224bb7acecfSBarry Smith + dm - the `DM`
6225793f3fe5SMatthew G. Knepley - dim - the dimension
6226793f3fe5SMatthew G. Knepley 
6227793f3fe5SMatthew G. Knepley   Output Parameters:
6228793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6229aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6230793f3fe5SMatthew G. Knepley 
6231793f3fe5SMatthew G. Knepley   Note:
6232793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6233a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6234793f3fe5SMatthew G. Knepley   then the interval is empty.
6235793f3fe5SMatthew G. Knepley 
6236793f3fe5SMatthew G. Knepley   Level: intermediate
6237793f3fe5SMatthew G. Knepley 
6238db781477SPatrick Sanan .seealso: `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6239793f3fe5SMatthew G. Knepley @*/
6240*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6241*d71ae5a4SJacob Faibussowitsch {
6242793f3fe5SMatthew G. Knepley   PetscInt d;
6243793f3fe5SMatthew G. Knepley 
6244793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6245793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62469566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
62477a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6248dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
6249793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6250793f3fe5SMatthew G. Knepley }
6251793f3fe5SMatthew G. Knepley 
62526636e97aSMatthew G Knepley /*@
6253bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6254f4d763aaSMatthew G. Knepley 
62558f700142SStefano Zampini   Collective on dm
62568f700142SStefano Zampini 
6257f4d763aaSMatthew G. Knepley   Input Parameter:
6258bb7acecfSBarry Smith . dm - The original `DM`
6259f4d763aaSMatthew G. Knepley 
6260f4d763aaSMatthew G. Knepley   Output Parameter:
6261bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6262f4d763aaSMatthew G. Knepley 
6263f4d763aaSMatthew G. Knepley   Level: intermediate
6264f4d763aaSMatthew G. Knepley 
6265bb7acecfSBarry Smith   Note:
6266bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6267bb7acecfSBarry 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
6268bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6269bb7acecfSBarry Smith 
6270bb7acecfSBarry Smith .seealso: `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6271f4d763aaSMatthew G. Knepley @*/
6272*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6273*d71ae5a4SJacob Faibussowitsch {
6274c26acbdeSMatthew G. Knepley   PetscSection section;
62752d4e4a49SMatthew G. Knepley   PetscBool    hasConstraints, ghasConstraints;
627614f150ffSMatthew G. Knepley 
627714f150ffSMatthew G. Knepley   PetscFunctionBegin;
627814f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
627914f150ffSMatthew G. Knepley   PetscValidPointer(odm, 2);
62809566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
62819566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
62829566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
62832d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6284c26acbdeSMatthew G. Knepley     *odm = dm;
6285c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6286c26acbdeSMatthew G. Knepley   }
628714f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6288c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
628914f150ffSMatthew G. Knepley     PetscSF      sf;
629014f150ffSMatthew G. Knepley 
62919566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
62929566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
62939566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
62949566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
62959566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
62969566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
62979566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
62989566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
62999566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
630014f150ffSMatthew G. Knepley   }
630114f150ffSMatthew G. Knepley   *odm = dm->dmBC;
630214f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
630314f150ffSMatthew G. Knepley }
6304f4d763aaSMatthew G. Knepley 
6305f4d763aaSMatthew G. Knepley /*@
6306cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6307f4d763aaSMatthew G. Knepley 
6308f4d763aaSMatthew G. Knepley   Input Parameter:
6309bb7acecfSBarry Smith . dm - The original `DM`
6310f4d763aaSMatthew G. Knepley 
6311cdb7a50dSMatthew G. Knepley   Output Parameters:
6312cdb7a50dSMatthew G. Knepley + num - The output sequence number
6313cdb7a50dSMatthew G. Knepley - val - The output sequence value
6314f4d763aaSMatthew G. Knepley 
6315f4d763aaSMatthew G. Knepley   Level: intermediate
6316f4d763aaSMatthew G. Knepley 
6317bb7acecfSBarry Smith   Note:
6318bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6319bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6320bb7acecfSBarry Smith 
6321bb7acecfSBarry Smith   Developer Note:
6322bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6323bb7acecfSBarry Smith   not directly related to the `DM`.
6324f4d763aaSMatthew G. Knepley 
6325db781477SPatrick Sanan .seealso: `VecView()`
6326f4d763aaSMatthew G. Knepley @*/
6327*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6328*d71ae5a4SJacob Faibussowitsch {
6329f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6330f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63319371c9d4SSatish Balay   if (num) {
63329371c9d4SSatish Balay     PetscValidIntPointer(num, 2);
63339371c9d4SSatish Balay     *num = dm->outputSequenceNum;
63349371c9d4SSatish Balay   }
63359371c9d4SSatish Balay   if (val) {
63369371c9d4SSatish Balay     PetscValidRealPointer(val, 3);
63379371c9d4SSatish Balay     *val = dm->outputSequenceVal;
63389371c9d4SSatish Balay   }
6339f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6340f4d763aaSMatthew G. Knepley }
6341f4d763aaSMatthew G. Knepley 
6342f4d763aaSMatthew G. Knepley /*@
6343cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6344f4d763aaSMatthew G. Knepley 
6345f4d763aaSMatthew G. Knepley   Input Parameters:
6346bb7acecfSBarry Smith + dm - The original `DM`
6347cdb7a50dSMatthew G. Knepley . num - The output sequence number
6348cdb7a50dSMatthew G. Knepley - val - The output sequence value
6349f4d763aaSMatthew G. Knepley 
6350f4d763aaSMatthew G. Knepley   Level: intermediate
6351f4d763aaSMatthew G. Knepley 
6352bb7acecfSBarry Smith   Note:
6353bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6354bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6355f4d763aaSMatthew G. Knepley 
6356db781477SPatrick Sanan .seealso: `VecView()`
6357f4d763aaSMatthew G. Knepley @*/
6358*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6359*d71ae5a4SJacob Faibussowitsch {
6360f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6361f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6362f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6363cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6364cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6365cdb7a50dSMatthew G. Knepley }
6366cdb7a50dSMatthew G. Knepley 
6367cdb7a50dSMatthew G. Knepley /*@C
6368bb7acecfSBarry Smith  DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6369cdb7a50dSMatthew G. Knepley 
6370cdb7a50dSMatthew G. Knepley   Input Parameters:
6371bb7acecfSBarry Smith + dm   - The original `DM`
6372cdb7a50dSMatthew G. Knepley . name - The sequence name
6373cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6374cdb7a50dSMatthew G. Knepley 
6375cdb7a50dSMatthew G. Knepley   Output Parameter:
6376cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6377cdb7a50dSMatthew G. Knepley 
6378cdb7a50dSMatthew G. Knepley   Level: intermediate
6379cdb7a50dSMatthew G. Knepley 
6380bb7acecfSBarry Smith   Note:
6381bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6382bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6383bb7acecfSBarry Smith 
6384bb7acecfSBarry Smith   Developer Note:
6385bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6386cdb7a50dSMatthew G. Knepley 
6387db781477SPatrick Sanan .seealso: `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6388cdb7a50dSMatthew G. Knepley @*/
6389*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6390*d71ae5a4SJacob Faibussowitsch {
6391cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6392cdb7a50dSMatthew G. Knepley 
6393cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6394cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6395cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6396064a246eSJacob Faibussowitsch   PetscValidRealPointer(val, 5);
63979566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6398cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6399cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6400cdb7a50dSMatthew G. Knepley     PetscScalar value;
6401cdb7a50dSMatthew G. Knepley 
64029566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
64034aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6404cdb7a50dSMatthew G. Knepley #endif
6405cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6406f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6407f4d763aaSMatthew G. Knepley }
64088e4ac7eaSMatthew G. Knepley 
64098e4ac7eaSMatthew G. Knepley /*@
6410bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64118e4ac7eaSMatthew G. Knepley 
64128e4ac7eaSMatthew G. Knepley   Not collective
64138e4ac7eaSMatthew G. Knepley 
64148e4ac7eaSMatthew G. Knepley   Input Parameter:
6415bb7acecfSBarry Smith . dm - The `DM`
64168e4ac7eaSMatthew G. Knepley 
64178e4ac7eaSMatthew G. Knepley   Output Parameter:
6418bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64198e4ac7eaSMatthew G. Knepley 
64208e4ac7eaSMatthew G. Knepley   Level: beginner
64218e4ac7eaSMatthew G. Knepley 
6422db781477SPatrick Sanan .seealso: `DMSetUseNatural()`, `DMCreate()`
64238e4ac7eaSMatthew G. Knepley @*/
6424*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6425*d71ae5a4SJacob Faibussowitsch {
64268e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64278e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6428534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
64298e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
64308e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
64318e4ac7eaSMatthew G. Knepley }
64328e4ac7eaSMatthew G. Knepley 
64338e4ac7eaSMatthew G. Knepley /*@
6434bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64358e4ac7eaSMatthew G. Knepley 
64368e4ac7eaSMatthew G. Knepley   Collective on dm
64378e4ac7eaSMatthew G. Knepley 
64388e4ac7eaSMatthew G. Knepley   Input Parameters:
6439bb7acecfSBarry Smith  + dm - The `DM`
6440bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64418e4ac7eaSMatthew G. Knepley 
6442bb7acecfSBarry Smith   Note:
6443bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
64445d3b26e6SMatthew G. Knepley 
64458e4ac7eaSMatthew G. Knepley   Level: beginner
64468e4ac7eaSMatthew G. Knepley 
6447db781477SPatrick Sanan .seealso: `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
64488e4ac7eaSMatthew G. Knepley @*/
6449*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6450*d71ae5a4SJacob Faibussowitsch {
64518e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64528e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64538833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
64548e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
64558e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
64568e4ac7eaSMatthew G. Knepley }
6457c58f1c22SToby Isaac 
6458c58f1c22SToby Isaac /*@C
6459bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6460c58f1c22SToby Isaac 
6461c58f1c22SToby Isaac   Not Collective
6462c58f1c22SToby Isaac 
6463c58f1c22SToby Isaac   Input Parameters:
6464bb7acecfSBarry Smith + dm   - The `DM` object
6465c58f1c22SToby Isaac - name - The label name
6466c58f1c22SToby Isaac 
6467c58f1c22SToby Isaac   Level: intermediate
6468c58f1c22SToby Isaac 
6469db781477SPatrick Sanan .seealso: `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6470c58f1c22SToby Isaac @*/
6471*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6472*d71ae5a4SJacob Faibussowitsch {
64735d80c0bfSVaclav Hapla   PetscBool flg;
64745d80c0bfSVaclav Hapla   DMLabel   label;
6475c58f1c22SToby Isaac 
6476c58f1c22SToby Isaac   PetscFunctionBegin;
6477c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6478c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
64799566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6480c58f1c22SToby Isaac   if (!flg) {
64819566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
64829566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
64839566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6484c58f1c22SToby Isaac   }
6485c58f1c22SToby Isaac   PetscFunctionReturn(0);
6486c58f1c22SToby Isaac }
6487c58f1c22SToby Isaac 
6488c58f1c22SToby Isaac /*@C
6489bb7acecfSBarry 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.
64900fdc7489SMatthew Knepley 
64910fdc7489SMatthew Knepley   Not Collective
64920fdc7489SMatthew Knepley 
64930fdc7489SMatthew Knepley   Input Parameters:
6494bb7acecfSBarry Smith + dm   - The `DM` object
64950fdc7489SMatthew Knepley . l    - The index for the label
64960fdc7489SMatthew Knepley - name - The label name
64970fdc7489SMatthew Knepley 
64980fdc7489SMatthew Knepley   Level: intermediate
64990fdc7489SMatthew Knepley 
6500db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
65010fdc7489SMatthew Knepley @*/
6502*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6503*d71ae5a4SJacob Faibussowitsch {
65040fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
65050fdc7489SMatthew Knepley   DMLabel     label;
65060fdc7489SMatthew Knepley   PetscInt    Nl, m;
65070fdc7489SMatthew Knepley   PetscBool   flg, match;
65080fdc7489SMatthew Knepley   const char *lname;
65090fdc7489SMatthew Knepley 
65100fdc7489SMatthew Knepley   PetscFunctionBegin;
65110fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6512064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
65139566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
65140fdc7489SMatthew Knepley   if (!flg) {
65159566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
65169566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
65179566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
65180fdc7489SMatthew Knepley   }
65199566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
652063a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
65210fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
65229566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
65239566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
65240fdc7489SMatthew Knepley     if (match) break;
65250fdc7489SMatthew Knepley   }
65260fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
65270fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
65280fdc7489SMatthew Knepley   else prev->next = orig->next;
65290fdc7489SMatthew Knepley   if (!l) {
65300fdc7489SMatthew Knepley     orig->next = dm->labels;
65310fdc7489SMatthew Knepley     dm->labels = orig;
65320fdc7489SMatthew Knepley   } else {
65339371c9d4SSatish Balay     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next)
65349371c9d4SSatish Balay       ;
65350fdc7489SMatthew Knepley     orig->next = prev->next;
65360fdc7489SMatthew Knepley     prev->next = orig;
65370fdc7489SMatthew Knepley   }
65380fdc7489SMatthew Knepley   PetscFunctionReturn(0);
65390fdc7489SMatthew Knepley }
65400fdc7489SMatthew Knepley 
65410fdc7489SMatthew Knepley /*@C
6542bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6543c58f1c22SToby Isaac 
6544c58f1c22SToby Isaac   Not Collective
6545c58f1c22SToby Isaac 
6546c58f1c22SToby Isaac   Input Parameters:
6547bb7acecfSBarry Smith + dm   - The `DM` object
6548c58f1c22SToby Isaac . name - The label name
6549c58f1c22SToby Isaac - point - The mesh point
6550c58f1c22SToby Isaac 
6551c58f1c22SToby Isaac   Output Parameter:
6552c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6553c58f1c22SToby Isaac 
6554c58f1c22SToby Isaac   Level: beginner
6555c58f1c22SToby Isaac 
6556db781477SPatrick Sanan .seealso: `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6557c58f1c22SToby Isaac @*/
6558*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6559*d71ae5a4SJacob Faibussowitsch {
6560c58f1c22SToby Isaac   DMLabel label;
6561c58f1c22SToby Isaac 
6562c58f1c22SToby Isaac   PetscFunctionBegin;
6563c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6564c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
65659566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
65667a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
65679566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
6568c58f1c22SToby Isaac   PetscFunctionReturn(0);
6569c58f1c22SToby Isaac }
6570c58f1c22SToby Isaac 
6571c58f1c22SToby Isaac /*@C
6572bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6573c58f1c22SToby Isaac 
6574c58f1c22SToby Isaac   Not Collective
6575c58f1c22SToby Isaac 
6576c58f1c22SToby Isaac   Input Parameters:
6577bb7acecfSBarry Smith + dm   - The `DM` object
6578c58f1c22SToby Isaac . name - The label name
6579c58f1c22SToby Isaac . point - The mesh point
6580c58f1c22SToby Isaac - value - The label value for this point
6581c58f1c22SToby Isaac 
6582c58f1c22SToby Isaac   Output Parameter:
6583c58f1c22SToby Isaac 
6584c58f1c22SToby Isaac   Level: beginner
6585c58f1c22SToby Isaac 
6586db781477SPatrick Sanan .seealso: `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6587c58f1c22SToby Isaac @*/
6588*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6589*d71ae5a4SJacob Faibussowitsch {
6590c58f1c22SToby Isaac   DMLabel label;
6591c58f1c22SToby Isaac 
6592c58f1c22SToby Isaac   PetscFunctionBegin;
6593c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6594c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
65959566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6596c58f1c22SToby Isaac   if (!label) {
65979566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
65989566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6599c58f1c22SToby Isaac   }
66009566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
6601c58f1c22SToby Isaac   PetscFunctionReturn(0);
6602c58f1c22SToby Isaac }
6603c58f1c22SToby Isaac 
6604c58f1c22SToby Isaac /*@C
6605bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6606c58f1c22SToby Isaac 
6607c58f1c22SToby Isaac   Not Collective
6608c58f1c22SToby Isaac 
6609c58f1c22SToby Isaac   Input Parameters:
6610bb7acecfSBarry Smith + dm   - The `DM` object
6611c58f1c22SToby Isaac . name - The label name
6612c58f1c22SToby Isaac . point - The mesh point
6613c58f1c22SToby Isaac - value - The label value for this point
6614c58f1c22SToby Isaac 
6615c58f1c22SToby Isaac   Level: beginner
6616c58f1c22SToby Isaac 
6617db781477SPatrick Sanan .seealso: `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6618c58f1c22SToby Isaac @*/
6619*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6620*d71ae5a4SJacob Faibussowitsch {
6621c58f1c22SToby Isaac   DMLabel label;
6622c58f1c22SToby Isaac 
6623c58f1c22SToby Isaac   PetscFunctionBegin;
6624c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6625c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66269566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6627c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
66289566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
6629c58f1c22SToby Isaac   PetscFunctionReturn(0);
6630c58f1c22SToby Isaac }
6631c58f1c22SToby Isaac 
6632c58f1c22SToby Isaac /*@C
6633bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6634c58f1c22SToby Isaac 
6635c58f1c22SToby Isaac   Not Collective
6636c58f1c22SToby Isaac 
6637c58f1c22SToby Isaac   Input Parameters:
6638bb7acecfSBarry Smith + dm   - The `DM` object
6639c58f1c22SToby Isaac - name - The label name
6640c58f1c22SToby Isaac 
6641c58f1c22SToby Isaac   Output Parameter:
6642c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6643c58f1c22SToby Isaac 
6644c58f1c22SToby Isaac   Level: beginner
6645c58f1c22SToby Isaac 
6646bb7acecfSBarry Smith   Developer Note:
6647bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6648bb7acecfSBarry Smith 
6649bb7acecfSBarry Smith .seealso: `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6650c58f1c22SToby Isaac @*/
6651*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6652*d71ae5a4SJacob Faibussowitsch {
6653c58f1c22SToby Isaac   DMLabel label;
6654c58f1c22SToby Isaac 
6655c58f1c22SToby Isaac   PetscFunctionBegin;
6656c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6657c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6658534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
66599566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6660c58f1c22SToby Isaac   *size = 0;
6661c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
66629566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
6663c58f1c22SToby Isaac   PetscFunctionReturn(0);
6664c58f1c22SToby Isaac }
6665c58f1c22SToby Isaac 
6666c58f1c22SToby Isaac /*@C
6667bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6668c58f1c22SToby Isaac 
6669c58f1c22SToby Isaac   Not Collective
6670c58f1c22SToby Isaac 
6671c58f1c22SToby Isaac   Input Parameters:
6672bb7acecfSBarry Smith + mesh - The `DM` object
6673c58f1c22SToby Isaac - name - The label name
6674c58f1c22SToby Isaac 
6675c58f1c22SToby Isaac   Output Parameter:
6676c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6677c58f1c22SToby Isaac 
6678c58f1c22SToby Isaac   Level: beginner
6679c58f1c22SToby Isaac 
6680db781477SPatrick Sanan .seealso: `DMLabelGetValueIS()`, `DMGetLabelSize()`
6681c58f1c22SToby Isaac @*/
6682*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6683*d71ae5a4SJacob Faibussowitsch {
6684c58f1c22SToby Isaac   DMLabel label;
6685c58f1c22SToby Isaac 
6686c58f1c22SToby Isaac   PetscFunctionBegin;
6687c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6688c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6689c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
66909566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6691c58f1c22SToby Isaac   *ids = NULL;
6692dab2e251SBlaise Bourdin   if (label) {
66939566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6694dab2e251SBlaise Bourdin   } else {
6695dab2e251SBlaise Bourdin     /* returning an empty IS */
66969566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6697dab2e251SBlaise Bourdin   }
6698c58f1c22SToby Isaac   PetscFunctionReturn(0);
6699c58f1c22SToby Isaac }
6700c58f1c22SToby Isaac 
6701c58f1c22SToby Isaac /*@C
6702c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6703c58f1c22SToby Isaac 
6704c58f1c22SToby Isaac   Not Collective
6705c58f1c22SToby Isaac 
6706c58f1c22SToby Isaac   Input Parameters:
6707bb7acecfSBarry Smith + dm - The `DM` object
6708c58f1c22SToby Isaac . name - The label name
6709c58f1c22SToby Isaac - value - The stratum value
6710c58f1c22SToby Isaac 
6711c58f1c22SToby Isaac   Output Parameter:
6712bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6713c58f1c22SToby Isaac 
6714c58f1c22SToby Isaac   Level: beginner
6715c58f1c22SToby Isaac 
6716db781477SPatrick Sanan .seealso: `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6717c58f1c22SToby Isaac @*/
6718*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6719*d71ae5a4SJacob Faibussowitsch {
6720c58f1c22SToby Isaac   DMLabel label;
6721c58f1c22SToby Isaac 
6722c58f1c22SToby Isaac   PetscFunctionBegin;
6723c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6724c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6725534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
67269566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6727c58f1c22SToby Isaac   *size = 0;
6728c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
67299566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
6730c58f1c22SToby Isaac   PetscFunctionReturn(0);
6731c58f1c22SToby Isaac }
6732c58f1c22SToby Isaac 
6733c58f1c22SToby Isaac /*@C
6734c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6735c58f1c22SToby Isaac 
6736c58f1c22SToby Isaac   Not Collective
6737c58f1c22SToby Isaac 
6738c58f1c22SToby Isaac   Input Parameters:
6739bb7acecfSBarry Smith + dm - The `DM` object
6740c58f1c22SToby Isaac . name - The label name
6741c58f1c22SToby Isaac - value - The stratum value
6742c58f1c22SToby Isaac 
6743c58f1c22SToby Isaac   Output Parameter:
6744c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6745c58f1c22SToby Isaac 
6746c58f1c22SToby Isaac   Level: beginner
6747c58f1c22SToby Isaac 
6748db781477SPatrick Sanan .seealso: `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6749c58f1c22SToby Isaac @*/
6750*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6751*d71ae5a4SJacob Faibussowitsch {
6752c58f1c22SToby Isaac   DMLabel label;
6753c58f1c22SToby Isaac 
6754c58f1c22SToby Isaac   PetscFunctionBegin;
6755c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6756c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6757c58f1c22SToby Isaac   PetscValidPointer(points, 4);
67589566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6759c58f1c22SToby Isaac   *points = NULL;
6760c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
67619566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
6762c58f1c22SToby Isaac   PetscFunctionReturn(0);
6763c58f1c22SToby Isaac }
6764c58f1c22SToby Isaac 
67654de306b1SToby Isaac /*@C
67669044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
67674de306b1SToby Isaac 
67684de306b1SToby Isaac   Not Collective
67694de306b1SToby Isaac 
67704de306b1SToby Isaac   Input Parameters:
6771bb7acecfSBarry Smith + dm - The `DM` object
67724de306b1SToby Isaac . name - The label name
67734de306b1SToby Isaac . value - The stratum value
67744de306b1SToby Isaac - points - The stratum points
67754de306b1SToby Isaac 
67764de306b1SToby Isaac   Level: beginner
67774de306b1SToby Isaac 
6778bb7acecfSBarry Smith .seealso: `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
67794de306b1SToby Isaac @*/
6780*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6781*d71ae5a4SJacob Faibussowitsch {
67824de306b1SToby Isaac   DMLabel label;
67834de306b1SToby Isaac 
67844de306b1SToby Isaac   PetscFunctionBegin;
67854de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67864de306b1SToby Isaac   PetscValidCharPointer(name, 2);
67874de306b1SToby Isaac   PetscValidPointer(points, 4);
67889566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67894de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
67909566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
67914de306b1SToby Isaac   PetscFunctionReturn(0);
67924de306b1SToby Isaac }
67934de306b1SToby Isaac 
6794c58f1c22SToby Isaac /*@C
6795bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6796c58f1c22SToby Isaac 
6797c58f1c22SToby Isaac   Not Collective
6798c58f1c22SToby Isaac 
6799c58f1c22SToby Isaac   Input Parameters:
6800bb7acecfSBarry Smith + dm   - The `DM` object
6801c58f1c22SToby Isaac . name - The label name
6802c58f1c22SToby Isaac - value - The label value for this point
6803c58f1c22SToby Isaac 
6804c58f1c22SToby Isaac   Output Parameter:
6805c58f1c22SToby Isaac 
6806c58f1c22SToby Isaac   Level: beginner
6807c58f1c22SToby Isaac 
6808bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6809c58f1c22SToby Isaac @*/
6810*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6811*d71ae5a4SJacob Faibussowitsch {
6812c58f1c22SToby Isaac   DMLabel label;
6813c58f1c22SToby Isaac 
6814c58f1c22SToby Isaac   PetscFunctionBegin;
6815c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6816c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
68179566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6818c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
68199566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
6820c58f1c22SToby Isaac   PetscFunctionReturn(0);
6821c58f1c22SToby Isaac }
6822c58f1c22SToby Isaac 
6823c58f1c22SToby Isaac /*@
6824bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6825c58f1c22SToby Isaac 
6826c58f1c22SToby Isaac   Not Collective
6827c58f1c22SToby Isaac 
6828c58f1c22SToby Isaac   Input Parameter:
6829bb7acecfSBarry Smith . dm   - The `DM` object
6830c58f1c22SToby Isaac 
6831c58f1c22SToby Isaac   Output Parameter:
6832c58f1c22SToby Isaac . numLabels - the number of Labels
6833c58f1c22SToby Isaac 
6834c58f1c22SToby Isaac   Level: intermediate
6835c58f1c22SToby Isaac 
6836bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6837c58f1c22SToby Isaac @*/
6838*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6839*d71ae5a4SJacob Faibussowitsch {
68405d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6841c58f1c22SToby Isaac   PetscInt    n    = 0;
6842c58f1c22SToby Isaac 
6843c58f1c22SToby Isaac   PetscFunctionBegin;
6844c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6845534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
68469371c9d4SSatish Balay   while (next) {
68479371c9d4SSatish Balay     ++n;
68489371c9d4SSatish Balay     next = next->next;
68499371c9d4SSatish Balay   }
6850c58f1c22SToby Isaac   *numLabels = n;
6851c58f1c22SToby Isaac   PetscFunctionReturn(0);
6852c58f1c22SToby Isaac }
6853c58f1c22SToby Isaac 
6854c58f1c22SToby Isaac /*@C
6855c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6856c58f1c22SToby Isaac 
6857c58f1c22SToby Isaac   Not Collective
6858c58f1c22SToby Isaac 
6859c58f1c22SToby Isaac   Input Parameters:
6860bb7acecfSBarry Smith + dm - The `DM` object
6861c58f1c22SToby Isaac - n  - the label number
6862c58f1c22SToby Isaac 
6863c58f1c22SToby Isaac   Output Parameter:
6864c58f1c22SToby Isaac . name - the label name
6865c58f1c22SToby Isaac 
6866c58f1c22SToby Isaac   Level: intermediate
6867c58f1c22SToby Isaac 
6868bb7acecfSBarry Smith   Developer Note:
6869bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
6870bb7acecfSBarry Smith 
6871bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6872c58f1c22SToby Isaac @*/
6873*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6874*d71ae5a4SJacob Faibussowitsch {
68755d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6876c58f1c22SToby Isaac   PetscInt    l    = 0;
6877c58f1c22SToby Isaac 
6878c58f1c22SToby Isaac   PetscFunctionBegin;
6879c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6880c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6881c58f1c22SToby Isaac   while (next) {
6882c58f1c22SToby Isaac     if (l == n) {
68839566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
6884c58f1c22SToby Isaac       PetscFunctionReturn(0);
6885c58f1c22SToby Isaac     }
6886c58f1c22SToby Isaac     ++l;
6887c58f1c22SToby Isaac     next = next->next;
6888c58f1c22SToby Isaac   }
688963a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6890c58f1c22SToby Isaac }
6891c58f1c22SToby Isaac 
6892c58f1c22SToby Isaac /*@C
6893bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
6894c58f1c22SToby Isaac 
6895c58f1c22SToby Isaac   Not Collective
6896c58f1c22SToby Isaac 
6897c58f1c22SToby Isaac   Input Parameters:
6898bb7acecfSBarry Smith + dm   - The `DM` object
6899c58f1c22SToby Isaac - name - The label name
6900c58f1c22SToby Isaac 
6901c58f1c22SToby Isaac   Output Parameter:
6902bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
6903c58f1c22SToby Isaac 
6904c58f1c22SToby Isaac   Level: intermediate
6905c58f1c22SToby Isaac 
6906bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6907c58f1c22SToby Isaac @*/
6908*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6909*d71ae5a4SJacob Faibussowitsch {
69105d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6911d67d17b1SMatthew G. Knepley   const char *lname;
6912c58f1c22SToby Isaac 
6913c58f1c22SToby Isaac   PetscFunctionBegin;
6914c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6915c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6916534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
6917c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6918c58f1c22SToby Isaac   while (next) {
69199566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
69209566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
6921c58f1c22SToby Isaac     if (*hasLabel) break;
6922c58f1c22SToby Isaac     next = next->next;
6923c58f1c22SToby Isaac   }
6924c58f1c22SToby Isaac   PetscFunctionReturn(0);
6925c58f1c22SToby Isaac }
6926c58f1c22SToby Isaac 
6927c58f1c22SToby Isaac /*@C
6928bb7acecfSBarry Smith   DMGetLabel - Return the label of a given name, or NULL, from a `DM`
6929c58f1c22SToby Isaac 
6930c58f1c22SToby Isaac   Not Collective
6931c58f1c22SToby Isaac 
6932c58f1c22SToby Isaac   Input Parameters:
6933bb7acecfSBarry Smith + dm   - The `DM` object
6934c58f1c22SToby Isaac - name - The label name
6935c58f1c22SToby Isaac 
6936c58f1c22SToby Isaac   Output Parameter:
6937bb7acecfSBarry Smith . label - The `DMLabel`, or NULL if the label is absent
6938c58f1c22SToby Isaac 
6939bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
6940bb7acecfSBarry Smith +   "depth"       - Holds the depth (co-dimension) of each mesh point
6941bb7acecfSBarry Smith .   "celltype"    - Holds the topological type of each cell
6942bb7acecfSBarry Smith .   "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
6943bb7acecfSBarry Smith .   "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
6944bb7acecfSBarry Smith .   "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
6945bb7acecfSBarry Smith -  "Vertex Sets" - Mirrors the vertex sets defined by GMsh
69466d7c9049SMatthew G. Knepley 
6947c58f1c22SToby Isaac   Level: intermediate
6948c58f1c22SToby Isaac 
6949bb7acecfSBarry Smith .seealso: `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
6950c58f1c22SToby Isaac @*/
6951*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6952*d71ae5a4SJacob Faibussowitsch {
69535d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6954c58f1c22SToby Isaac   PetscBool   hasLabel;
6955d67d17b1SMatthew G. Knepley   const char *lname;
6956c58f1c22SToby Isaac 
6957c58f1c22SToby Isaac   PetscFunctionBegin;
6958c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6959c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6960c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6961c58f1c22SToby Isaac   *label = NULL;
6962c58f1c22SToby Isaac   while (next) {
69639566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
69649566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
6965c58f1c22SToby Isaac     if (hasLabel) {
6966c58f1c22SToby Isaac       *label = next->label;
6967c58f1c22SToby Isaac       break;
6968c58f1c22SToby Isaac     }
6969c58f1c22SToby Isaac     next = next->next;
6970c58f1c22SToby Isaac   }
6971c58f1c22SToby Isaac   PetscFunctionReturn(0);
6972c58f1c22SToby Isaac }
6973c58f1c22SToby Isaac 
6974c58f1c22SToby Isaac /*@C
6975bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
6976c58f1c22SToby Isaac 
6977c58f1c22SToby Isaac   Not Collective
6978c58f1c22SToby Isaac 
6979c58f1c22SToby Isaac   Input Parameters:
6980bb7acecfSBarry Smith + dm - The `DM` object
6981c58f1c22SToby Isaac - n  - the label number
6982c58f1c22SToby Isaac 
6983c58f1c22SToby Isaac   Output Parameter:
6984c58f1c22SToby Isaac . label - the label
6985c58f1c22SToby Isaac 
6986c58f1c22SToby Isaac   Level: intermediate
6987c58f1c22SToby Isaac 
6988bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6989c58f1c22SToby Isaac @*/
6990*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
6991*d71ae5a4SJacob Faibussowitsch {
69925d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6993c58f1c22SToby Isaac   PetscInt    l    = 0;
6994c58f1c22SToby Isaac 
6995c58f1c22SToby Isaac   PetscFunctionBegin;
6996c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6997c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6998c58f1c22SToby Isaac   while (next) {
6999c58f1c22SToby Isaac     if (l == n) {
7000c58f1c22SToby Isaac       *label = next->label;
7001c58f1c22SToby Isaac       PetscFunctionReturn(0);
7002c58f1c22SToby Isaac     }
7003c58f1c22SToby Isaac     ++l;
7004c58f1c22SToby Isaac     next = next->next;
7005c58f1c22SToby Isaac   }
700663a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7007c58f1c22SToby Isaac }
7008c58f1c22SToby Isaac 
7009c58f1c22SToby Isaac /*@C
7010bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7011c58f1c22SToby Isaac 
7012c58f1c22SToby Isaac   Not Collective
7013c58f1c22SToby Isaac 
7014c58f1c22SToby Isaac   Input Parameters:
7015bb7acecfSBarry Smith + dm   - The `DM` object
7016bb7acecfSBarry Smith - label - The `DMLabel`
7017c58f1c22SToby Isaac 
7018c58f1c22SToby Isaac   Level: developer
7019c58f1c22SToby Isaac 
7020bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7021c58f1c22SToby Isaac @*/
7022*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7023*d71ae5a4SJacob Faibussowitsch {
70245d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7025c58f1c22SToby Isaac   PetscBool   hasLabel;
7026d67d17b1SMatthew G. Knepley   const char *lname;
70275d80c0bfSVaclav Hapla   PetscBool   flg;
7028c58f1c22SToby Isaac 
7029c58f1c22SToby Isaac   PetscFunctionBegin;
7030c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70319566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
70329566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
70337a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
70349566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7035c58f1c22SToby Isaac   tmpLabel->label  = label;
7036c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
70375d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
70385d80c0bfSVaclav Hapla   *p = tmpLabel;
70399566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
70409566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
70415d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
70429566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7043ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7044c58f1c22SToby Isaac   PetscFunctionReturn(0);
7045c58f1c22SToby Isaac }
7046c58f1c22SToby Isaac 
7047c58f1c22SToby Isaac /*@C
70484a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
70494a7ee7d0SMatthew G. Knepley 
70504a7ee7d0SMatthew G. Knepley   Not Collective
70514a7ee7d0SMatthew G. Knepley 
70524a7ee7d0SMatthew G. Knepley   Input Parameters:
7053bb7acecfSBarry Smith + dm    - The `DM` object
7054bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
70554a7ee7d0SMatthew G. Knepley 
7056bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7057bb7acecfSBarry Smith +  "depth"       - Holds the depth (co-dimension) of each mesh point
7058bb7acecfSBarry Smith .  "celltype"    - Holds the topological type of each cell
7059bb7acecfSBarry Smith .  "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7060bb7acecfSBarry Smith .  "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7061bb7acecfSBarry Smith .  "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7062bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
70634a7ee7d0SMatthew G. Knepley 
70644a7ee7d0SMatthew G. Knepley   Level: intermediate
70654a7ee7d0SMatthew G. Knepley 
7066bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
70674a7ee7d0SMatthew G. Knepley @*/
7068*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7069*d71ae5a4SJacob Faibussowitsch {
70704a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
70714a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
70724a7ee7d0SMatthew G. Knepley   const char *name, *lname;
70734a7ee7d0SMatthew G. Knepley 
70744a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
70754a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70764a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
70779566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
70784a7ee7d0SMatthew G. Knepley   while (next) {
70799566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70809566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
70814a7ee7d0SMatthew G. Knepley     if (hasLabel) {
70829566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
70839566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
70844a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
70859566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
70864a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
70879566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
70884a7ee7d0SMatthew G. Knepley       next->label = label;
70894a7ee7d0SMatthew G. Knepley       break;
70904a7ee7d0SMatthew G. Knepley     }
70914a7ee7d0SMatthew G. Knepley     next = next->next;
70924a7ee7d0SMatthew G. Knepley   }
70934a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
70944a7ee7d0SMatthew G. Knepley }
70954a7ee7d0SMatthew G. Knepley 
70964a7ee7d0SMatthew G. Knepley /*@C
7097bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7098c58f1c22SToby Isaac 
7099c58f1c22SToby Isaac   Not Collective
7100c58f1c22SToby Isaac 
7101c58f1c22SToby Isaac   Input Parameters:
7102bb7acecfSBarry Smith + dm   - The `DM` object
7103c58f1c22SToby Isaac - name - The label name
7104c58f1c22SToby Isaac 
7105c58f1c22SToby Isaac   Output Parameter:
7106bb7acecfSBarry Smith . label - The `DMLabel`, or NULL if the label is absent. Pass in NULL to call `DMLabelDestroy()` on the label, otherwise the
7107bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7108c58f1c22SToby Isaac 
7109c58f1c22SToby Isaac   Level: developer
7110c58f1c22SToby Isaac 
7111bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7112c58f1c22SToby Isaac @*/
7113*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7114*d71ae5a4SJacob Faibussowitsch {
711595d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7116c58f1c22SToby Isaac   PetscBool   hasLabel;
7117d67d17b1SMatthew G. Knepley   const char *lname;
7118c58f1c22SToby Isaac 
7119c58f1c22SToby Isaac   PetscFunctionBegin;
7120c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7121e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7122e5472504SVaclav Hapla   if (label) {
7123e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7124c58f1c22SToby Isaac     *label = NULL;
7125e5472504SVaclav Hapla   }
71265d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
71279566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
71289566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7129c58f1c22SToby Isaac     if (hasLabel) {
713095d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
71319566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
713295d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
71339566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7134ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
713595d578d6SVaclav Hapla       if (label) *label = link->label;
71369566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
71379566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7138c58f1c22SToby Isaac       break;
7139c58f1c22SToby Isaac     }
7140c58f1c22SToby Isaac   }
7141c58f1c22SToby Isaac   PetscFunctionReturn(0);
7142c58f1c22SToby Isaac }
7143c58f1c22SToby Isaac 
7144306894acSVaclav Hapla /*@
7145bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7146306894acSVaclav Hapla 
7147306894acSVaclav Hapla   Not Collective
7148306894acSVaclav Hapla 
7149306894acSVaclav Hapla   Input Parameters:
7150bb7acecfSBarry Smith + dm   - The `DM` object
7151bb7acecfSBarry Smith . label - The `DMLabel` to be removed from the `DM`
7152306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7153306894acSVaclav Hapla 
7154306894acSVaclav Hapla   Level: developer
7155306894acSVaclav Hapla 
7156bb7acecfSBarry Smith   Note:
7157306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7158bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7159306894acSVaclav Hapla   *label nullified.
7160306894acSVaclav Hapla 
7161bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7162306894acSVaclav Hapla @*/
7163*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7164*d71ae5a4SJacob Faibussowitsch {
716543e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7166306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7167306894acSVaclav Hapla 
7168306894acSVaclav Hapla   PetscFunctionBegin;
7169306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7170306894acSVaclav Hapla   PetscValidPointer(label, 2);
7171f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7172306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7173306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
71745d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
717543e45a93SVaclav Hapla     if (*label == link->label) {
7176306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
717743e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7178306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7179ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
718043e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
71819566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
71829566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7183306894acSVaclav Hapla       break;
7184306894acSVaclav Hapla     }
7185306894acSVaclav Hapla   }
71867a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7187306894acSVaclav Hapla   PetscFunctionReturn(0);
7188306894acSVaclav Hapla }
7189306894acSVaclav Hapla 
7190c58f1c22SToby Isaac /*@C
7191c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7192c58f1c22SToby Isaac 
7193c58f1c22SToby Isaac   Not Collective
7194c58f1c22SToby Isaac 
7195c58f1c22SToby Isaac   Input Parameters:
7196bb7acecfSBarry Smith + dm   - The `DM` object
7197c58f1c22SToby Isaac - name - The label name
7198c58f1c22SToby Isaac 
7199c58f1c22SToby Isaac   Output Parameter:
7200c58f1c22SToby Isaac . output - The flag for output
7201c58f1c22SToby Isaac 
7202c58f1c22SToby Isaac   Level: developer
7203c58f1c22SToby Isaac 
7204bb7acecfSBarry Smith .seealso: `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7205c58f1c22SToby Isaac @*/
7206*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7207*d71ae5a4SJacob Faibussowitsch {
72085d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7209d67d17b1SMatthew G. Knepley   const char *lname;
7210c58f1c22SToby Isaac 
7211c58f1c22SToby Isaac   PetscFunctionBegin;
7212c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7213dadcf809SJacob Faibussowitsch   PetscValidCharPointer(name, 2);
7214dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(output, 3);
7215c58f1c22SToby Isaac   while (next) {
7216c58f1c22SToby Isaac     PetscBool flg;
7217c58f1c22SToby Isaac 
72189566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72199566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
72209371c9d4SSatish Balay     if (flg) {
72219371c9d4SSatish Balay       *output = next->output;
72229371c9d4SSatish Balay       PetscFunctionReturn(0);
72239371c9d4SSatish Balay     }
7224c58f1c22SToby Isaac     next = next->next;
7225c58f1c22SToby Isaac   }
722698921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7227c58f1c22SToby Isaac }
7228c58f1c22SToby Isaac 
7229c58f1c22SToby Isaac /*@C
7230bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7231c58f1c22SToby Isaac 
7232c58f1c22SToby Isaac   Not Collective
7233c58f1c22SToby Isaac 
7234c58f1c22SToby Isaac   Input Parameters:
7235bb7acecfSBarry Smith + dm     - The `DM` object
7236c58f1c22SToby Isaac . name   - The label name
7237bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7238c58f1c22SToby Isaac 
7239c58f1c22SToby Isaac   Level: developer
7240c58f1c22SToby Isaac 
7241bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7242c58f1c22SToby Isaac @*/
7243*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7244*d71ae5a4SJacob Faibussowitsch {
72455d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7246d67d17b1SMatthew G. Knepley   const char *lname;
7247c58f1c22SToby Isaac 
7248c58f1c22SToby Isaac   PetscFunctionBegin;
7249c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7250534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7251c58f1c22SToby Isaac   while (next) {
7252c58f1c22SToby Isaac     PetscBool flg;
7253c58f1c22SToby Isaac 
72549566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72559566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
72569371c9d4SSatish Balay     if (flg) {
72579371c9d4SSatish Balay       next->output = output;
72589371c9d4SSatish Balay       PetscFunctionReturn(0);
72599371c9d4SSatish Balay     }
7260c58f1c22SToby Isaac     next = next->next;
7261c58f1c22SToby Isaac   }
726298921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7263c58f1c22SToby Isaac }
7264c58f1c22SToby Isaac 
7265c58f1c22SToby Isaac /*@
7266bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7267c58f1c22SToby Isaac 
7268d083f849SBarry Smith   Collective on dmA
7269c58f1c22SToby Isaac 
7270d8d19677SJose E. Roman   Input Parameters:
7271bb7acecfSBarry Smith + dmA - The `DM` object with initial labels
7272bb7acecfSBarry Smith . dmB - The `DM` object to which labels are copied
7273bb7acecfSBarry Smith . mode - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7274bb7acecfSBarry Smith . all  - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7275bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7276c58f1c22SToby Isaac 
7277c58f1c22SToby Isaac   Level: intermediate
7278c58f1c22SToby Isaac 
7279bb7acecfSBarry Smith   Note:
72802cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7281c58f1c22SToby Isaac 
7282bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7283c58f1c22SToby Isaac @*/
7284*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7285*d71ae5a4SJacob Faibussowitsch {
72862cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7287c58f1c22SToby Isaac   const char *name;
7288c58f1c22SToby Isaac   PetscBool   flg;
72895d80c0bfSVaclav Hapla   DMLabelLink link;
7290c58f1c22SToby Isaac 
72915d80c0bfSVaclav Hapla   PetscFunctionBegin;
72925d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
72935d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
72945d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
72955d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
72967a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
72975d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
72985d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
72995d80c0bfSVaclav Hapla     label = link->label;
73009566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
73015d80c0bfSVaclav Hapla     if (!all) {
73029566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7303c58f1c22SToby Isaac       if (flg) continue;
73049566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
73057d5acc75SStefano Zampini       if (flg) continue;
73069566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7307ba2698f1SMatthew G. Knepley       if (flg) continue;
73085d80c0bfSVaclav Hapla     }
73099566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
73102cbb9b06SVaclav Hapla     if (labelOld) {
73112cbb9b06SVaclav Hapla       switch (emode) {
7312*d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7313*d71ae5a4SJacob Faibussowitsch         continue;
7314*d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7315*d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7316*d71ae5a4SJacob Faibussowitsch         break;
7317*d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7318*d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7319*d71ae5a4SJacob Faibussowitsch       default:
7320*d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
73212cbb9b06SVaclav Hapla       }
73222cbb9b06SVaclav Hapla     }
73235d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
73249566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
73255d80c0bfSVaclav Hapla     } else {
73265d80c0bfSVaclav Hapla       labelNew = label;
73275d80c0bfSVaclav Hapla     }
73289566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
73299566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7330c58f1c22SToby Isaac   }
7331c58f1c22SToby Isaac   PetscFunctionReturn(0);
7332c58f1c22SToby Isaac }
7333461a15a0SLisandro Dalcin 
7334609dae6eSVaclav Hapla /*@C
7335bb7acecfSBarry Smith   DMCompareLabels - Compare labels of two `DMPLEX` meshes
7336609dae6eSVaclav Hapla 
73375efe38ccSVaclav Hapla   Collective
7338609dae6eSVaclav Hapla 
7339609dae6eSVaclav Hapla   Input Parameters:
7340bb7acecfSBarry Smith + dm0 - First `DM` object
7341bb7acecfSBarry Smith - dm1 - Second `DM` object
7342609dae6eSVaclav Hapla 
7343609dae6eSVaclav Hapla   Output Parameters
73445efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
7345609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
7346609dae6eSVaclav Hapla 
7347609dae6eSVaclav Hapla   Level: intermediate
7348609dae6eSVaclav Hapla 
7349609dae6eSVaclav Hapla   Notes:
7350bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7351bb7acecfSBarry Smith 
7352bb7acecfSBarry Smith   If equal is passed as NULL and difference is found, an error is thrown on all processes.
7353bb7acecfSBarry Smith 
7354bb7acecfSBarry Smith   Make sure to pass equal is NULL on all processes or none of them.
7355609dae6eSVaclav Hapla 
73565efe38ccSVaclav Hapla   The output message is set independently on each rank.
7357bb7acecfSBarry Smith 
7358bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7359bb7acecfSBarry Smith 
7360bb7acecfSBarry Smith   If message is passed as NULL and a difference is found, the difference description is printed to stderr in synchronized manner.
7361bb7acecfSBarry Smith 
7362bb7acecfSBarry Smith   Make sure to pass message as NULL on all processes or no processes.
7363609dae6eSVaclav Hapla 
7364609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7365bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7366609dae6eSVaclav Hapla 
7367bb7acecfSBarry Smith   Fortran Note:
7368bb7acecfSBarry Smith   This function is not available from Fortran.
7369609dae6eSVaclav Hapla 
7370bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7371609dae6eSVaclav Hapla @*/
7372*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7373*d71ae5a4SJacob Faibussowitsch {
73745efe38ccSVaclav Hapla   PetscInt    n, i;
7375609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
73765efe38ccSVaclav Hapla   PetscBool   eq;
7377609dae6eSVaclav Hapla   MPI_Comm    comm;
73785efe38ccSVaclav Hapla   PetscMPIInt rank;
7379609dae6eSVaclav Hapla 
7380609dae6eSVaclav Hapla   PetscFunctionBegin;
7381609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7382609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7383609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
73845efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal, 3);
7385609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
73869566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
73879566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
73885efe38ccSVaclav Hapla   {
73895efe38ccSVaclav Hapla     PetscInt n1;
73905efe38ccSVaclav Hapla 
73919566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
73929566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
73935efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
739448a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
73959566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
73965efe38ccSVaclav Hapla     if (!eq) goto finish;
73975efe38ccSVaclav Hapla   }
73985efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7399609dae6eSVaclav Hapla     DMLabel     l0, l1;
7400609dae6eSVaclav Hapla     const char *name;
7401609dae6eSVaclav Hapla     char       *msgInner;
7402609dae6eSVaclav Hapla 
7403609dae6eSVaclav Hapla     /* Ignore label order */
74049566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
74059566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
74069566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7407609dae6eSVaclav Hapla     if (!l1) {
740863a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
74095efe38ccSVaclav Hapla       eq = PETSC_FALSE;
74105efe38ccSVaclav Hapla       break;
7411609dae6eSVaclav Hapla     }
74129566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
74139566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
74149566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
74155efe38ccSVaclav Hapla     if (!eq) break;
7416609dae6eSVaclav Hapla   }
74179566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7418609dae6eSVaclav Hapla finish:
74195efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7420609dae6eSVaclav Hapla   if (message) {
7421609dae6eSVaclav Hapla     *message = NULL;
742248a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
74235efe38ccSVaclav Hapla   } else {
742448a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
74259566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
74265efe38ccSVaclav Hapla   }
74275efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
74285efe38ccSVaclav Hapla   if (equal) *equal = eq;
74297a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
7430609dae6eSVaclav Hapla   PetscFunctionReturn(0);
7431609dae6eSVaclav Hapla }
7432609dae6eSVaclav Hapla 
7433*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7434*d71ae5a4SJacob Faibussowitsch {
7435461a15a0SLisandro Dalcin   PetscFunctionBegin;
7436461a15a0SLisandro Dalcin   PetscValidPointer(label, 2);
7437461a15a0SLisandro Dalcin   if (!*label) {
74389566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
74399566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7440461a15a0SLisandro Dalcin   }
74419566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
7442461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
7443461a15a0SLisandro Dalcin }
7444461a15a0SLisandro Dalcin 
74450fdc7489SMatthew Knepley /*
74460fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
74470fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
74480fdc7489SMatthew Knepley   (label, id) pair in the DM.
74490fdc7489SMatthew Knepley 
74500fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
74510fdc7489SMatthew Knepley   each label.
74520fdc7489SMatthew Knepley */
7453*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7454*d71ae5a4SJacob Faibussowitsch {
74550fdc7489SMatthew Knepley   DMUniversalLabel ul;
74560fdc7489SMatthew Knepley   PetscBool       *active;
74570fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
74580fdc7489SMatthew Knepley 
74590fdc7489SMatthew Knepley   PetscFunctionBegin;
74609566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
74619566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
74629566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
74639566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
74640fdc7489SMatthew Knepley   ul->Nl = 0;
74650fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
74660fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
74670fdc7489SMatthew Knepley     const char *name;
74680fdc7489SMatthew Knepley 
74699566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
74709566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
74719566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
74720fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
74730fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
74740fdc7489SMatthew Knepley   }
74759566063dSJacob 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));
74760fdc7489SMatthew Knepley   ul->Nv = 0;
74770fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
74780fdc7489SMatthew Knepley     DMLabel     label;
74790fdc7489SMatthew Knepley     PetscInt    nv;
74800fdc7489SMatthew Knepley     const char *name;
74810fdc7489SMatthew Knepley 
74820fdc7489SMatthew Knepley     if (!active[l]) continue;
74839566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
74849566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
74859566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
74869566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
74870fdc7489SMatthew Knepley     ul->indices[m] = l;
74880fdc7489SMatthew Knepley     ul->Nv += nv;
74890fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
74900fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
74910fdc7489SMatthew Knepley     ++m;
74920fdc7489SMatthew Knepley   }
74930fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
74940fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
74950fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
74960fdc7489SMatthew Knepley   }
74970fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
74980fdc7489SMatthew Knepley     PetscInt b;
74990fdc7489SMatthew Knepley 
75000fdc7489SMatthew Knepley     ul->masks[l] = 0;
75010fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
75020fdc7489SMatthew Knepley   }
75039566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
75040fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
75050fdc7489SMatthew Knepley     DMLabel         label;
75060fdc7489SMatthew Knepley     IS              valueIS;
75070fdc7489SMatthew Knepley     const PetscInt *varr;
75080fdc7489SMatthew Knepley     PetscInt        nv, v;
75090fdc7489SMatthew Knepley 
75100fdc7489SMatthew Knepley     if (!active[l]) continue;
75119566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
75129566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
75139566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
75149566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7515ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
75169566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
75179566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
75189566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
75190fdc7489SMatthew Knepley     ++m;
75200fdc7489SMatthew Knepley   }
75219566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
75220fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
75230fdc7489SMatthew Knepley     PetscInt  uval   = 0;
75240fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
75250fdc7489SMatthew Knepley 
75260fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
75270fdc7489SMatthew Knepley       DMLabel  label;
75280649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
75290fdc7489SMatthew Knepley 
75300fdc7489SMatthew Knepley       if (!active[l]) continue;
75319566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
75329566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
75339566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
75349371c9d4SSatish Balay       if (val == defval) {
75359371c9d4SSatish Balay         ++m;
75369371c9d4SSatish Balay         continue;
75379371c9d4SSatish Balay       }
75380649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
75390fdc7489SMatthew Knepley       marked = PETSC_TRUE;
75409566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
754163a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
75420fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
75430fdc7489SMatthew Knepley       ++m;
75440fdc7489SMatthew Knepley     }
75459566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
75460fdc7489SMatthew Knepley   }
75479566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
75480fdc7489SMatthew Knepley   *universal = ul;
75490fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75500fdc7489SMatthew Knepley }
75510fdc7489SMatthew Knepley 
7552*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7553*d71ae5a4SJacob Faibussowitsch {
75540fdc7489SMatthew Knepley   PetscInt l;
75550fdc7489SMatthew Knepley 
75560fdc7489SMatthew Knepley   PetscFunctionBegin;
75579566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
75589566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
75599566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
75609566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
75619566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
75620fdc7489SMatthew Knepley   *universal = NULL;
75630fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75640fdc7489SMatthew Knepley }
75650fdc7489SMatthew Knepley 
7566*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7567*d71ae5a4SJacob Faibussowitsch {
75680fdc7489SMatthew Knepley   PetscFunctionBegin;
75690fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
75700fdc7489SMatthew Knepley   *ulabel = ul->label;
75710fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75720fdc7489SMatthew Knepley }
75730fdc7489SMatthew Knepley 
7574*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7575*d71ae5a4SJacob Faibussowitsch {
75760fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
75770fdc7489SMatthew Knepley 
75780fdc7489SMatthew Knepley   PetscFunctionBegin;
7579064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
75800fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
75819566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
75829566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
75830fdc7489SMatthew Knepley   }
75840fdc7489SMatthew Knepley   if (preserveOrder) {
75850fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
75860fdc7489SMatthew Knepley       const char *name;
75870fdc7489SMatthew Knepley       PetscBool   match;
75880fdc7489SMatthew Knepley 
75899566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
75909566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
759163a3b9bcSJacob 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]);
75920fdc7489SMatthew Knepley     }
75930fdc7489SMatthew Knepley   }
75940fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75950fdc7489SMatthew Knepley }
75960fdc7489SMatthew Knepley 
7597*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7598*d71ae5a4SJacob Faibussowitsch {
75990fdc7489SMatthew Knepley   PetscInt l;
76000fdc7489SMatthew Knepley 
76010fdc7489SMatthew Knepley   PetscFunctionBegin;
76020fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
76030fdc7489SMatthew Knepley     DMLabel  label;
76040fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
76050fdc7489SMatthew Knepley 
76060fdc7489SMatthew Knepley     if (lval) {
76079566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
76089566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
76099566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
76100fdc7489SMatthew Knepley     }
76110fdc7489SMatthew Knepley   }
76120fdc7489SMatthew Knepley   PetscFunctionReturn(0);
76130fdc7489SMatthew Knepley }
7614a8fb8f29SToby Isaac 
7615a8fb8f29SToby Isaac /*@
7616bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7617bb7acecfSBarry Smith 
7618bb7acecfSBarry Smith   Not collective
7619a8fb8f29SToby Isaac 
7620a8fb8f29SToby Isaac   Input Parameter:
7621bb7acecfSBarry Smith . dm - The `DM` object
7622a8fb8f29SToby Isaac 
7623a8fb8f29SToby Isaac   Output Parameter:
7624bb7acecfSBarry Smith . cdm - The coarse `DM`
7625a8fb8f29SToby Isaac 
7626a8fb8f29SToby Isaac   Level: intermediate
7627a8fb8f29SToby Isaac 
7628bb7acecfSBarry Smith .seealso: `DMSetCoarseDM()`, `DMCoarsen()`
7629a8fb8f29SToby Isaac @*/
7630*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7631*d71ae5a4SJacob Faibussowitsch {
7632a8fb8f29SToby Isaac   PetscFunctionBegin;
7633a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7634a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7635a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7636a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7637a8fb8f29SToby Isaac }
7638a8fb8f29SToby Isaac 
7639a8fb8f29SToby Isaac /*@
7640bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7641a8fb8f29SToby Isaac 
7642a8fb8f29SToby Isaac   Input Parameters:
7643bb7acecfSBarry Smith + dm - The `DM` object
7644bb7acecfSBarry Smith - cdm - The coarse `DM`
7645a8fb8f29SToby Isaac 
7646a8fb8f29SToby Isaac   Level: intermediate
7647a8fb8f29SToby Isaac 
7648bb7acecfSBarry Smith   Note:
7649bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7650bb7acecfSBarry Smith 
7651bb7acecfSBarry Smith .seealso: `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7652a8fb8f29SToby Isaac @*/
7653*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7654*d71ae5a4SJacob Faibussowitsch {
7655a8fb8f29SToby Isaac   PetscFunctionBegin;
7656a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7657a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
765889d734beSBarry Smith   if (dm == cdm) cdm = NULL;
76599566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
76609566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7661a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7662a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7663a8fb8f29SToby Isaac }
7664a8fb8f29SToby Isaac 
766588bdff64SToby Isaac /*@
7666bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
766788bdff64SToby Isaac 
766888bdff64SToby Isaac   Input Parameter:
7669bb7acecfSBarry Smith . dm - The `DM` object
767088bdff64SToby Isaac 
767188bdff64SToby Isaac   Output Parameter:
7672bb7acecfSBarry Smith . fdm - The fine `DM`
767388bdff64SToby Isaac 
767488bdff64SToby Isaac   Level: intermediate
767588bdff64SToby Isaac 
7676bb7acecfSBarry Smith .seealso: `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
767788bdff64SToby Isaac @*/
7678*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7679*d71ae5a4SJacob Faibussowitsch {
768088bdff64SToby Isaac   PetscFunctionBegin;
768188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
768288bdff64SToby Isaac   PetscValidPointer(fdm, 2);
768388bdff64SToby Isaac   *fdm = dm->fineMesh;
768488bdff64SToby Isaac   PetscFunctionReturn(0);
768588bdff64SToby Isaac }
768688bdff64SToby Isaac 
768788bdff64SToby Isaac /*@
7688bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
768988bdff64SToby Isaac 
769088bdff64SToby Isaac   Input Parameters:
7691bb7acecfSBarry Smith + dm - The `DM` object
7692bb7acecfSBarry Smith - fdm - The fine `DM`
769388bdff64SToby Isaac 
7694bb7acecfSBarry Smith   Level: developer
769588bdff64SToby Isaac 
7696bb7acecfSBarry Smith   Note:
7697bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7698bb7acecfSBarry Smith 
7699bb7acecfSBarry Smith .seealso: `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
770088bdff64SToby Isaac @*/
7701*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7702*d71ae5a4SJacob Faibussowitsch {
770388bdff64SToby Isaac   PetscFunctionBegin;
770488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
770588bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
770689d734beSBarry Smith   if (dm == fdm) fdm = NULL;
77079566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
77089566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
770988bdff64SToby Isaac   dm->fineMesh = fdm;
771088bdff64SToby Isaac   PetscFunctionReturn(0);
771188bdff64SToby Isaac }
771288bdff64SToby Isaac 
7713a6ba4734SToby Isaac /*@C
7714bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7715a6ba4734SToby Isaac 
7716783e2ec8SMatthew G. Knepley   Collective on dm
7717783e2ec8SMatthew G. Knepley 
7718a6ba4734SToby Isaac   Input Parameters:
7719bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7720bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7721a6ba4734SToby Isaac . name     - The BC name
772245480ffeSMatthew G. Knepley . label    - The label defining constrained points
7723bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
772445480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7725a6ba4734SToby Isaac . field    - The field to constrain
772645480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7727a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7728a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
772956cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7730a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7731a6ba4734SToby Isaac 
773245480ffeSMatthew G. Knepley   Output Parameter:
773345480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
773445480ffeSMatthew G. Knepley 
7735a6ba4734SToby Isaac   Options Database Keys:
7736a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7737a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7738a6ba4734SToby Isaac 
7739bb7acecfSBarry Smith   Notes:
7740bb7acecfSBarry 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:
774156cf3b9cSMatthew G. Knepley 
774256cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
774356cf3b9cSMatthew G. Knepley 
7744bb7acecfSBarry Smith   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is:
774556cf3b9cSMatthew G. Knepley 
774656cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
774756cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
774856cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
774956cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
775056cf3b9cSMatthew G. Knepley 
775156cf3b9cSMatthew G. Knepley + dim - the spatial dimension
775256cf3b9cSMatthew G. Knepley . Nf - the number of fields
775356cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
775456cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
775556cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
775656cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
775756cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
775856cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
775956cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
776056cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
776156cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
776256cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
776356cf3b9cSMatthew G. Knepley . t - current time
776456cf3b9cSMatthew G. Knepley . x - coordinates of the current point
776556cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
776656cf3b9cSMatthew G. Knepley . constants - constant parameters
776756cf3b9cSMatthew G. Knepley - bcval - output values at the current point
776856cf3b9cSMatthew G. Knepley 
7769ed808b8fSJed Brown   Level: intermediate
7770a6ba4734SToby Isaac 
7771db781477SPatrick Sanan .seealso: `DSGetBoundary()`, `PetscDSAddBoundary()`
7772a6ba4734SToby Isaac @*/
7773*d71ae5a4SJacob 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)
7774*d71ae5a4SJacob Faibussowitsch {
7775e5e52638SMatthew G. Knepley   PetscDS ds;
7776a6ba4734SToby Isaac 
7777a6ba4734SToby Isaac   PetscFunctionBegin;
7778a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7779783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
778045480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
778145480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
778245480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
778345480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
778401a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
77859566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7786799db056SMatthew G. Knepley   /* Complete label */
7787799db056SMatthew G. Knepley   if (label) {
7788799db056SMatthew G. Knepley     PetscObject  obj;
7789799db056SMatthew G. Knepley     PetscClassId id;
7790799db056SMatthew G. Knepley 
7791799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7792799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7793799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7794799db056SMatthew G. Knepley       DM plex;
7795799db056SMatthew G. Knepley 
7796799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7797799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7798799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7799799db056SMatthew G. Knepley     }
7800799db056SMatthew G. Knepley   }
78019566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
7802a6ba4734SToby Isaac   PetscFunctionReturn(0);
7803a6ba4734SToby Isaac }
7804a6ba4734SToby Isaac 
780545480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7806*d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
7807*d71ae5a4SJacob Faibussowitsch {
7808e5e52638SMatthew G. Knepley   PetscDS     ds;
7809dff059c6SToby Isaac   DMBoundary *lastnext;
7810e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7811e6f8dbb6SToby Isaac 
7812e6f8dbb6SToby Isaac   PetscFunctionBegin;
78139566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7814e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
781547a1f5adSToby Isaac   if (dm->boundary) {
781647a1f5adSToby Isaac     DMBoundary next = dm->boundary;
781747a1f5adSToby Isaac 
781847a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
781947a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
782047a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
782147a1f5adSToby Isaac     while (next) {
782247a1f5adSToby Isaac       DMBoundary b = next;
782347a1f5adSToby Isaac 
782447a1f5adSToby Isaac       next = b->next;
78259566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7826a6ba4734SToby Isaac     }
782747a1f5adSToby Isaac     dm->boundary = NULL;
7828a6ba4734SToby Isaac   }
782947a1f5adSToby Isaac 
7830dff059c6SToby Isaac   lastnext = &(dm->boundary);
7831e6f8dbb6SToby Isaac   while (dsbound) {
7832e6f8dbb6SToby Isaac     DMBoundary dmbound;
7833e6f8dbb6SToby Isaac 
78349566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7835e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
783645480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
783747a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7838dff059c6SToby Isaac     *lastnext = dmbound;
7839dff059c6SToby Isaac     lastnext  = &(dmbound->next);
7840dff059c6SToby Isaac     dsbound   = dsbound->next;
7841a6ba4734SToby Isaac   }
7842a6ba4734SToby Isaac   PetscFunctionReturn(0);
7843a6ba4734SToby Isaac }
7844a6ba4734SToby Isaac 
7845bb7acecfSBarry Smith /* TODO: missing manual page */
7846*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7847*d71ae5a4SJacob Faibussowitsch {
7848b95f2879SToby Isaac   DMBoundary b;
7849a6ba4734SToby Isaac 
7850a6ba4734SToby Isaac   PetscFunctionBegin;
7851a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7852534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7853a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
78549566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7855b95f2879SToby Isaac   b = dm->boundary;
7856a6ba4734SToby Isaac   while (b && !(*isBd)) {
7857e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7858e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7859a6ba4734SToby Isaac     PetscInt   i;
7860a6ba4734SToby Isaac 
786145480ffeSMatthew G. Knepley     if (label) {
78629566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7863a6ba4734SToby Isaac     }
7864a6ba4734SToby Isaac     b = b->next;
7865a6ba4734SToby Isaac   }
7866a6ba4734SToby Isaac   PetscFunctionReturn(0);
7867a6ba4734SToby Isaac }
78684d6f44ffSToby Isaac 
78694d6f44ffSToby Isaac /*@C
7870bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
7871a6e0b375SMatthew G. Knepley 
7872bb7acecfSBarry Smith   Collective on dm
78734d6f44ffSToby Isaac 
78744d6f44ffSToby Isaac   Input Parameters:
7875bb7acecfSBarry Smith + dm      - The `DM`
78760709b2feSToby Isaac . time    - The time
78774d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
78784d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
78794d6f44ffSToby Isaac - mode    - The insertion mode for values
78804d6f44ffSToby Isaac 
78814d6f44ffSToby Isaac   Output Parameter:
78824d6f44ffSToby Isaac . X - vector
78834d6f44ffSToby Isaac 
78844d6f44ffSToby Isaac    Calling sequence of func:
788577b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
78864d6f44ffSToby Isaac 
78874d6f44ffSToby Isaac +  dim - The spatial dimension
78888ec8862eSJed Brown .  time - The time at which to sample
78894d6f44ffSToby Isaac .  x   - The coordinates
789077b739a6SMatthew Knepley .  Nc  - The number of components
78914d6f44ffSToby Isaac .  u   - The output field values
78924d6f44ffSToby Isaac -  ctx - optional user-defined function context
78934d6f44ffSToby Isaac 
78944d6f44ffSToby Isaac   Level: developer
78954d6f44ffSToby Isaac 
7896bb7acecfSBarry Smith   Developer Notes:
7897bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7898bb7acecfSBarry Smith 
7899bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7900bb7acecfSBarry Smith 
7901db781477SPatrick Sanan .seealso: `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
79024d6f44ffSToby Isaac @*/
7903*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
7904*d71ae5a4SJacob Faibussowitsch {
79054d6f44ffSToby Isaac   Vec localX;
79064d6f44ffSToby Isaac 
79074d6f44ffSToby Isaac   PetscFunctionBegin;
79084d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79099566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
79109566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
79119566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
79129566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
79139566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
79144d6f44ffSToby Isaac   PetscFunctionReturn(0);
79154d6f44ffSToby Isaac }
79164d6f44ffSToby Isaac 
7917a6e0b375SMatthew G. Knepley /*@C
7918bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
7919a6e0b375SMatthew G. Knepley 
7920a6e0b375SMatthew G. Knepley   Not collective
7921a6e0b375SMatthew G. Knepley 
7922a6e0b375SMatthew G. Knepley   Input Parameters:
7923bb7acecfSBarry Smith + dm      - The `DM`
7924a6e0b375SMatthew G. Knepley . time    - The time
7925a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7926a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7927a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7928a6e0b375SMatthew G. Knepley 
7929a6e0b375SMatthew G. Knepley   Output Parameter:
7930a6e0b375SMatthew G. Knepley . localX - vector
7931a6e0b375SMatthew G. Knepley 
7932a6e0b375SMatthew G. Knepley    Calling sequence of func:
793377b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7934a6e0b375SMatthew G. Knepley 
7935a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7936a6e0b375SMatthew G. Knepley .  x   - The coordinates
793777b739a6SMatthew Knepley .  Nc  - The number of components
7938a6e0b375SMatthew G. Knepley .  u   - The output field values
7939a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7940a6e0b375SMatthew G. Knepley 
7941a6e0b375SMatthew G. Knepley   Level: developer
7942a6e0b375SMatthew G. Knepley 
7943bb7acecfSBarry Smith   Developer Notes:
7944bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7945bb7acecfSBarry Smith 
7946bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7947bb7acecfSBarry Smith 
7948db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7949a6e0b375SMatthew G. Knepley @*/
7950*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
7951*d71ae5a4SJacob Faibussowitsch {
79524d6f44ffSToby Isaac   PetscFunctionBegin;
79534d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7954064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
79559566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal)(dm, time, funcs, ctxs, mode, localX));
79564d6f44ffSToby Isaac   PetscFunctionReturn(0);
79574d6f44ffSToby Isaac }
79584d6f44ffSToby Isaac 
7959a6e0b375SMatthew G. Knepley /*@C
7960bb7acecfSBarry 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.
7961a6e0b375SMatthew G. Knepley 
7962bb7acecfSBarry Smith   Collective on dm
7963a6e0b375SMatthew G. Knepley 
7964a6e0b375SMatthew G. Knepley   Input Parameters:
7965bb7acecfSBarry Smith + dm      - The `DM`
7966a6e0b375SMatthew G. Knepley . time    - The time
7967bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
7968a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7969bb7acecfSBarry Smith . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
7970a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7971a6e0b375SMatthew G. Knepley 
7972a6e0b375SMatthew G. Knepley   Output Parameter:
7973a6e0b375SMatthew G. Knepley . X - vector
7974a6e0b375SMatthew G. Knepley 
7975a6e0b375SMatthew G. Knepley    Calling sequence of func:
797677b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7977a6e0b375SMatthew G. Knepley 
7978a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7979a6e0b375SMatthew G. Knepley .  x   - The coordinates
798077b739a6SMatthew Knepley .  Nc  - The number of components
7981a6e0b375SMatthew G. Knepley .  u   - The output field values
7982a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7983a6e0b375SMatthew G. Knepley 
7984a6e0b375SMatthew G. Knepley   Level: developer
7985a6e0b375SMatthew G. Knepley 
7986bb7acecfSBarry Smith   Developer Notes:
7987bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7988bb7acecfSBarry Smith 
7989bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7990bb7acecfSBarry Smith 
7991db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
7992a6e0b375SMatthew G. Knepley @*/
7993*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
7994*d71ae5a4SJacob Faibussowitsch {
79952c53366bSMatthew G. Knepley   Vec localX;
79962c53366bSMatthew G. Knepley 
79972c53366bSMatthew G. Knepley   PetscFunctionBegin;
79982c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79999566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
80009566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
80019566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
80029566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
80039566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
80042c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
80052c53366bSMatthew G. Knepley }
80062c53366bSMatthew G. Knepley 
8007a6e0b375SMatthew G. Knepley /*@C
8008bb7acecfSBarry 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.
8009a6e0b375SMatthew G. Knepley 
8010a6e0b375SMatthew G. Knepley   Not collective
8011a6e0b375SMatthew G. Knepley 
8012a6e0b375SMatthew G. Knepley   Input Parameters:
8013bb7acecfSBarry Smith + dm      - The `DM`
8014a6e0b375SMatthew G. Knepley . time    - The time
8015bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
8016a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8017a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8018a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8019a6e0b375SMatthew G. Knepley 
8020a6e0b375SMatthew G. Knepley   Output Parameter:
8021a6e0b375SMatthew G. Knepley . localX - vector
8022a6e0b375SMatthew G. Knepley 
8023a6e0b375SMatthew G. Knepley    Calling sequence of func:
802477b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8025a6e0b375SMatthew G. Knepley 
8026a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8027a6e0b375SMatthew G. Knepley .  x   - The coordinates
802877b739a6SMatthew Knepley .  Nc  - The number of components
8029a6e0b375SMatthew G. Knepley .  u   - The output field values
8030a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8031a6e0b375SMatthew G. Knepley 
8032a6e0b375SMatthew G. Knepley   Level: developer
8033a6e0b375SMatthew G. Knepley 
8034bb7acecfSBarry Smith   Developer Notes:
8035bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8036bb7acecfSBarry Smith 
8037bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8038bb7acecfSBarry Smith 
8039db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8040a6e0b375SMatthew G. Knepley @*/
8041*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
8042*d71ae5a4SJacob Faibussowitsch {
80434d6f44ffSToby Isaac   PetscFunctionBegin;
80444d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8045064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
80469566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal)(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
80474d6f44ffSToby Isaac   PetscFunctionReturn(0);
80484d6f44ffSToby Isaac }
80492716604bSToby Isaac 
8050a6e0b375SMatthew G. Knepley /*@C
8051bb7acecfSBarry 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.
8052a6e0b375SMatthew G. Knepley 
8053a6e0b375SMatthew G. Knepley   Not collective
8054a6e0b375SMatthew G. Knepley 
8055a6e0b375SMatthew G. Knepley   Input Parameters:
8056bb7acecfSBarry Smith + dm      - The `DM`
8057a6e0b375SMatthew G. Knepley . time    - The time
8058a6e0b375SMatthew G. Knepley . localU  - The input field vector
8059a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8060a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8061a6e0b375SMatthew G. Knepley 
8062a6e0b375SMatthew G. Knepley   Output Parameter:
8063a6e0b375SMatthew G. Knepley . localX  - The output vector
8064a6e0b375SMatthew G. Knepley 
8065a6e0b375SMatthew G. Knepley    Calling sequence of func:
8066a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8067a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8068a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8069a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8070a6e0b375SMatthew G. Knepley 
8071a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8072a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8073a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8074a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8075a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8076a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8077a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8078a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8079a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8080a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8081a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8082a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8083a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8084a6e0b375SMatthew G. Knepley .  t            - The current time
8085a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8086a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8087a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8088a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8089a6e0b375SMatthew G. Knepley 
8090bb7acecfSBarry Smith   Note:
8091bb7acecfSBarry 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.
8092bb7acecfSBarry 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
8093bb7acecfSBarry 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
8094a6e0b375SMatthew 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.
8095a6e0b375SMatthew G. Knepley 
8096a6e0b375SMatthew G. Knepley   Level: intermediate
8097a6e0b375SMatthew G. Knepley 
8098bb7acecfSBarry Smith   Developer Notes:
8099bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8100bb7acecfSBarry Smith 
8101bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8102bb7acecfSBarry Smith 
8103db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8104a6e0b375SMatthew G. Knepley @*/
8105*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec localX)
8106*d71ae5a4SJacob Faibussowitsch {
81078c6c5593SMatthew G. Knepley   PetscFunctionBegin;
81088c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81098c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
81108c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
81119566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal)(dm, time, localU, funcs, mode, localX));
81128c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
81138c6c5593SMatthew G. Knepley }
81148c6c5593SMatthew G. Knepley 
8115a6e0b375SMatthew G. Knepley /*@C
8116a6e0b375SMatthew 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.
8117a6e0b375SMatthew G. Knepley 
8118a6e0b375SMatthew G. Knepley   Not collective
8119a6e0b375SMatthew G. Knepley 
8120a6e0b375SMatthew G. Knepley   Input Parameters:
8121bb7acecfSBarry Smith + dm      - The `DM`
8122a6e0b375SMatthew G. Knepley . time    - The time
8123bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8124a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8125a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8126bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8127a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8128a6e0b375SMatthew G. Knepley . localU  - The input field vector
8129a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8130a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8131a6e0b375SMatthew G. Knepley 
8132a6e0b375SMatthew G. Knepley   Output Parameter:
8133a6e0b375SMatthew G. Knepley . localX  - The output vector
8134a6e0b375SMatthew G. Knepley 
8135a6e0b375SMatthew G. Knepley    Calling sequence of func:
8136a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8137a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8138a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8139a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8140a6e0b375SMatthew G. Knepley 
8141a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8142a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8143a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8144a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8145a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8146a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8147a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8148a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8149a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8150a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8151a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8152a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8153a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8154a6e0b375SMatthew G. Knepley .  t            - The current time
8155a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8156a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8157a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8158a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8159a6e0b375SMatthew G. Knepley 
8160bb7acecfSBarry Smith   Note:
8161bb7acecfSBarry 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.
8162bb7acecfSBarry 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
8163bb7acecfSBarry 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
8164a6e0b375SMatthew 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.
8165a6e0b375SMatthew G. Knepley 
8166a6e0b375SMatthew G. Knepley   Level: intermediate
8167a6e0b375SMatthew G. Knepley 
8168bb7acecfSBarry Smith   Developer Notes:
8169bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8170bb7acecfSBarry Smith 
8171bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8172bb7acecfSBarry Smith 
8173d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8174a6e0b375SMatthew G. Knepley @*/
8175*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec localX)
8176*d71ae5a4SJacob Faibussowitsch {
81778c6c5593SMatthew G. Knepley   PetscFunctionBegin;
81788c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8179064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8180064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
81819566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
81828c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
81838c6c5593SMatthew G. Knepley }
81848c6c5593SMatthew G. Knepley 
81852716604bSToby Isaac /*@C
8186d29d7c6eSMatthew 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.
8187d29d7c6eSMatthew G. Knepley 
8188d29d7c6eSMatthew G. Knepley   Not collective
8189d29d7c6eSMatthew G. Knepley 
8190d29d7c6eSMatthew G. Knepley   Input Parameters:
8191bb7acecfSBarry Smith + dm      - The `DM`
8192d29d7c6eSMatthew G. Knepley . time    - The time
8193bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8194d29d7c6eSMatthew G. Knepley . numIds  - The number of label ids to use
8195d29d7c6eSMatthew G. Knepley . ids     - The label ids to use for marking
8196bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8197d29d7c6eSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8198d29d7c6eSMatthew G. Knepley . U       - The input field vector
8199d29d7c6eSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8200d29d7c6eSMatthew G. Knepley - mode    - The insertion mode for values
8201d29d7c6eSMatthew G. Knepley 
8202d29d7c6eSMatthew G. Knepley   Output Parameter:
8203d29d7c6eSMatthew G. Knepley . X       - The output vector
8204d29d7c6eSMatthew G. Knepley 
8205d29d7c6eSMatthew G. Knepley    Calling sequence of func:
8206d29d7c6eSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8207d29d7c6eSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8208d29d7c6eSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8209d29d7c6eSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8210d29d7c6eSMatthew G. Knepley 
8211d29d7c6eSMatthew G. Knepley +  dim          - The spatial dimension
8212d29d7c6eSMatthew G. Knepley .  Nf           - The number of input fields
8213d29d7c6eSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8214d29d7c6eSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8215d29d7c6eSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8216d29d7c6eSMatthew G. Knepley .  u            - The field values at this point in space
8217d29d7c6eSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8218d29d7c6eSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8219d29d7c6eSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8220d29d7c6eSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8221d29d7c6eSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8222d29d7c6eSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8223d29d7c6eSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8224d29d7c6eSMatthew G. Knepley .  t            - The current time
8225d29d7c6eSMatthew G. Knepley .  x            - The coordinates of this point
8226d29d7c6eSMatthew G. Knepley .  numConstants - The number of constants
8227d29d7c6eSMatthew G. Knepley .  constants    - The value of each constant
8228d29d7c6eSMatthew G. Knepley -  f            - The value of the function at this point in space
8229d29d7c6eSMatthew G. Knepley 
8230bb7acecfSBarry Smith   Note:
8231bb7acecfSBarry 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.
8232bb7acecfSBarry 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
8233bb7acecfSBarry 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
8234d29d7c6eSMatthew 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.
8235d29d7c6eSMatthew G. Knepley 
8236d29d7c6eSMatthew G. Knepley   Level: intermediate
8237d29d7c6eSMatthew G. Knepley 
8238bb7acecfSBarry Smith   Developer Notes:
8239bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8240bb7acecfSBarry Smith 
8241bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8242bb7acecfSBarry Smith 
8243d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8244d29d7c6eSMatthew G. Knepley @*/
8245*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec U, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec X)
8246*d71ae5a4SJacob Faibussowitsch {
8247d29d7c6eSMatthew G. Knepley   DM  dmIn;
8248d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8249d29d7c6eSMatthew G. Knepley 
8250d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8251d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8252d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8253d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8254d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
825572fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
825672fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8257d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8258d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8259d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8260d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8261d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
8262d29d7c6eSMatthew G. Knepley   PetscFunctionReturn(0);
8263d29d7c6eSMatthew G. Knepley }
8264d29d7c6eSMatthew G. Knepley 
8265d29d7c6eSMatthew G. Knepley /*@C
8266ece3a9fcSMatthew 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.
8267ece3a9fcSMatthew G. Knepley 
8268ece3a9fcSMatthew G. Knepley   Not collective
8269ece3a9fcSMatthew G. Knepley 
8270ece3a9fcSMatthew G. Knepley   Input Parameters:
8271bb7acecfSBarry Smith + dm      - The `DM`
8272ece3a9fcSMatthew G. Knepley . time    - The time
8273bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain boundary to output
8274ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8275ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8276bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8277ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8278ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8279ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8280ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8281ece3a9fcSMatthew G. Knepley 
8282ece3a9fcSMatthew G. Knepley   Output Parameter:
8283ece3a9fcSMatthew G. Knepley . localX  - The output vector
8284ece3a9fcSMatthew G. Knepley 
8285ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8286ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8287ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8288ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8289ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8290ece3a9fcSMatthew G. Knepley 
8291ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8292ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8293ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8294ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8295ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8296ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8297ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8298ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8299ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8300ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8301ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8302ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8303ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8304ece3a9fcSMatthew G. Knepley .  t            - The current time
8305ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8306ece3a9fcSMatthew G. Knepley .  n            - The face normal
8307ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8308ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8309ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8310ece3a9fcSMatthew G. Knepley 
8311ece3a9fcSMatthew G. Knepley   Note:
8312bb7acecfSBarry 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.
8313bb7acecfSBarry 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
8314bb7acecfSBarry 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
8315ece3a9fcSMatthew 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.
8316ece3a9fcSMatthew G. Knepley 
8317ece3a9fcSMatthew G. Knepley   Level: intermediate
8318ece3a9fcSMatthew G. Knepley 
8319bb7acecfSBarry Smith   Developer Notes:
8320bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8321bb7acecfSBarry Smith 
8322bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8323bb7acecfSBarry Smith 
8324db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8325ece3a9fcSMatthew G. Knepley @*/
8326*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec localX)
8327*d71ae5a4SJacob Faibussowitsch {
8328ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8329ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8330064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8331064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
83329566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8333ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8334ece3a9fcSMatthew G. Knepley }
8335ece3a9fcSMatthew G. Knepley 
8336ece3a9fcSMatthew G. Knepley /*@C
83372716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
83382716604bSToby Isaac 
8339bb7acecfSBarry Smith   Collective on dm
8340bb7acecfSBarry Smith 
83412716604bSToby Isaac   Input Parameters:
8342bb7acecfSBarry Smith + dm    - The `DM`
83430709b2feSToby Isaac . time  - The time
83442716604bSToby Isaac . funcs - The functions to evaluate for each field component
83452716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8346574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
83472716604bSToby Isaac 
83482716604bSToby Isaac   Output Parameter:
83492716604bSToby Isaac . diff - The diff ||u - u_h||_2
83502716604bSToby Isaac 
83512716604bSToby Isaac   Level: developer
83522716604bSToby Isaac 
8353bb7acecfSBarry Smith   Developer Notes:
8354bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8355bb7acecfSBarry Smith 
8356bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8357bb7acecfSBarry Smith 
8358db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
83592716604bSToby Isaac @*/
8360*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8361*d71ae5a4SJacob Faibussowitsch {
83622716604bSToby Isaac   PetscFunctionBegin;
83632716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8364b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
83659566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm, time, funcs, ctxs, X, diff));
83662716604bSToby Isaac   PetscFunctionReturn(0);
83672716604bSToby Isaac }
8368b698f381SToby Isaac 
8369b698f381SToby Isaac /*@C
8370b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8371b698f381SToby Isaac 
8372d083f849SBarry Smith   Collective on dm
8373d083f849SBarry Smith 
8374b698f381SToby Isaac   Input Parameters:
8375bb7acecfSBarry Smith + dm    - The `DM`
8376b698f381SToby Isaac , time  - The time
8377b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8378b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8379574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8380b698f381SToby Isaac - n     - The vector to project along
8381b698f381SToby Isaac 
8382b698f381SToby Isaac   Output Parameter:
8383b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8384b698f381SToby Isaac 
8385b698f381SToby Isaac   Level: developer
8386b698f381SToby Isaac 
8387bb7acecfSBarry Smith   Developer Notes:
8388bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8389bb7acecfSBarry Smith 
8390bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8391bb7acecfSBarry Smith 
8392bb7acecfSBarry Smith .seealso: `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8393b698f381SToby Isaac @*/
8394*d71ae5a4SJacob 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)
8395*d71ae5a4SJacob Faibussowitsch {
8396b698f381SToby Isaac   PetscFunctionBegin;
8397b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8398b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
83999566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm, time, funcs, ctxs, X, n, diff));
8400b698f381SToby Isaac   PetscFunctionReturn(0);
8401b698f381SToby Isaac }
8402b698f381SToby Isaac 
84032a16baeaSToby Isaac /*@C
84042a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
84052a16baeaSToby Isaac 
8406d083f849SBarry Smith   Collective on dm
8407d083f849SBarry Smith 
84082a16baeaSToby Isaac   Input Parameters:
8409bb7acecfSBarry Smith + dm    - The `DM`
84102a16baeaSToby Isaac . time  - The time
84112a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
84122a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8413574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84142a16baeaSToby Isaac 
84152a16baeaSToby Isaac   Output Parameter:
84162a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
84172a16baeaSToby Isaac 
84182a16baeaSToby Isaac   Level: developer
84192a16baeaSToby Isaac 
8420bb7acecfSBarry Smith   Developer Notes:
8421bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8422bb7acecfSBarry Smith 
8423bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8424bb7acecfSBarry Smith 
8425db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
84262a16baeaSToby Isaac @*/
8427*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8428*d71ae5a4SJacob Faibussowitsch {
84292a16baeaSToby Isaac   PetscFunctionBegin;
84302a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84312a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84329566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm, time, funcs, ctxs, X, diff));
84332a16baeaSToby Isaac   PetscFunctionReturn(0);
84342a16baeaSToby Isaac }
84352a16baeaSToby Isaac 
8436df0b854cSToby Isaac /*@C
8437bb7acecfSBarry Smith  DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8438502a2867SDave May 
8439502a2867SDave May  Not Collective
8440502a2867SDave May 
8441502a2867SDave May  Input Parameter:
8442bb7acecfSBarry Smith .  dm    - The `DM`
8443502a2867SDave May 
84440a19bb7dSprj-  Output Parameters:
84450a19bb7dSprj- +  nranks - the number of neighbours
84460a19bb7dSprj- -  ranks - the neighbors ranks
8447502a2867SDave May 
8448bb7acecfSBarry Smith  Note:
8449bb7acecfSBarry Smith  Do not free the array, it is freed when the `DM` is destroyed.
8450502a2867SDave May 
8451502a2867SDave May  Level: beginner
8452502a2867SDave May 
8453db781477SPatrick Sanan  .seealso: `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8454502a2867SDave May @*/
8455*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8456*d71ae5a4SJacob Faibussowitsch {
8457502a2867SDave May   PetscFunctionBegin;
8458502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84599566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm, nranks, ranks));
8460502a2867SDave May   PetscFunctionReturn(0);
8461502a2867SDave May }
8462502a2867SDave May 
8463531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8464531c7667SBarry Smith 
8465531c7667SBarry Smith /*
8466531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8467531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8468531c7667SBarry Smith */
8469*d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8470*d71ae5a4SJacob Faibussowitsch {
8471531c7667SBarry Smith   PetscFunctionBegin;
8472531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8473531c7667SBarry Smith     Vec x1local;
8474531c7667SBarry Smith     DM  dm;
84759566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
84767a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
84779566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
84789566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
84799566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8480531c7667SBarry Smith     x1 = x1local;
8481531c7667SBarry Smith   }
84829566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8483531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8484531c7667SBarry Smith     DM dm;
84859566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
84869566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8487531c7667SBarry Smith   }
8488531c7667SBarry Smith   PetscFunctionReturn(0);
8489531c7667SBarry Smith }
8490531c7667SBarry Smith 
8491531c7667SBarry Smith /*@
8492bb7acecfSBarry Smith     MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8493531c7667SBarry Smith 
8494531c7667SBarry Smith     Input Parameter:
8495bb7acecfSBarry Smith .    coloring - the `MatFDColoring` object
8496531c7667SBarry Smith 
8497bb7acecfSBarry Smith     Developer Note:
8498bb7acecfSBarry Smith     this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8499531c7667SBarry Smith 
85001b266c99SBarry Smith     Level: advanced
85011b266c99SBarry Smith 
8502db781477SPatrick Sanan .seealso: `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8503531c7667SBarry Smith @*/
8504*d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8505*d71ae5a4SJacob Faibussowitsch {
8506531c7667SBarry Smith   PetscFunctionBegin;
8507531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8508531c7667SBarry Smith   PetscFunctionReturn(0);
8509531c7667SBarry Smith }
85108320bc6fSPatrick Sanan 
85118320bc6fSPatrick Sanan /*@
8512bb7acecfSBarry Smith     DMGetCompatibility - determine if two `DM`s are compatible
85138320bc6fSPatrick Sanan 
85148320bc6fSPatrick Sanan     Collective
85158320bc6fSPatrick Sanan 
85168320bc6fSPatrick Sanan     Input Parameters:
8517bb7acecfSBarry Smith +    dm1 - the first `DM`
8518bb7acecfSBarry Smith -    dm2 - the second `DM`
85198320bc6fSPatrick Sanan 
85208320bc6fSPatrick Sanan     Output Parameters:
8521bb7acecfSBarry Smith +    compatible - whether or not the two `DM`s are compatible
8522bb7acecfSBarry Smith -    set - whether or not the compatible value was actually determined and set
85238320bc6fSPatrick Sanan 
85248320bc6fSPatrick Sanan     Notes:
8525bb7acecfSBarry Smith     Two `DM`s are deemed compatible if they represent the same parallel decomposition
85263d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
85278320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
8528bb7acecfSBarry Smith     Loosely speaking, compatible `DM`s represent the same domain and parallel
85293d862458SPatrick Sanan     decomposition, but hold different data.
85308320bc6fSPatrick Sanan 
85318320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
8532bb7acecfSBarry Smith     over a pair of vectors obtained from different `DM`s.
85338320bc6fSPatrick Sanan 
8534bb7acecfSBarry Smith     For example, two `DMDA` objects are compatible if they have the same local
85358320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
85368320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
8537bb7acecfSBarry Smith     either `DM` in bounds for a loop over vectors derived from either `DM`.
85388320bc6fSPatrick Sanan 
8539bb7acecfSBarry Smith     Consider the operation of summing data living on a 2-dof `DMDA` to data living
8540bb7acecfSBarry Smith     on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
85418320bc6fSPatrick Sanan .vb
85428320bc6fSPatrick Sanan   ...
85439566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
85448320bc6fSPatrick Sanan   if (set && compatible)  {
85459566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
85469566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
85479566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
85488320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
85498320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
85508320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
85518320bc6fSPatrick Sanan       }
85528320bc6fSPatrick Sanan     }
85539566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
85549566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
85558320bc6fSPatrick Sanan   } else {
85568320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
85578320bc6fSPatrick Sanan   }
85588320bc6fSPatrick Sanan   ...
85598320bc6fSPatrick Sanan .ve
85608320bc6fSPatrick Sanan 
8561bb7acecfSBarry Smith     Checking compatibility might be expensive for a given implementation of `DM`,
85628320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
85638320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
85648320bc6fSPatrick Sanan     always check the "set" output parameter.
85658320bc6fSPatrick Sanan 
8566bb7acecfSBarry Smith     A `DM` is always compatible with itself.
85678320bc6fSPatrick Sanan 
8568bb7acecfSBarry Smith     In the current implementation, `DM`s which live on "unequal" communicators
85698320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
85708320bc6fSPatrick Sanan     incompatible.
85718320bc6fSPatrick Sanan 
85728320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
8573bb7acecfSBarry Smith     is required on each rank. However, in `DM` implementations which store all this
85748320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
85758320bc6fSPatrick Sanan 
8576bb7acecfSBarry Smith     Developer Note:
8577bb7acecfSBarry Smith     Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
85783d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8579a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
8580bb7acecfSBarry Smith     compatibility. It is left to `DM` implementers to ensure that symmetry is
85818320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
85823d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
8583bb7acecfSBarry Smith     of other `DM` types and let *set = PETSC_FALSE if found.
85848320bc6fSPatrick Sanan 
85858320bc6fSPatrick Sanan     Level: advanced
85868320bc6fSPatrick Sanan 
8587db781477SPatrick Sanan .seealso: `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
85888320bc6fSPatrick Sanan @*/
8589*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8590*d71ae5a4SJacob Faibussowitsch {
85918320bc6fSPatrick Sanan   PetscMPIInt compareResult;
85928320bc6fSPatrick Sanan   DMType      type, type2;
85938320bc6fSPatrick Sanan   PetscBool   sameType;
85948320bc6fSPatrick Sanan 
85958320bc6fSPatrick Sanan   PetscFunctionBegin;
8596a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
85978320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
85988320bc6fSPatrick Sanan 
85998320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8600a5bc1bf3SBarry Smith   if (dm1 == dm2) {
86018320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86028320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
86038320bc6fSPatrick Sanan     PetscFunctionReturn(0);
86048320bc6fSPatrick Sanan   }
86058320bc6fSPatrick Sanan 
86068320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
86078320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
86088320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
86098320bc6fSPatrick Sanan      determined by the implementation-specific logic */
86109566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
86118320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
86128320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86138320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
86148320bc6fSPatrick Sanan     PetscFunctionReturn(0);
86158320bc6fSPatrick Sanan   }
86168320bc6fSPatrick Sanan 
86178320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8618a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8619dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
8620b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
86218320bc6fSPatrick Sanan   }
86228320bc6fSPatrick Sanan 
8623a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
86248320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
86259566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
86269566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
86279566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
86288320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8629dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
86308320bc6fSPatrick Sanan   } else {
86318320bc6fSPatrick Sanan     *set = PETSC_FALSE;
86328320bc6fSPatrick Sanan   }
86338320bc6fSPatrick Sanan   PetscFunctionReturn(0);
86348320bc6fSPatrick Sanan }
8635c0f0dcc3SMatthew G. Knepley 
8636c0f0dcc3SMatthew G. Knepley /*@C
8637bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8638c0f0dcc3SMatthew G. Knepley 
8639bb7acecfSBarry Smith   Logically Collective on dm
8640c0f0dcc3SMatthew G. Knepley 
8641c0f0dcc3SMatthew G. Knepley   Input Parameters:
8642bb7acecfSBarry Smith + DM - the `DM`
8643c0f0dcc3SMatthew G. Knepley . f - the monitor function
8644c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8645c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8646c0f0dcc3SMatthew G. Knepley 
8647c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8648bb7acecfSBarry Smith - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8649c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8650c0f0dcc3SMatthew G. Knepley 
8651bb7acecfSBarry Smith   Note:
8652c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8653bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8654c0f0dcc3SMatthew G. Knepley   order in which they were set.
8655c0f0dcc3SMatthew G. Knepley 
8656bb7acecfSBarry Smith   Fortran Note:
8657bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8658bb7acecfSBarry Smith 
8659bb7acecfSBarry Smith   Developer Note:
8660bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8661c0f0dcc3SMatthew G. Knepley 
8662c0f0dcc3SMatthew G. Knepley   Level: intermediate
8663c0f0dcc3SMatthew G. Knepley 
8664bb7acecfSBarry Smith .seealso: `DMMonitorCancel()`,`DMMonitorSetFromOptions()`, `DMMonitor()`
8665c0f0dcc3SMatthew G. Knepley @*/
8666*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8667*d71ae5a4SJacob Faibussowitsch {
8668c0f0dcc3SMatthew G. Knepley   PetscInt m;
8669c0f0dcc3SMatthew G. Knepley 
8670c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8671c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8672c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8673c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8674c0f0dcc3SMatthew G. Knepley 
86759566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
8676c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8677c0f0dcc3SMatthew G. Knepley   }
86787a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8679c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8680c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8681c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
8682c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8683c0f0dcc3SMatthew G. Knepley }
8684c0f0dcc3SMatthew G. Knepley 
8685c0f0dcc3SMatthew G. Knepley /*@
8686bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8687c0f0dcc3SMatthew G. Knepley 
8688bb7acecfSBarry Smith   Logically Collective on dm
8689c0f0dcc3SMatthew G. Knepley 
8690c0f0dcc3SMatthew G. Knepley   Input Parameter:
8691c0f0dcc3SMatthew G. Knepley . dm - the DM
8692c0f0dcc3SMatthew G. Knepley 
8693c0f0dcc3SMatthew G. Knepley   Options Database Key:
8694c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8695bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8696c0f0dcc3SMatthew G. Knepley   set via the options database
8697c0f0dcc3SMatthew G. Knepley 
8698bb7acecfSBarry Smith   Note:
8699bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8700c0f0dcc3SMatthew G. Knepley 
8701c0f0dcc3SMatthew G. Knepley   Level: intermediate
8702c0f0dcc3SMatthew G. Knepley 
8703bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`,  `DMMonitor()`
8704c0f0dcc3SMatthew G. Knepley @*/
8705*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8706*d71ae5a4SJacob Faibussowitsch {
8707c0f0dcc3SMatthew G. Knepley   PetscInt m;
8708c0f0dcc3SMatthew G. Knepley 
8709c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8710c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8711c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
87129566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8713c0f0dcc3SMatthew G. Knepley   }
8714c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8715c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8716c0f0dcc3SMatthew G. Knepley }
8717c0f0dcc3SMatthew G. Knepley 
8718c0f0dcc3SMatthew G. Knepley /*@C
8719c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8720c0f0dcc3SMatthew G. Knepley 
8721bb7acecfSBarry Smith   Collective on dm
8722c0f0dcc3SMatthew G. Knepley 
8723c0f0dcc3SMatthew G. Knepley   Input Parameters:
8724bb7acecfSBarry Smith + dm   - `DM` object you wish to monitor
8725c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8726c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8727c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8728c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8729bb7acecfSBarry 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
8730c0f0dcc3SMatthew G. Knepley 
8731c0f0dcc3SMatthew G. Knepley   Output Parameter:
8732c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8733c0f0dcc3SMatthew G. Knepley 
8734c0f0dcc3SMatthew G. Knepley   Level: developer
8735c0f0dcc3SMatthew G. Knepley 
8736db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8737db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
8738db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
8739db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8740c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8741db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8742bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8743c0f0dcc3SMatthew G. Knepley @*/
8744*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8745*d71ae5a4SJacob Faibussowitsch {
8746c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8747c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8748c0f0dcc3SMatthew G. Knepley 
8749c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8750c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87519566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8752c0f0dcc3SMatthew G. Knepley   if (*flg) {
8753c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8754c0f0dcc3SMatthew G. Knepley 
87559566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
87569566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)viewer));
87579566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
87589566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8759c0f0dcc3SMatthew G. Knepley   }
8760c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8761c0f0dcc3SMatthew G. Knepley }
8762c0f0dcc3SMatthew G. Knepley 
8763c0f0dcc3SMatthew G. Knepley /*@
8764c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8765c0f0dcc3SMatthew G. Knepley 
8766bb7acecfSBarry Smith    Collective on dm
8767c0f0dcc3SMatthew G. Knepley 
8768c0f0dcc3SMatthew G. Knepley    Input Parameters:
8769bb7acecfSBarry Smith .  dm - The `DM`
8770c0f0dcc3SMatthew G. Knepley 
8771c0f0dcc3SMatthew G. Knepley    Level: developer
8772c0f0dcc3SMatthew G. Knepley 
8773bb7acecfSBarry Smith    Question:
8774bb7acecfSBarry Smith    Note should indicate when during the life of the `DM` the monitor is run. It appears to be related to the discretization process seems rather specialized
8775bb7acecfSBarry Smith    since some `DM` have no concept of discretization
8776bb7acecfSBarry Smith 
8777bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8778c0f0dcc3SMatthew G. Knepley @*/
8779*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8780*d71ae5a4SJacob Faibussowitsch {
8781c0f0dcc3SMatthew G. Knepley   PetscInt m;
8782c0f0dcc3SMatthew G. Knepley 
8783c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8784c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8785c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
878648a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
8787c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8788c0f0dcc3SMatthew G. Knepley }
87892e4af2aeSMatthew G. Knepley 
87902e4af2aeSMatthew G. Knepley /*@
8791bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
87922e4af2aeSMatthew G. Knepley 
8793bb7acecfSBarry Smith   Collective on dm
87942e4af2aeSMatthew G. Knepley 
87952e4af2aeSMatthew G. Knepley   Input Parameters:
8796bb7acecfSBarry Smith + dm     - The `DM`
87976b867d5aSJose E. Roman - sol    - The solution vector
87982e4af2aeSMatthew G. Knepley 
87996b867d5aSJose E. Roman   Input/Output Parameter:
88006b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
88016b867d5aSJose E. Roman            contains the error in each field
88026b867d5aSJose E. Roman 
88036b867d5aSJose E. Roman   Output Parameter:
88046b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
88052e4af2aeSMatthew G. Knepley 
8806bb7acecfSBarry Smith   Note:
8807bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
88082e4af2aeSMatthew G. Knepley 
88092e4af2aeSMatthew G. Knepley   Level: developer
88102e4af2aeSMatthew G. Knepley 
8811db781477SPatrick Sanan .seealso: `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
88122e4af2aeSMatthew G. Knepley @*/
8813*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
8814*d71ae5a4SJacob Faibussowitsch {
88152e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
88162e4af2aeSMatthew G. Knepley   void    **ctxs;
88172e4af2aeSMatthew G. Knepley   PetscReal time;
88182e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
88192e4af2aeSMatthew G. Knepley 
88202e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
88219566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
88229566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
88239566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
88242e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
88252e4af2aeSMatthew G. Knepley     PetscDS         ds;
88262e4af2aeSMatthew G. Knepley     DMLabel         label;
88272e4af2aeSMatthew G. Knepley     IS              fieldIS;
88282e4af2aeSMatthew G. Knepley     const PetscInt *fields;
88292e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
88302e4af2aeSMatthew G. Knepley 
88319566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
88329566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
88339566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
88342e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
88352e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
88369566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
88372e4af2aeSMatthew G. Knepley     }
88389566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
88392e4af2aeSMatthew G. Knepley   }
8840ad540459SPierre 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);
88419566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
88429566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
88432e4af2aeSMatthew G. Knepley   if (errorVec) {
88442e4af2aeSMatthew G. Knepley     DM             edm;
88452e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
88462e4af2aeSMatthew G. Knepley     PetscBool      simplex;
88472e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
88482e4af2aeSMatthew G. Knepley 
88499566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
88509566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
88519566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
88529566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
88532e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
88549566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
88552e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
88562e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
88572e4af2aeSMatthew G. Knepley       PetscQuadrature q;
88582e4af2aeSMatthew G. Knepley       const char     *name;
88592e4af2aeSMatthew G. Knepley 
88609566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
88619566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
88629566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
88639566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
88649566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
88659566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
88669566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
88679566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
88682e4af2aeSMatthew G. Knepley     }
88699566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
88702e4af2aeSMatthew G. Knepley 
88719566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
88729566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
88739566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
88749566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
88752e4af2aeSMatthew G. Knepley   }
88769566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
88772e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
88782e4af2aeSMatthew G. Knepley }
88799a2a23afSMatthew G. Knepley 
88809a2a23afSMatthew G. Knepley /*@
8881bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
88829a2a23afSMatthew G. Knepley 
88839a2a23afSMatthew G. Knepley   Not collective
88849a2a23afSMatthew G. Knepley 
88859a2a23afSMatthew G. Knepley   Input Parameter:
8886bb7acecfSBarry Smith . dm     - The `DM`
88879a2a23afSMatthew G. Knepley 
88889a2a23afSMatthew G. Knepley   Output Parameter:
8889a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
88909a2a23afSMatthew G. Knepley 
88919a2a23afSMatthew G. Knepley   Level: advanced
88929a2a23afSMatthew G. Knepley 
8893bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
88949a2a23afSMatthew G. Knepley @*/
8895*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
8896*d71ae5a4SJacob Faibussowitsch {
88979a2a23afSMatthew G. Knepley   PetscFunctionBegin;
88989a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88999566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
89009a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
89019a2a23afSMatthew G. Knepley }
89029a2a23afSMatthew G. Knepley 
89039a2a23afSMatthew G. Knepley /*@
8904ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
89059a2a23afSMatthew G. Knepley 
89069a2a23afSMatthew G. Knepley   Not collective
89079a2a23afSMatthew G. Knepley 
89089a2a23afSMatthew G. Knepley   Input Parameters:
8909bb7acecfSBarry Smith + dm     - The `DM`
8910bb7acecfSBarry Smith . label  - The `DMLabel`
8911ac17215fSMatthew G. Knepley . value  - The label value indicating the region
8912ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
89139a2a23afSMatthew G. Knepley 
89149a2a23afSMatthew G. Knepley   Output Parameter:
8915bb7acecfSBarry Smith . aux    - The `Vec` holding auxiliary field data
89169a2a23afSMatthew G. Knepley 
8917bb7acecfSBarry Smith   Note:
8918bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
891904c51a94SMatthew G. Knepley 
89209a2a23afSMatthew G. Knepley   Level: advanced
89219a2a23afSMatthew G. Knepley 
8922bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
89239a2a23afSMatthew G. Knepley @*/
8924*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
8925*d71ae5a4SJacob Faibussowitsch {
8926ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
892704c51a94SMatthew G. Knepley   PetscBool       has;
89289a2a23afSMatthew G. Knepley 
89299a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89309a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89319a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
89329a2a23afSMatthew G. Knepley   key.label = label;
89339a2a23afSMatthew G. Knepley   key.value = value;
8934ac17215fSMatthew G. Knepley   key.part  = part;
89359566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
89369566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
89379566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
89389a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
89399a2a23afSMatthew G. Knepley }
89409a2a23afSMatthew G. Knepley 
89419a2a23afSMatthew G. Knepley /*@
8942bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
89439a2a23afSMatthew G. Knepley 
8944bb7acecfSBarry Smith   Not collective because auxilary vectors are not parallel
89459a2a23afSMatthew G. Knepley 
89469a2a23afSMatthew G. Knepley   Input Parameters:
8947bb7acecfSBarry Smith + dm     - The `DM`
8948bb7acecfSBarry Smith . label  - The `DMLabel`
89499a2a23afSMatthew G. Knepley . value  - The label value indicating the region
8950ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
8951bb7acecfSBarry Smith - aux    - The `Vec` holding auxiliary field data
89529a2a23afSMatthew G. Knepley 
89539a2a23afSMatthew G. Knepley   Level: advanced
89549a2a23afSMatthew G. Knepley 
8955bb7acecfSBarry Smith .seealso: `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
89569a2a23afSMatthew G. Knepley @*/
8957*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
8958*d71ae5a4SJacob Faibussowitsch {
89599a2a23afSMatthew G. Knepley   Vec             old;
89609a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
89619a2a23afSMatthew G. Knepley 
89629a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89639a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89649a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
89659a2a23afSMatthew G. Knepley   key.label = label;
89669a2a23afSMatthew G. Knepley   key.value = value;
8967ac17215fSMatthew G. Knepley   key.part  = part;
89689566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
89699566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
89709566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)old));
89719566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
89729566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
89739a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
89749a2a23afSMatthew G. Knepley }
89759a2a23afSMatthew G. Knepley 
89769a2a23afSMatthew G. Knepley /*@C
8977bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
89789a2a23afSMatthew G. Knepley 
89799a2a23afSMatthew G. Knepley   Not collective
89809a2a23afSMatthew G. Knepley 
89819a2a23afSMatthew G. Knepley   Input Parameter:
8982bb7acecfSBarry Smith . dm      - The `DM`
89839a2a23afSMatthew G. Knepley 
89849a2a23afSMatthew G. Knepley   Output Parameters:
8985bb7acecfSBarry Smith + labels  - The `DMLabel`s for each `Vec`
8986bb7acecfSBarry Smith . values  - The label values for each `Vec`
8987bb7acecfSBarry Smith - parts   - The equation parts for each `Vec`
89889a2a23afSMatthew G. Knepley 
8989bb7acecfSBarry Smith   Note:
8990bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
89919a2a23afSMatthew G. Knepley 
89929a2a23afSMatthew G. Knepley   Level: advanced
89939a2a23afSMatthew G. Knepley 
8994bb7acecfSBarry Smith .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMSetAuxiliaryVec()`, DMCopyAuxiliaryVec()`
89959a2a23afSMatthew G. Knepley @*/
8996*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
8997*d71ae5a4SJacob Faibussowitsch {
89989a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
89999a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
90009a2a23afSMatthew G. Knepley 
90019a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90029a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90039a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
9004dadcf809SJacob Faibussowitsch   PetscValidIntPointer(values, 3);
9005dadcf809SJacob Faibussowitsch   PetscValidIntPointer(parts, 4);
90069566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
90079566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
90089566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
90099371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
90109371c9d4SSatish Balay     labels[i] = keys[i].label;
90119371c9d4SSatish Balay     values[i] = keys[i].value;
90129371c9d4SSatish Balay     parts[i]  = keys[i].part;
90139371c9d4SSatish Balay   }
90149566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
90159a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
90169a2a23afSMatthew G. Knepley }
90179a2a23afSMatthew G. Knepley 
90189a2a23afSMatthew G. Knepley /*@
9019bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
90209a2a23afSMatthew G. Knepley 
90219a2a23afSMatthew G. Knepley   Not collective
90229a2a23afSMatthew G. Knepley 
90239a2a23afSMatthew G. Knepley   Input Parameter:
9024bb7acecfSBarry Smith . dm    - The `DM`
90259a2a23afSMatthew G. Knepley 
90269a2a23afSMatthew G. Knepley   Output Parameter:
9027bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
90289a2a23afSMatthew G. Knepley 
90299a2a23afSMatthew G. Knepley   Level: advanced
90309a2a23afSMatthew G. Knepley 
9031bb7acecfSBarry Smith   Note:
9032bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9033bb7acecfSBarry Smith 
9034db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
90359a2a23afSMatthew G. Knepley @*/
9036*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9037*d71ae5a4SJacob Faibussowitsch {
90389a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90399a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90409566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
90419566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
90429a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
90439a2a23afSMatthew G. Knepley }
9044b5a892a1SMatthew G. Knepley 
9045b5a892a1SMatthew G. Knepley /*@C
9046bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9047b5a892a1SMatthew G. Knepley 
9048b5a892a1SMatthew G. Knepley   Not collective
9049b5a892a1SMatthew G. Knepley 
9050b5a892a1SMatthew G. Knepley   Input Parameters:
9051bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9052b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9053b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9054b5a892a1SMatthew G. Knepley 
9055b5a892a1SMatthew G. Knepley   Output Parameters:
9056bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9057b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9058b5a892a1SMatthew G. Knepley 
9059b5a892a1SMatthew G. Knepley   Level: advanced
9060b5a892a1SMatthew G. Knepley 
9061bb7acecfSBarry Smith   Note:
9062bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9063bb7acecfSBarry Smith 
9064bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9065bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9066bb7acecfSBarry Smith 
9067bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9068bb7acecfSBarry Smith 
9069bb7acecfSBarry Smith .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9070b5a892a1SMatthew G. Knepley @*/
9071*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9072*d71ae5a4SJacob Faibussowitsch {
9073b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9074b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9075b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9076b5a892a1SMatthew G. Knepley 
9077b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
90789371c9d4SSatish Balay   if (!nO) {
90799371c9d4SSatish Balay     *ornt  = 0;
90809371c9d4SSatish Balay     *found = PETSC_TRUE;
90819371c9d4SSatish Balay     PetscFunctionReturn(0);
90829371c9d4SSatish Balay   }
9083b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9084b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9085b5a892a1SMatthew G. Knepley 
90869371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
90879371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
90889371c9d4SSatish Balay     if (c == cS) {
90899371c9d4SSatish Balay       *ornt = o;
90909371c9d4SSatish Balay       break;
90919371c9d4SSatish Balay     }
9092b5a892a1SMatthew G. Knepley   }
9093b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9094b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9095b5a892a1SMatthew G. Knepley }
9096b5a892a1SMatthew G. Knepley 
9097b5a892a1SMatthew G. Knepley /*@C
9098bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9099b5a892a1SMatthew G. Knepley 
9100b5a892a1SMatthew G. Knepley   Not collective
9101b5a892a1SMatthew G. Knepley 
9102b5a892a1SMatthew G. Knepley   Input Parameters:
9103bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9104b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9105b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9106b5a892a1SMatthew G. Knepley 
9107b5a892a1SMatthew G. Knepley   Output Parameters:
9108bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9109b5a892a1SMatthew G. Knepley 
9110b5a892a1SMatthew G. Knepley   Level: advanced
9111b5a892a1SMatthew G. Knepley 
9112bb7acecfSBarry Smith   Note:
9113bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9114bb7acecfSBarry Smith 
9115bb7acecfSBarry Smith   Developer Note:
9116bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9117bb7acecfSBarry Smith 
9118bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9119b5a892a1SMatthew G. Knepley @*/
9120*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9121*d71ae5a4SJacob Faibussowitsch {
9122b5a892a1SMatthew G. Knepley   PetscBool found;
9123b5a892a1SMatthew G. Knepley 
9124b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91259566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
91267a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9127b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9128b5a892a1SMatthew G. Knepley }
9129b5a892a1SMatthew G. Knepley 
9130b5a892a1SMatthew G. Knepley /*@C
9131bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9132b5a892a1SMatthew G. Knepley 
9133b5a892a1SMatthew G. Knepley   Not collective
9134b5a892a1SMatthew G. Knepley 
9135b5a892a1SMatthew G. Knepley   Input Parameters:
9136bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9137b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9138b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9139b5a892a1SMatthew G. Knepley 
9140b5a892a1SMatthew G. Knepley   Output Parameters:
9141bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9142b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9143b5a892a1SMatthew G. Knepley 
9144b5a892a1SMatthew G. Knepley   Level: advanced
9145b5a892a1SMatthew G. Knepley 
9146bb7acecfSBarry Smith   Note:
9147bb7acecfSBarry Smith   An arrangement is a vertex order
9148bb7acecfSBarry Smith 
9149bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9150bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9151bb7acecfSBarry Smith 
9152bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9153bb7acecfSBarry Smith 
9154bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangment()`
9155b5a892a1SMatthew G. Knepley @*/
9156*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9157*d71ae5a4SJacob Faibussowitsch {
9158b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9159b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9160b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9161b5a892a1SMatthew G. Knepley 
9162b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91639371c9d4SSatish Balay   if (!nO) {
91649371c9d4SSatish Balay     *ornt  = 0;
91659371c9d4SSatish Balay     *found = PETSC_TRUE;
91669371c9d4SSatish Balay     PetscFunctionReturn(0);
91679371c9d4SSatish Balay   }
9168b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9169b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9170b5a892a1SMatthew G. Knepley 
91719371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
91729371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
91739371c9d4SSatish Balay     if (c == cS) {
91749371c9d4SSatish Balay       *ornt = o;
91759371c9d4SSatish Balay       break;
91769371c9d4SSatish Balay     }
9177b5a892a1SMatthew G. Knepley   }
9178b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9179b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9180b5a892a1SMatthew G. Knepley }
9181b5a892a1SMatthew G. Knepley 
9182b5a892a1SMatthew G. Knepley /*@C
9183bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9184b5a892a1SMatthew G. Knepley 
9185b5a892a1SMatthew G. Knepley   Not collective
9186b5a892a1SMatthew G. Knepley 
9187b5a892a1SMatthew G. Knepley   Input Parameters:
9188bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9189b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9190b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9191b5a892a1SMatthew G. Knepley 
9192b5a892a1SMatthew G. Knepley   Output Parameters:
9193bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9194b5a892a1SMatthew G. Knepley 
9195b5a892a1SMatthew G. Knepley   Level: advanced
9196b5a892a1SMatthew G. Knepley 
9197bb7acecfSBarry Smith   Note:
9198bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9199bb7acecfSBarry Smith 
9200bb7acecfSBarry Smith   Developer Note:
9201bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9202bb7acecfSBarry Smith 
9203bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9204b5a892a1SMatthew G. Knepley @*/
9205*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9206*d71ae5a4SJacob Faibussowitsch {
9207b5a892a1SMatthew G. Knepley   PetscBool found;
9208b5a892a1SMatthew G. Knepley 
9209b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92109566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
92117a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9212b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9213b5a892a1SMatthew G. Knepley }
9214012bc364SMatthew G. Knepley 
9215012bc364SMatthew G. Knepley /*@C
9216012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9217012bc364SMatthew G. Knepley 
9218012bc364SMatthew G. Knepley   Not collective
9219012bc364SMatthew G. Knepley 
9220012bc364SMatthew G. Knepley   Input Parameters:
9221bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9222012bc364SMatthew G. Knepley - point - Coordinates of the point
9223012bc364SMatthew G. Knepley 
9224012bc364SMatthew G. Knepley   Output Parameters:
9225012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
9226012bc364SMatthew G. Knepley 
9227012bc364SMatthew G. Knepley   Level: advanced
9228012bc364SMatthew G. Knepley 
9229bb7acecfSBarry Smith .seealso: `DM`, `DMPolytopeType`, `DMLocatePoints()`
9230012bc364SMatthew G. Knepley @*/
9231*d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9232*d71ae5a4SJacob Faibussowitsch {
9233012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9234012bc364SMatthew G. Knepley   PetscInt  d;
9235012bc364SMatthew G. Knepley 
9236012bc364SMatthew G. Knepley   PetscFunctionBegin;
9237012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9238012bc364SMatthew G. Knepley   switch (ct) {
9239012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9240012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9241012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
92429371c9d4SSatish Balay       if (point[d] < -1.0) {
92439371c9d4SSatish Balay         *inside = PETSC_FALSE;
92449371c9d4SSatish Balay         break;
92459371c9d4SSatish Balay       }
9246012bc364SMatthew G. Knepley       sum += point[d];
9247012bc364SMatthew G. Knepley     }
92489371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
92499371c9d4SSatish Balay       *inside = PETSC_FALSE;
92509371c9d4SSatish Balay       break;
92519371c9d4SSatish Balay     }
9252012bc364SMatthew G. Knepley     break;
9253012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9254012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9255012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
92569371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
92579371c9d4SSatish Balay         *inside = PETSC_FALSE;
9258012bc364SMatthew G. Knepley         break;
92599371c9d4SSatish Balay       }
92609371c9d4SSatish Balay     break;
9261*d71ae5a4SJacob Faibussowitsch   default:
9262*d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9263012bc364SMatthew G. Knepley   }
9264012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
9265012bc364SMatthew G. Knepley }
9266