xref: /petsc/src/dm/interface/dm.c (revision 46560f82ebbebbe6388a7cb77cc9fc314bc23602)
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 @*/
50d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreate(MPI_Comm comm, DM *dm)
51d71ae5a4SJacob 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 
6262e5d2d2SJDBetteridge   ((PetscObject)v)->non_cyclic_references = &DMCountNonCyclicReferences;
6362e5d2d2SJDBetteridge 
6449be4549SMatthew G. Knepley   v->setupcalled          = PETSC_FALSE;
6549be4549SMatthew G. Knepley   v->setfromoptionscalled = PETSC_FALSE;
660298fd71SBarry Smith   v->ltogmap              = NULL;
67a4ea9b21SRichard Tran Mills   v->bind_below           = 0;
681411c6eeSJed Brown   v->bs                   = 1;
69171400e9SBarry Smith   v->coloringtype         = IS_COLORING_GLOBAL;
709566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
719566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
72c58f1c22SToby Isaac   v->labels                    = NULL;
7334aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7434aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
75c58f1c22SToby Isaac   v->depthLabel                = NULL;
76ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
771bb6d2a8SBarry Smith   v->localSection              = NULL;
781bb6d2a8SBarry Smith   v->globalSection             = NULL;
793b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
803b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
8179769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
826858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
836858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
846858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8596173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
86435a35e8SMatthew G Knepley   {
87435a35e8SMatthew G Knepley     PetscInt i;
88435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
890298fd71SBarry Smith       v->nullspaceConstructors[i]     = NULL;
90f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
91435a35e8SMatthew G Knepley     }
92435a35e8SMatthew G Knepley   }
939566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
949566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(v, NULL, NULL, ds));
959566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
969566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
9714f150ffSMatthew G. Knepley   v->dmBC              = NULL;
98a8fb8f29SToby Isaac   v->coarseMesh        = NULL;
99f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
100cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
1019566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v, VECSTANDARD));
1029566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v, MATAIJ));
1034a7a4c06SLawrence Mitchell 
1041411c6eeSJed Brown   *dm = v;
105a4121054SBarry Smith   PetscFunctionReturn(0);
106a4121054SBarry Smith }
107a4121054SBarry Smith 
10838221697SMatthew G. Knepley /*@
109bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
11038221697SMatthew G. Knepley 
111d083f849SBarry Smith   Collective
11238221697SMatthew G. Knepley 
11338221697SMatthew G. Knepley   Input Parameter:
114bb7acecfSBarry Smith . dm - The original `DM` object
11538221697SMatthew G. Knepley 
11638221697SMatthew G. Knepley   Output Parameter:
117bb7acecfSBarry Smith . newdm  - The new `DM` object
11838221697SMatthew G. Knepley 
11938221697SMatthew G. Knepley   Level: beginner
12038221697SMatthew G. Knepley 
1211cb8cacdSPatrick Sanan   Notes:
122bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
123bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
124bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1251bb6d2a8SBarry Smith 
126bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
12789706ed2SPatrick Sanan 
128bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
129bb7acecfSBarry Smith 
130bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMSetType()`, `DMConvert()`
13138221697SMatthew G. Knepley @*/
132d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm)
133d71ae5a4SJacob Faibussowitsch {
13438221697SMatthew G. Knepley   PetscSF  sf;
13538221697SMatthew G. Knepley   Vec      coords;
13638221697SMatthew G. Knepley   void    *ctx;
1376858538eSMatthew G. Knepley   PetscInt dim, cdim, i;
13838221697SMatthew G. Knepley 
13938221697SMatthew G. Knepley   PetscFunctionBegin;
14038221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14138221697SMatthew G. Knepley   PetscValidPointer(newdm, 2);
1429566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1439566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
144ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
145ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
146c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
1479566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1489566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1499566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1509566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1519566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1529566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
153dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1543f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1559566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1569566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1579566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1589566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
1596858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1606858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
161be4c1c3eSMatthew G. Knepley       DM           ncdm;
162be4c1c3eSMatthew G. Knepley       PetscSection cs;
1635a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
164be4c1c3eSMatthew G. Knepley 
1656858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1669566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
1679566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1685a0206caSToby Isaac       if (pEndMax >= 0) {
1696858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1706858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1719566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1726858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1736858538eSMatthew G. Knepley         else PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1749566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
175be4c1c3eSMatthew G. Knepley       }
176be4c1c3eSMatthew G. Knepley     }
1776858538eSMatthew G. Knepley   }
1789566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1799566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1809566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
18138221697SMatthew G. Knepley   if (coords) {
1829566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
18338221697SMatthew G. Knepley   } else {
1849566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1859566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
18638221697SMatthew G. Knepley   }
1876858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
1886858538eSMatthew G. Knepley   if (coords) {
1896858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
1906858538eSMatthew G. Knepley   } else {
1916858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
1926858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
1936858538eSMatthew G. Knepley   }
19490b157c4SStefano Zampini   {
1954fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
1966858538eSMatthew G. Knepley 
1974fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
1984fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L));
199c6b900c6SMatthew G. Knepley   }
20034aa8a36SMatthew G. Knepley   {
20134aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
20234aa8a36SMatthew G. Knepley 
2039566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2049566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
20534aa8a36SMatthew G. Knepley   }
20638221697SMatthew G. Knepley   PetscFunctionReturn(0);
20738221697SMatthew G. Knepley }
20838221697SMatthew G. Knepley 
2099a42bb27SBarry Smith /*@C
210bb7acecfSBarry Smith        DMSetVecType - Sets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2119a42bb27SBarry Smith 
212d083f849SBarry Smith    Logically Collective on da
2139a42bb27SBarry Smith 
214147403d9SBarry Smith    Input Parameters:
2159a42bb27SBarry Smith +  da - initial distributed array
216bb7acecfSBarry Smith -  ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2179a42bb27SBarry Smith 
2189a42bb27SBarry Smith    Options Database:
219147403d9SBarry Smith .   -dm_vec_type ctype - the type of vector to create
2209a42bb27SBarry Smith 
2219a42bb27SBarry Smith    Level: intermediate
2229a42bb27SBarry Smith 
223bb7acecfSBarry Smith .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
224bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2259a42bb27SBarry Smith @*/
226d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVecType(DM da, VecType ctype)
227d71ae5a4SJacob Faibussowitsch {
2289a42bb27SBarry Smith   PetscFunctionBegin;
2299a42bb27SBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
2309566063dSJacob Faibussowitsch   PetscCall(PetscFree(da->vectype));
2319566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&da->vectype));
2329a42bb27SBarry Smith   PetscFunctionReturn(0);
2339a42bb27SBarry Smith }
2349a42bb27SBarry Smith 
235c0dedaeaSBarry Smith /*@C
236bb7acecfSBarry Smith        DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
237c0dedaeaSBarry Smith 
238d083f849SBarry Smith    Logically Collective on da
239c0dedaeaSBarry Smith 
240c0dedaeaSBarry Smith    Input Parameter:
241c0dedaeaSBarry Smith .  da - initial distributed array
242c0dedaeaSBarry Smith 
243c0dedaeaSBarry Smith    Output Parameter:
244c0dedaeaSBarry Smith .  ctype - the vector type
245c0dedaeaSBarry Smith 
246c0dedaeaSBarry Smith    Level: intermediate
247c0dedaeaSBarry Smith 
248db781477SPatrick Sanan .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
249c0dedaeaSBarry Smith @*/
250d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
251d71ae5a4SJacob Faibussowitsch {
252c0dedaeaSBarry Smith   PetscFunctionBegin;
253c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
254c0dedaeaSBarry Smith   *ctype = da->vectype;
255c0dedaeaSBarry Smith   PetscFunctionReturn(0);
256c0dedaeaSBarry Smith }
257c0dedaeaSBarry Smith 
2585f1ad066SMatthew G Knepley /*@
259bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2605f1ad066SMatthew G Knepley 
2615f1ad066SMatthew G Knepley   Not collective
2625f1ad066SMatthew G Knepley 
2635f1ad066SMatthew G Knepley   Input Parameter:
264bb7acecfSBarry Smith . v - The `Vec`
2655f1ad066SMatthew G Knepley 
2665f1ad066SMatthew G Knepley   Output Parameter:
267bb7acecfSBarry Smith . dm - The `DM`
2685f1ad066SMatthew G Knepley 
2695f1ad066SMatthew G Knepley   Level: intermediate
2705f1ad066SMatthew G Knepley 
271bb7acecfSBarry Smith   Note:
272bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
273bb7acecfSBarry Smith 
274bb7acecfSBarry Smith .seealso: `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2755f1ad066SMatthew G Knepley @*/
276d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
277d71ae5a4SJacob Faibussowitsch {
2785f1ad066SMatthew G Knepley   PetscFunctionBegin;
2795f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2805f1ad066SMatthew G Knepley   PetscValidPointer(dm, 2);
2819566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
2825f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2835f1ad066SMatthew G Knepley }
2845f1ad066SMatthew G Knepley 
2855f1ad066SMatthew G Knepley /*@
286bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
2875f1ad066SMatthew G Knepley 
2885f1ad066SMatthew G Knepley   Not collective
2895f1ad066SMatthew G Knepley 
2905f1ad066SMatthew G Knepley   Input Parameters:
291bb7acecfSBarry Smith + v - The `Vec`
292bb7acecfSBarry Smith - dm - The `DM`
2935f1ad066SMatthew G Knepley 
294bb7acecfSBarry Smith   Note:
295bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
296d9805387SMatthew G. Knepley 
297bb7acecfSBarry 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.
298bb7acecfSBarry Smith 
299bb7acecfSBarry Smith   Level: developer
3005f1ad066SMatthew G Knepley 
301db781477SPatrick Sanan .seealso: `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3025f1ad066SMatthew G Knepley @*/
303d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
304d71ae5a4SJacob Faibussowitsch {
3055f1ad066SMatthew G Knepley   PetscFunctionBegin;
3065f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
307d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3089566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3095f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
3105f1ad066SMatthew G Knepley }
3115f1ad066SMatthew G Knepley 
312521d9a4cSLisandro Dalcin /*@C
313bb7acecfSBarry Smith        DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3148f1509bcSBarry Smith 
315d083f849SBarry Smith    Logically Collective on dm
3168f1509bcSBarry Smith 
3178f1509bcSBarry Smith    Input Parameters:
318bb7acecfSBarry Smith +  dm - the `DM` context
3198f1509bcSBarry Smith -  ctype - the matrix type
3208f1509bcSBarry Smith 
3218f1509bcSBarry Smith    Options Database:
3228f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3238f1509bcSBarry Smith 
3248f1509bcSBarry Smith    Level: intermediate
3258f1509bcSBarry Smith 
326db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
327bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3288f1509bcSBarry Smith @*/
329d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
330d71ae5a4SJacob Faibussowitsch {
3318f1509bcSBarry Smith   PetscFunctionBegin;
3328f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3338f1509bcSBarry Smith   dm->coloringtype = ctype;
3348f1509bcSBarry Smith   PetscFunctionReturn(0);
3358f1509bcSBarry Smith }
3368f1509bcSBarry Smith 
3378f1509bcSBarry Smith /*@C
338bb7acecfSBarry Smith        DMGetISColoringType - Gets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
339521d9a4cSLisandro Dalcin 
340d083f849SBarry Smith    Logically Collective on dm
341521d9a4cSLisandro Dalcin 
342521d9a4cSLisandro Dalcin    Input Parameter:
343bb7acecfSBarry Smith .  dm - the `DM` context
3448f1509bcSBarry Smith 
3458f1509bcSBarry Smith    Output Parameter:
3468f1509bcSBarry Smith .  ctype - the matrix type
3478f1509bcSBarry Smith 
3488f1509bcSBarry Smith    Options Database:
3498f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3508f1509bcSBarry Smith 
3518f1509bcSBarry Smith    Level: intermediate
3528f1509bcSBarry Smith 
353db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
354bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3558f1509bcSBarry Smith @*/
356d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
357d71ae5a4SJacob Faibussowitsch {
3588f1509bcSBarry Smith   PetscFunctionBegin;
3598f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3608f1509bcSBarry Smith   *ctype = dm->coloringtype;
3618f1509bcSBarry Smith   PetscFunctionReturn(0);
3628f1509bcSBarry Smith }
3638f1509bcSBarry Smith 
3648f1509bcSBarry Smith /*@C
365bb7acecfSBarry Smith        DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3668f1509bcSBarry Smith 
367d083f849SBarry Smith    Logically Collective on dm
3688f1509bcSBarry Smith 
3698f1509bcSBarry Smith    Input Parameters:
370bb7acecfSBarry Smith +  dm - the `DM` context
371bb7acecfSBarry Smith -  ctype - the matrix type, for example `MATMPIAIJ`
372521d9a4cSLisandro Dalcin 
373521d9a4cSLisandro Dalcin    Options Database:
374bb7acecfSBarry Smith .   -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
375521d9a4cSLisandro Dalcin 
376521d9a4cSLisandro Dalcin    Level: intermediate
377521d9a4cSLisandro Dalcin 
378bb7acecfSBarry Smith .seealso: `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`, `DMSetMatType()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
379521d9a4cSLisandro Dalcin @*/
380d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
381d71ae5a4SJacob Faibussowitsch {
382521d9a4cSLisandro Dalcin   PetscFunctionBegin;
383521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3849566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->mattype));
3859566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
386521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
387521d9a4cSLisandro Dalcin }
388521d9a4cSLisandro Dalcin 
389c0dedaeaSBarry Smith /*@C
390bb7acecfSBarry Smith        DMGetMatType - Gets the type of matrix created with `DMCreateMatrix()`
391c0dedaeaSBarry Smith 
392d083f849SBarry Smith    Logically Collective on dm
393c0dedaeaSBarry Smith 
394c0dedaeaSBarry Smith    Input Parameter:
395bb7acecfSBarry Smith .  dm - the `DM` context
396c0dedaeaSBarry Smith 
397c0dedaeaSBarry Smith    Output Parameter:
398c0dedaeaSBarry Smith .  ctype - the matrix type
399c0dedaeaSBarry Smith 
400c0dedaeaSBarry Smith    Level: intermediate
401c0dedaeaSBarry Smith 
402db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`, `DMSetMatType()`, `DMGetMatType()`
403c0dedaeaSBarry Smith @*/
404d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
405d71ae5a4SJacob Faibussowitsch {
406c0dedaeaSBarry Smith   PetscFunctionBegin;
407c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
408c0dedaeaSBarry Smith   *ctype = dm->mattype;
409c0dedaeaSBarry Smith   PetscFunctionReturn(0);
410c0dedaeaSBarry Smith }
411c0dedaeaSBarry Smith 
412c688c046SMatthew G Knepley /*@
413bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
414c688c046SMatthew G Knepley 
415c688c046SMatthew G Knepley   Not collective
416c688c046SMatthew G Knepley 
417c688c046SMatthew G Knepley   Input Parameter:
418bb7acecfSBarry Smith . A - The `Mat`
419c688c046SMatthew G Knepley 
420c688c046SMatthew G Knepley   Output Parameter:
421bb7acecfSBarry Smith . dm - The `DM`
422c688c046SMatthew G Knepley 
423c688c046SMatthew G Knepley   Level: intermediate
424c688c046SMatthew G Knepley 
425bb7acecfSBarry Smith   Note:
426bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
427bb7acecfSBarry Smith 
428bb7acecfSBarry Smith   Developer Note:
429bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4308f1509bcSBarry Smith 
431db781477SPatrick Sanan .seealso: `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
432c688c046SMatthew G Knepley @*/
433d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
434d71ae5a4SJacob Faibussowitsch {
435c688c046SMatthew G Knepley   PetscFunctionBegin;
436c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
437c688c046SMatthew G Knepley   PetscValidPointer(dm, 2);
4389566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
439c688c046SMatthew G Knepley   PetscFunctionReturn(0);
440c688c046SMatthew G Knepley }
441c688c046SMatthew G Knepley 
442c688c046SMatthew G Knepley /*@
443bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
444c688c046SMatthew G Knepley 
445c688c046SMatthew G Knepley   Not collective
446c688c046SMatthew G Knepley 
447c688c046SMatthew G Knepley   Input Parameters:
448c688c046SMatthew G Knepley + A - The Mat
449c688c046SMatthew G Knepley - dm - The DM
450c688c046SMatthew G Knepley 
451bb7acecfSBarry Smith   Level: developer
452c688c046SMatthew G Knepley 
453bb7acecfSBarry Smith   Note:
454bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
455bb7acecfSBarry Smith 
456bb7acecfSBarry Smith   Developer Note:
457bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
458bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4598f1509bcSBarry Smith 
460db781477SPatrick Sanan .seealso: `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
461c688c046SMatthew G Knepley @*/
462d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
463d71ae5a4SJacob Faibussowitsch {
464c688c046SMatthew G Knepley   PetscFunctionBegin;
465c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4668865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4679566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
468c688c046SMatthew G Knepley   PetscFunctionReturn(0);
469c688c046SMatthew G Knepley }
470c688c046SMatthew G Knepley 
4719a42bb27SBarry Smith /*@C
472bb7acecfSBarry Smith    DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4739a42bb27SBarry Smith 
474d083f849SBarry Smith    Logically Collective on dm
4759a42bb27SBarry Smith 
476d8d19677SJose E. Roman    Input Parameters:
477bb7acecfSBarry Smith +  da - the `DM` context
478bb7acecfSBarry Smith -  prefix - the prefix to prepend
4799a42bb27SBarry Smith 
4809a42bb27SBarry Smith    Notes:
4819a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4829a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4839a42bb27SBarry Smith 
4849a42bb27SBarry Smith    Level: advanced
4859a42bb27SBarry Smith 
486bb7acecfSBarry Smith .seealso: `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
4879a42bb27SBarry Smith @*/
488d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
489d71ae5a4SJacob Faibussowitsch {
4909a42bb27SBarry Smith   PetscFunctionBegin;
4919a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4929566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
4931baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
4941baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
4959a42bb27SBarry Smith   PetscFunctionReturn(0);
4969a42bb27SBarry Smith }
4979a42bb27SBarry Smith 
49831697293SDave May /*@C
499bb7acecfSBarry Smith    DMAppendOptionsPrefix - Appends an additional string to an already exising prefix used for searching for
500bb7acecfSBarry Smith    `DM` options in the options database.
50131697293SDave May 
502d083f849SBarry Smith    Logically Collective on dm
50331697293SDave May 
50431697293SDave May    Input Parameters:
505bb7acecfSBarry Smith +  dm - the `DM` context
506bb7acecfSBarry Smith -  prefix - the string to append to the current prefix
50731697293SDave May 
50831697293SDave May    Notes:
509bb7acecfSBarry 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.
51031697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
51131697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
51231697293SDave May 
51331697293SDave May    Level: advanced
51431697293SDave May 
515bb7acecfSBarry Smith .seealso: `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
51631697293SDave May @*/
517d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
518d71ae5a4SJacob Faibussowitsch {
51931697293SDave May   PetscFunctionBegin;
52031697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5219566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
52231697293SDave May   PetscFunctionReturn(0);
52331697293SDave May }
52431697293SDave May 
52531697293SDave May /*@C
52631697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
527bb7acecfSBarry Smith    DM options in the options database.
52831697293SDave May 
52931697293SDave May    Not Collective
53031697293SDave May 
53131697293SDave May    Input Parameters:
532bb7acecfSBarry Smith .  dm - the `DM` context
53331697293SDave May 
53431697293SDave May    Output Parameters:
53531697293SDave May .  prefix - pointer to the prefix string used is returned
53631697293SDave May 
537bb7acecfSBarry Smith    Fortran Note:
53895452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
53931697293SDave May    sufficient length to hold the prefix.
54031697293SDave May 
54131697293SDave May    Level: advanced
54231697293SDave May 
543bb7acecfSBarry Smith .seealso: `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
54431697293SDave May @*/
545d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
546d71ae5a4SJacob Faibussowitsch {
54731697293SDave May   PetscFunctionBegin;
54831697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5499566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
55031697293SDave May   PetscFunctionReturn(0);
55131697293SDave May }
55231697293SDave May 
55362e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
554d71ae5a4SJacob Faibussowitsch {
5556eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
55688bdff64SToby Isaac 
55788bdff64SToby Isaac   PetscFunctionBegin;
558aab5bcd8SJed Brown   *ncrefct = 0;
55988bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
56088bdff64SToby Isaac     refct--;
56188bdff64SToby Isaac     if (recurseCoarse) {
56288bdff64SToby Isaac       PetscInt coarseCount;
56388bdff64SToby Isaac 
56462e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
56588bdff64SToby Isaac       refct += coarseCount;
56688bdff64SToby Isaac     }
56788bdff64SToby Isaac   }
56888bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56988bdff64SToby Isaac     refct--;
57088bdff64SToby Isaac     if (recurseFine) {
57188bdff64SToby Isaac       PetscInt fineCount;
57288bdff64SToby Isaac 
57362e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
57488bdff64SToby Isaac       refct += fineCount;
57588bdff64SToby Isaac     }
57688bdff64SToby Isaac   }
57788bdff64SToby Isaac   *ncrefct = refct;
57888bdff64SToby Isaac   PetscFunctionReturn(0);
57988bdff64SToby Isaac }
58088bdff64SToby Isaac 
58162e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */
58262e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct)
58362e5d2d2SJDBetteridge {
58462e5d2d2SJDBetteridge   PetscFunctionBegin;
58562e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct));
58662e5d2d2SJDBetteridge   PetscFunctionReturn(0);
58762e5d2d2SJDBetteridge }
58862e5d2d2SJDBetteridge 
589d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
590d71ae5a4SJacob Faibussowitsch {
5915d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
592354557abSToby Isaac 
593354557abSToby Isaac   PetscFunctionBegin;
594354557abSToby Isaac   /* destroy the labels */
595354557abSToby Isaac   while (next) {
596354557abSToby Isaac     DMLabelLink tmp = next->next;
597354557abSToby Isaac 
5985d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
599ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
6009566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
6019566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
602354557abSToby Isaac     next = tmp;
603354557abSToby Isaac   }
6045d80c0bfSVaclav Hapla   dm->labels = NULL;
605354557abSToby Isaac   PetscFunctionReturn(0);
606354557abSToby Isaac }
607354557abSToby Isaac 
608d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
609d71ae5a4SJacob Faibussowitsch {
6106858538eSMatthew G. Knepley   PetscFunctionBegin;
6116858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6126858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6136858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6146858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6156858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6166858538eSMatthew G. Knepley   PetscFunctionReturn(0);
6176858538eSMatthew G. Knepley }
6186858538eSMatthew G. Knepley 
6191fb7b255SJunchao Zhang /*@C
620bb7acecfSBarry Smith     DMDestroy - Destroys a `DM`.
62147c6ae99SBarry Smith 
622d083f849SBarry Smith     Collective on dm
62347c6ae99SBarry Smith 
62447c6ae99SBarry Smith     Input Parameter:
625bb7acecfSBarry Smith .   dm - the `DM` object to destroy
62647c6ae99SBarry Smith 
62747c6ae99SBarry Smith     Level: developer
62847c6ae99SBarry Smith 
629bb7acecfSBarry Smith .seealso: `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
63047c6ae99SBarry Smith @*/
631d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
632d71ae5a4SJacob Faibussowitsch {
6336eb26441SStefano Zampini   PetscInt       cnt;
634dfe15315SJed Brown   DMNamedVecLink nlink, nnext;
63547c6ae99SBarry Smith 
63647c6ae99SBarry Smith   PetscFunctionBegin;
6376bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6386bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm), DM_CLASSID, 1);
63987e657c6SBarry Smith 
64088bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
64162e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
64288bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
6439371c9d4SSatish Balay   if (--cnt > 0) {
6449371c9d4SSatish Balay     *dm = NULL;
6459371c9d4SSatish Balay     PetscFunctionReturn(0);
6469371c9d4SSatish Balay   }
6476bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6486bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6496eb26441SStefano Zampini 
6509566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6519566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
6526eb26441SStefano Zampini 
653f490541aSPeter Brune   nnext              = (*dm)->namedglobal;
6540298fd71SBarry Smith   (*dm)->namedglobal = NULL;
655f490541aSPeter Brune   for (nlink = nnext; nlink; nlink = nnext) { /* Destroy the named vectors */
6562348bcf4SPeter Brune     nnext = nlink->next;
6577a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN, ((PetscObject)*dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM still has Vec named '%s' checked out", nlink->name);
6589566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6599566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6609566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
6612348bcf4SPeter Brune   }
662f490541aSPeter Brune   nnext             = (*dm)->namedlocal;
6630298fd71SBarry Smith   (*dm)->namedlocal = NULL;
664f490541aSPeter Brune   for (nlink = nnext; nlink; nlink = nnext) { /* Destroy the named local vectors */
665f490541aSPeter Brune     nnext = nlink->next;
6667a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN, ((PetscObject)*dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM still has Vec named '%s' checked out", nlink->name);
6679566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6689566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6699566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
670f490541aSPeter Brune   }
6712348bcf4SPeter Brune 
672b17ce1afSJed Brown   /* Destroy the list of hooks */
673c833c3b5SJed Brown   {
674c833c3b5SJed Brown     DMCoarsenHookLink link, next;
675b17ce1afSJed Brown     for (link = (*dm)->coarsenhook; link; link = next) {
676b17ce1afSJed Brown       next = link->next;
6779566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
678b17ce1afSJed Brown     }
6790298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
680c833c3b5SJed Brown   }
681c833c3b5SJed Brown   {
682c833c3b5SJed Brown     DMRefineHookLink link, next;
683c833c3b5SJed Brown     for (link = (*dm)->refinehook; link; link = next) {
684c833c3b5SJed Brown       next = link->next;
6859566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
686c833c3b5SJed Brown     }
6870298fd71SBarry Smith     (*dm)->refinehook = NULL;
688c833c3b5SJed Brown   }
689be081cd6SPeter Brune   {
690be081cd6SPeter Brune     DMSubDomainHookLink link, next;
691be081cd6SPeter Brune     for (link = (*dm)->subdomainhook; link; link = next) {
692be081cd6SPeter Brune       next = link->next;
6939566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
694be081cd6SPeter Brune     }
6950298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
696be081cd6SPeter Brune   }
697baf369e7SPeter Brune   {
698baf369e7SPeter Brune     DMGlobalToLocalHookLink link, next;
699baf369e7SPeter Brune     for (link = (*dm)->gtolhook; link; link = next) {
700baf369e7SPeter Brune       next = link->next;
7019566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
702baf369e7SPeter Brune     }
7030298fd71SBarry Smith     (*dm)->gtolhook = NULL;
704baf369e7SPeter Brune   }
705d4d07f1eSToby Isaac   {
706d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link, next;
707d4d07f1eSToby Isaac     for (link = (*dm)->ltoghook; link; link = next) {
708d4d07f1eSToby Isaac       next = link->next;
7099566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
710d4d07f1eSToby Isaac     }
711d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
712d4d07f1eSToby Isaac   }
713aa1993deSMatthew G Knepley   /* Destroy the work arrays */
714aa1993deSMatthew G Knepley   {
715aa1993deSMatthew G Knepley     DMWorkLink link, next;
7167a8be351SBarry Smith     PetscCheck(!(*dm)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out");
717aa1993deSMatthew G Knepley     for (link = (*dm)->workin; link; link = next) {
718aa1993deSMatthew G Knepley       next = link->next;
7199566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
7209566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
721aa1993deSMatthew G Knepley     }
7220298fd71SBarry Smith     (*dm)->workin = NULL;
723aa1993deSMatthew G Knepley   }
724c58f1c22SToby Isaac   /* destroy the labels */
7259566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
726f4cdcedcSVaclav Hapla   /* destroy the fields */
7279566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
728f4cdcedcSVaclav Hapla   /* destroy the boundaries */
729e6f8dbb6SToby Isaac   {
730e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
731e6f8dbb6SToby Isaac     while (next) {
732e6f8dbb6SToby Isaac       DMBoundary b = next;
733e6f8dbb6SToby Isaac 
734e6f8dbb6SToby Isaac       next = b->next;
7359566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
736e6f8dbb6SToby Isaac     }
737e6f8dbb6SToby Isaac   }
738b17ce1afSJed Brown 
7399566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7409566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7419566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
74252536dc3SBarry Smith 
74348a46eb9SPierre Jolivet   if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7449566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7459566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7469566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7479566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
74888ed4aceSMatthew G Knepley 
7499566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7509566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
7519566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7529566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7539566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7549566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7559566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
756736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
75748a46eb9SPierre Jolivet     if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7589566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
759736995cdSBlaise Bourdin   }
7609a2a23afSMatthew G. Knepley   {
7619a2a23afSMatthew G. Knepley     Vec     *auxData;
7629a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7639a2a23afSMatthew G. Knepley 
7649566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetSize((*dm)->auxData, &n));
7659566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &auxData));
7669566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetVals((*dm)->auxData, &off, auxData));
7679566063dSJacob Faibussowitsch     for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
7689566063dSJacob Faibussowitsch     PetscCall(PetscFree(auxData));
7699566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
7709a2a23afSMatthew G. Knepley   }
77148a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7726eb26441SStefano Zampini 
7739566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
77448a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7759566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7764fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7779566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7789566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7796858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7806858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7819566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7829566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7839566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7846636e97aSMatthew G Knepley 
7859566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7869566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
787e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7889566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
789732e2eb9SMatthew G Knepley 
79048a46eb9SPierre Jolivet   if ((*dm)->ops->destroy) PetscCall((*(*dm)->ops->destroy)(*dm));
7919566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
792f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7939566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7949566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
795f918ec44SMatthew G. Knepley #endif
796435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7979566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
79847c6ae99SBarry Smith   PetscFunctionReturn(0);
79947c6ae99SBarry Smith }
80047c6ae99SBarry Smith 
801d7bf68aeSBarry Smith /*@
802bb7acecfSBarry Smith     DMSetUp - sets up the data structures inside a `DM` object
803d7bf68aeSBarry Smith 
804d083f849SBarry Smith     Collective on dm
805d7bf68aeSBarry Smith 
806d7bf68aeSBarry Smith     Input Parameter:
807bb7acecfSBarry Smith .   dm - the `DM` object to setup
808d7bf68aeSBarry Smith 
809bb7acecfSBarry Smith     Level: intermediate
810d7bf68aeSBarry Smith 
811bb7acecfSBarry Smith     Note:
812bb7acecfSBarry Smith     This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
813bb7acecfSBarry Smith 
814bb7acecfSBarry Smith .seealso: `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
815d7bf68aeSBarry Smith @*/
816d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
817d71ae5a4SJacob Faibussowitsch {
818d7bf68aeSBarry Smith   PetscFunctionBegin;
819171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8208387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
821dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8228387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
823d7bf68aeSBarry Smith   PetscFunctionReturn(0);
824d7bf68aeSBarry Smith }
825d7bf68aeSBarry Smith 
826d7bf68aeSBarry Smith /*@
827bb7acecfSBarry Smith     DMSetFromOptions - sets parameters in a `DM` from the options database
828d7bf68aeSBarry Smith 
829d083f849SBarry Smith     Collective on dm
830d7bf68aeSBarry Smith 
831d7bf68aeSBarry Smith     Input Parameter:
832bb7acecfSBarry Smith .   dm - the `DM` object to set options for
833d7bf68aeSBarry Smith 
834732e2eb9SMatthew G Knepley     Options Database:
835bb7acecfSBarry Smith +   -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
836bb7acecfSBarry Smith .   -dm_vec_type <type>  - type of vector to create inside `DM`
837bb7acecfSBarry Smith .   -dm_mat_type <type>  - type of matrix to create inside `DM`
838a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
839bb7acecfSBarry 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`
840732e2eb9SMatthew G Knepley 
8419318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8429318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8439318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
844cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
845bb7acecfSBarry Smith . -dm_plex_shape <shape>            - The domain shape, such as `DM_SHAPE_BOX`, `DM_SHAPE_SPHERE`, etc.
8469318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8479318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8489318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
849bb7acecfSBarry Smith . -dm_plex_simplex <bool>           - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
850bb7acecfSBarry Smith . -dm_plex_interpolate <bool>       - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8519318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8529318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8539318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8549318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
855bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>        - Specify the `DMBoundaryType `for each direction
8569318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8579318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8589318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8599318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
860bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8619318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8629318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8639318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8649318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
865bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
866d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
867d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
868d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
869d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
870d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
871909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
872909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8739318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8749318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8759318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8769318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8779318fe57SMatthew G. Knepley 
878384a6580SVaclav Hapla     DMPLEX Specific Checks
879bb7acecfSBarry Smith +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
880bb7acecfSBarry Smith .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
881bb7acecfSBarry Smith .   -dm_plex_check_faces           - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type - `DMPlexCheckFaces()`
882bb7acecfSBarry Smith .   -dm_plex_check_geometry        - Check that cells have positive volume - `DMPlexCheckGeometry()`
883bb7acecfSBarry Smith .   -dm_plex_check_pointsf         - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
884bb7acecfSBarry Smith .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
885384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
886d7bf68aeSBarry Smith 
88795eb5ee5SVaclav Hapla     Level: intermediate
88895eb5ee5SVaclav Hapla 
889bb7acecfSBarry Smith .seealso: `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
890bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
891bb7acecfSBarry Smith          `DMSetOptionsPrefix()`, `DM`, `DMType`, `DMPLEX`, `DMDA`
892d7bf68aeSBarry Smith @*/
893d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
894d71ae5a4SJacob Faibussowitsch {
8957781c08eSBarry Smith   char      typeName[256];
896ca266f36SBarry Smith   PetscBool flg;
897d7bf68aeSBarry Smith 
898d7bf68aeSBarry Smith   PetscFunctionBegin;
899171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90049be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
9019566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
9029566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
903dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
904d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
9059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
9069566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9071baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9091baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
9109566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9119566063dSJacob 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));
912dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
913f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
914dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
915d0609cedSBarry Smith   PetscOptionsEnd();
916d7bf68aeSBarry Smith   PetscFunctionReturn(0);
917d7bf68aeSBarry Smith }
918d7bf68aeSBarry Smith 
919fc9bc008SSatish Balay /*@C
920bb7acecfSBarry Smith    DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
921fe2efc57SMark 
922bb7acecfSBarry Smith    Collective on dm
923fe2efc57SMark 
924fe2efc57SMark    Input Parameters:
925bb7acecfSBarry Smith +  dm - the `DM` object
926bb7acecfSBarry Smith .  obj - optional object that provides the prefix for the options database (if NULL then the prefix in obj is used)
927bb7acecfSBarry Smith -  optionname - option string that is used to activate viewing
928fe2efc57SMark 
929fe2efc57SMark    Level: intermediate
930bb7acecfSBarry Smith 
931bb7acecfSBarry Smith    Note:
932bb7acecfSBarry Smith    See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
933bb7acecfSBarry Smith 
934bb7acecfSBarry Smith .seealso: `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`, `PetscObjectViewFromOptions()`
935fe2efc57SMark @*/
936d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
937d71ae5a4SJacob Faibussowitsch {
938fe2efc57SMark   PetscFunctionBegin;
939fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9409566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
941fe2efc57SMark   PetscFunctionReturn(0);
942fe2efc57SMark }
943fe2efc57SMark 
944fe2efc57SMark /*@C
945bb7acecfSBarry 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
946bb7acecfSBarry Smith     save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
94747c6ae99SBarry Smith 
948d083f849SBarry Smith     Collective on dm
94947c6ae99SBarry Smith 
950d8d19677SJose E. Roman     Input Parameters:
951bb7acecfSBarry Smith +   dm - the `DM` object to view
95247c6ae99SBarry Smith -   v - the viewer
95347c6ae99SBarry Smith 
954cd7e8a5eSksagiyam     Notes:
955bb7acecfSBarry Smith     Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
956bb7acecfSBarry Smith     meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
957bb7acecfSBarry Smith     before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
958cd7e8a5eSksagiyam 
959224748a4SBarry Smith     Level: beginner
96047c6ae99SBarry Smith 
961bb7acecfSBarry Smith .seealso: `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat`(), `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
96247c6ae99SBarry Smith 
96347c6ae99SBarry Smith @*/
964d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
965d71ae5a4SJacob Faibussowitsch {
96632c0f0efSBarry Smith   PetscBool         isbinary;
96776a8abe0SBarry Smith   PetscMPIInt       size;
96876a8abe0SBarry Smith   PetscViewerFormat format;
96947c6ae99SBarry Smith 
97047c6ae99SBarry Smith   PetscFunctionBegin;
971171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97248a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
973b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
97474903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
97574903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
97674903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
97774903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
97874903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
97974903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
98074903a4fSStefano Zampini      in an error here */
98174903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9829566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
983b1b135c8SBarry Smith 
9849566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9859566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
98676a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
9879566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9889566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
98932c0f0efSBarry Smith   if (isbinary) {
99055849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
99132c0f0efSBarry Smith     char     type[256];
99232c0f0efSBarry Smith 
9939566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
9949566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, 256));
9959566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
99632c0f0efSBarry Smith   }
997dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
99847c6ae99SBarry Smith   PetscFunctionReturn(0);
99947c6ae99SBarry Smith }
100047c6ae99SBarry Smith 
100147c6ae99SBarry Smith /*@
1002bb7acecfSBarry Smith     DMCreateGlobalVector - Creates a global vector from a `DM` object. A global vector is a parallel vector that has no duplicate values shared between MPI ranks,
1003bb7acecfSBarry Smith     that is it has no ghost locations.
100447c6ae99SBarry Smith 
1005d083f849SBarry Smith     Collective on dm
100647c6ae99SBarry Smith 
100747c6ae99SBarry Smith     Input Parameter:
1008bb7acecfSBarry Smith .   dm - the `DM` object
100947c6ae99SBarry Smith 
101047c6ae99SBarry Smith     Output Parameter:
101147c6ae99SBarry Smith .   vec - the global vector
101247c6ae99SBarry Smith 
1013073dac72SJed Brown     Level: beginner
101447c6ae99SBarry Smith 
1015bb7acecfSBarry Smith .seealso: `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1016bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
101747c6ae99SBarry Smith @*/
1018d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1019d71ae5a4SJacob Faibussowitsch {
102047c6ae99SBarry Smith   PetscFunctionBegin;
1021171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1022b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1023dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
102476bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1025c6b011d8SStefano Zampini     DM vdm;
1026c6b011d8SStefano Zampini 
10279566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10287a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1029c6b011d8SStefano Zampini   }
103047c6ae99SBarry Smith   PetscFunctionReturn(0);
103147c6ae99SBarry Smith }
103247c6ae99SBarry Smith 
103347c6ae99SBarry Smith /*@
1034bb7acecfSBarry Smith     DMCreateLocalVector - Creates a local vector from a `DM` object.
103547c6ae99SBarry Smith 
103647c6ae99SBarry Smith     Not Collective
103747c6ae99SBarry Smith 
103847c6ae99SBarry Smith     Input Parameter:
1039bb7acecfSBarry Smith .   dm - the `DM` object
104047c6ae99SBarry Smith 
104147c6ae99SBarry Smith     Output Parameter:
104247c6ae99SBarry Smith .   vec - the local vector
104347c6ae99SBarry Smith 
1044073dac72SJed Brown     Level: beginner
104547c6ae99SBarry Smith 
1046bb7acecfSBarry Smith     Notes:
1047bb7acecfSBarry Smith     A local vector usually has ghost locations that contain values that are owned by different MPI ranks. A global vector has no ghost locations.
1048bb7acecfSBarry Smith 
1049bb7acecfSBarry Smith  .seealso: `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1050bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
105147c6ae99SBarry Smith @*/
1052d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1053d71ae5a4SJacob Faibussowitsch {
105447c6ae99SBarry Smith   PetscFunctionBegin;
1055171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1056b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1057dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
105876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1059c6b011d8SStefano Zampini     DM vdm;
1060c6b011d8SStefano Zampini 
10619566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10627a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1063c6b011d8SStefano Zampini   }
106447c6ae99SBarry Smith   PetscFunctionReturn(0);
106547c6ae99SBarry Smith }
106647c6ae99SBarry Smith 
10671411c6eeSJed Brown /*@
1068bb7acecfSBarry Smith    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10691411c6eeSJed Brown 
1070d083f849SBarry Smith    Collective on dm
10711411c6eeSJed Brown 
10721411c6eeSJed Brown    Input Parameter:
1073bb7acecfSBarry Smith .  dm - the `DM` that provides the mapping
10741411c6eeSJed Brown 
10751411c6eeSJed Brown    Output Parameter:
10761411c6eeSJed Brown .  ltog - the mapping
10771411c6eeSJed Brown 
1078bb7acecfSBarry Smith    Level: advanced
10791411c6eeSJed Brown 
10801411c6eeSJed Brown    Notes:
1081bb7acecfSBarry Smith    The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10821411c6eeSJed Brown 
1083bb7acecfSBarry Smith    Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1084bb7acecfSBarry Smith    need to use this function with those objects.
1085bb7acecfSBarry Smith 
1086bb7acecfSBarry Smith    This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1087bb7acecfSBarry Smith 
1088bb7acecfSBarry Smith .seealso: `DMCreateLocalVector()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1089bb7acecfSBarry Smith           `DMCreateMatrix()`
10901411c6eeSJed Brown @*/
1091d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1092d71ae5a4SJacob Faibussowitsch {
10930be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10941411c6eeSJed Brown 
10951411c6eeSJed Brown   PetscFunctionBegin;
10961411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10971411c6eeSJed Brown   PetscValidPointer(ltog, 2);
10981411c6eeSJed Brown   if (!dm->ltogmap) {
109937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
110037d0c07bSMatthew G Knepley 
11019566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
110237d0c07bSMatthew G Knepley     if (section) {
1103a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
110437d0c07bSMatthew G Knepley       PetscInt       *ltog;
1105ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
110637d0c07bSMatthew G Knepley 
11079566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11089566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11099566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11109566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
111137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1112e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
111337d0c07bSMatthew G Knepley 
111437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11159566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11169566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11179566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11189566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11191a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11201a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1121ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11225227eafbSStefano Zampini 
1123e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11245227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1125e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1126e6befd46SJed Brown             cind++;
1127e6befd46SJed Brown           } else {
11285227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1129e6befd46SJed Brown           }
113037d0c07bSMatthew G Knepley         }
113137d0c07bSMatthew G Knepley       }
1132bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11339371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11349371c9d4SSatish Balay       bsLocal[1] = bs;
11359566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11369371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11379371c9d4SSatish Balay         bs = 1;
11389371c9d4SSatish Balay       } else {
11399371c9d4SSatish Balay         bs = bsMinMax[0];
11409371c9d4SSatish Balay       }
11417591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11427591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1143ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1144ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1145ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1146ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1147ca469d19SJed Brown         }
1148ccf3bd66SMatthew G. Knepley         n /= bs;
1149ccf3bd66SMatthew G. Knepley       }
11509566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1151dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
115237d0c07bSMatthew G Knepley   }
11531411c6eeSJed Brown   *ltog = dm->ltogmap;
11541411c6eeSJed Brown   PetscFunctionReturn(0);
11551411c6eeSJed Brown }
11561411c6eeSJed Brown 
11571411c6eeSJed Brown /*@
1158bb7acecfSBarry Smith    DMGetBlockSize - Gets the inherent block size associated with a `DM`
11591411c6eeSJed Brown 
11601411c6eeSJed Brown    Not Collective
11611411c6eeSJed Brown 
11621411c6eeSJed Brown    Input Parameter:
1163bb7acecfSBarry Smith .  dm - the `DM` with block structure
11641411c6eeSJed Brown 
11651411c6eeSJed Brown    Output Parameter:
11661411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11671411c6eeSJed Brown 
11681411c6eeSJed Brown    Level: intermediate
11691411c6eeSJed Brown 
1170bb7acecfSBarry Smith    Note:
1171bb7acecfSBarry Smith    This might be the number of degrees of freedom at each grid point for a structured grid.
1172bb7acecfSBarry Smith 
1173bb7acecfSBarry Smith    Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1174bb7acecfSBarry Smith    rather different locations in the vectors may have a different block size.
1175bb7acecfSBarry Smith 
1176db781477SPatrick Sanan .seealso: `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11771411c6eeSJed Brown @*/
1178d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1179d71ae5a4SJacob Faibussowitsch {
11801411c6eeSJed Brown   PetscFunctionBegin;
11811411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1182534a8f05SLisandro Dalcin   PetscValidIntPointer(bs, 2);
11837a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11841411c6eeSJed Brown   *bs = dm->bs;
11851411c6eeSJed Brown   PetscFunctionReturn(0);
11861411c6eeSJed Brown }
11871411c6eeSJed Brown 
118848eeb7c8SBarry Smith /*@C
1189bb7acecfSBarry Smith     DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1190bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
119147c6ae99SBarry Smith 
1192a5bc1bf3SBarry Smith     Collective on dmc
119347c6ae99SBarry Smith 
1194d8d19677SJose E. Roman     Input Parameters:
1195bb7acecfSBarry Smith +   dmc - the `DM` object
1196bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
119747c6ae99SBarry Smith 
1198d8d19677SJose E. Roman     Output Parameters:
119947c6ae99SBarry Smith +  mat - the interpolation
1200bb7acecfSBarry Smith -  vec - the scaling (optional), see `DMCreateInterpolationScale()`
120147c6ae99SBarry Smith 
120247c6ae99SBarry Smith     Level: developer
120347c6ae99SBarry Smith 
120495452b02SPatrick Sanan     Notes:
1205bb7acecfSBarry Smith     For `DMDA` objects this only works for "uniform refinement", that is the refined mesh was obtained `DMRefine()` or the coarse mesh was obtained by
1206bb7acecfSBarry Smith     DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1207d52bd9f3SBarry Smith 
1208bb7acecfSBarry Smith     For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1209bb7acecfSBarry Smith     vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
121085afcc9aSBarry Smith 
1211bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
121247c6ae99SBarry Smith @*/
1213d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1214d71ae5a4SJacob Faibussowitsch {
121547c6ae99SBarry Smith   PetscFunctionBegin;
1216a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1217a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
1218c7d20fa0SStefano Zampini   PetscValidPointer(mat, 3);
12199566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1220dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12219566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
122247c6ae99SBarry Smith   PetscFunctionReturn(0);
122347c6ae99SBarry Smith }
122447c6ae99SBarry Smith 
12253ad4599aSBarry Smith /*@
1226bb7acecfSBarry Smith     DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is the transpose of the interpolation between the `DM`.
1227bb7acecfSBarry Smith     xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual) restriction. In other words xcoarse is the coarse
1228bb7acecfSBarry Smith     representation of xfine.
12292ed6491fSPatrick Sanan 
12302ed6491fSPatrick Sanan   Input Parameters:
1231bb7acecfSBarry Smith +      dac - `DM` that defines a coarse mesh
1232bb7acecfSBarry Smith .      daf - `DM` that defines a fine mesh
12332ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12342ed6491fSPatrick Sanan 
12352ed6491fSPatrick Sanan   Output Parameter:
12362ed6491fSPatrick Sanan .    scale - the scaled vector
12372ed6491fSPatrick Sanan 
1238bb7acecfSBarry Smith   Level: advanced
12392ed6491fSPatrick Sanan 
1240e9c74fd6SRichard Tran Mills   Developer Notes:
1241bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1242e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1243e9c74fd6SRichard Tran Mills 
1244bb7acecfSBarry Smith .seealso: `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, DMCreateRestriction()`, `DMCreateGlobalVector()`
12452ed6491fSPatrick Sanan @*/
1246d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1247d71ae5a4SJacob Faibussowitsch {
12482ed6491fSPatrick Sanan   Vec         fine;
12492ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12509704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1251e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12529704db99SRichard Tran Mills #endif
12532ed6491fSPatrick Sanan 
12542ed6491fSPatrick Sanan   PetscFunctionBegin;
12559566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12569566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12579566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12589704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12599704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12609704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12619704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12629566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1263e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12649566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12659566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12669566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
126783aa49f4SRichard Tran Mills   }
12689704db99SRichard Tran Mills #endif
12699566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12709566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12719566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12722ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12732ed6491fSPatrick Sanan }
12742ed6491fSPatrick Sanan 
12752ed6491fSPatrick Sanan /*@
1276bb7acecfSBarry Smith     DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1277bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12783ad4599aSBarry Smith 
1279a5bc1bf3SBarry Smith     Collective on dmc
12803ad4599aSBarry Smith 
1281d8d19677SJose E. Roman     Input Parameters:
1282bb7acecfSBarry Smith +   dmc - the `DM` object
1283bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
12843ad4599aSBarry Smith 
12853ad4599aSBarry Smith     Output Parameter:
12863ad4599aSBarry Smith .  mat - the restriction
12873ad4599aSBarry Smith 
12883ad4599aSBarry Smith     Level: developer
12893ad4599aSBarry Smith 
1290bb7acecfSBarry Smith     Note:
1291bb7acecfSBarry Smith     This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1292bb7acecfSBarry Smith     matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12933ad4599aSBarry Smith 
1294bb7acecfSBarry Smith .seealso: `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12953ad4599aSBarry Smith @*/
1296d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1297d71ae5a4SJacob Faibussowitsch {
12983ad4599aSBarry Smith   PetscFunctionBegin;
1299a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1300a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13015a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13029566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1303dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13049566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13053ad4599aSBarry Smith   PetscFunctionReturn(0);
13063ad4599aSBarry Smith }
13073ad4599aSBarry Smith 
130847c6ae99SBarry Smith /*@
1309bb7acecfSBarry Smith     DMCreateInjection - Gets injection matrix between two `DM` objects. This is an operator that applied to a vector obtained with
1310bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting the values
1311bb7acecfSBarry Smith     on the coarse grid points. This compares to the operator obtained by `DMCreateRestriction()` or the transpose of the operator obtained
1312bb7acecfSBarry Smith     by `DMCreateInterpolation()` that uses a "local weighted average" of the values around the coarse grid point as the coarse grid value.
131347c6ae99SBarry Smith 
1314a5bc1bf3SBarry Smith     Collective on dac
131547c6ae99SBarry Smith 
1316d8d19677SJose E. Roman     Input Parameters:
1317bb7acecfSBarry Smith +   dac - the `DM` object
1318bb7acecfSBarry Smith -   daf - the second, finer `DM` object
131947c6ae99SBarry Smith 
132047c6ae99SBarry Smith     Output Parameter:
13216dbf9973SLawrence Mitchell .   mat - the injection
132247c6ae99SBarry Smith 
132347c6ae99SBarry Smith     Level: developer
132447c6ae99SBarry Smith 
1325bb7acecfSBarry Smith    Note:
1326bb7acecfSBarry Smith     For `DMDA` objects this only works for "uniform refinement", that is the refined mesh was obtained `DMRefine()` or the coarse mesh was obtained by
1327bb7acecfSBarry Smith         `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
132885afcc9aSBarry Smith 
1329bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1330bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
133147c6ae99SBarry Smith @*/
1332d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1333d71ae5a4SJacob Faibussowitsch {
133447c6ae99SBarry Smith   PetscFunctionBegin;
1335a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1336a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13375a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13389566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1339dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13409566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
134147c6ae99SBarry Smith   PetscFunctionReturn(0);
134247c6ae99SBarry Smith }
134347c6ae99SBarry Smith 
1344b412c318SBarry Smith /*@
1345bb7acecfSBarry Smith   DMCreateMassMatrix - Gets the mass matrix between two `DM` objects, M_ij = \int \phi_i \psi_j where the \phi are Galerkin basis functions for a
1346bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1347bd041c0cSMatthew G. Knepley 
1348a5bc1bf3SBarry Smith   Collective on dac
1349bd041c0cSMatthew G. Knepley 
1350d8d19677SJose E. Roman   Input Parameters:
1351bb7acecfSBarry Smith + dmc - the target `DM` object
1352bb7acecfSBarry Smith - dmf - the source `DM` object
1353bd041c0cSMatthew G. Knepley 
1354bd041c0cSMatthew G. Knepley   Output Parameter:
1355b4937a87SMatthew G. Knepley . mat - the mass matrix
1356bd041c0cSMatthew G. Knepley 
1357bd041c0cSMatthew G. Knepley   Level: developer
1358bd041c0cSMatthew G. Knepley 
1359bb7acecfSBarry Smith   Notes:
1360bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1361bb7acecfSBarry Smith 
1362bb7acecfSBarry Smith   if dmc is dmf then x^t M x is an approximation to the L2 norm of the vector x which is obtained by `DMCreateGlobalVector()`
1363bb7acecfSBarry Smith 
1364bb7acecfSBarry Smith .seealso: `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1365bd041c0cSMatthew G. Knepley @*/
1366d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1367d71ae5a4SJacob Faibussowitsch {
1368bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1369b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1370b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13715a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13725b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1373dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13745b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
1375b4937a87SMatthew G. Knepley   PetscFunctionReturn(0);
1376b4937a87SMatthew G. Knepley }
1377b4937a87SMatthew G. Knepley 
1378b4937a87SMatthew G. Knepley /*@
1379bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1380b4937a87SMatthew G. Knepley 
1381b4937a87SMatthew G. Knepley   Collective on dm
1382b4937a87SMatthew G. Knepley 
1383b4937a87SMatthew G. Knepley   Input Parameter:
1384bb7acecfSBarry Smith . dm - the `DM` object
1385b4937a87SMatthew G. Knepley 
1386b4937a87SMatthew G. Knepley   Output Parameter:
1387bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1388b4937a87SMatthew G. Knepley 
1389b4937a87SMatthew G. Knepley   Level: developer
1390b4937a87SMatthew G. Knepley 
1391bb7acecfSBarry Smith   Note:
1392bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1393bb7acecfSBarry Smith 
1394bb7acecfSBarry Smith .seealso: `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1395b4937a87SMatthew G. Knepley @*/
1396d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1397d71ae5a4SJacob Faibussowitsch {
1398b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1399b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1400b4937a87SMatthew G. Knepley   PetscValidPointer(lm, 2);
1401dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
1402bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1403bd041c0cSMatthew G. Knepley }
1404bd041c0cSMatthew G. Knepley 
1405bd041c0cSMatthew G. Knepley /*@
1406bb7acecfSBarry Smith     DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1407bb7acecfSBarry Smith     of a PDE on the `DM`.
140847c6ae99SBarry Smith 
1409d083f849SBarry Smith     Collective on dm
141047c6ae99SBarry Smith 
1411d8d19677SJose E. Roman     Input Parameters:
1412bb7acecfSBarry Smith +   dm - the `DM` object
1413bb7acecfSBarry Smith -   ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
141447c6ae99SBarry Smith 
141547c6ae99SBarry Smith     Output Parameter:
141647c6ae99SBarry Smith .   coloring - the coloring
141747c6ae99SBarry Smith 
1418ec5066bdSBarry Smith     Notes:
1419bb7acecfSBarry 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
1420bb7acecfSBarry Smith     matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1421ec5066bdSBarry Smith 
1422bb7acecfSBarry Smith     This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
1423ec5066bdSBarry Smith 
142447c6ae99SBarry Smith     Level: developer
142547c6ae99SBarry Smith 
1426bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1427aab9d709SJed Brown @*/
1428d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1429d71ae5a4SJacob 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()`
1467aab9d709SJed Brown @*/
1468d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1469d71ae5a4SJacob Faibussowitsch {
147047c6ae99SBarry Smith   PetscFunctionBegin;
1471171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1472064a246eSJacob Faibussowitsch   PetscValidPointer(mat, 2);
14739566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14749566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1475dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
147676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1477c6b011d8SStefano Zampini     DM mdm;
1478c6b011d8SStefano Zampini 
14799566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14807a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1481c6b011d8SStefano Zampini   }
1482e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1483e5e52638SMatthew G. Knepley   if (dm->Nf) {
1484e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1485649ef022SMatthew Knepley     PetscInt     Nf, f;
1486e571a35bSMatthew G. Knepley 
14879566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1488649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1489649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14909566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14919566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14929566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1493649ef022SMatthew Knepley         break;
1494e571a35bSMatthew G. Knepley       }
1495649ef022SMatthew Knepley     }
1496649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1497649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
14989566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
14999566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15009566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1501e571a35bSMatthew G. Knepley       }
1502e571a35bSMatthew G. Knepley     }
1503e571a35bSMatthew G. Knepley   }
15049566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
150547c6ae99SBarry Smith   PetscFunctionReturn(0);
150647c6ae99SBarry Smith }
150747c6ae99SBarry Smith 
1508732e2eb9SMatthew G Knepley /*@
1509bb7acecfSBarry Smith   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and `ISLocalToGlobalMapping` will be
1510bb7acecfSBarry 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
1511bb7acecfSBarry Smith   `MatSetPreallocationCOO()` and `MatSetValuesCOO()` will be used.
1512aa0f6e3cSJed Brown 
1513aa0f6e3cSJed Brown   Logically Collective on dm
1514aa0f6e3cSJed Brown 
1515aa0f6e3cSJed Brown   Input Parameters:
1516bb7acecfSBarry Smith + dm - the `DM`
1517bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1518aa0f6e3cSJed Brown 
1519aa0f6e3cSJed Brown   Level: developer
1520aa0f6e3cSJed Brown 
1521bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1522aa0f6e3cSJed Brown @*/
1523d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1524d71ae5a4SJacob Faibussowitsch {
1525aa0f6e3cSJed Brown   PetscFunctionBegin;
1526aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1527aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
1528aa0f6e3cSJed Brown   PetscFunctionReturn(0);
1529aa0f6e3cSJed Brown }
1530aa0f6e3cSJed Brown 
1531aa0f6e3cSJed Brown /*@
1532bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1533732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1534732e2eb9SMatthew G Knepley 
1535d083f849SBarry Smith   Logically Collective on dm
1536732e2eb9SMatthew G Knepley 
1537d8d19677SJose E. Roman   Input Parameters:
1538bb7acecfSBarry Smith + dm - the `DM`
1539bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1540732e2eb9SMatthew G Knepley 
1541732e2eb9SMatthew G Knepley   Level: developer
1542f27dd7c6SMatthew G. Knepley 
1543f27dd7c6SMatthew G. Knepley   Options Database Keys:
1544bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1545f27dd7c6SMatthew G. Knepley 
1546bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1547732e2eb9SMatthew G Knepley @*/
1548d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1549d71ae5a4SJacob Faibussowitsch {
1550732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1551732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1552732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1553732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1554732e2eb9SMatthew G Knepley }
1555732e2eb9SMatthew G Knepley 
1556b06ff27eSHong Zhang /*@
1557bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1558bb7acecfSBarry Smith     but the array for numerical values will not be allocated.
1559b06ff27eSHong Zhang 
1560d083f849SBarry Smith   Logically Collective on dm
1561b06ff27eSHong Zhang 
1562d8d19677SJose E. Roman   Input Parameters:
1563bb7acecfSBarry Smith + dm - the `DM`
1564bb7acecfSBarry Smith - only - `PETSC_TRUE` if you only want matrix stucture
1565b06ff27eSHong Zhang 
1566b06ff27eSHong Zhang   Level: developer
1567bb7acecfSBarry Smith 
1568bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1569b06ff27eSHong Zhang @*/
1570d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1571d71ae5a4SJacob Faibussowitsch {
1572b06ff27eSHong Zhang   PetscFunctionBegin;
1573b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1574b06ff27eSHong Zhang   dm->structure_only = only;
1575b06ff27eSHong Zhang   PetscFunctionReturn(0);
1576b06ff27eSHong Zhang }
1577b06ff27eSHong Zhang 
1578a89ea682SMatthew G Knepley /*@C
1579bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1580a89ea682SMatthew G Knepley 
1581a89ea682SMatthew G Knepley   Not Collective
1582a89ea682SMatthew G Knepley 
1583a89ea682SMatthew G Knepley   Input Parameters:
1584bb7acecfSBarry Smith + dm - the `DM` object
1585a5b23f4aSJose E. Roman . count - The minimum size
1586bb7acecfSBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, or MPIU_INT)
1587a89ea682SMatthew G Knepley 
1588a89ea682SMatthew G Knepley   Output Parameter:
1589a89ea682SMatthew G Knepley . array - the work array
1590a89ea682SMatthew G Knepley 
1591a89ea682SMatthew G Knepley   Level: developer
1592a89ea682SMatthew G Knepley 
1593bb7acecfSBarry Smith   Note:
1594bb7acecfSBarry Smith   A `DM` may stash the array between instantations so using this routine may be more efficient than calling `PetscMalloc()`
1595bb7acecfSBarry Smith 
1596bb7acecfSBarry Smith   The array may contain nonzero values
1597bb7acecfSBarry Smith 
1598bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1599a89ea682SMatthew G Knepley @*/
1600d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1601d71ae5a4SJacob Faibussowitsch {
1602aa1993deSMatthew G Knepley   DMWorkLink  link;
160369291d52SBarry Smith   PetscMPIInt dsize;
1604a89ea682SMatthew G Knepley 
1605a89ea682SMatthew G Knepley   PetscFunctionBegin;
1606a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1607aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1608aa1993deSMatthew G Knepley   if (dm->workin) {
1609aa1993deSMatthew G Knepley     link       = dm->workin;
1610aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1611aa1993deSMatthew G Knepley   } else {
16124dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1613a89ea682SMatthew G Knepley   }
16149566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dtype, &dsize));
16155056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16169566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16179566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1618854ce69bSBarry Smith     link->bytes = dsize * count;
1619aa1993deSMatthew G Knepley   }
1620aa1993deSMatthew G Knepley   link->next  = dm->workout;
1621aa1993deSMatthew G Knepley   dm->workout = link;
1622cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
162300d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
162400d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
162500d952a4SJed Brown #endif
1626aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
1627a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1628a89ea682SMatthew G Knepley }
1629a89ea682SMatthew G Knepley 
1630aa1993deSMatthew G Knepley /*@C
1631bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1632aa1993deSMatthew G Knepley 
1633aa1993deSMatthew G Knepley   Not Collective
1634aa1993deSMatthew G Knepley 
1635aa1993deSMatthew G Knepley   Input Parameters:
1636bb7acecfSBarry Smith + dm - the `DM` object
1637a5b23f4aSJose E. Roman . count - The minimum size
163869291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1639aa1993deSMatthew G Knepley 
1640aa1993deSMatthew G Knepley   Output Parameter:
1641aa1993deSMatthew G Knepley . array - the work array
1642aa1993deSMatthew G Knepley 
1643aa1993deSMatthew G Knepley   Level: developer
1644aa1993deSMatthew G Knepley 
164595452b02SPatrick Sanan   Developer Notes:
1646bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1647147403d9SBarry Smith 
1648bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1649aa1993deSMatthew G Knepley @*/
1650d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1651d71ae5a4SJacob Faibussowitsch {
1652aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1653aa1993deSMatthew G Knepley 
1654aa1993deSMatthew G Knepley   PetscFunctionBegin;
1655aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1656aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1657aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1658aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1659aa1993deSMatthew G Knepley       *p            = link->next;
1660aa1993deSMatthew G Knepley       link->next    = dm->workin;
1661aa1993deSMatthew G Knepley       dm->workin    = link;
16620298fd71SBarry Smith       *(void **)mem = NULL;
1663aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1664aa1993deSMatthew G Knepley     }
1665aa1993deSMatthew G Knepley   }
1666aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1667aa1993deSMatthew G Knepley }
1668e7c4fc90SDmitry Karpeev 
16698cda7954SMatthew G. Knepley /*@C
1670bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1671bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
16728cda7954SMatthew G. Knepley 
1673bb7acecfSBarry Smith   Logically collective on dm
16748cda7954SMatthew G. Knepley 
16758cda7954SMatthew G. Knepley   Input Parameters:
1676bb7acecfSBarry Smith + dm     - The `DM`
16778cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16788cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
16798cda7954SMatthew G. Knepley 
1680147403d9SBarry Smith   Calling sequence of nullsp:
1681147403d9SBarry Smith .vb
1682147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1683147403d9SBarry Smith .ve
1684bb7acecfSBarry Smith +  dm        - The present `DM`
1685bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1686147403d9SBarry Smith .  field     - The field number in dm
1687147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16888cda7954SMatthew G. Knepley 
168949762cbcSSatish Balay   Level: intermediate
169049762cbcSSatish Balay 
1691bb7acecfSBarry Smith   Fortran Notes:
1692bb7acecfSBarry Smith   This function is not available from Fortran.
1693bb7acecfSBarry Smith 
1694bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1695147403d9SBarry Smith @*/
1696d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1697d71ae5a4SJacob Faibussowitsch {
1698435a35e8SMatthew G Knepley   PetscFunctionBegin;
1699435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17007a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1701435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1702435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1703435a35e8SMatthew G Knepley }
1704435a35e8SMatthew G Knepley 
17058cda7954SMatthew G. Knepley /*@C
1706bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17078cda7954SMatthew G. Knepley 
17088cda7954SMatthew G. Knepley   Not collective
17098cda7954SMatthew G. Knepley 
17108cda7954SMatthew G. Knepley   Input Parameters:
1711bb7acecfSBarry Smith + dm     - The `DM`
17128cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17138cda7954SMatthew G. Knepley 
17148cda7954SMatthew G. Knepley   Output Parameter:
17158cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17168cda7954SMatthew G. Knepley 
1717147403d9SBarry Smith   Calling sequence of nullsp:
1718147403d9SBarry Smith .vb
1719147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1720147403d9SBarry Smith .ve
1721147403d9SBarry Smith +  dm        - The present DM
1722147403d9SBarry Smith .  origField - The field number given above, in the original DM
1723147403d9SBarry Smith .  field     - The field number in dm
1724147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17258cda7954SMatthew G. Knepley 
1726bb7acecfSBarry Smith   Fortran Note:
1727bb7acecfSBarry Smith   This function is not available from Fortran.
17288cda7954SMatthew G. Knepley 
172949762cbcSSatish Balay    Level: intermediate
173049762cbcSSatish Balay 
1731bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1732147403d9SBarry Smith @*/
1733d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1734d71ae5a4SJacob Faibussowitsch {
17350a50eb56SMatthew G. Knepley   PetscFunctionBegin;
17360a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1737f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
17387a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
17390a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
17400a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
17410a50eb56SMatthew G. Knepley }
17420a50eb56SMatthew G. Knepley 
17438cda7954SMatthew G. Knepley /*@C
1744bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
17458cda7954SMatthew G. Knepley 
1746bb7acecfSBarry Smith   Logically collective on dm
17478cda7954SMatthew G. Knepley 
17488cda7954SMatthew G. Knepley   Input Parameters:
1749bb7acecfSBarry Smith + dm     - The `DM`
17508cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17518cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
17528cda7954SMatthew G. Knepley 
1753147403d9SBarry Smith   Calling sequence of nullsp:
1754147403d9SBarry Smith .vb
1755147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1756147403d9SBarry Smith .ve
1757bb7acecfSBarry Smith +  dm        - The present `DM`
1758bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1759147403d9SBarry Smith .  field     - The field number in dm
1760147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17618cda7954SMatthew G. Knepley 
1762bb7acecfSBarry Smith   Fortran Note:
1763bb7acecfSBarry Smith   This function is not available from Fortran.
17648cda7954SMatthew G. Knepley 
176549762cbcSSatish Balay    Level: intermediate
176649762cbcSSatish Balay 
1767bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1768bb7acecfSBarry Smith           `MatNullSpace`
1769147403d9SBarry Smith @*/
1770d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1771d71ae5a4SJacob Faibussowitsch {
1772f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1773f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17747a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1775f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1776f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1777f9d4088aSMatthew G. Knepley }
1778f9d4088aSMatthew G. Knepley 
17798cda7954SMatthew G. Knepley /*@C
1780bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
17818cda7954SMatthew G. Knepley 
17828cda7954SMatthew G. Knepley   Not collective
17838cda7954SMatthew G. Knepley 
17848cda7954SMatthew G. Knepley   Input Parameters:
1785bb7acecfSBarry Smith + dm     - The `DM`
17868cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17878cda7954SMatthew G. Knepley 
17888cda7954SMatthew G. Knepley   Output Parameter:
17898cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
17908cda7954SMatthew G. Knepley 
1791147403d9SBarry Smith   Calling sequence of nullsp:
1792147403d9SBarry Smith .vb
1793147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1794147403d9SBarry Smith .ve
1795bb7acecfSBarry Smith +  dm        - The present `DM`
1796bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1797147403d9SBarry Smith .  field     - The field number in dm
1798147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17998cda7954SMatthew G. Knepley 
1800bb7acecfSBarry Smith   Fortran Note:
1801bb7acecfSBarry Smith   This function is not available from Fortran.
18028cda7954SMatthew G. Knepley 
180349762cbcSSatish Balay    Level: intermediate
180449762cbcSSatish Balay 
1805bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1806bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1807147403d9SBarry Smith @*/
1808d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1809d71ae5a4SJacob Faibussowitsch {
1810f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1811f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1812f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
18137a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1814f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1815f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1816f9d4088aSMatthew G. Knepley }
1817f9d4088aSMatthew G. Knepley 
18184f3b5142SJed Brown /*@C
1819bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18204d343eeaSMatthew G Knepley 
18214d343eeaSMatthew G Knepley   Not collective
18224d343eeaSMatthew G Knepley 
18234d343eeaSMatthew G Knepley   Input Parameter:
1824bb7acecfSBarry Smith . dm - the `DM` object
18254d343eeaSMatthew G Knepley 
18264d343eeaSMatthew G Knepley   Output Parameters:
18270298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
1828bb7acecfSBarry Smith . fieldNames - The number of each field (or NULL if not requested)
18290298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
18304d343eeaSMatthew G Knepley 
18314d343eeaSMatthew G Knepley   Level: intermediate
18324d343eeaSMatthew G Knepley 
1833bb7acecfSBarry Smith   Note:
183421c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1835bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1836bb7acecfSBarry Smith   `PetscFree()`.
183721c9b008SJed Brown 
1838bb7acecfSBarry Smith   Fortran Note:
1839bb7acecfSBarry Smith   Not available in Fortran.
1840bb7acecfSBarry Smith 
1841bb7acecfSBarry Smith   Developer Note:
1842bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1843bb7acecfSBarry Smith   likely be removed.
1844bb7acecfSBarry Smith 
1845bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1846bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18474d343eeaSMatthew G Knepley @*/
1848d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1849d71ae5a4SJacob Faibussowitsch {
185037d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
18514d343eeaSMatthew G Knepley 
18524d343eeaSMatthew G Knepley   PetscFunctionBegin;
18534d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
185469ca1f37SDmitry Karpeev   if (numFields) {
1855534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields, 2);
185669ca1f37SDmitry Karpeev     *numFields = 0;
185769ca1f37SDmitry Karpeev   }
185837d0c07bSMatthew G Knepley   if (fieldNames) {
185937d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames, 3);
18600298fd71SBarry Smith     *fieldNames = NULL;
186169ca1f37SDmitry Karpeev   }
186269ca1f37SDmitry Karpeev   if (fields) {
186369ca1f37SDmitry Karpeev     PetscValidPointer(fields, 4);
18640298fd71SBarry Smith     *fields = NULL;
186569ca1f37SDmitry Karpeev   }
18669566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
186737d0c07bSMatthew G Knepley   if (section) {
18683a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
186937d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
187037d0c07bSMatthew G Knepley 
18719566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
18729566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
18739566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
18749566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
187537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
187637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
18779566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
187837d0c07bSMatthew G Knepley     }
187937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
188037d0c07bSMatthew G Knepley       PetscInt gdof;
188137d0c07bSMatthew G Knepley 
18829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
188337d0c07bSMatthew G Knepley       if (gdof > 0) {
188437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
18853a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
188637d0c07bSMatthew G Knepley 
18879566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
18889566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
18893a544194SStefano Zampini           fpdof = fdof - fcdof;
18903a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
18913a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
18923a544194SStefano Zampini             fieldNc[f] = 1;
18933a544194SStefano Zampini           }
18943a544194SStefano Zampini           fieldSizes[f] += fpdof;
189537d0c07bSMatthew G Knepley         }
189637d0c07bSMatthew G Knepley       }
189737d0c07bSMatthew G Knepley     }
189837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
18999566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
190037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
190137d0c07bSMatthew G Knepley     }
190237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
190337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
190437d0c07bSMatthew G Knepley 
19059566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
190637d0c07bSMatthew G Knepley       if (gdof > 0) {
19079566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
190837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
190937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
191037d0c07bSMatthew G Knepley 
19119566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19129566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1913ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
191437d0c07bSMatthew G Knepley         }
191537d0c07bSMatthew G Knepley       }
191637d0c07bSMatthew G Knepley     }
19178865f1eaSKarl Rupp     if (numFields) *numFields = nF;
191837d0c07bSMatthew G Knepley     if (fieldNames) {
19199566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
192037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
192137d0c07bSMatthew G Knepley         const char *fieldName;
192237d0c07bSMatthew G Knepley 
19239566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19249566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
192537d0c07bSMatthew G Knepley       }
192637d0c07bSMatthew G Knepley     }
192737d0c07bSMatthew G Knepley     if (fields) {
19289566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
192937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19303a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19313a544194SStefano Zampini 
19329566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19333a544194SStefano Zampini         in[0] = -fieldNc[f];
19343a544194SStefano Zampini         in[1] = fieldNc[f];
19351c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19363a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19379566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
193837d0c07bSMatthew G Knepley       }
193937d0c07bSMatthew G Knepley     }
19409566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1941dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19424d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
19434d343eeaSMatthew G Knepley }
19444d343eeaSMatthew G Knepley 
194516621825SDmitry Karpeev /*@C
1946bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1947bb7acecfSBarry Smith                           corresponding to different fields: each `IS` contains the global indices of the dofs of the
1948bb7acecfSBarry Smith                           corresponding field, defined by `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
1949bb7acecfSBarry Smith                           The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
1950e7c4fc90SDmitry Karpeev 
1951e7c4fc90SDmitry Karpeev   Not collective
1952e7c4fc90SDmitry Karpeev 
1953e7c4fc90SDmitry Karpeev   Input Parameter:
1954bb7acecfSBarry Smith . dm - the `DM` object
1955e7c4fc90SDmitry Karpeev 
1956e7c4fc90SDmitry Karpeev   Output Parameters:
1957bb7acecfSBarry Smith + len       - The number of fields (or NULL if not requested)
19580298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
19590298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
1960bb7acecfSBarry Smith - dmlist    - The `DM`s for each field subproblem (or NULL, if not requested; if NULL is returned, no `DM`s are defined)
1961e7c4fc90SDmitry Karpeev 
1962e7c4fc90SDmitry Karpeev   Level: intermediate
1963e7c4fc90SDmitry Karpeev 
1964bb7acecfSBarry Smith   Note:
1965e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1966bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
1967bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
1968e7c4fc90SDmitry Karpeev 
1969bb7acecfSBarry Smith   Fortran Note:
1970bb7acecfSBarry Smith   Not available in Fortran.
1971bb7acecfSBarry Smith 
1972bb7acecfSBarry Smith   Developer Note:
1973bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
1974bb7acecfSBarry Smith 
1975bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
1976e7c4fc90SDmitry Karpeev @*/
1977d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1978d71ae5a4SJacob Faibussowitsch {
1979e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1980e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19818865f1eaSKarl Rupp   if (len) {
1982534a8f05SLisandro Dalcin     PetscValidIntPointer(len, 2);
19838865f1eaSKarl Rupp     *len = 0;
19848865f1eaSKarl Rupp   }
19858865f1eaSKarl Rupp   if (namelist) {
19868865f1eaSKarl Rupp     PetscValidPointer(namelist, 3);
1987ea78f98cSLisandro Dalcin     *namelist = NULL;
19888865f1eaSKarl Rupp   }
19898865f1eaSKarl Rupp   if (islist) {
19908865f1eaSKarl Rupp     PetscValidPointer(islist, 4);
1991ea78f98cSLisandro Dalcin     *islist = NULL;
19928865f1eaSKarl Rupp   }
19938865f1eaSKarl Rupp   if (dmlist) {
19948865f1eaSKarl Rupp     PetscValidPointer(dmlist, 5);
1995ea78f98cSLisandro Dalcin     *dmlist = NULL;
19968865f1eaSKarl Rupp   }
1997f3f0edfdSDmitry Karpeev   /*
1998f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1999f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2000f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2001f3f0edfdSDmitry Karpeev    */
20027a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
200316621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2004435a35e8SMatthew G Knepley     PetscSection section;
2005435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2006435a35e8SMatthew G Knepley 
20079566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20089566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2009435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2010f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20119566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20129566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20139566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2014435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2015435a35e8SMatthew G Knepley         const char *fieldName;
2016435a35e8SMatthew G Knepley 
20179566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
201803dc3394SMatthew G. Knepley         if (namelist) {
20199566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20209566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2021435a35e8SMatthew G Knepley         }
202203dc3394SMatthew G. Knepley       }
2023435a35e8SMatthew G Knepley     } else {
20249566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2025e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20260298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2027e7c4fc90SDmitry Karpeev     }
2028dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
202916621825SDmitry Karpeev   PetscFunctionReturn(0);
203016621825SDmitry Karpeev }
203116621825SDmitry Karpeev 
2032412a4547SAlexis Marboeuf /*@C
2033412a4547SAlexis Marboeuf   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
2034412a4547SAlexis Marboeuf                   The fields are defined by DMCreateFieldIS().
2035435a35e8SMatthew G Knepley 
2036435a35e8SMatthew G Knepley   Not collective
2037435a35e8SMatthew G Knepley 
2038435a35e8SMatthew G Knepley   Input Parameters:
2039bb7acecfSBarry Smith + dm        - The `DM `object
2040bb7acecfSBarry Smith . numFields - The number of fields to select
20412adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2042435a35e8SMatthew G Knepley 
2043435a35e8SMatthew G Knepley   Output Parameters:
2044bb7acecfSBarry Smith + is - The global indices for all the degrees of freedom in the new sub `DM`
2045bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2046435a35e8SMatthew G Knepley 
2047bb7acecfSBarry Smith   Note:
2048bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20495d3b26e6SMatthew G. Knepley 
2050435a35e8SMatthew G Knepley   Level: intermediate
2051435a35e8SMatthew G Knepley 
2052bb7acecfSBarry Smith .seealso: `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `DM`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2053435a35e8SMatthew G Knepley @*/
2054d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2055d71ae5a4SJacob Faibussowitsch {
2056435a35e8SMatthew G Knepley   PetscFunctionBegin;
2057435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2058dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields, 3);
20598865f1eaSKarl Rupp   if (is) PetscValidPointer(is, 4);
20608865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm, 5);
2061dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
2062435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
2063435a35e8SMatthew G Knepley }
2064435a35e8SMatthew G Knepley 
20652adcc780SMatthew G. Knepley /*@C
2066bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
20672adcc780SMatthew G. Knepley 
20682adcc780SMatthew G. Knepley   Not collective
20692adcc780SMatthew G. Knepley 
2070d8d19677SJose E. Roman   Input Parameters:
2071bb7acecfSBarry Smith + dms - The `DM` objects
2072bb7acecfSBarry Smith - n - The number of `DM`s
20732adcc780SMatthew G. Knepley 
20742adcc780SMatthew G. Knepley   Output Parameters:
2075bb7acecfSBarry Smith + is - The global indices for each of subproblem within the super `DM`, or NULL
2076bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
20772adcc780SMatthew G. Knepley 
2078bb7acecfSBarry Smith   Note:
2079bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20805d3b26e6SMatthew G. Knepley 
20812adcc780SMatthew G. Knepley   Level: intermediate
20822adcc780SMatthew G. Knepley 
2083bb7acecfSBarry Smith .seealso: `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
20842adcc780SMatthew G. Knepley @*/
2085d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
2086d71ae5a4SJacob Faibussowitsch {
20872adcc780SMatthew G. Knepley   PetscInt i;
20882adcc780SMatthew G. Knepley 
20892adcc780SMatthew G. Knepley   PetscFunctionBegin;
20902adcc780SMatthew G. Knepley   PetscValidPointer(dms, 1);
2091ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
20922adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is, 3);
2093a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm, 4);
2094bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2095bb7acecfSBarry Smith   if (n) {
2096b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2097dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
20982adcc780SMatthew G. Knepley   }
20992adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
21002adcc780SMatthew G. Knepley }
21012adcc780SMatthew G. Knepley 
210216621825SDmitry Karpeev /*@C
2103bb7acecfSBarry Smith   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a problem into subproblems
2104bb7acecfSBarry Smith                           corresponding to restrictions to pairs of nested subdomains: each `IS` contains the global
2105bb7acecfSBarry Smith                           indices of the dofs of the corresponding subdomains with in the dofs of the original `DM`.
2106bb7acecfSBarry Smith                           The inner subdomains conceptually define a nonoverlapping covering, while outer subdomains can overlap.
2107bb7acecfSBarry Smith                           The optional list of `DM`s define a `DM` for each subproblem.
210816621825SDmitry Karpeev 
210916621825SDmitry Karpeev   Not collective
211016621825SDmitry Karpeev 
211116621825SDmitry Karpeev   Input Parameter:
2112bb7acecfSBarry Smith . dm - the `DM` object
211316621825SDmitry Karpeev 
211416621825SDmitry Karpeev   Output Parameters:
2115bb7acecfSBarry Smith + n            - The number of subproblems in the domain decomposition (or NULL if not requested)
21160298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
21170298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
21180298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
2119bb7acecfSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no `DM`s are defined)
212016621825SDmitry Karpeev 
212116621825SDmitry Karpeev   Level: intermediate
212216621825SDmitry Karpeev 
2123bb7acecfSBarry Smith   Note:
212416621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2125bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2126bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
212716621825SDmitry Karpeev 
2128bb7acecfSBarry Smith   Questions:
2129bb7acecfSBarry Smith   The dmlist is for the inner subdomains or the outer subdomains or all subdomains?
2130bb7acecfSBarry Smith 
2131bb7acecfSBarry Smith .seealso: `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldDecomposition()`
213216621825SDmitry Karpeev @*/
2133d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2134d71ae5a4SJacob Faibussowitsch {
2135be081cd6SPeter Brune   DMSubDomainHookLink link;
2136be081cd6SPeter Brune   PetscInt            i, l;
213716621825SDmitry Karpeev 
213816621825SDmitry Karpeev   PetscFunctionBegin;
213916621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21409371c9d4SSatish Balay   if (n) {
21419371c9d4SSatish Balay     PetscValidIntPointer(n, 2);
21429371c9d4SSatish Balay     *n = 0;
21439371c9d4SSatish Balay   }
21449371c9d4SSatish Balay   if (namelist) {
21459371c9d4SSatish Balay     PetscValidPointer(namelist, 3);
21469371c9d4SSatish Balay     *namelist = NULL;
21479371c9d4SSatish Balay   }
21489371c9d4SSatish Balay   if (innerislist) {
21499371c9d4SSatish Balay     PetscValidPointer(innerislist, 4);
21509371c9d4SSatish Balay     *innerislist = NULL;
21519371c9d4SSatish Balay   }
21529371c9d4SSatish Balay   if (outerislist) {
21539371c9d4SSatish Balay     PetscValidPointer(outerislist, 5);
21549371c9d4SSatish Balay     *outerislist = NULL;
21559371c9d4SSatish Balay   }
21569371c9d4SSatish Balay   if (dmlist) {
21579371c9d4SSatish Balay     PetscValidPointer(dmlist, 6);
21589371c9d4SSatish Balay     *dmlist = NULL;
21599371c9d4SSatish Balay   }
2160f3f0edfdSDmitry Karpeev   /*
2161f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2162f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2163f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2164f3f0edfdSDmitry Karpeev    */
21657a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
216616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2167dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
216814a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2169f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2170be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2171be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
21729566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2173be081cd6SPeter Brune         }
2174648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2175e7c4fc90SDmitry Karpeev       }
217614a18fd3SPeter Brune     }
2177bb7acecfSBarry Smith     if (n) *n = l;
217814a18fd3SPeter Brune   }
2179e30e807fSPeter Brune   PetscFunctionReturn(0);
2180e30e807fSPeter Brune }
2181e30e807fSPeter Brune 
2182e30e807fSPeter Brune /*@C
2183e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2184e30e807fSPeter Brune 
2185e30e807fSPeter Brune   Not collective
2186e30e807fSPeter Brune 
2187e30e807fSPeter Brune   Input Parameters:
2188bb7acecfSBarry Smith + dm - the `DM` object
2189e30e807fSPeter Brune . n  - the number of subdomain scatters
2190e30e807fSPeter Brune - subdms - the local subdomains
2191e30e807fSPeter Brune 
2192e30e807fSPeter Brune   Output Parameters:
21936b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2194e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2195e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2196e30e807fSPeter Brune 
2197bb7acecfSBarry Smith   Note:
2198bb7acecfSBarry Smith     This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2199e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2200e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2201e30e807fSPeter Brune   solution and residual data.
2202e30e807fSPeter Brune 
2203bb7acecfSBarry Smith   Questions:
2204bb7acecfSBarry Smith   Can the subdms input be anything or are they exactly the `DM` obtained from `DMCreateDomainDecomposition()`?
2205bb7acecfSBarry Smith 
2206e30e807fSPeter Brune   Level: developer
2207e30e807fSPeter Brune 
2208bb7acecfSBarry Smith .seealso: `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2209e30e807fSPeter Brune @*/
2210d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat)
2211d71ae5a4SJacob Faibussowitsch {
2212e30e807fSPeter Brune   PetscFunctionBegin;
2213e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2214e30e807fSPeter Brune   PetscValidPointer(subdms, 3);
2215dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
2216e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2217e7c4fc90SDmitry Karpeev }
2218e7c4fc90SDmitry Karpeev 
221947c6ae99SBarry Smith /*@
2220bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
222147c6ae99SBarry Smith 
2222d083f849SBarry Smith   Collective on dm
222347c6ae99SBarry Smith 
2224d8d19677SJose E. Roman   Input Parameters:
2225bb7acecfSBarry Smith + dm   - the `DM` object
2226bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
222747c6ae99SBarry Smith 
222847c6ae99SBarry Smith   Output Parameter:
2229bb7acecfSBarry Smith . dmf - the refined `D`M, or NULL
2230ae0a1c52SMatthew G Knepley 
2231f27dd7c6SMatthew G. Knepley   Options Database Keys:
2232412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2233412e9a14SMatthew G. Knepley 
2234bb7acecfSBarry Smith   Note:
2235bb7acecfSBarry Smith   If no refinement was done, the return value is NULL
223647c6ae99SBarry Smith 
223747c6ae99SBarry Smith   Level: developer
223847c6ae99SBarry Smith 
2239bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
224047c6ae99SBarry Smith @*/
2241d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2242d71ae5a4SJacob Faibussowitsch {
2243c833c3b5SJed Brown   DMRefineHookLink link;
224447c6ae99SBarry Smith 
224547c6ae99SBarry Smith   PetscFunctionBegin;
2246732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22479566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2248dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
22494057135bSMatthew G Knepley   if (*dmf) {
225043842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
22518865f1eaSKarl Rupp 
22529566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
22538865f1eaSKarl Rupp 
2254644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
22550598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2256656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
22578865f1eaSKarl Rupp 
22589566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2259c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
22601baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2261c833c3b5SJed Brown     }
2262c833c3b5SJed Brown   }
22639566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
2264c833c3b5SJed Brown   PetscFunctionReturn(0);
2265c833c3b5SJed Brown }
2266c833c3b5SJed Brown 
2267bb9467b5SJed Brown /*@C
2268c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2269c833c3b5SJed Brown 
2270bb7acecfSBarry Smith    Logically Collective on coarse
2271c833c3b5SJed Brown 
22724165533cSJose E. Roman    Input Parameters:
2273bb7acecfSBarry Smith +  coarse - `DM` on which to run a hook when interpolating to a finer level
2274bb7acecfSBarry Smith .  refinehook - function to run when setting up the finer level
2275bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels (once per `SNESSolve`())
22760298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2277c833c3b5SJed Brown 
2278c833c3b5SJed Brown    Calling sequence of refinehook:
2279c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2280c833c3b5SJed Brown 
2281bb7acecfSBarry Smith +  coarse - coarse level `DM`
2282bb7acecfSBarry Smith .  fine - fine level `DM` to interpolate problem to
2283c833c3b5SJed Brown -  ctx - optional user-defined function context
2284c833c3b5SJed Brown 
2285c833c3b5SJed Brown    Calling sequence for interphook:
2286c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2287c833c3b5SJed Brown 
2288bb7acecfSBarry Smith +  coarse - coarse level `DM`
2289c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2290bb7acecfSBarry Smith .  fine - fine level `DM` to update
2291c833c3b5SJed Brown -  ctx - optional user-defined function context
2292c833c3b5SJed Brown 
2293c833c3b5SJed Brown    Level: advanced
2294c833c3b5SJed Brown 
2295c833c3b5SJed Brown    Notes:
2296bb7acecfSBarry Smith    This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2297bb7acecfSBarry Smith    passed to fine grids while grid sequencing.
2298bb7acecfSBarry Smith 
2299bb7acecfSBarry Smith    The actual interpolation is done when `DMInterpolate()` is called.
2300c833c3b5SJed Brown 
2301c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2302c833c3b5SJed Brown 
2303bb7acecfSBarry Smith    Fortran Note:
2304bb7acecfSBarry Smith    This function is not available from Fortran.
2305bb9467b5SJed Brown 
2306bb7acecfSBarry Smith .seealso: `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2307c833c3b5SJed Brown @*/
2308d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2309d71ae5a4SJacob Faibussowitsch {
2310c833c3b5SJed Brown   DMRefineHookLink link, *p;
2311c833c3b5SJed Brown 
2312c833c3b5SJed Brown   PetscFunctionBegin;
2313c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23143d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23153d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
23163d8e3701SJed Brown   }
23179566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2318c833c3b5SJed Brown   link->refinehook = refinehook;
2319c833c3b5SJed Brown   link->interphook = interphook;
2320c833c3b5SJed Brown   link->ctx        = ctx;
23210298fd71SBarry Smith   link->next       = NULL;
2322c833c3b5SJed Brown   *p               = link;
2323c833c3b5SJed Brown   PetscFunctionReturn(0);
2324c833c3b5SJed Brown }
2325c833c3b5SJed Brown 
23263d8e3701SJed Brown /*@C
2327bb7acecfSBarry Smith    DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2328bb7acecfSBarry Smith     a nonlinear problem to a finer grid
23293d8e3701SJed Brown 
2330bb7acecfSBarry Smith    Logically Collective on coarse
23313d8e3701SJed Brown 
23324165533cSJose E. Roman    Input Parameters:
2333bb7acecfSBarry Smith +  coarse - the `DM` on which to run a hook when restricting to a coarser level
2334bb7acecfSBarry Smith .  refinehook - function to run when setting up a finer level
2335bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels
23363d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
23373d8e3701SJed Brown 
23383d8e3701SJed Brown    Level: advanced
23393d8e3701SJed Brown 
2340bb7acecfSBarry Smith    Note:
23413d8e3701SJed Brown    This function does nothing if the hook is not in the list.
23423d8e3701SJed Brown 
2343bb7acecfSBarry Smith    Fortran Note:
2344bb7acecfSBarry Smith    This function is not available from Fortran.
23453d8e3701SJed Brown 
2346bb7acecfSBarry Smith .seealso: `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
23473d8e3701SJed Brown @*/
2348d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2349d71ae5a4SJacob Faibussowitsch {
23503d8e3701SJed Brown   DMRefineHookLink link, *p;
23513d8e3701SJed Brown 
23523d8e3701SJed Brown   PetscFunctionBegin;
23533d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23543d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
23553d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
23563d8e3701SJed Brown       link = *p;
23573d8e3701SJed Brown       *p   = link->next;
23589566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
23593d8e3701SJed Brown       break;
23603d8e3701SJed Brown     }
23613d8e3701SJed Brown   }
23623d8e3701SJed Brown   PetscFunctionReturn(0);
23633d8e3701SJed Brown }
23643d8e3701SJed Brown 
2365c833c3b5SJed Brown /*@
2366bb7acecfSBarry Smith    DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2367c833c3b5SJed Brown 
2368c833c3b5SJed Brown    Collective if any hooks are
2369c833c3b5SJed Brown 
23704165533cSJose E. Roman    Input Parameters:
2371bb7acecfSBarry Smith +  coarse - coarser `DM` to use as a base
2372bb7acecfSBarry Smith .  interp - interpolation matrix, apply using `MatInterpolate()`
2373bb7acecfSBarry Smith -  fine - finer `DM` to update
2374c833c3b5SJed Brown 
2375c833c3b5SJed Brown    Level: developer
2376c833c3b5SJed Brown 
2377bb7acecfSBarry Smith    Developer Note:
2378bb7acecfSBarry Smith    This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2379bb7acecfSBarry Smith    an API with consistent terminology.
2380bb7acecfSBarry Smith 
2381bb7acecfSBarry Smith .seealso: `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2382c833c3b5SJed Brown @*/
2383d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2384d71ae5a4SJacob Faibussowitsch {
2385c833c3b5SJed Brown   DMRefineHookLink link;
2386c833c3b5SJed Brown 
2387c833c3b5SJed Brown   PetscFunctionBegin;
2388c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
23891baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
23904057135bSMatthew G Knepley   }
239147c6ae99SBarry Smith   PetscFunctionReturn(0);
239247c6ae99SBarry Smith }
239347c6ae99SBarry Smith 
2394eb3f98d2SBarry Smith /*@
23951f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
23961f3379b2SToby Isaac 
2397bb7acecfSBarry Smith    Collective on dm
23981f3379b2SToby Isaac 
23994165533cSJose E. Roman    Input Parameters:
2400bb7acecfSBarry Smith +  coarse - coarse `DM`
2401bb7acecfSBarry Smith .  fine   - fine `DM`
2402bb7acecfSBarry Smith .  interp - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2403bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2404bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24051f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
24061f3379b2SToby Isaac 
24074165533cSJose E. Roman    Output Parameter:
24081f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
24091f3379b2SToby Isaac 
24101f3379b2SToby Isaac    Level: developer
24111f3379b2SToby Isaac 
2412bb7acecfSBarry Smith    Note:
2413bb7acecfSBarry Smith    This function exists because the interpolation of a solution vector between meshes is not always a linear
24141f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24151f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24161f3379b2SToby Isaac    slope-limiting reconstruction.
24171f3379b2SToby Isaac 
2418bb7acecfSBarry Smith    Developer Note:
2419bb7acecfSBarry Smith    This doesn't just interpolate "solutions" so its API name is questionable.
2420bb7acecfSBarry Smith 
2421bb7acecfSBarry Smith .seealso: `DMInterpolate()`, `DMCreateInterpolation()`
24221f3379b2SToby Isaac @*/
2423d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2424d71ae5a4SJacob Faibussowitsch {
24251f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24261f3379b2SToby Isaac 
24271f3379b2SToby Isaac   PetscFunctionBegin;
24281f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24291f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24301f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24311f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24321f3379b2SToby Isaac 
24339566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24341f3379b2SToby Isaac   if (interpsol) {
24359566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24361f3379b2SToby Isaac   } else if (interp) {
24379566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
243898921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24391f3379b2SToby Isaac   PetscFunctionReturn(0);
24401f3379b2SToby Isaac }
24411f3379b2SToby Isaac 
24421f3379b2SToby Isaac /*@
2443bb7acecfSBarry Smith     DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2444eb3f98d2SBarry Smith 
2445eb3f98d2SBarry Smith     Not Collective
2446eb3f98d2SBarry Smith 
2447eb3f98d2SBarry Smith     Input Parameter:
2448bb7acecfSBarry Smith .   dm - the `DM` object
2449eb3f98d2SBarry Smith 
2450eb3f98d2SBarry Smith     Output Parameter:
2451eb3f98d2SBarry Smith .   level - number of refinements
2452eb3f98d2SBarry Smith 
2453eb3f98d2SBarry Smith     Level: developer
2454eb3f98d2SBarry Smith 
2455bb7acecfSBarry Smith     Note:
2456bb7acecfSBarry Smith     This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2457bb7acecfSBarry Smith 
2458bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2459eb3f98d2SBarry Smith @*/
2460d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2461d71ae5a4SJacob Faibussowitsch {
2462eb3f98d2SBarry Smith   PetscFunctionBegin;
2463eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2464eb3f98d2SBarry Smith   *level = dm->levelup;
2465eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2466eb3f98d2SBarry Smith }
2467eb3f98d2SBarry Smith 
2468fef3a512SBarry Smith /*@
2469bb7acecfSBarry Smith     DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2470fef3a512SBarry Smith 
2471fef3a512SBarry Smith     Not Collective
2472fef3a512SBarry Smith 
2473d8d19677SJose E. Roman     Input Parameters:
2474bb7acecfSBarry Smith +   dm - the `DM` object
2475fef3a512SBarry Smith -   level - number of refinements
2476fef3a512SBarry Smith 
2477fef3a512SBarry Smith     Level: advanced
2478fef3a512SBarry Smith 
247995452b02SPatrick Sanan     Notes:
2480bb7acecfSBarry Smith     This value is used by `PCMG` to determine how many multigrid levels to use
2481fef3a512SBarry Smith 
2482bb7acecfSBarry Smith     The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2483bb7acecfSBarry Smith 
2484bb7acecfSBarry Smith .seealso: `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2485fef3a512SBarry Smith @*/
2486d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2487d71ae5a4SJacob Faibussowitsch {
2488fef3a512SBarry Smith   PetscFunctionBegin;
2489fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2490fef3a512SBarry Smith   dm->levelup = level;
2491fef3a512SBarry Smith   PetscFunctionReturn(0);
2492fef3a512SBarry Smith }
2493fef3a512SBarry Smith 
2494d410b0cfSMatthew G. Knepley /*@
2495bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2496d410b0cfSMatthew G. Knepley 
2497d410b0cfSMatthew G. Knepley   Collective on dm
2498d410b0cfSMatthew G. Knepley 
2499f1a722f8SMatthew G. Knepley   Input Parameters:
2500bb7acecfSBarry Smith + dm     - the `DM` object
2501d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2502d410b0cfSMatthew G. Knepley 
2503d410b0cfSMatthew G. Knepley   Output Parameter:
2504bb7acecfSBarry Smith . dme - the extruded `DM`, or NULL
2505d410b0cfSMatthew G. Knepley 
2506bb7acecfSBarry Smith   Note:
2507bb7acecfSBarry Smith   If no extrusion was done, the return value is NULL
2508d410b0cfSMatthew G. Knepley 
2509d410b0cfSMatthew G. Knepley   Level: developer
2510d410b0cfSMatthew G. Knepley 
2511bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2512d410b0cfSMatthew G. Knepley @*/
2513d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2514d71ae5a4SJacob Faibussowitsch {
2515d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2516d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2517dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2518d410b0cfSMatthew G. Knepley   if (*dme) {
2519d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25209566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2521d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25229566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2523d410b0cfSMatthew G. Knepley   }
2524d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2525d410b0cfSMatthew G. Knepley }
2526d410b0cfSMatthew G. Knepley 
2527d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2528d71ae5a4SJacob Faibussowitsch {
2529ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2530ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2531ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2532ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2533ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2534ca3d3a14SMatthew G. Knepley }
2535ca3d3a14SMatthew G. Knepley 
2536d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2537d71ae5a4SJacob Faibussowitsch {
2538ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2539ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2540ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2541ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2542ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2543ca3d3a14SMatthew G. Knepley }
2544ca3d3a14SMatthew G. Knepley 
2545ca3d3a14SMatthew G. Knepley /*@
2546bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2547ca3d3a14SMatthew G. Knepley 
2548ca3d3a14SMatthew G. Knepley   Input Parameter:
2549ca3d3a14SMatthew G. Knepley . dm - The DM
2550ca3d3a14SMatthew G. Knepley 
2551ca3d3a14SMatthew G. Knepley   Output Parameter:
2552ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2553ca3d3a14SMatthew G. Knepley 
2554ca3d3a14SMatthew G. Knepley   Level: developer
2555ca3d3a14SMatthew G. Knepley 
2556bb7acecfSBarry Smith .seealso: `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2557ca3d3a14SMatthew G. Knepley @*/
2558d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2559d71ae5a4SJacob Faibussowitsch {
2560ca3d3a14SMatthew G. Knepley   Vec tv;
2561ca3d3a14SMatthew G. Knepley 
2562ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2563ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2564534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
25659566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2566ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2567ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2568ca3d3a14SMatthew G. Knepley }
2569ca3d3a14SMatthew G. Knepley 
2570d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2571d71ae5a4SJacob Faibussowitsch {
2572ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2573ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2574ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2575ca3d3a14SMatthew G. Knepley 
2576ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
25779566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
25789566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
25799566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
25809566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
25819566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
25829566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
25839566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
25849566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2585ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
25869566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2587ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2588ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2589ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
25909566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2591ca3d3a14SMatthew G. Knepley       if (!dof) continue;
25929566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
25939566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2594ca3d3a14SMatthew G. Knepley     }
2595ca3d3a14SMatthew G. Knepley   }
25969566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
25979566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
25989566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2599ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2600ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26019566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2602ca3d3a14SMatthew G. Knepley       if (dof) {
2603ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2604ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2605ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2606ca3d3a14SMatthew G. Knepley 
2607ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26089566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26099566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26109566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2611ca3d3a14SMatthew G. Knepley       }
2612ca3d3a14SMatthew G. Knepley     }
2613ca3d3a14SMatthew G. Knepley   }
26149566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
2615ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2616ca3d3a14SMatthew G. Knepley }
2617ca3d3a14SMatthew G. Knepley 
2618d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2619d71ae5a4SJacob Faibussowitsch {
2620ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2621ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2622ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2623ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2624ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2625ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2626ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26279566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
2628ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2629ca3d3a14SMatthew G. Knepley }
2630ca3d3a14SMatthew G. Knepley 
2631bb9467b5SJed Brown /*@C
2632bb7acecfSBarry Smith    DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2633baf369e7SPeter Brune 
2634bb7acecfSBarry Smith    Logically Collective on dm
2635baf369e7SPeter Brune 
26364165533cSJose E. Roman    Input Parameters:
2637bb7acecfSBarry Smith +  dm - the `DM`
2638bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2639bb7acecfSBarry Smith .  endhook - function to run after `DMGlobalToLocalEnd()` has completed
26400298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2641baf369e7SPeter Brune 
2642baf369e7SPeter Brune    Calling sequence for beginhook:
2643baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2644baf369e7SPeter Brune 
2645baf369e7SPeter Brune +  dm - global DM
2646baf369e7SPeter Brune .  g - global vector
2647baf369e7SPeter Brune .  mode - mode
2648baf369e7SPeter Brune .  l - local vector
2649baf369e7SPeter Brune -  ctx - optional user-defined function context
2650baf369e7SPeter Brune 
2651baf369e7SPeter Brune    Calling sequence for endhook:
2652ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2653baf369e7SPeter Brune 
2654baf369e7SPeter Brune +  global - global DM
2655baf369e7SPeter Brune -  ctx - optional user-defined function context
2656baf369e7SPeter Brune 
2657baf369e7SPeter Brune    Level: advanced
2658baf369e7SPeter Brune 
2659bb7acecfSBarry Smith    Note:
2660bb7acecfSBarry 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.
2661bb7acecfSBarry Smith 
2662bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2663baf369e7SPeter Brune @*/
2664d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx)
2665d71ae5a4SJacob Faibussowitsch {
2666baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2667baf369e7SPeter Brune 
2668baf369e7SPeter Brune   PetscFunctionBegin;
2669baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2670baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
26719566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2672baf369e7SPeter Brune   link->beginhook = beginhook;
2673baf369e7SPeter Brune   link->endhook   = endhook;
2674baf369e7SPeter Brune   link->ctx       = ctx;
26750298fd71SBarry Smith   link->next      = NULL;
2676baf369e7SPeter Brune   *p              = link;
2677baf369e7SPeter Brune   PetscFunctionReturn(0);
2678baf369e7SPeter Brune }
2679baf369e7SPeter Brune 
2680d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2681d71ae5a4SJacob Faibussowitsch {
26824c274da1SToby Isaac   Mat          cMat;
268379769bd5SJed Brown   Vec          cVec, cBias;
26844c274da1SToby Isaac   PetscSection section, cSec;
26854c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
26864c274da1SToby Isaac 
26874c274da1SToby Isaac   PetscFunctionBegin;
26884c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26899566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
26904c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
26915db9a05bSToby Isaac     PetscInt nRows;
26925db9a05bSToby Isaac 
26939566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
26945db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
26959566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
26969566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
26979566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
26989566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
26999566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27004c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27019566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27024c274da1SToby Isaac       if (dof) {
27034c274da1SToby Isaac         PetscScalar *vals;
27049566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27059566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27064c274da1SToby Isaac       }
27074c274da1SToby Isaac     }
27089566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27094c274da1SToby Isaac   }
27104c274da1SToby Isaac   PetscFunctionReturn(0);
27114c274da1SToby Isaac }
27124c274da1SToby Isaac 
271347c6ae99SBarry Smith /*@
271401729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
271501729b5cSPatrick Sanan 
2716d083f849SBarry Smith     Neighbor-wise Collective on dm
271701729b5cSPatrick Sanan 
271801729b5cSPatrick Sanan     Input Parameters:
2719bb7acecfSBarry Smith +   dm - the `DM` object
272001729b5cSPatrick Sanan .   g - the global vector
2721bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
272201729b5cSPatrick Sanan -   l - the local vector
272301729b5cSPatrick Sanan 
272401729b5cSPatrick Sanan     Notes:
2725bb7acecfSBarry Smith     The communication involved in this update can be overlapped with computation by instead using
2726bb7acecfSBarry Smith     `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2727bb7acecfSBarry Smith 
2728bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
272901729b5cSPatrick Sanan 
273001729b5cSPatrick Sanan     Level: beginner
273101729b5cSPatrick Sanan 
2732bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
2733bb7acecfSBarry Smith           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`,
2734bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
273501729b5cSPatrick Sanan @*/
2736d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2737d71ae5a4SJacob Faibussowitsch {
273801729b5cSPatrick Sanan   PetscFunctionBegin;
27399566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27409566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
274101729b5cSPatrick Sanan   PetscFunctionReturn(0);
274201729b5cSPatrick Sanan }
274301729b5cSPatrick Sanan 
274401729b5cSPatrick Sanan /*@
274547c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
274647c6ae99SBarry Smith 
2747d083f849SBarry Smith     Neighbor-wise Collective on dm
274847c6ae99SBarry Smith 
274947c6ae99SBarry Smith     Input Parameters:
2750bb7acecfSBarry Smith +   dm - the `DM` object
275147c6ae99SBarry Smith .   g - the global vector
2752bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
275347c6ae99SBarry Smith -   l - the local vector
275447c6ae99SBarry Smith 
275501729b5cSPatrick Sanan     Level: intermediate
275647c6ae99SBarry Smith 
2757bb7acecfSBarry Smith     Notes:
2758bb7acecfSBarry Smith     The operation is completed with `DMGlobalToLocalEnd()`
2759bb7acecfSBarry Smith 
2760bb7acecfSBarry Smith     One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2761bb7acecfSBarry Smith 
2762bb7acecfSBarry Smith     `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2763bb7acecfSBarry Smith 
2764bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2765bb7acecfSBarry Smith 
2766bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
276747c6ae99SBarry Smith @*/
2768d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2769d71ae5a4SJacob Faibussowitsch {
27707128ae9fSMatthew G Knepley   PetscSF                 sf;
2771baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
277247c6ae99SBarry Smith 
277347c6ae99SBarry Smith   PetscFunctionBegin;
2774171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2775baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
27761baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2777baf369e7SPeter Brune   }
27789566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
27797128ae9fSMatthew G Knepley   if (sf) {
2780ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2781ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2782d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
27837128ae9fSMatthew G Knepley 
27847a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
27859566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
27869566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
27879566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
27889566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
27899566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
27907128ae9fSMatthew G Knepley   } else {
27919566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
27927128ae9fSMatthew G Knepley   }
279347c6ae99SBarry Smith   PetscFunctionReturn(0);
279447c6ae99SBarry Smith }
279547c6ae99SBarry Smith 
279647c6ae99SBarry Smith /*@
279747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
279847c6ae99SBarry Smith 
2799d083f849SBarry Smith     Neighbor-wise Collective on dm
280047c6ae99SBarry Smith 
280147c6ae99SBarry Smith     Input Parameters:
2802bb7acecfSBarry Smith +   dm - the `DM` object
280347c6ae99SBarry Smith .   g - the global vector
2804bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
280547c6ae99SBarry Smith -   l - the local vector
280647c6ae99SBarry Smith 
280701729b5cSPatrick Sanan     Level: intermediate
280847c6ae99SBarry Smith 
2809bb7acecfSBarry Smith     Note:
2810bb7acecfSBarry Smith     See `DMGlobalToLocalBegin()` for details.
2811bb7acecfSBarry Smith 
2812bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
281347c6ae99SBarry Smith @*/
2814d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2815d71ae5a4SJacob Faibussowitsch {
28167128ae9fSMatthew G Knepley   PetscSF                 sf;
2817ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2818ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2819ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2820baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2821d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
282247c6ae99SBarry Smith 
282347c6ae99SBarry Smith   PetscFunctionBegin;
2824171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28259566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28269566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28277128ae9fSMatthew G Knepley   if (sf) {
28287a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28297128ae9fSMatthew G Knepley 
28309566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28319566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28329566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28339566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28349566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28359566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28367128ae9fSMatthew G Knepley   } else {
28379566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28387128ae9fSMatthew G Knepley   }
28399566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2840baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28419566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2842baf369e7SPeter Brune   }
284347c6ae99SBarry Smith   PetscFunctionReturn(0);
284447c6ae99SBarry Smith }
284547c6ae99SBarry Smith 
2846d4d07f1eSToby Isaac /*@C
2847d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2848d4d07f1eSToby Isaac 
2849bb7acecfSBarry Smith    Logically Collective on dm
2850d4d07f1eSToby Isaac 
28514165533cSJose E. Roman    Input Parameters:
2852bb7acecfSBarry Smith +  dm - the `DM`
2853bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2854bb7acecfSBarry Smith .  endhook - function to run after `DMLocalToGlobalEnd()` has completed
2855d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2856d4d07f1eSToby Isaac 
2857d4d07f1eSToby Isaac    Calling sequence for beginhook:
2858d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2859d4d07f1eSToby Isaac 
2860bb7acecfSBarry Smith +  dm - global `DM`
2861d4d07f1eSToby Isaac .  l - local vector
2862d4d07f1eSToby Isaac .  mode - mode
2863d4d07f1eSToby Isaac .  g - global vector
2864d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2865d4d07f1eSToby Isaac 
2866d4d07f1eSToby Isaac    Calling sequence for endhook:
2867d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2868d4d07f1eSToby Isaac 
2869bb7acecfSBarry Smith +  global - global `DM`
2870d4d07f1eSToby Isaac .  l - local vector
2871d4d07f1eSToby Isaac .  mode - mode
2872d4d07f1eSToby Isaac .  g - global vector
2873d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2874d4d07f1eSToby Isaac 
2875d4d07f1eSToby Isaac    Level: advanced
2876d4d07f1eSToby Isaac 
2877bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2878d4d07f1eSToby Isaac @*/
2879d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx)
2880d71ae5a4SJacob Faibussowitsch {
2881d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2882d4d07f1eSToby Isaac 
2883d4d07f1eSToby Isaac   PetscFunctionBegin;
2884d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2885d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
28869566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2887d4d07f1eSToby Isaac   link->beginhook = beginhook;
2888d4d07f1eSToby Isaac   link->endhook   = endhook;
2889d4d07f1eSToby Isaac   link->ctx       = ctx;
2890d4d07f1eSToby Isaac   link->next      = NULL;
2891d4d07f1eSToby Isaac   *p              = link;
2892d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2893d4d07f1eSToby Isaac }
2894d4d07f1eSToby Isaac 
2895d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2896d71ae5a4SJacob Faibussowitsch {
28974c274da1SToby Isaac   Mat          cMat;
28984c274da1SToby Isaac   Vec          cVec;
28994c274da1SToby Isaac   PetscSection section, cSec;
29004c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
29014c274da1SToby Isaac 
29024c274da1SToby Isaac   PetscFunctionBegin;
29034c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29049566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29054c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
29065db9a05bSToby Isaac     PetscInt nRows;
29075db9a05bSToby Isaac 
29089566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29095db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
29109566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29119566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29129566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29134c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29149566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29154c274da1SToby Isaac       if (dof) {
29164c274da1SToby Isaac         PetscInt     d;
29174c274da1SToby Isaac         PetscScalar *vals;
29189566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29199566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29204c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29214c274da1SToby Isaac          * we just extracted */
2922ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29234c274da1SToby Isaac       }
29244c274da1SToby Isaac     }
29259566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29269566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29274c274da1SToby Isaac   }
29284c274da1SToby Isaac   PetscFunctionReturn(0);
29294c274da1SToby Isaac }
293001729b5cSPatrick Sanan /*@
293101729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
293201729b5cSPatrick Sanan 
2933d083f849SBarry Smith     Neighbor-wise Collective on dm
293401729b5cSPatrick Sanan 
293501729b5cSPatrick Sanan     Input Parameters:
2936bb7acecfSBarry Smith +   dm - the `DM` object
293701729b5cSPatrick Sanan .   l - the local vector
2938bb7acecfSBarry 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.
293901729b5cSPatrick Sanan -   g - the global vector
294001729b5cSPatrick Sanan 
294101729b5cSPatrick Sanan     Notes:
294201729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
2943bb7acecfSBarry Smith     `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
294401729b5cSPatrick Sanan 
2945bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2946bb7acecfSBarry Smith 
2947bb7acecfSBarry 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.
2948bb7acecfSBarry Smith 
2949bb7acecfSBarry Smith     Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
295001729b5cSPatrick Sanan 
295101729b5cSPatrick Sanan     Level: beginner
295201729b5cSPatrick Sanan 
2953bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`,  `DMGlobaToLocallHookAdd()`
295401729b5cSPatrick Sanan @*/
2955d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
2956d71ae5a4SJacob Faibussowitsch {
295701729b5cSPatrick Sanan   PetscFunctionBegin;
29589566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
29599566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
296001729b5cSPatrick Sanan   PetscFunctionReturn(0);
296101729b5cSPatrick Sanan }
29624c274da1SToby Isaac 
296347c6ae99SBarry Smith /*@
296401729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
29659a42bb27SBarry Smith 
2966d083f849SBarry Smith     Neighbor-wise Collective on dm
29679a42bb27SBarry Smith 
29689a42bb27SBarry Smith     Input Parameters:
2969bb7acecfSBarry Smith +   dm - the `DM` object
2970f6813fd5SJed Brown .   l - the local vector
2971bb7acecfSBarry Smith .   mode - `if INSERT_VALUES` then no parallel communication is used, if `ADD_VALUES` then all ghost points from the same base point accumulate into that base point.
29721eb28f2eSBarry Smith -   g - the global vector
29739a42bb27SBarry Smith 
297495452b02SPatrick Sanan     Notes:
2975bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2976bb7acecfSBarry Smith 
2977bb7acecfSBarry 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.
2978bb7acecfSBarry Smith 
2979bb7acecfSBarry Smith     Use `DMLocalToGlobalEnd()` to complete the communication process.
2980bb7acecfSBarry Smith 
2981bb7acecfSBarry Smith     `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
2982bb7acecfSBarry Smith 
2983bb7acecfSBarry Smith     `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
29849a42bb27SBarry Smith 
298501729b5cSPatrick Sanan     Level: intermediate
29869a42bb27SBarry Smith 
2987bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
29889a42bb27SBarry Smith @*/
2989d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
2990d71ae5a4SJacob Faibussowitsch {
29917128ae9fSMatthew G Knepley   PetscSF                 sf;
299284330215SMatthew G. Knepley   PetscSection            s, gs;
2993d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2994ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2995ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2996ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2997fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
2998d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
29999a42bb27SBarry Smith 
30009a42bb27SBarry Smith   PetscFunctionBegin;
3001171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3002d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30031baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3004d4d07f1eSToby Isaac   }
30059566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30069566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30079566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30087128ae9fSMatthew G Knepley   switch (mode) {
30097128ae9fSMatthew G Knepley   case INSERT_VALUES:
30107128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3011d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3012d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3013d71ae5a4SJacob Faibussowitsch     break;
30147128ae9fSMatthew G Knepley   case ADD_VALUES:
30157128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3016d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3017d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3018d71ae5a4SJacob Faibussowitsch     break;
3019d71ae5a4SJacob Faibussowitsch   default:
3020d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30217128ae9fSMatthew G Knepley   }
3022ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30239566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3024ca3d3a14SMatthew G. Knepley     if (transform) {
30259566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30269566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30279566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30289566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3029fa88e482SJed Brown     } else if (isInsert) {
30309566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3031fa88e482SJed Brown     } else {
30329566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3033fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3034ca3d3a14SMatthew G. Knepley     }
3035fa88e482SJed Brown     if (s && isInsert) {
30369566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3037fa88e482SJed Brown     } else {
30389566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3039fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3040fa88e482SJed Brown     }
3041ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30429566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
304384330215SMatthew G. Knepley     } else if (s && isInsert) {
304484330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
304584330215SMatthew G. Knepley 
30469566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
30479566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
30489566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
304984330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3050b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
305184330215SMatthew G. Knepley 
30529566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
30539566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
30549566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
30559566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
30569566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
30579566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3058b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
305903442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
30607a8be351SBarry 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);
3061b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3062b3b16f48SMatthew G. Knepley         if (!gcdof) {
306384330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3064b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3065b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
306684330215SMatthew G. Knepley           const PetscInt *cdofs;
306784330215SMatthew G. Knepley           PetscInt        cind = 0;
306884330215SMatthew G. Knepley 
30699566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3070b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
30719371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
30729371c9d4SSatish Balay               ++cind;
30739371c9d4SSatish Balay               continue;
30749371c9d4SSatish Balay             }
3075b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
307684330215SMatthew G. Knepley           }
30777a8be351SBarry 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);
307884330215SMatthew G. Knepley       }
3079ca3d3a14SMatthew G. Knepley     }
3080fa88e482SJed Brown     if (g_inplace) {
30819566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3082fa88e482SJed Brown     } else {
30839566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3084fa88e482SJed Brown     }
3085ca3d3a14SMatthew G. Knepley     if (transform) {
30869566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
30879566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3088fa88e482SJed Brown     } else if (l_inplace) {
30899566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3090ca3d3a14SMatthew G. Knepley     } else {
30919566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3092ca3d3a14SMatthew G. Knepley     }
30937128ae9fSMatthew G Knepley   } else {
30949566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
30957128ae9fSMatthew G Knepley   }
30969a42bb27SBarry Smith   PetscFunctionReturn(0);
30979a42bb27SBarry Smith }
30989a42bb27SBarry Smith 
30999a42bb27SBarry Smith /*@
31009a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
310147c6ae99SBarry Smith 
3102d083f849SBarry Smith     Neighbor-wise Collective on dm
310347c6ae99SBarry Smith 
310447c6ae99SBarry Smith     Input Parameters:
3105bb7acecfSBarry Smith +   dm - the `DM` object
3106f6813fd5SJed Brown .   l - the local vector
3107bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
3108f6813fd5SJed Brown -   g - the global vector
310947c6ae99SBarry Smith 
311001729b5cSPatrick Sanan     Level: intermediate
311147c6ae99SBarry Smith 
3112bb7acecfSBarry Smith     Note:
3113bb7acecfSBarry Smith     See `DMLocalToGlobalBegin()` for full details
3114bb7acecfSBarry Smith 
3115bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalEnd()`
311647c6ae99SBarry Smith @*/
3117d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3118d71ae5a4SJacob Faibussowitsch {
31197128ae9fSMatthew G Knepley   PetscSF                 sf;
312084330215SMatthew G. Knepley   PetscSection            s;
3121d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3122ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
312347c6ae99SBarry Smith 
312447c6ae99SBarry Smith   PetscFunctionBegin;
3125171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31269566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31279566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31287128ae9fSMatthew G Knepley   switch (mode) {
31297128ae9fSMatthew G Knepley   case INSERT_VALUES:
3130d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3131d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3132d71ae5a4SJacob Faibussowitsch     break;
31337128ae9fSMatthew G Knepley   case ADD_VALUES:
3134d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3135d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3136d71ae5a4SJacob Faibussowitsch     break;
3137d71ae5a4SJacob Faibussowitsch   default:
3138d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31397128ae9fSMatthew G Knepley   }
314084330215SMatthew G. Knepley   if (sf && !isInsert) {
3141ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3142ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3143ca3d3a14SMatthew G. Knepley     Vec                tmpl;
314484330215SMatthew G. Knepley 
31459566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3146ca3d3a14SMatthew G. Knepley     if (transform) {
31479566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31489566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3149ca3d3a14SMatthew G. Knepley     } else {
31509566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3151ca3d3a14SMatthew G. Knepley     }
31529566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
31539566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3154ca3d3a14SMatthew G. Knepley     if (transform) {
31559566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31569566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3157ca3d3a14SMatthew G. Knepley     } else {
31589566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3159ca3d3a14SMatthew G. Knepley     }
31609566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
316184330215SMatthew G. Knepley   } else if (s && isInsert) {
31627128ae9fSMatthew G Knepley   } else {
31639566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
31647128ae9fSMatthew G Knepley   }
3165d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
31669566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3167d4d07f1eSToby Isaac   }
316847c6ae99SBarry Smith   PetscFunctionReturn(0);
316947c6ae99SBarry Smith }
317047c6ae99SBarry Smith 
3171f089877aSRichard Tran Mills /*@
3172bb7acecfSBarry Smith    DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include ghost points
3173bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3174bb7acecfSBarry Smith    points in the second are set correctly from values on other MPI ranks. Must be followed by `DMLocalToLocalEnd()`.
3175f089877aSRichard Tran Mills 
3176d083f849SBarry Smith    Neighbor-wise Collective on dm
3177f089877aSRichard Tran Mills 
3178f089877aSRichard Tran Mills    Input Parameters:
3179bb7acecfSBarry Smith +  dm - the `DM` object
3180bc0a1609SRichard Tran Mills .  g - the original local vector
3181bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3182f089877aSRichard Tran Mills 
3183bc0a1609SRichard Tran Mills    Output Parameter:
3184bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3185f089877aSRichard Tran Mills 
3186f089877aSRichard Tran Mills    Level: intermediate
3187f089877aSRichard Tran Mills 
3188bb7acecfSBarry Smith .seealso: `DMLocalToLocalEnd(), `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalEnd()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3189f089877aSRichard Tran Mills @*/
3190d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3191d71ae5a4SJacob Faibussowitsch {
3192f089877aSRichard Tran Mills   PetscFunctionBegin;
3193f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31949f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
31959f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
31969f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
3197f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3198f089877aSRichard Tran Mills }
3199f089877aSRichard Tran Mills 
3200f089877aSRichard Tran Mills /*@
3201bb7acecfSBarry Smith    DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3202bb7acecfSBarry Smith    points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3203f089877aSRichard Tran Mills 
3204d083f849SBarry Smith    Neighbor-wise Collective on dm
3205f089877aSRichard Tran Mills 
3206f089877aSRichard Tran Mills    Input Parameters:
3207bb7acecfSBarry Smith +  da - the `DM` object
3208bc0a1609SRichard Tran Mills .  g - the original local vector
3209bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3210f089877aSRichard Tran Mills 
3211bc0a1609SRichard Tran Mills    Output Parameter:
3212bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3213f089877aSRichard Tran Mills 
3214f089877aSRichard Tran Mills    Level: intermediate
3215f089877aSRichard Tran Mills 
3216bb7acecfSBarry Smith .seealso: `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalBegin()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3217f089877aSRichard Tran Mills @*/
3218d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3219d71ae5a4SJacob Faibussowitsch {
3220f089877aSRichard Tran Mills   PetscFunctionBegin;
3221f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32229f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32239f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32249f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
3225f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3226f089877aSRichard Tran Mills }
3227f089877aSRichard Tran Mills 
322847c6ae99SBarry Smith /*@
3229bb7acecfSBarry Smith     DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
323047c6ae99SBarry Smith 
3231d083f849SBarry Smith     Collective on dm
323247c6ae99SBarry Smith 
3233d8d19677SJose E. Roman     Input Parameters:
3234bb7acecfSBarry Smith +   dm - the `DM` object
3235bb7acecfSBarry Smith -   comm - the communicator to contain the new `DM` object (or MPI_COMM_NULL)
323647c6ae99SBarry Smith 
323747c6ae99SBarry Smith     Output Parameter:
3238bb7acecfSBarry Smith .   dmc - the coarsened `DM`
323947c6ae99SBarry Smith 
324047c6ae99SBarry Smith     Level: developer
324147c6ae99SBarry Smith 
3242bb7acecfSBarry Smith .seealso: `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
324347c6ae99SBarry Smith @*/
3244d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3245d71ae5a4SJacob Faibussowitsch {
3246b17ce1afSJed Brown   DMCoarsenHookLink link;
324747c6ae99SBarry Smith 
324847c6ae99SBarry Smith   PetscFunctionBegin;
3249171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32509566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3251dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3252b9d85ea2SLisandro Dalcin   if (*dmc) {
3253a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
32549566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
325543842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
32569566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3257644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
32580598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3259656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
32609566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3261b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
32629566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3263b17ce1afSJed Brown     }
3264b9d85ea2SLisandro Dalcin   }
32659566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
32667a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3267b17ce1afSJed Brown   PetscFunctionReturn(0);
3268b17ce1afSJed Brown }
3269b17ce1afSJed Brown 
3270bb9467b5SJed Brown /*@C
3271b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3272b17ce1afSJed Brown 
3273bb7acecfSBarry Smith    Logically Collective on fine
3274b17ce1afSJed Brown 
32754165533cSJose E. Roman    Input Parameters:
3276bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3277b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3278bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
32790298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3280b17ce1afSJed Brown 
3281b17ce1afSJed Brown    Calling sequence of coarsenhook:
3282b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3283b17ce1afSJed Brown 
3284bb7acecfSBarry Smith +  fine - fine level `DM`
3285bb7acecfSBarry Smith .  coarse - coarse level `DM` to restrict problem to
3286b17ce1afSJed Brown -  ctx - optional user-defined function context
3287b17ce1afSJed Brown 
3288b17ce1afSJed Brown    Calling sequence for restricthook:
3289c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3290bb7acecfSBarry Smith $
3291bb7acecfSBarry Smith +  fine - fine level `DM`
3292bb7acecfSBarry Smith .  mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3293c833c3b5SJed Brown .  rscale - scaling vector for restriction
3294c833c3b5SJed Brown .  inject - matrix restricting by injection
3295b17ce1afSJed Brown .  coarse - coarse level DM to update
3296b17ce1afSJed Brown -  ctx - optional user-defined function context
3297b17ce1afSJed Brown 
3298b17ce1afSJed Brown    Level: advanced
3299b17ce1afSJed Brown 
3300b17ce1afSJed Brown    Notes:
3301bb7acecfSBarry 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`.
3302b17ce1afSJed Brown 
3303b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3304b17ce1afSJed Brown 
3305b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3306bb7acecfSBarry Smith    extract the finest level information from its context (instead of from the `SNES`).
3307b17ce1afSJed Brown 
3308bb7acecfSBarry Smith    The hooks are automatically called by `DMRestrict()`
3309bb7acecfSBarry Smith 
3310bb7acecfSBarry Smith    Fortran Note:
3311bb7acecfSBarry Smith    This function is not available from Fortran.
3312bb9467b5SJed Brown 
3313db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3314b17ce1afSJed Brown @*/
3315d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3316d71ae5a4SJacob Faibussowitsch {
3317b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3318b17ce1afSJed Brown 
3319b17ce1afSJed Brown   PetscFunctionBegin;
3320b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33211e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33221e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
33231e3d8eccSJed Brown   }
33249566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3325b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3326b17ce1afSJed Brown   link->restricthook = restricthook;
3327b17ce1afSJed Brown   link->ctx          = ctx;
33280298fd71SBarry Smith   link->next         = NULL;
3329b17ce1afSJed Brown   *p                 = link;
3330b17ce1afSJed Brown   PetscFunctionReturn(0);
3331b17ce1afSJed Brown }
3332b17ce1afSJed Brown 
3333dc822a44SJed Brown /*@C
3334bb7acecfSBarry Smith    DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3335dc822a44SJed Brown 
3336bb7acecfSBarry Smith    Logically Collective on fine
3337dc822a44SJed Brown 
33384165533cSJose E. Roman    Input Parameters:
3339bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3340dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3341bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels
3342dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3343dc822a44SJed Brown 
3344dc822a44SJed Brown    Level: advanced
3345dc822a44SJed Brown 
3346bb7acecfSBarry Smith    Note:
3347dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3348dc822a44SJed Brown 
3349bb7acecfSBarry Smith    Fortran Note:
3350bb7acecfSBarry Smith    This function is not available from Fortran.
3351dc822a44SJed Brown 
3352db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3353dc822a44SJed Brown @*/
3354d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3355d71ae5a4SJacob Faibussowitsch {
3356dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3357dc822a44SJed Brown 
3358dc822a44SJed Brown   PetscFunctionBegin;
3359dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3360dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3361dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3362dc822a44SJed Brown       link = *p;
3363dc822a44SJed Brown       *p   = link->next;
33649566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3365dc822a44SJed Brown       break;
3366dc822a44SJed Brown     }
3367dc822a44SJed Brown   }
3368dc822a44SJed Brown   PetscFunctionReturn(0);
3369dc822a44SJed Brown }
3370dc822a44SJed Brown 
3371b17ce1afSJed Brown /*@
3372bb7acecfSBarry Smith    DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3373b17ce1afSJed Brown 
3374b17ce1afSJed Brown    Collective if any hooks are
3375b17ce1afSJed Brown 
33764165533cSJose E. Roman    Input Parameters:
3377bb7acecfSBarry Smith +  fine - finer `DM` from which the data is obtained
3378bb7acecfSBarry Smith .  restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3379e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3380bb7acecfSBarry Smith .  inject - injection matrix, also use `MatRestrict()`
3381e91eccc2SStefano Zampini -  coarse - coarser DM to update
3382b17ce1afSJed Brown 
3383b17ce1afSJed Brown    Level: developer
3384b17ce1afSJed Brown 
3385bb7acecfSBarry Smith    Developer Note:
3386bb7acecfSBarry Smith    Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3387bb7acecfSBarry Smith 
3388bb7acecfSBarry Smith .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3389b17ce1afSJed Brown @*/
3390d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3391d71ae5a4SJacob Faibussowitsch {
3392b17ce1afSJed Brown   DMCoarsenHookLink link;
3393b17ce1afSJed Brown 
3394b17ce1afSJed Brown   PetscFunctionBegin;
3395b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
33961baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3397b17ce1afSJed Brown   }
339847c6ae99SBarry Smith   PetscFunctionReturn(0);
339947c6ae99SBarry Smith }
340047c6ae99SBarry Smith 
3401bb9467b5SJed Brown /*@C
3402be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
34035dbd56e3SPeter Brune 
3404d083f849SBarry Smith    Logically Collective on global
34055dbd56e3SPeter Brune 
34064165533cSJose E. Roman    Input Parameters:
3407bb7acecfSBarry Smith +  global - global `DM`
3408bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
34095dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
34100298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
34115dbd56e3SPeter Brune 
3412ec4806b8SPeter Brune    Calling sequence for ddhook:
3413ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3414ec4806b8SPeter Brune 
3415bb7acecfSBarry Smith +  global - global `DM`
3416bb7acecfSBarry Smith .  block  - block `DM`
3417ec4806b8SPeter Brune -  ctx - optional user-defined function context
3418ec4806b8SPeter Brune 
34195dbd56e3SPeter Brune    Calling sequence for restricthook:
3420ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
34215dbd56e3SPeter Brune 
3422bb7acecfSBarry Smith +  global - global `DM`
34235dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
34245dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3425bb7acecfSBarry Smith .  block  - block `DM`
34265dbd56e3SPeter Brune -  ctx - optional user-defined function context
34275dbd56e3SPeter Brune 
34285dbd56e3SPeter Brune    Level: advanced
34295dbd56e3SPeter Brune 
34305dbd56e3SPeter Brune    Notes:
3431bb7acecfSBarry Smith    This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
34325dbd56e3SPeter Brune 
34335dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
34345dbd56e3SPeter Brune 
34355dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3436bb7acecfSBarry Smith    extract the global information from its context (instead of from the `SNES`).
34375dbd56e3SPeter Brune 
3438bb7acecfSBarry Smith    Fortran Note:
3439bb7acecfSBarry Smith    This function is not available from Fortran.
3440bb9467b5SJed Brown 
3441bb7acecfSBarry Smith .seealso: `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
34425dbd56e3SPeter Brune @*/
3443d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3444d71ae5a4SJacob Faibussowitsch {
3445be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34465dbd56e3SPeter Brune 
34475dbd56e3SPeter Brune   PetscFunctionBegin;
34485dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3449b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
3450b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3451b3a6b972SJed Brown   }
34529566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
34535dbd56e3SPeter Brune   link->restricthook = restricthook;
3454be081cd6SPeter Brune   link->ddhook       = ddhook;
34555dbd56e3SPeter Brune   link->ctx          = ctx;
34560298fd71SBarry Smith   link->next         = NULL;
34575dbd56e3SPeter Brune   *p                 = link;
34585dbd56e3SPeter Brune   PetscFunctionReturn(0);
34595dbd56e3SPeter Brune }
34605dbd56e3SPeter Brune 
3461b3a6b972SJed Brown /*@C
3462b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3463b3a6b972SJed Brown 
3464bb7acecfSBarry Smith    Logically Collective on global
3465b3a6b972SJed Brown 
34664165533cSJose E. Roman    Input Parameters:
3467bb7acecfSBarry Smith +  global - global `DM`
3468bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
3469b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3470b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3471b3a6b972SJed Brown 
3472b3a6b972SJed Brown    Level: advanced
3473b3a6b972SJed Brown 
3474bb7acecfSBarry Smith    Fortran Note:
3475bb7acecfSBarry Smith    This function is not available from Fortran.
3476b3a6b972SJed Brown 
3477db781477SPatrick Sanan .seealso: `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3478b3a6b972SJed Brown @*/
3479d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3480d71ae5a4SJacob Faibussowitsch {
3481b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3482b3a6b972SJed Brown 
3483b3a6b972SJed Brown   PetscFunctionBegin;
3484b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3485b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3486b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3487b3a6b972SJed Brown       link = *p;
3488b3a6b972SJed Brown       *p   = link->next;
34899566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3490b3a6b972SJed Brown       break;
3491b3a6b972SJed Brown     }
3492b3a6b972SJed Brown   }
3493b3a6b972SJed Brown   PetscFunctionReturn(0);
3494b3a6b972SJed Brown }
3495b3a6b972SJed Brown 
34965dbd56e3SPeter Brune /*@
3497bb7acecfSBarry Smith    DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
34985dbd56e3SPeter Brune 
34995dbd56e3SPeter Brune    Collective if any hooks are
35005dbd56e3SPeter Brune 
35014165533cSJose E. Roman    Input Parameters:
3502bb7acecfSBarry Smith +  fine - finer `DM` to use as a base
3503be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3504be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
3505bb7acecfSBarry Smith -  coarse - coarser `DM` to update
35065dbd56e3SPeter Brune 
35075dbd56e3SPeter Brune    Level: developer
35085dbd56e3SPeter Brune 
3509db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
35105dbd56e3SPeter Brune @*/
3511d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3512d71ae5a4SJacob Faibussowitsch {
3513be081cd6SPeter Brune   DMSubDomainHookLink link;
35145dbd56e3SPeter Brune 
35155dbd56e3SPeter Brune   PetscFunctionBegin;
3516be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35171baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35185dbd56e3SPeter Brune   }
35195dbd56e3SPeter Brune   PetscFunctionReturn(0);
35205dbd56e3SPeter Brune }
35215dbd56e3SPeter Brune 
35225fe1f584SPeter Brune /*@
3523bb7acecfSBarry Smith     DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35245fe1f584SPeter Brune 
35255fe1f584SPeter Brune     Not Collective
35265fe1f584SPeter Brune 
35275fe1f584SPeter Brune     Input Parameter:
3528bb7acecfSBarry Smith .   dm - the `DM` object
35295fe1f584SPeter Brune 
35305fe1f584SPeter Brune     Output Parameter:
35316a7d9d85SPeter Brune .   level - number of coarsenings
35325fe1f584SPeter Brune 
35335fe1f584SPeter Brune     Level: developer
35345fe1f584SPeter Brune 
3535bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35365fe1f584SPeter Brune @*/
3537d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3538d71ae5a4SJacob Faibussowitsch {
35395fe1f584SPeter Brune   PetscFunctionBegin;
35405fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3541b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level, 2);
35425fe1f584SPeter Brune   *level = dm->leveldown;
35435fe1f584SPeter Brune   PetscFunctionReturn(0);
35445fe1f584SPeter Brune }
35455fe1f584SPeter Brune 
35469a64c4a8SMatthew G. Knepley /*@
3547bb7acecfSBarry Smith     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
35489a64c4a8SMatthew G. Knepley 
3549bb7acecfSBarry Smith     Collective on dm
35509a64c4a8SMatthew G. Knepley 
35519a64c4a8SMatthew G. Knepley     Input Parameters:
3552bb7acecfSBarry Smith +   dm - the `DM` object
35539a64c4a8SMatthew G. Knepley -   level - number of coarsenings
35549a64c4a8SMatthew G. Knepley 
35559a64c4a8SMatthew G. Knepley     Level: developer
35569a64c4a8SMatthew G. Knepley 
3557bb7acecfSBarry Smith     Note:
3558bb7acecfSBarry Smith     This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3559bb7acecfSBarry Smith 
3560bb7acecfSBarry Smith .seealso: `DMSetCoarsenLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35619a64c4a8SMatthew G. Knepley @*/
3562d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3563d71ae5a4SJacob Faibussowitsch {
35649a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
35659a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35669a64c4a8SMatthew G. Knepley   dm->leveldown = level;
35679a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
35689a64c4a8SMatthew G. Knepley }
35699a64c4a8SMatthew G. Knepley 
357047c6ae99SBarry Smith /*@C
3571bb7acecfSBarry Smith     DMRefineHierarchy - Refines a `DM` object, all levels at once
357247c6ae99SBarry Smith 
3573d083f849SBarry Smith     Collective on dm
357447c6ae99SBarry Smith 
3575d8d19677SJose E. Roman     Input Parameters:
3576bb7acecfSBarry Smith +   dm - the `DM` object
357747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
357847c6ae99SBarry Smith 
357947c6ae99SBarry Smith     Output Parameter:
3580bb7acecfSBarry Smith .   dmf - the refined `DM` hierarchy
358147c6ae99SBarry Smith 
358247c6ae99SBarry Smith     Level: developer
358347c6ae99SBarry Smith 
3584bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
358547c6ae99SBarry Smith @*/
3586d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3587d71ae5a4SJacob Faibussowitsch {
358847c6ae99SBarry Smith   PetscFunctionBegin;
3589171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35907a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
359147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3592b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf, 3);
359347c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
3594dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
359547c6ae99SBarry Smith   } else if (dm->ops->refine) {
359647c6ae99SBarry Smith     PetscInt i;
359747c6ae99SBarry Smith 
35989566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
359948a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3600ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No RefineHierarchy for this DM yet");
360147c6ae99SBarry Smith   PetscFunctionReturn(0);
360247c6ae99SBarry Smith }
360347c6ae99SBarry Smith 
360447c6ae99SBarry Smith /*@C
3605bb7acecfSBarry Smith     DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
360647c6ae99SBarry Smith 
3607d083f849SBarry Smith     Collective on dm
360847c6ae99SBarry Smith 
3609d8d19677SJose E. Roman     Input Parameters:
3610bb7acecfSBarry Smith +   dm - the `DM` object
361147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
361247c6ae99SBarry Smith 
361347c6ae99SBarry Smith     Output Parameter:
3614bb7acecfSBarry Smith .   dmc - the coarsened `DM` hierarchy
361547c6ae99SBarry Smith 
361647c6ae99SBarry Smith     Level: developer
361747c6ae99SBarry Smith 
3618bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
361947c6ae99SBarry Smith @*/
3620d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3621d71ae5a4SJacob Faibussowitsch {
362247c6ae99SBarry Smith   PetscFunctionBegin;
3623171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36247a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
362547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
362647c6ae99SBarry Smith   PetscValidPointer(dmc, 3);
362747c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
3628dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
362947c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
363047c6ae99SBarry Smith     PetscInt i;
363147c6ae99SBarry Smith 
36329566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
363348a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3634ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No CoarsenHierarchy for this DM yet");
363547c6ae99SBarry Smith   PetscFunctionReturn(0);
363647c6ae99SBarry Smith }
363747c6ae99SBarry Smith 
36381a266240SBarry Smith /*@C
3639bb7acecfSBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36401a266240SBarry Smith 
3641bb7acecfSBarry Smith     Logically Collective if the function is collective
36421a266240SBarry Smith 
36431a266240SBarry Smith     Input Parameters:
3644bb7acecfSBarry Smith +   dm - the `DM` object
36451a266240SBarry Smith -   destroy - the destroy function
36461a266240SBarry Smith 
36471a266240SBarry Smith     Level: intermediate
36481a266240SBarry Smith 
3649bb7acecfSBarry Smith .seealso: `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3650f07f9ceaSJed Brown @*/
3651d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3652d71ae5a4SJacob Faibussowitsch {
36531a266240SBarry Smith   PetscFunctionBegin;
3654171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36551a266240SBarry Smith   dm->ctxdestroy = destroy;
36561a266240SBarry Smith   PetscFunctionReturn(0);
36571a266240SBarry Smith }
36581a266240SBarry Smith 
3659b07ff414SBarry Smith /*@
3660bb7acecfSBarry Smith     DMSetApplicationContext - Set a user context into a `DM` object
366147c6ae99SBarry Smith 
366247c6ae99SBarry Smith     Not Collective
366347c6ae99SBarry Smith 
366447c6ae99SBarry Smith     Input Parameters:
3665bb7acecfSBarry Smith +   dm - the `DM` object
366647c6ae99SBarry Smith -   ctx - the user context
366747c6ae99SBarry Smith 
366847c6ae99SBarry Smith     Level: intermediate
366947c6ae99SBarry Smith 
3670bb7acecfSBarry Smith     Note:
3671bb7acecfSBarry Smith     A user context is a way to pass problem specific information that is accessable whenever the `DM` is available
3672bb7acecfSBarry Smith 
3673bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
367447c6ae99SBarry Smith @*/
3675d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3676d71ae5a4SJacob Faibussowitsch {
367747c6ae99SBarry Smith   PetscFunctionBegin;
3678171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
367947c6ae99SBarry Smith   dm->ctx = ctx;
368047c6ae99SBarry Smith   PetscFunctionReturn(0);
368147c6ae99SBarry Smith }
368247c6ae99SBarry Smith 
368347c6ae99SBarry Smith /*@
3684bb7acecfSBarry Smith     DMGetApplicationContext - Gets a user context from a `DM` object
368547c6ae99SBarry Smith 
368647c6ae99SBarry Smith     Not Collective
368747c6ae99SBarry Smith 
368847c6ae99SBarry Smith     Input Parameter:
3689bb7acecfSBarry Smith .   dm - the `DM` object
369047c6ae99SBarry Smith 
369147c6ae99SBarry Smith     Output Parameter:
369247c6ae99SBarry Smith .   ctx - the user context
369347c6ae99SBarry Smith 
369447c6ae99SBarry Smith     Level: intermediate
369547c6ae99SBarry Smith 
3696bb7acecfSBarry Smith     Note:
3697bb7acecfSBarry Smith     A user context is a way to pass problem specific information that is accessable whenever the `DM` is available
3698bb7acecfSBarry Smith 
3699bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
370047c6ae99SBarry Smith @*/
3701d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3702d71ae5a4SJacob Faibussowitsch {
370347c6ae99SBarry Smith   PetscFunctionBegin;
3704171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37051b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
370647c6ae99SBarry Smith   PetscFunctionReturn(0);
370747c6ae99SBarry Smith }
370847c6ae99SBarry Smith 
370908da532bSDmitry Karpeev /*@C
3710bb7acecfSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
371108da532bSDmitry Karpeev 
3712d083f849SBarry Smith     Logically Collective on dm
371308da532bSDmitry Karpeev 
3714d8d19677SJose E. Roman     Input Parameters:
371508da532bSDmitry Karpeev +   dm - the DM object
37160298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
371708da532bSDmitry Karpeev 
371808da532bSDmitry Karpeev     Level: intermediate
371908da532bSDmitry Karpeev 
3720bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3721db781477SPatrick Sanan          `DMSetJacobian()`
372208da532bSDmitry Karpeev @*/
3723d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3724d71ae5a4SJacob Faibussowitsch {
372508da532bSDmitry Karpeev   PetscFunctionBegin;
37265a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
372708da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
372808da532bSDmitry Karpeev   PetscFunctionReturn(0);
372908da532bSDmitry Karpeev }
373008da532bSDmitry Karpeev 
373108da532bSDmitry Karpeev /*@
3732bb7acecfSBarry Smith     DMHasVariableBounds - does the `DM` object have a variable bounds function?
373308da532bSDmitry Karpeev 
373408da532bSDmitry Karpeev     Not Collective
373508da532bSDmitry Karpeev 
373608da532bSDmitry Karpeev     Input Parameter:
3737bb7acecfSBarry Smith .   dm - the `DM` object to destroy
373808da532bSDmitry Karpeev 
373908da532bSDmitry Karpeev     Output Parameter:
3740bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the variable bounds function exists
374108da532bSDmitry Karpeev 
374208da532bSDmitry Karpeev     Level: developer
374308da532bSDmitry Karpeev 
3744bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
374508da532bSDmitry Karpeev @*/
3746d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3747d71ae5a4SJacob Faibussowitsch {
374808da532bSDmitry Karpeev   PetscFunctionBegin;
37495a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3750534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
375108da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
375208da532bSDmitry Karpeev   PetscFunctionReturn(0);
375308da532bSDmitry Karpeev }
375408da532bSDmitry Karpeev 
375508da532bSDmitry Karpeev /*@C
3756bb7acecfSBarry Smith     DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
375708da532bSDmitry Karpeev 
3758d083f849SBarry Smith     Logically Collective on dm
375908da532bSDmitry Karpeev 
3760f899ff85SJose E. Roman     Input Parameter:
3761bb7acecfSBarry Smith .   dm - the `DM` object
376208da532bSDmitry Karpeev 
376308da532bSDmitry Karpeev     Output parameters:
376408da532bSDmitry Karpeev +   xl - lower bound
376508da532bSDmitry Karpeev -   xu - upper bound
376608da532bSDmitry Karpeev 
3767907376e6SBarry Smith     Level: advanced
3768907376e6SBarry Smith 
376995452b02SPatrick Sanan     Notes:
377095452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
377108da532bSDmitry Karpeev 
3772bb7acecfSBarry Smith .seealso: `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
377308da532bSDmitry Karpeev @*/
3774d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3775d71ae5a4SJacob Faibussowitsch {
377608da532bSDmitry Karpeev   PetscFunctionBegin;
37775a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
377808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
37795a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3780dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
378108da532bSDmitry Karpeev   PetscFunctionReturn(0);
378208da532bSDmitry Karpeev }
378308da532bSDmitry Karpeev 
3784b0ae01b7SPeter Brune /*@
3785bb7acecfSBarry Smith     DMHasColoring - does the `DM` object have a method of providing a coloring?
3786b0ae01b7SPeter Brune 
3787b0ae01b7SPeter Brune     Not Collective
3788b0ae01b7SPeter Brune 
3789b0ae01b7SPeter Brune     Input Parameter:
3790b0ae01b7SPeter Brune .   dm - the DM object
3791b0ae01b7SPeter Brune 
3792b0ae01b7SPeter Brune     Output Parameter:
3793bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3794b0ae01b7SPeter Brune 
3795b0ae01b7SPeter Brune     Level: developer
3796b0ae01b7SPeter Brune 
3797bb7acecfSBarry Smith .seealso: `DMCreateColoring()`
3798b0ae01b7SPeter Brune @*/
3799d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3800d71ae5a4SJacob Faibussowitsch {
3801b0ae01b7SPeter Brune   PetscFunctionBegin;
38025a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3803534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3804b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3805b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3806b0ae01b7SPeter Brune }
3807b0ae01b7SPeter Brune 
38083ad4599aSBarry Smith /*@
3809bb7acecfSBarry Smith     DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38103ad4599aSBarry Smith 
38113ad4599aSBarry Smith     Not Collective
38123ad4599aSBarry Smith 
38133ad4599aSBarry Smith     Input Parameter:
3814bb7acecfSBarry Smith .   dm - the `DM` object
38153ad4599aSBarry Smith 
38163ad4599aSBarry Smith     Output Parameter:
3817bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38183ad4599aSBarry Smith 
38193ad4599aSBarry Smith     Level: developer
38203ad4599aSBarry Smith 
3821bb7acecfSBarry Smith .seealso: `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38223ad4599aSBarry Smith @*/
3823d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3824d71ae5a4SJacob Faibussowitsch {
38253ad4599aSBarry Smith   PetscFunctionBegin;
38265a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3827534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
38283ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38293ad4599aSBarry Smith   PetscFunctionReturn(0);
38303ad4599aSBarry Smith }
38313ad4599aSBarry Smith 
3832a7058e45SLawrence Mitchell /*@
3833bb7acecfSBarry Smith     DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3834a7058e45SLawrence Mitchell 
3835a7058e45SLawrence Mitchell     Not Collective
3836a7058e45SLawrence Mitchell 
3837a7058e45SLawrence Mitchell     Input Parameter:
3838bb7acecfSBarry Smith .   dm - the `DM` object
3839a7058e45SLawrence Mitchell 
3840a7058e45SLawrence Mitchell     Output Parameter:
3841bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3842a7058e45SLawrence Mitchell 
3843a7058e45SLawrence Mitchell     Level: developer
3844a7058e45SLawrence Mitchell 
3845bb7acecfSBarry Smith .seealso: `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3846a7058e45SLawrence Mitchell @*/
3847d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3848d71ae5a4SJacob Faibussowitsch {
3849a7058e45SLawrence Mitchell   PetscFunctionBegin;
38505a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3851534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3852dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3853ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
3854a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3855a7058e45SLawrence Mitchell }
3856a7058e45SLawrence Mitchell 
38570298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3858264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3859264ace61SBarry Smith 
3860264ace61SBarry Smith /*@C
3861bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3862264ace61SBarry Smith 
3863d083f849SBarry Smith   Collective on dm
3864264ace61SBarry Smith 
3865264ace61SBarry Smith   Input Parameters:
3866bb7acecfSBarry Smith + dm     - The `DM` object
3867bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3868264ace61SBarry Smith 
3869264ace61SBarry Smith   Options Database Key:
3870bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3871264ace61SBarry Smith 
3872264ace61SBarry Smith   Level: intermediate
3873264ace61SBarry Smith 
3874bb7acecfSBarry Smith   Note:
3875bb7acecfSBarry Smith   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPLEXCreateBoxMesh()`
3876bb7acecfSBarry Smith 
3877bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3878264ace61SBarry Smith @*/
3879d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3880d71ae5a4SJacob Faibussowitsch {
3881264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3882264ace61SBarry Smith   PetscBool match;
3883264ace61SBarry Smith 
3884264ace61SBarry Smith   PetscFunctionBegin;
3885264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38869566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
3887264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3888264ace61SBarry Smith 
38899566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
38909566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
38917a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3892264ace61SBarry Smith 
3893dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
38949566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
38959566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
38969566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
3897264ace61SBarry Smith   PetscFunctionReturn(0);
3898264ace61SBarry Smith }
3899264ace61SBarry Smith 
3900264ace61SBarry Smith /*@C
3901bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3902264ace61SBarry Smith 
3903264ace61SBarry Smith   Not Collective
3904264ace61SBarry Smith 
3905264ace61SBarry Smith   Input Parameter:
3906bb7acecfSBarry Smith . dm  - The `DM`
3907264ace61SBarry Smith 
3908264ace61SBarry Smith   Output Parameter:
3909bb7acecfSBarry Smith . type - The `DMType` name
3910264ace61SBarry Smith 
3911264ace61SBarry Smith   Level: intermediate
3912264ace61SBarry Smith 
3913bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3914264ace61SBarry Smith @*/
3915d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3916d71ae5a4SJacob Faibussowitsch {
3917264ace61SBarry Smith   PetscFunctionBegin;
3918264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3919c959eef4SJed Brown   PetscValidPointer(type, 2);
39209566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3921264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3922264ace61SBarry Smith   PetscFunctionReturn(0);
3923264ace61SBarry Smith }
3924264ace61SBarry Smith 
392567a56275SMatthew G Knepley /*@C
3926bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
392767a56275SMatthew G Knepley 
3928d083f849SBarry Smith   Collective on dm
392967a56275SMatthew G Knepley 
393067a56275SMatthew G Knepley   Input Parameters:
3931bb7acecfSBarry Smith + dm - the `DM`
3932bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
393367a56275SMatthew G Knepley 
393467a56275SMatthew G Knepley   Output Parameter:
3935bb7acecfSBarry Smith . M - pointer to new `DM`
393667a56275SMatthew G Knepley 
393767a56275SMatthew G Knepley   Notes:
3938bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3939bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3940bb7acecfSBarry Smith   of the input `DM`.
394167a56275SMatthew G Knepley 
394267a56275SMatthew G Knepley   Level: intermediate
394367a56275SMatthew G Knepley 
3944bb7acecfSBarry Smith .seealso: `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
394567a56275SMatthew G Knepley @*/
3946d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3947d71ae5a4SJacob Faibussowitsch {
394867a56275SMatthew G Knepley   DM        B;
394967a56275SMatthew G Knepley   char      convname[256];
3950c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
395167a56275SMatthew G Knepley 
395267a56275SMatthew G Knepley   PetscFunctionBegin;
395367a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
395467a56275SMatthew G Knepley   PetscValidType(dm, 1);
395567a56275SMatthew G Knepley   PetscValidPointer(M, 3);
39569566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
39579566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3958c067b6caSMatthew G. Knepley   if (sametype) {
3959c067b6caSMatthew G. Knepley     *M = dm;
39609566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
3961c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3962c067b6caSMatthew G. Knepley   } else {
39630298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
396467a56275SMatthew G Knepley 
396567a56275SMatthew G Knepley     /*
396667a56275SMatthew G Knepley        Order of precedence:
396767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
396867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
396967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
397067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
397167a56275SMatthew G Knepley        5) Use a really basic converter.
397267a56275SMatthew G Knepley     */
397367a56275SMatthew G Knepley 
397467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
39759566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39769566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
39779566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
39789566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
39799566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
39809566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
398167a56275SMatthew G Knepley     if (conv) goto foundconv;
398267a56275SMatthew G Knepley 
398367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
39849566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
39859566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
39869566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39879566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
39889566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
39899566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
39909566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
39919566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
399267a56275SMatthew G Knepley     if (conv) {
39939566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
399467a56275SMatthew G Knepley       goto foundconv;
399567a56275SMatthew G Knepley     }
399667a56275SMatthew G Knepley 
399767a56275SMatthew G Knepley #if 0
399867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
399967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40009566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
400167a56275SMatthew G Knepley     if (conv) goto foundconv;
400267a56275SMatthew G Knepley 
400367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
400467a56275SMatthew G Knepley     if (dm->ops->convert) {
400567a56275SMatthew G Knepley       conv = dm->ops->convert;
400667a56275SMatthew G Knepley     }
400767a56275SMatthew G Knepley     if (conv) goto foundconv;
400867a56275SMatthew G Knepley #endif
400967a56275SMatthew G Knepley 
401067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
401198921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
401267a56275SMatthew G Knepley 
401367a56275SMatthew G Knepley   foundconv:
40149566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40159566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
401612fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
401790b157c4SStefano Zampini     {
40184fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40196858538eSMatthew G. Knepley 
40204fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40214fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4022c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40239566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40249566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40259566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40269566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
402712fa691eSMatthew G. Knepley     }
40289566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
402967a56275SMatthew G Knepley   }
40309566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
403167a56275SMatthew G Knepley   PetscFunctionReturn(0);
403267a56275SMatthew G Knepley }
4033264ace61SBarry Smith 
4034264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4035264ace61SBarry Smith 
4036264ace61SBarry Smith /*@C
4037bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40381c84c290SBarry Smith 
40391c84c290SBarry Smith   Not Collective
40401c84c290SBarry Smith 
40411c84c290SBarry Smith   Input Parameters:
40421c84c290SBarry Smith + name        - The name of a new user-defined creation routine
40431c84c290SBarry Smith - create_func - The creation routine itself
40441c84c290SBarry Smith 
40451c84c290SBarry Smith   Notes:
4046bb7acecfSBarry Smith   DMRegister() may be called multiple times to add several user-defined `DM`s
40471c84c290SBarry Smith 
40481c84c290SBarry Smith   Sample usage:
40491c84c290SBarry Smith .vb
4050bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40511c84c290SBarry Smith .ve
40521c84c290SBarry Smith 
40531c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
40541c84c290SBarry Smith .vb
40551c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
40561c84c290SBarry Smith     DMSetType(DM,"my_da");
40571c84c290SBarry Smith .ve
40581c84c290SBarry Smith    or at runtime via the option
40591c84c290SBarry Smith .vb
40601c84c290SBarry Smith     -da_type my_da
40611c84c290SBarry Smith .ve
4062264ace61SBarry Smith 
4063264ace61SBarry Smith   Level: advanced
40641c84c290SBarry Smith 
4065bb7acecfSBarry Smith .seealso: `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4066264ace61SBarry Smith @*/
4067d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4068d71ae5a4SJacob Faibussowitsch {
4069264ace61SBarry Smith   PetscFunctionBegin;
40709566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
40719566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
4072264ace61SBarry Smith   PetscFunctionReturn(0);
4073264ace61SBarry Smith }
4074264ace61SBarry Smith 
4075b859378eSBarry Smith /*@C
4076bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4077b859378eSBarry Smith 
4078d083f849SBarry Smith   Collective on viewer
4079b859378eSBarry Smith 
4080b859378eSBarry Smith   Input Parameters:
4081bb7acecfSBarry Smith + newdm - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4082bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4083bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4084bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4085b859378eSBarry Smith 
4086b859378eSBarry Smith    Level: intermediate
4087b859378eSBarry Smith 
4088b859378eSBarry Smith   Notes:
408955849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4090b859378eSBarry Smith 
4091bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4092bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4093bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4094cd7e8a5eSksagiyam 
4095b859378eSBarry Smith   Notes for advanced users:
4096b859378eSBarry Smith   Most users should not need to know the details of the binary storage
4097bb7acecfSBarry Smith   format, since `DMLoad()` and `DMView()` completely hide these details.
4098b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
4099b859378eSBarry Smith   format is
4100b859378eSBarry Smith .vb
4101b859378eSBarry Smith      has not yet been determined
4102b859378eSBarry Smith .ve
4103b859378eSBarry Smith 
4104db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4105b859378eSBarry Smith @*/
4106d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4107d71ae5a4SJacob Faibussowitsch {
41089331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4109b859378eSBarry Smith 
4110b859378eSBarry Smith   PetscFunctionBegin;
4111b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4112b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41139566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41149566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41159566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41169566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41179331c7a4SMatthew G. Knepley   if (isbinary) {
41189331c7a4SMatthew G. Knepley     PetscInt classid;
41199331c7a4SMatthew G. Knepley     char     type[256];
4120b859378eSBarry Smith 
41219566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41227a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41239566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41249566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4125dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41269331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4127dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41289331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41299566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
4130b859378eSBarry Smith   PetscFunctionReturn(0);
4131b859378eSBarry Smith }
4132b859378eSBarry Smith 
41337da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41347da65231SMatthew G Knepley 
4135d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4136d71ae5a4SJacob Faibussowitsch {
41371d47ebbbSSatish Balay   PetscInt f;
41381b30c384SMatthew G Knepley 
41397da65231SMatthew G Knepley   PetscFunctionBegin;
414063a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
414148a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41427da65231SMatthew G Knepley   PetscFunctionReturn(0);
41437da65231SMatthew G Knepley }
41447da65231SMatthew G Knepley 
4145d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4146d71ae5a4SJacob Faibussowitsch {
41471b30c384SMatthew G Knepley   PetscInt f, g;
41487da65231SMatthew G Knepley 
41497da65231SMatthew G Knepley   PetscFunctionBegin;
415063a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41511d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
41529566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
415348a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
41549566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
41557da65231SMatthew G Knepley   }
41567da65231SMatthew G Knepley   PetscFunctionReturn(0);
41577da65231SMatthew G Knepley }
4158e7c4fc90SDmitry Karpeev 
4159d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4160d71ae5a4SJacob Faibussowitsch {
41610c5b8624SToby Isaac   PetscInt           localSize, bs;
41620c5b8624SToby Isaac   PetscMPIInt        size;
41630c5b8624SToby Isaac   Vec                x, xglob;
41640c5b8624SToby Isaac   const PetscScalar *xarray;
4165e759306cSMatthew G. Knepley 
4166e759306cSMatthew G. Knepley   PetscFunctionBegin;
41679566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
41689566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
41699566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
41709566063dSJacob Faibussowitsch   PetscCall(VecChop(x, tol));
41719566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
41720c5b8624SToby Isaac   if (size > 1) {
41739566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
41749566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
41759566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
41769566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
41770c5b8624SToby Isaac   } else {
41780c5b8624SToby Isaac     xglob = x;
41790c5b8624SToby Isaac   }
41809566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
41810c5b8624SToby Isaac   if (size > 1) {
41829566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
41839566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
41840c5b8624SToby Isaac   }
41859566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
4186e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4187e759306cSMatthew G. Knepley }
4188e759306cSMatthew G. Knepley 
418988ed4aceSMatthew G Knepley /*@
4190bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4191061576a5SJed Brown 
4192061576a5SJed Brown   Input Parameter:
4193bb7acecfSBarry Smith . dm - The `DM`
4194061576a5SJed Brown 
4195061576a5SJed Brown   Output Parameter:
4196bb7acecfSBarry Smith . section - The `PetscSection`
4197061576a5SJed Brown 
4198061576a5SJed Brown   Options Database Keys:
4199bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4200061576a5SJed Brown 
4201061576a5SJed Brown   Level: advanced
4202061576a5SJed Brown 
4203061576a5SJed Brown   Notes:
4204bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4205061576a5SJed Brown 
4206bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4207061576a5SJed Brown 
4208db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4209061576a5SJed Brown @*/
4210d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4211d71ae5a4SJacob Faibussowitsch {
4212061576a5SJed Brown   PetscFunctionBegin;
42139566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
4214061576a5SJed Brown   PetscFunctionReturn(0);
4215061576a5SJed Brown }
4216061576a5SJed Brown 
4217061576a5SJed Brown /*@
4218bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
421988ed4aceSMatthew G Knepley 
422088ed4aceSMatthew G Knepley   Input Parameter:
4221bb7acecfSBarry Smith . dm - The `DM`
422288ed4aceSMatthew G Knepley 
422388ed4aceSMatthew G Knepley   Output Parameter:
4224bb7acecfSBarry Smith . section - The `PetscSection`
422588ed4aceSMatthew G Knepley 
4226e5893cccSMatthew G. Knepley   Options Database Keys:
4227bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4228e5893cccSMatthew G. Knepley 
422988ed4aceSMatthew G Knepley   Level: intermediate
423088ed4aceSMatthew G Knepley 
4231bb7acecfSBarry Smith   Note:
4232bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
423388ed4aceSMatthew G Knepley 
4234db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetGlobalSection()`
423588ed4aceSMatthew G Knepley @*/
4236d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4237d71ae5a4SJacob Faibussowitsch {
423888ed4aceSMatthew G Knepley   PetscFunctionBegin;
423988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
424088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42411bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4242e5e52638SMatthew G. Knepley     PetscInt d;
4243e5e52638SMatthew G. Knepley 
424445480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
424545480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
424645480ffeSMatthew G. Knepley       PetscViewer       viewer;
424745480ffeSMatthew G. Knepley       PetscViewerFormat format;
424845480ffeSMatthew G. Knepley       PetscBool         flg;
424945480ffeSMatthew G. Knepley 
42509566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
42519566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
425245480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
42539566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
42549566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
425545480ffeSMatthew G. Knepley       }
425645480ffeSMatthew G. Knepley       if (flg) {
42579566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
42589566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
42599566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
426045480ffeSMatthew G. Knepley       }
426145480ffeSMatthew G. Knepley     }
4262dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
42639566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
42642f0f8703SMatthew G. Knepley   }
42651bb6d2a8SBarry Smith   *section = dm->localSection;
426688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
426788ed4aceSMatthew G Knepley }
426888ed4aceSMatthew G Knepley 
426988ed4aceSMatthew G Knepley /*@
4270bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4271061576a5SJed Brown 
4272061576a5SJed Brown   Input Parameters:
4273bb7acecfSBarry Smith + dm - The `DM`
4274bb7acecfSBarry Smith - section - The `PetscSection`
4275061576a5SJed Brown 
4276061576a5SJed Brown   Level: advanced
4277061576a5SJed Brown 
4278061576a5SJed Brown   Notes:
4279bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4280061576a5SJed Brown 
4281bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4282061576a5SJed Brown 
4283db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4284061576a5SJed Brown @*/
4285d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4286d71ae5a4SJacob Faibussowitsch {
4287061576a5SJed Brown   PetscFunctionBegin;
42889566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
4289061576a5SJed Brown   PetscFunctionReturn(0);
4290061576a5SJed Brown }
4291061576a5SJed Brown 
4292061576a5SJed Brown /*@
4293bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
429488ed4aceSMatthew G Knepley 
429588ed4aceSMatthew G Knepley   Input Parameters:
4296bb7acecfSBarry Smith + dm - The `DM`
4297bb7acecfSBarry Smith - section - The `PetscSection`
429888ed4aceSMatthew G Knepley 
429988ed4aceSMatthew G Knepley   Level: intermediate
430088ed4aceSMatthew G Knepley 
4301bb7acecfSBarry Smith   Note:
4302bb7acecfSBarry Smith   Any existing Section will be destroyed
430388ed4aceSMatthew G Knepley 
4304bb7acecfSBarry Smith .seealso: `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
430588ed4aceSMatthew G Knepley @*/
4306d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4307d71ae5a4SJacob Faibussowitsch {
4308c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4309af122d2aSMatthew G Knepley   PetscInt f;
431088ed4aceSMatthew G Knepley 
431188ed4aceSMatthew G Knepley   PetscFunctionBegin;
431288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4313b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43149566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43159566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43161bb6d2a8SBarry Smith   dm->localSection = section;
43179566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4318af122d2aSMatthew G Knepley   if (numFields) {
43199566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4320af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43210f21e855SMatthew G. Knepley       PetscObject disc;
4322af122d2aSMatthew G Knepley       const char *name;
4323af122d2aSMatthew G Knepley 
43249566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43259566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43269566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4327af122d2aSMatthew G Knepley     }
4328af122d2aSMatthew G Knepley   }
4329e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43309566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
433188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
433288ed4aceSMatthew G Knepley }
433388ed4aceSMatthew G Knepley 
43349435951eSToby Isaac /*@
4335bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
43369435951eSToby Isaac 
4337e228b242SToby Isaac   not collective
4338e228b242SToby Isaac 
43399435951eSToby Isaac   Input Parameter:
4340bb7acecfSBarry Smith . dm - The `DM`
43419435951eSToby Isaac 
4342d8d19677SJose E. Roman   Output Parameters:
4343bb7acecfSBarry 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.
4344bb7acecfSBarry 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.
434579769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
43469435951eSToby Isaac 
43479435951eSToby Isaac   Level: advanced
43489435951eSToby Isaac 
4349bb7acecfSBarry Smith   Note:
4350bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
43519435951eSToby Isaac 
4352db781477SPatrick Sanan .seealso: `DMSetDefaultConstraints()`
43539435951eSToby Isaac @*/
4354d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4355d71ae5a4SJacob Faibussowitsch {
43569435951eSToby Isaac   PetscFunctionBegin;
43579435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4358dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
43593b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
43603b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
436179769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
43629435951eSToby Isaac   PetscFunctionReturn(0);
43639435951eSToby Isaac }
43649435951eSToby Isaac 
43659435951eSToby Isaac /*@
4366bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
43679435951eSToby Isaac 
4368bb7acecfSBarry 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()`.
43699435951eSToby Isaac 
4370bb7acecfSBarry 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.
43719435951eSToby Isaac 
4372e228b242SToby Isaac   collective on dm
4373e228b242SToby Isaac 
43749435951eSToby Isaac   Input Parameters:
4375bb7acecfSBarry Smith + dm - The `DM`
4376bb7acecfSBarry 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).
4377bb7acecfSBarry 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).
4378bb7acecfSBarry 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).
43799435951eSToby Isaac 
43809435951eSToby Isaac   Level: advanced
43819435951eSToby Isaac 
4382bb7acecfSBarry Smith   Note:
4383bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
43849435951eSToby Isaac 
4385db781477SPatrick Sanan .seealso: `DMGetDefaultConstraints()`
43869435951eSToby Isaac @*/
4387d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4388d71ae5a4SJacob Faibussowitsch {
4389e228b242SToby Isaac   PetscMPIInt result;
43909435951eSToby Isaac 
43919435951eSToby Isaac   PetscFunctionBegin;
43929435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4393e228b242SToby Isaac   if (section) {
4394e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43959566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
43967a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4397e228b242SToby Isaac   }
4398e228b242SToby Isaac   if (mat) {
4399e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44009566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44017a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4402e228b242SToby Isaac   }
440379769bd5SJed Brown   if (bias) {
440479769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44059566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
440679769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
440779769bd5SJed Brown   }
44089566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44099566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44103b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44119566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44129566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44133b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44149566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44159566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
441679769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44179435951eSToby Isaac   PetscFunctionReturn(0);
44189435951eSToby Isaac }
44199435951eSToby Isaac 
4420497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4421507e4973SMatthew G. Knepley /*
4422bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4423507e4973SMatthew G. Knepley 
4424507e4973SMatthew G. Knepley   Input Parameters:
4425bb7acecfSBarry Smith + dm - The `DM`
4426bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4427bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4428507e4973SMatthew G. Knepley 
4429507e4973SMatthew G. Knepley   Level: intermediate
4430507e4973SMatthew G. Knepley 
4431db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`
4432507e4973SMatthew G. Knepley */
4433d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4434d71ae5a4SJacob Faibussowitsch {
4435507e4973SMatthew G. Knepley   MPI_Comm        comm;
4436507e4973SMatthew G. Knepley   PetscLayout     layout;
4437507e4973SMatthew G. Knepley   const PetscInt *ranges;
4438507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4439507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4440507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4441507e4973SMatthew G. Knepley 
4442507e4973SMatthew G. Knepley   PetscFunctionBegin;
44439566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4444507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44459566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
44469566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44479566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
44489566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
44499566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
44509566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
44519566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
44529566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
44539566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4454507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4455f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4456507e4973SMatthew G. Knepley 
44579566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
44589566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
44599566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
44609566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
44619566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
44629566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4463507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
44649371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
44659371c9d4SSatish 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));
44669371c9d4SSatish Balay       valid = PETSC_FALSE;
44679371c9d4SSatish Balay     }
44689371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
44699371c9d4SSatish 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));
44709371c9d4SSatish Balay       valid = PETSC_FALSE;
44719371c9d4SSatish Balay     }
4472507e4973SMatthew G. Knepley     if (gdof < 0) {
4473507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4474507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4475507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4476507e4973SMatthew G. Knepley 
44779566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4478507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
44799371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
44809371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
44819371c9d4SSatish Balay           valid = PETSC_FALSE;
44829371c9d4SSatish Balay           break;
44839371c9d4SSatish Balay         }
4484507e4973SMatthew G. Knepley       }
4485507e4973SMatthew G. Knepley     }
4486507e4973SMatthew G. Knepley   }
44879566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
44889566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
44891c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4490507e4973SMatthew G. Knepley   if (!gvalid) {
44919566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4492507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4493507e4973SMatthew G. Knepley   }
4494507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4495507e4973SMatthew G. Knepley }
4496f741bcd2SMatthew G. Knepley #endif
4497507e4973SMatthew G. Knepley 
449888ed4aceSMatthew G Knepley /*@
4499bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
450088ed4aceSMatthew G Knepley 
4501d083f849SBarry Smith   Collective on dm
45028b1ab98fSJed Brown 
450388ed4aceSMatthew G Knepley   Input Parameter:
4504bb7acecfSBarry Smith . dm - The `DM`
450588ed4aceSMatthew G Knepley 
450688ed4aceSMatthew G Knepley   Output Parameter:
4507bb7acecfSBarry Smith . section - The `PetscSection`
450888ed4aceSMatthew G Knepley 
450988ed4aceSMatthew G Knepley   Level: intermediate
451088ed4aceSMatthew G Knepley 
4511bb7acecfSBarry Smith   Note:
4512bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
451388ed4aceSMatthew G Knepley 
4514db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`
451588ed4aceSMatthew G Knepley @*/
4516d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4517d71ae5a4SJacob Faibussowitsch {
451888ed4aceSMatthew G Knepley   PetscFunctionBegin;
451988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
452088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
45211bb6d2a8SBarry Smith   if (!dm->globalSection) {
4522fd59a867SMatthew G. Knepley     PetscSection s;
4523fd59a867SMatthew G. Knepley 
45249566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
45257a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
45267a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45279566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
45289566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
45299566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
45309566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
453188ed4aceSMatthew G Knepley   }
45321bb6d2a8SBarry Smith   *section = dm->globalSection;
453388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
453488ed4aceSMatthew G Knepley }
453588ed4aceSMatthew G Knepley 
4536b21d0597SMatthew G Knepley /*@
4537bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4538b21d0597SMatthew G Knepley 
4539b21d0597SMatthew G Knepley   Input Parameters:
4540bb7acecfSBarry Smith + dm - The `DM`
45415080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4542b21d0597SMatthew G Knepley 
4543b21d0597SMatthew G Knepley   Level: intermediate
4544b21d0597SMatthew G Knepley 
4545bb7acecfSBarry Smith   Note:
4546bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4547b21d0597SMatthew G Knepley 
4548db781477SPatrick Sanan .seealso: `DMGetGlobalSection()`, `DMSetLocalSection()`
4549b21d0597SMatthew G Knepley @*/
4550d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4551d71ae5a4SJacob Faibussowitsch {
4552b21d0597SMatthew G Knepley   PetscFunctionBegin;
4553b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45545080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
45559566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45569566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
45571bb6d2a8SBarry Smith   dm->globalSection = section;
4558497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
45599566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4560507e4973SMatthew G. Knepley #endif
4561b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4562b21d0597SMatthew G Knepley }
4563b21d0597SMatthew G Knepley 
456488ed4aceSMatthew G Knepley /*@
4565bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4566bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
456788ed4aceSMatthew G Knepley 
456888ed4aceSMatthew G Knepley   Input Parameter:
4569bb7acecfSBarry Smith . dm - The `DM`
457088ed4aceSMatthew G Knepley 
457188ed4aceSMatthew G Knepley   Output Parameter:
4572bb7acecfSBarry Smith . sf - The `PetscSF`
457388ed4aceSMatthew G Knepley 
457488ed4aceSMatthew G Knepley   Level: intermediate
457588ed4aceSMatthew G Knepley 
4576bb7acecfSBarry Smith   Note:
4577bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
457888ed4aceSMatthew G Knepley 
4579db781477SPatrick Sanan .seealso: `DMSetSectionSF()`, `DMCreateSectionSF()`
458088ed4aceSMatthew G Knepley @*/
4581d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4582d71ae5a4SJacob Faibussowitsch {
458388ed4aceSMatthew G Knepley   PetscInt nroots;
458488ed4aceSMatthew G Knepley 
458588ed4aceSMatthew G Knepley   PetscFunctionBegin;
458688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
458788ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
458848a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
45899566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
459088ed4aceSMatthew G Knepley   if (nroots < 0) {
459188ed4aceSMatthew G Knepley     PetscSection section, gSection;
459288ed4aceSMatthew G Knepley 
45939566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
459431ea6d37SMatthew G Knepley     if (section) {
45959566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
45969566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
459731ea6d37SMatthew G Knepley     } else {
45980298fd71SBarry Smith       *sf = NULL;
459931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
460031ea6d37SMatthew G Knepley     }
460188ed4aceSMatthew G Knepley   }
46021bb6d2a8SBarry Smith   *sf = dm->sectionSF;
460388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
460488ed4aceSMatthew G Knepley }
460588ed4aceSMatthew G Knepley 
460688ed4aceSMatthew G Knepley /*@
4607bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
460888ed4aceSMatthew G Knepley 
460988ed4aceSMatthew G Knepley   Input Parameters:
4610bb7acecfSBarry Smith + dm - The `DM`
4611bb7acecfSBarry Smith - sf - The `PetscSF`
461288ed4aceSMatthew G Knepley 
461388ed4aceSMatthew G Knepley   Level: intermediate
461488ed4aceSMatthew G Knepley 
4615bb7acecfSBarry Smith   Note:
4616bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
461788ed4aceSMatthew G Knepley 
4618db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMCreateSectionSF()`
461988ed4aceSMatthew G Knepley @*/
4620d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4621d71ae5a4SJacob Faibussowitsch {
462288ed4aceSMatthew G Knepley   PetscFunctionBegin;
462388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4624b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46259566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46269566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
46271bb6d2a8SBarry Smith   dm->sectionSF = sf;
462888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
462988ed4aceSMatthew G Knepley }
463088ed4aceSMatthew G Knepley 
463188ed4aceSMatthew G Knepley /*@C
4632bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
463388ed4aceSMatthew G Knepley   describing the data layout.
463488ed4aceSMatthew G Knepley 
463588ed4aceSMatthew G Knepley   Input Parameters:
4636bb7acecfSBarry Smith + dm - The `DM`
4637bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4638bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
463988ed4aceSMatthew G Knepley 
46401bb6d2a8SBarry Smith   Level: developer
46411bb6d2a8SBarry Smith 
4642bb7acecfSBarry Smith   Note:
4643bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4644bb7acecfSBarry Smith 
4645bb7acecfSBarry Smith   Developer Note:
4646bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4647bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4648bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
46491bb6d2a8SBarry Smith 
4650db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
465188ed4aceSMatthew G Knepley @*/
4652d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4653d71ae5a4SJacob Faibussowitsch {
465488ed4aceSMatthew G Knepley   PetscFunctionBegin;
465588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46569566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
465788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
465888ed4aceSMatthew G Knepley }
4659af122d2aSMatthew G Knepley 
4660b21d0597SMatthew G Knepley /*@
4661bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4662bb7acecfSBarry Smith 
4663bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4664b21d0597SMatthew G Knepley 
4665b21d0597SMatthew G Knepley   Input Parameter:
4666bb7acecfSBarry Smith . dm - The `DM`
4667b21d0597SMatthew G Knepley 
4668b21d0597SMatthew G Knepley   Output Parameter:
4669bb7acecfSBarry Smith . sf - The `PetscSF`
4670b21d0597SMatthew G Knepley 
4671b21d0597SMatthew G Knepley   Level: intermediate
4672b21d0597SMatthew G Knepley 
4673bb7acecfSBarry Smith   Note:
4674bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4675b21d0597SMatthew G Knepley 
4676db781477SPatrick Sanan .seealso: `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4677b21d0597SMatthew G Knepley @*/
4678d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4679d71ae5a4SJacob Faibussowitsch {
4680b21d0597SMatthew G Knepley   PetscFunctionBegin;
4681b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4682b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4683b21d0597SMatthew G Knepley   *sf = dm->sf;
4684b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4685b21d0597SMatthew G Knepley }
4686b21d0597SMatthew G Knepley 
4687057b4bcdSMatthew G Knepley /*@
4688bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4689bb7acecfSBarry Smith 
4690bb7acecfSBarry Smith   Collective on dm
4691057b4bcdSMatthew G Knepley 
4692057b4bcdSMatthew G Knepley   Input Parameters:
4693bb7acecfSBarry Smith + dm - The `DM`
4694bb7acecfSBarry Smith - sf - The` PetscSF`
4695057b4bcdSMatthew G Knepley 
4696057b4bcdSMatthew G Knepley   Level: intermediate
4697057b4bcdSMatthew G Knepley 
4698db781477SPatrick Sanan .seealso: `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4699057b4bcdSMatthew G Knepley @*/
4700d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4701d71ae5a4SJacob Faibussowitsch {
4702057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4703057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4704b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47059566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47069566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4707057b4bcdSMatthew G Knepley   dm->sf = sf;
4708057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4709057b4bcdSMatthew G Knepley }
4710057b4bcdSMatthew G Knepley 
47114f37162bSMatthew G. Knepley /*@
4712bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
47134f37162bSMatthew G. Knepley 
47144f37162bSMatthew G. Knepley   Input Parameter:
4715bb7acecfSBarry Smith . dm - The `DM`
47164f37162bSMatthew G. Knepley 
47174f37162bSMatthew G. Knepley   Output Parameter:
4718bb7acecfSBarry Smith . sf - The `PetscSF`
47194f37162bSMatthew G. Knepley 
47204f37162bSMatthew G. Knepley   Level: intermediate
47214f37162bSMatthew G. Knepley 
4722bb7acecfSBarry Smith   Note:
4723bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
47244f37162bSMatthew G. Knepley 
4725db781477SPatrick Sanan .seealso: `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47264f37162bSMatthew G. Knepley @*/
4727d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4728d71ae5a4SJacob Faibussowitsch {
47294f37162bSMatthew G. Knepley   PetscFunctionBegin;
47304f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47314f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
47324f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
47334f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
47344f37162bSMatthew G. Knepley }
47354f37162bSMatthew G. Knepley 
47364f37162bSMatthew G. Knepley /*@
47374f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
47384f37162bSMatthew G. Knepley 
47394f37162bSMatthew G. Knepley   Input Parameters:
47404f37162bSMatthew G. Knepley + dm - The DM
47414f37162bSMatthew G. Knepley - sf - The PetscSF
47424f37162bSMatthew G. Knepley 
47434f37162bSMatthew G. Knepley   Level: intermediate
47444f37162bSMatthew G. Knepley 
4745db781477SPatrick Sanan .seealso: `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47464f37162bSMatthew G. Knepley @*/
4747d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4748d71ae5a4SJacob Faibussowitsch {
47494f37162bSMatthew G. Knepley   PetscFunctionBegin;
47504f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47514f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47529566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47539566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
47544f37162bSMatthew G. Knepley   dm->sfNatural = sf;
47554f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
47564f37162bSMatthew G. Knepley }
47574f37162bSMatthew G. Knepley 
4758d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4759d71ae5a4SJacob Faibussowitsch {
476034aa8a36SMatthew G. Knepley   PetscClassId id;
476134aa8a36SMatthew G. Knepley 
476234aa8a36SMatthew G. Knepley   PetscFunctionBegin;
47639566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
476434aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
47659566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
476634aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
47679566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
476817c1d62eSMatthew G. Knepley   } else {
47699566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
477034aa8a36SMatthew G. Knepley   }
477134aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
477234aa8a36SMatthew G. Knepley }
477334aa8a36SMatthew G. Knepley 
4774d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4775d71ae5a4SJacob Faibussowitsch {
477644a7f3ddSMatthew G. Knepley   RegionField *tmpr;
477744a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
477844a7f3ddSMatthew G. Knepley 
477944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
478044a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
47819566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
478244a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
47839371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
47849371c9d4SSatish Balay     tmpr[f].disc        = NULL;
47859371c9d4SSatish Balay     tmpr[f].label       = NULL;
47869371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
47879371c9d4SSatish Balay   }
47889566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
478944a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
479044a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
479144a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
479244a7f3ddSMatthew G. Knepley }
479344a7f3ddSMatthew G. Knepley 
479444a7f3ddSMatthew G. Knepley /*@
479544a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
479644a7f3ddSMatthew G. Knepley 
4797d083f849SBarry Smith   Logically collective on dm
479844a7f3ddSMatthew G. Knepley 
479944a7f3ddSMatthew G. Knepley   Input Parameter:
480044a7f3ddSMatthew G. Knepley . dm - The DM
480144a7f3ddSMatthew G. Knepley 
480244a7f3ddSMatthew G. Knepley   Level: intermediate
480344a7f3ddSMatthew G. Knepley 
4804db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
480544a7f3ddSMatthew G. Knepley @*/
4806d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4807d71ae5a4SJacob Faibussowitsch {
480844a7f3ddSMatthew G. Knepley   PetscInt f;
480944a7f3ddSMatthew G. Knepley 
481044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
481144a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
481244a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
48139566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
48149566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
481544a7f3ddSMatthew G. Knepley   }
48169566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
481744a7f3ddSMatthew G. Knepley   dm->fields = NULL;
481844a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
481944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
482044a7f3ddSMatthew G. Knepley }
482144a7f3ddSMatthew G. Knepley 
4822689b5837SMatthew G. Knepley /*@
4823689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4824689b5837SMatthew G. Knepley 
4825689b5837SMatthew G. Knepley   Not collective
4826689b5837SMatthew G. Knepley 
4827689b5837SMatthew G. Knepley   Input Parameter:
4828689b5837SMatthew G. Knepley . dm - The DM
4829689b5837SMatthew G. Knepley 
4830689b5837SMatthew G. Knepley   Output Parameter:
4831689b5837SMatthew G. Knepley . Nf - The number of fields
4832689b5837SMatthew G. Knepley 
4833689b5837SMatthew G. Knepley   Level: intermediate
4834689b5837SMatthew G. Knepley 
4835db781477SPatrick Sanan .seealso: `DMSetNumFields()`, `DMSetField()`
4836689b5837SMatthew G. Knepley @*/
4837d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4838d71ae5a4SJacob Faibussowitsch {
48390f21e855SMatthew G. Knepley   PetscFunctionBegin;
48400f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4841534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
484244a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4843af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4844af122d2aSMatthew G Knepley }
4845af122d2aSMatthew G Knepley 
4846689b5837SMatthew G. Knepley /*@
4847689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4848689b5837SMatthew G. Knepley 
4849d083f849SBarry Smith   Logically collective on dm
4850689b5837SMatthew G. Knepley 
4851689b5837SMatthew G. Knepley   Input Parameters:
4852689b5837SMatthew G. Knepley + dm - The DM
4853689b5837SMatthew G. Knepley - Nf - The number of fields
4854689b5837SMatthew G. Knepley 
4855689b5837SMatthew G. Knepley   Level: intermediate
4856689b5837SMatthew G. Knepley 
4857db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetField()`
4858689b5837SMatthew G. Knepley @*/
4859d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4860d71ae5a4SJacob Faibussowitsch {
48610f21e855SMatthew G. Knepley   PetscInt Nf, f;
4862af122d2aSMatthew G Knepley 
4863af122d2aSMatthew G Knepley   PetscFunctionBegin;
4864af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48659566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
48660f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
48670f21e855SMatthew G. Knepley     PetscContainer obj;
48680f21e855SMatthew G. Knepley 
48699566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
48709566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
48719566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4872af122d2aSMatthew G Knepley   }
4873af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4874af122d2aSMatthew G Knepley }
4875af122d2aSMatthew G Knepley 
4876c1929be8SMatthew G. Knepley /*@
4877bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4878c1929be8SMatthew G. Knepley 
4879c1929be8SMatthew G. Knepley   Not collective
4880c1929be8SMatthew G. Knepley 
4881c1929be8SMatthew G. Knepley   Input Parameters:
4882bb7acecfSBarry Smith + dm - The `DM`
4883c1929be8SMatthew G. Knepley - f  - The field number
4884c1929be8SMatthew G. Knepley 
488544a7f3ddSMatthew G. Knepley   Output Parameters:
4886bb7acecfSBarry Smith + label - The label indicating the support of the field, or NULL for the entire mesh (pass in NULL if not needed)
4887bb7acecfSBarry Smith - disc - The discretization object (pass in NULL if not needed)
4888c1929be8SMatthew G. Knepley 
488944a7f3ddSMatthew G. Knepley   Level: intermediate
4890c1929be8SMatthew G. Knepley 
4891db781477SPatrick Sanan .seealso: `DMAddField()`, `DMSetField()`
4892c1929be8SMatthew G. Knepley @*/
4893d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4894d71ae5a4SJacob Faibussowitsch {
4895af122d2aSMatthew G Knepley   PetscFunctionBegin;
4896af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4897bb7acecfSBarry Smith   PetscValidPointer(disc, 4);
48987a8be351SBarry 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);
489944a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4900bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
4901decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4902decb47aaSMatthew G. Knepley }
4903decb47aaSMatthew G. Knepley 
4904083401c6SMatthew G. Knepley /* Does not clear the DS */
4905d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4906d71ae5a4SJacob Faibussowitsch {
4907083401c6SMatthew G. Knepley   PetscFunctionBegin;
49089566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
49099566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
49109566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4911083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4912bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
49139566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4914bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
4915083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4916083401c6SMatthew G. Knepley }
4917083401c6SMatthew G. Knepley 
4918*46560f82SMatthew G. Knepley /*@C
4919bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
4920bb7acecfSBarry Smith   the field numbering.
4921c1929be8SMatthew G. Knepley 
4922d083f849SBarry Smith   Logically collective on dm
4923c1929be8SMatthew G. Knepley 
4924c1929be8SMatthew G. Knepley   Input Parameters:
4925bb7acecfSBarry Smith + dm    - The `DM`
4926c1929be8SMatthew G. Knepley . f     - The field number
492744a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4928bb7acecfSBarry Smith - disc - The discretization object
4929c1929be8SMatthew G. Knepley 
493044a7f3ddSMatthew G. Knepley   Level: intermediate
4931c1929be8SMatthew G. Knepley 
4932db781477SPatrick Sanan .seealso: `DMAddField()`, `DMGetField()`
4933c1929be8SMatthew G. Knepley @*/
4934d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4935d71ae5a4SJacob Faibussowitsch {
4936decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4937decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4938e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
4939bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
49407a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
4941bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
4942bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
49439566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
494444a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
494544a7f3ddSMatthew G. Knepley }
494644a7f3ddSMatthew G. Knepley 
4947*46560f82SMatthew G. Knepley /*@C
4948bb7acecfSBarry 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)
4949bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
495044a7f3ddSMatthew G. Knepley 
4951d083f849SBarry Smith   Logically collective on dm
495244a7f3ddSMatthew G. Knepley 
495344a7f3ddSMatthew G. Knepley   Input Parameters:
4954bb7acecfSBarry Smith + dm    - The `DM`
495544a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4956bb7acecfSBarry Smith - disc - The discretization object
495744a7f3ddSMatthew G. Knepley 
495844a7f3ddSMatthew G. Knepley   Level: intermediate
495944a7f3ddSMatthew G. Knepley 
4960bb7acecfSBarry Smith   Notes:
4961bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
4962bb7acecfSBarry Smith 
4963bb7acecfSBarry Smith   For example, a piecewise continous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
4964bb7acecfSBarry 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
4965bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
4966bb7acecfSBarry Smith 
4967bb7acecfSBarry Smith .seealso: `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
496844a7f3ddSMatthew G. Knepley @*/
4969d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
4970d71ae5a4SJacob Faibussowitsch {
497144a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
497244a7f3ddSMatthew G. Knepley 
497344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
497444a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4975064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4976bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
49779566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
497844a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
4979bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
49809566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4981bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
4982bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
49839566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
4984af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4985af122d2aSMatthew G Knepley }
49866636e97aSMatthew G Knepley 
4987e5e52638SMatthew G. Knepley /*@
4988e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4989e0b68406SMatthew Knepley 
4990e0b68406SMatthew Knepley   Logically collective on dm
4991e0b68406SMatthew Knepley 
4992e0b68406SMatthew Knepley   Input Parameters:
4993bb7acecfSBarry Smith + dm          - The `DM`
4994e0b68406SMatthew Knepley . f           - The field index
4995bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
4996e0b68406SMatthew Knepley 
4997e0b68406SMatthew Knepley   Level: intermediate
4998e0b68406SMatthew Knepley 
4999db781477SPatrick Sanan .seealso: `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5000e0b68406SMatthew Knepley @*/
5001d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5002d71ae5a4SJacob Faibussowitsch {
5003e0b68406SMatthew Knepley   PetscFunctionBegin;
500463a3b9bcSJacob 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);
5005e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
5006e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5007e0b68406SMatthew Knepley }
5008e0b68406SMatthew Knepley 
5009e0b68406SMatthew Knepley /*@
5010e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5011e0b68406SMatthew Knepley 
5012bb7acecfSBarry Smith   Not collective
5013e0b68406SMatthew Knepley 
5014e0b68406SMatthew Knepley   Input Parameters:
5015bb7acecfSBarry Smith + dm          - The `DM`
5016e0b68406SMatthew Knepley - f           - The field index
5017e0b68406SMatthew Knepley 
5018e0b68406SMatthew Knepley   Output Parameter:
5019e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5020e0b68406SMatthew Knepley 
5021e0b68406SMatthew Knepley   Level: intermediate
5022e0b68406SMatthew Knepley 
5023bb7acecfSBarry Smith  .seealso: `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5024e0b68406SMatthew Knepley @*/
5025d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5026d71ae5a4SJacob Faibussowitsch {
5027e0b68406SMatthew Knepley   PetscFunctionBegin;
502863a3b9bcSJacob 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);
5029e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
5030e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5031e0b68406SMatthew Knepley }
5032e0b68406SMatthew Knepley 
5033e0b68406SMatthew Knepley /*@
5034bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5035e5e52638SMatthew G. Knepley 
5036d083f849SBarry Smith   Collective on dm
5037e5e52638SMatthew G. Knepley 
5038e5e52638SMatthew G. Knepley   Input Parameter:
5039bb7acecfSBarry Smith . dm - The `DM`
5040e5e52638SMatthew G. Knepley 
5041e5e52638SMatthew G. Knepley   Output Parameter:
5042bb7acecfSBarry Smith . newdm - The `DM`
5043e5e52638SMatthew G. Knepley 
5044e5e52638SMatthew G. Knepley   Level: advanced
5045e5e52638SMatthew G. Knepley 
5046db781477SPatrick Sanan .seealso: `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5047e5e52638SMatthew G. Knepley @*/
5048d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5049d71ae5a4SJacob Faibussowitsch {
5050e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5051e5e52638SMatthew G. Knepley 
5052e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5053e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
50549566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
50559566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5056e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5057e5e52638SMatthew G. Knepley     DMLabel     label;
5058e5e52638SMatthew G. Knepley     PetscObject field;
505934aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5060e5e52638SMatthew G. Knepley 
50619566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
50629566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
50639566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
50649566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
506534aa8a36SMatthew G. Knepley   }
506634aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
506734aa8a36SMatthew G. Knepley }
506834aa8a36SMatthew G. Knepley 
506934aa8a36SMatthew G. Knepley /*@
507034aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
507134aa8a36SMatthew G. Knepley 
507234aa8a36SMatthew G. Knepley   Not collective
507334aa8a36SMatthew G. Knepley 
507434aa8a36SMatthew G. Knepley   Input Parameters:
507534aa8a36SMatthew G. Knepley + dm - The DM object
507634aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
507734aa8a36SMatthew G. Knepley 
5078d8d19677SJose E. Roman   Output Parameters:
507934aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
508034aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
508134aa8a36SMatthew G. Knepley 
508234aa8a36SMatthew G. Knepley   Notes:
508334aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
508434aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
508534aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5086979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
508734aa8a36SMatthew G. Knepley 
508834aa8a36SMatthew G. Knepley   Level: developer
508934aa8a36SMatthew G. Knepley 
5090db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
509134aa8a36SMatthew G. Knepley @*/
5092d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5093d71ae5a4SJacob Faibussowitsch {
509434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
509534aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5096534a8f05SLisandro Dalcin   if (useCone) PetscValidBoolPointer(useCone, 3);
5097534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
509834aa8a36SMatthew G. Knepley   if (f < 0) {
509934aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
510034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
510134aa8a36SMatthew G. Knepley   } else {
510234aa8a36SMatthew G. Knepley     PetscInt Nf;
510334aa8a36SMatthew G. Knepley 
51049566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51057a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
510634aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
510734aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
510834aa8a36SMatthew G. Knepley   }
510934aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
511034aa8a36SMatthew G. Knepley }
511134aa8a36SMatthew G. Knepley 
511234aa8a36SMatthew G. Knepley /*@
511334aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
511434aa8a36SMatthew G. Knepley 
511534aa8a36SMatthew G. Knepley   Not collective
511634aa8a36SMatthew G. Knepley 
511734aa8a36SMatthew G. Knepley   Input Parameters:
511834aa8a36SMatthew G. Knepley + dm         - The DM object
511934aa8a36SMatthew G. Knepley . f          - The field number
512034aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
512134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
512234aa8a36SMatthew G. Knepley 
512334aa8a36SMatthew G. Knepley   Notes:
512434aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
512534aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
512634aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5127979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
512834aa8a36SMatthew G. Knepley 
512934aa8a36SMatthew G. Knepley   Level: developer
513034aa8a36SMatthew G. Knepley 
5131db781477SPatrick Sanan .seealso: `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
513234aa8a36SMatthew G. Knepley @*/
5133d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5134d71ae5a4SJacob Faibussowitsch {
513534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
513634aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
513734aa8a36SMatthew G. Knepley   if (f < 0) {
513834aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
513934aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
514034aa8a36SMatthew G. Knepley   } else {
514134aa8a36SMatthew G. Knepley     PetscInt Nf;
514234aa8a36SMatthew G. Knepley 
51439566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51447a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
514534aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
514634aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5147e5e52638SMatthew G. Knepley   }
5148e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5149e5e52638SMatthew G. Knepley }
5150e5e52638SMatthew G. Knepley 
5151b0441da4SMatthew G. Knepley /*@
5152b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5153b0441da4SMatthew G. Knepley 
5154b0441da4SMatthew G. Knepley   Not collective
5155b0441da4SMatthew G. Knepley 
5156f899ff85SJose E. Roman   Input Parameter:
5157b0441da4SMatthew G. Knepley . dm - The DM object
5158b0441da4SMatthew G. Knepley 
5159d8d19677SJose E. Roman   Output Parameters:
5160b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5161b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5162b0441da4SMatthew G. Knepley 
5163b0441da4SMatthew G. Knepley   Notes:
5164b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5165b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5166b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5167b0441da4SMatthew G. Knepley 
5168b0441da4SMatthew G. Knepley   Level: developer
5169b0441da4SMatthew G. Knepley 
5170db781477SPatrick Sanan .seealso: `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5171b0441da4SMatthew G. Knepley @*/
5172d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5173d71ae5a4SJacob Faibussowitsch {
5174b0441da4SMatthew G. Knepley   PetscInt Nf;
5175b0441da4SMatthew G. Knepley 
5176b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5177b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5178064a246eSJacob Faibussowitsch   if (useCone) PetscValidBoolPointer(useCone, 2);
5179064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
51809566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5181b0441da4SMatthew G. Knepley   if (!Nf) {
51829566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5183b0441da4SMatthew G. Knepley   } else {
51849566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5185b0441da4SMatthew G. Knepley   }
5186b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5187b0441da4SMatthew G. Knepley }
5188b0441da4SMatthew G. Knepley 
5189b0441da4SMatthew G. Knepley /*@
5190b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5191b0441da4SMatthew G. Knepley 
5192b0441da4SMatthew G. Knepley   Not collective
5193b0441da4SMatthew G. Knepley 
5194b0441da4SMatthew G. Knepley   Input Parameters:
5195b0441da4SMatthew G. Knepley + dm         - The DM object
5196b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5197b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5198b0441da4SMatthew G. Knepley 
5199b0441da4SMatthew G. Knepley   Notes:
5200b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5201b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5202b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5203b0441da4SMatthew G. Knepley 
5204b0441da4SMatthew G. Knepley   Level: developer
5205b0441da4SMatthew G. Knepley 
5206db781477SPatrick Sanan .seealso: `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5207b0441da4SMatthew G. Knepley @*/
5208d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5209d71ae5a4SJacob Faibussowitsch {
5210b0441da4SMatthew G. Knepley   PetscInt Nf;
5211b0441da4SMatthew G. Knepley 
5212b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5213b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52149566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5215b0441da4SMatthew G. Knepley   if (!Nf) {
52169566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5217b0441da4SMatthew G. Knepley   } else {
52189566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5219e5e52638SMatthew G. Knepley   }
5220e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5221e5e52638SMatthew G. Knepley }
5222e5e52638SMatthew G. Knepley 
5223d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5224d71ae5a4SJacob Faibussowitsch {
5225799db056SMatthew G. Knepley   DM           plex;
5226799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5227799db056SMatthew G. Knepley   const char **names;
5228799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5229799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5230799db056SMatthew G. Knepley   size_t       len;
5231799db056SMatthew G. Knepley   MPI_Comm     comm;
5232799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5233783e2ec8SMatthew G. Knepley 
5234783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5235799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5236799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5237799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5238799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5239799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5240799db056SMatthew G. Knepley     PetscDS  dsBC;
5241799db056SMatthew G. Knepley     PetscInt numBd;
5242799db056SMatthew G. Knepley 
5243799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5244799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5245799db056SMatthew G. Knepley     maxLabels += numBd;
5246799db056SMatthew G. Knepley   }
5247799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5248799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5249799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5250799db056SMatthew G. Knepley     PetscDS  dsBC;
5251799db056SMatthew G. Knepley     PetscInt numBd, bd;
5252799db056SMatthew G. Knepley 
5253799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5254799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5255799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5256799db056SMatthew G. Knepley       DMLabel      label;
5257799db056SMatthew G. Knepley       PetscInt     field;
5258799db056SMatthew G. Knepley       PetscObject  obj;
5259799db056SMatthew G. Knepley       PetscClassId id;
5260799db056SMatthew G. Knepley 
5261799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
52629566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
52639566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5264799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
52659371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
52669371c9d4SSatish Balay         if (labels[l] == label) break;
5267799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5268783e2ec8SMatthew G. Knepley     }
5269799db056SMatthew G. Knepley   }
5270799db056SMatthew G. Knepley   /* Get label names */
5271799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5272799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
52739371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
52749371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
52759371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
52769371c9d4SSatish Balay   }
5277799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5278799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5279799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5280799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscStrcpy(&sendNames[gmaxLen * l], names[l]));
5281799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5282799db056SMatthew G. Knepley   /* Put all names on all processes */
5283799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5284799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5285799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5286799db056SMatthew G. Knepley   gNl = displs[size];
52879371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
52889371c9d4SSatish Balay     counts[p] *= gmaxLen;
52899371c9d4SSatish Balay     displs[p] *= gmaxLen;
52909371c9d4SSatish Balay   }
5291799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5292799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5293799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5294799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5295799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5296799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5297799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
52989371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
52999371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
53009566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5301799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
53029566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5303799db056SMatthew G. Knepley     ++gl;
5304783e2ec8SMatthew G. Knepley   }
5305799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
5306783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5307783e2ec8SMatthew G. Knepley }
5308783e2ec8SMatthew G. Knepley 
5309d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5310d71ae5a4SJacob Faibussowitsch {
5311e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5312e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5313e5e52638SMatthew G. Knepley 
5314e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5315e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
53169566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5317e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
53189371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
53199371c9d4SSatish Balay     tmpd[s].ds     = NULL;
53209371c9d4SSatish Balay     tmpd[s].label  = NULL;
53219371c9d4SSatish Balay     tmpd[s].fields = NULL;
53229371c9d4SSatish Balay   }
53239566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5324e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5325e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5326e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5327e5e52638SMatthew G. Knepley }
5328e5e52638SMatthew G. Knepley 
5329e5e52638SMatthew G. Knepley /*@
5330e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5331e5e52638SMatthew G. Knepley 
5332e5e52638SMatthew G. Knepley   Not collective
5333e5e52638SMatthew G. Knepley 
5334e5e52638SMatthew G. Knepley   Input Parameter:
5335e5e52638SMatthew G. Knepley . dm - The DM
5336e5e52638SMatthew G. Knepley 
5337e5e52638SMatthew G. Knepley   Output Parameter:
5338e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5339e5e52638SMatthew G. Knepley 
5340e5e52638SMatthew G. Knepley   Level: intermediate
5341e5e52638SMatthew G. Knepley 
5342db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMGetCellDS()`
5343e5e52638SMatthew G. Knepley @*/
5344d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5345d71ae5a4SJacob Faibussowitsch {
5346e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5347e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5348534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5349e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5350e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5351e5e52638SMatthew G. Knepley }
5352e5e52638SMatthew G. Knepley 
5353e5e52638SMatthew G. Knepley /*@
5354e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5355e5e52638SMatthew G. Knepley 
5356d083f849SBarry Smith   Logically collective on dm
5357e5e52638SMatthew G. Knepley 
5358e5e52638SMatthew G. Knepley   Input Parameter:
5359e5e52638SMatthew G. Knepley . dm - The DM
5360e5e52638SMatthew G. Knepley 
5361e5e52638SMatthew G. Knepley   Level: intermediate
5362e5e52638SMatthew G. Knepley 
5363db781477SPatrick Sanan .seealso: `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5364e5e52638SMatthew G. Knepley @*/
5365d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5366d71ae5a4SJacob Faibussowitsch {
5367e5e52638SMatthew G. Knepley   PetscInt s;
5368e5e52638SMatthew G. Knepley 
5369e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5370e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5371e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
53729566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
53739566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
53749566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5375e5e52638SMatthew G. Knepley   }
53769566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5377e5e52638SMatthew G. Knepley   dm->probs = NULL;
5378e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5379e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5380e5e52638SMatthew G. Knepley }
5381e5e52638SMatthew G. Knepley 
5382e5e52638SMatthew G. Knepley /*@
5383e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5384e5e52638SMatthew G. Knepley 
5385e5e52638SMatthew G. Knepley   Not collective
5386e5e52638SMatthew G. Knepley 
5387e5e52638SMatthew G. Knepley   Input Parameter:
5388e5e52638SMatthew G. Knepley . dm    - The DM
5389e5e52638SMatthew G. Knepley 
5390e5e52638SMatthew G. Knepley   Output Parameter:
5391e5e52638SMatthew G. Knepley . prob - The default PetscDS
5392e5e52638SMatthew G. Knepley 
5393e5e52638SMatthew G. Knepley   Level: intermediate
5394e5e52638SMatthew G. Knepley 
5395db781477SPatrick Sanan .seealso: `DMGetCellDS()`, `DMGetRegionDS()`
5396e5e52638SMatthew G. Knepley @*/
5397d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5398d71ae5a4SJacob Faibussowitsch {
5399e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5400e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5401e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5402b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5403b0143b4dSMatthew G. Knepley     PetscDS ds;
5404b0143b4dSMatthew G. Knepley 
54059566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
54069566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, NULL, ds));
54079566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5408b0143b4dSMatthew G. Knepley   }
5409b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5410e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5411e5e52638SMatthew G. Knepley }
5412e5e52638SMatthew G. Knepley 
5413e5e52638SMatthew G. Knepley /*@
5414e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5415e5e52638SMatthew G. Knepley 
5416e5e52638SMatthew G. Knepley   Not collective
5417e5e52638SMatthew G. Knepley 
5418e5e52638SMatthew G. Knepley   Input Parameters:
5419e5e52638SMatthew G. Knepley + dm    - The DM
5420e5e52638SMatthew G. Knepley - point - Cell for the DS
5421e5e52638SMatthew G. Knepley 
5422e5e52638SMatthew G. Knepley   Output Parameter:
5423e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5424e5e52638SMatthew G. Knepley 
5425e5e52638SMatthew G. Knepley   Level: developer
5426e5e52638SMatthew G. Knepley 
5427db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMSetRegionDS()`
5428e5e52638SMatthew G. Knepley @*/
5429d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5430d71ae5a4SJacob Faibussowitsch {
5431e5e52638SMatthew G. Knepley   PetscDS  probDef = NULL;
5432e5e52638SMatthew G. Knepley   PetscInt s;
5433e5e52638SMatthew G. Knepley 
5434e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5435e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5436e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
543763a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
5438e5e52638SMatthew G. Knepley   *prob = NULL;
5439e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5440e5e52638SMatthew G. Knepley     PetscInt val;
5441e5e52638SMatthew G. Knepley 
54429371c9d4SSatish Balay     if (!dm->probs[s].label) {
54439371c9d4SSatish Balay       probDef = dm->probs[s].ds;
54449371c9d4SSatish Balay     } else {
54459566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
54469371c9d4SSatish Balay       if (val >= 0) {
54479371c9d4SSatish Balay         *prob = dm->probs[s].ds;
54489371c9d4SSatish Balay         break;
54499371c9d4SSatish Balay       }
5450e5e52638SMatthew G. Knepley     }
5451e5e52638SMatthew G. Knepley   }
5452e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5453e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5454e5e52638SMatthew G. Knepley }
5455e5e52638SMatthew G. Knepley 
5456e5e52638SMatthew G. Knepley /*@
5457e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5458e5e52638SMatthew G. Knepley 
5459e5e52638SMatthew G. Knepley   Not collective
5460e5e52638SMatthew G. Knepley 
5461e5e52638SMatthew G. Knepley   Input Parameters:
5462e5e52638SMatthew G. Knepley + dm    - The DM
5463e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5464e5e52638SMatthew G. Knepley 
5465b3cf3223SMatthew G. Knepley   Output Parameters:
5466b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5467b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5468e5e52638SMatthew G. Knepley 
5469154ca461SJed Brown   Note:
5470154ca461SJed Brown   If a non-NULL label is given, but there is no PetscDS on that specific label,
5471154ca461SJed Brown   the PetscDS for the full domain (if present) is returned. Returns with
5472154ca461SJed Brown   fields=NULL and prob=NULL if there is no PetscDS for the full domain.
5473e5e52638SMatthew G. Knepley 
5474e5e52638SMatthew G. Knepley   Level: advanced
5475e5e52638SMatthew G. Knepley 
5476db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5477e5e52638SMatthew G. Knepley @*/
5478d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5479d71ae5a4SJacob Faibussowitsch {
5480e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5481e5e52638SMatthew G. Knepley 
5482e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5483e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5484e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
54859371c9d4SSatish Balay   if (fields) {
54869371c9d4SSatish Balay     PetscValidPointer(fields, 3);
54879371c9d4SSatish Balay     *fields = NULL;
54889371c9d4SSatish Balay   }
54899371c9d4SSatish Balay   if (ds) {
54909371c9d4SSatish Balay     PetscValidPointer(ds, 4);
54919371c9d4SSatish Balay     *ds = NULL;
54929371c9d4SSatish Balay   }
5493e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5494154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5495b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5496b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
5497154ca461SJed Brown       if (dm->probs[s].label) PetscFunctionReturn(0);
5498b3cf3223SMatthew G. Knepley     }
5499e5e52638SMatthew G. Knepley   }
55002df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5501e5e52638SMatthew G. Knepley }
5502e5e52638SMatthew G. Knepley 
5503e5e52638SMatthew G. Knepley /*@
5504bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5505083401c6SMatthew G. Knepley 
5506083401c6SMatthew G. Knepley   Collective on dm
5507083401c6SMatthew G. Knepley 
5508083401c6SMatthew G. Knepley   Input Parameters:
5509bb7acecfSBarry Smith + dm     - The `DM`
5510bb7acecfSBarry Smith . label  - The `DMLabel` defining the mesh region, or NULL for the entire mesh
5511bb7acecfSBarry Smith . fields - The IS containing the `DM` field numbers for the fields in this `PetscDS`, or NULL for all fields
5512bb7acecfSBarry Smith - prob   - The `PetscDS` defined on the given region
5513083401c6SMatthew G. Knepley 
5514bb7acecfSBarry Smith   Note:
5515bb7acecfSBarry 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,
5516083401c6SMatthew G. Knepley   the fields argument is ignored.
5517083401c6SMatthew G. Knepley 
5518083401c6SMatthew G. Knepley   Level: advanced
5519083401c6SMatthew G. Knepley 
5520db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5521083401c6SMatthew G. Knepley @*/
5522d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5523d71ae5a4SJacob Faibussowitsch {
5524083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5525083401c6SMatthew G. Knepley 
5526083401c6SMatthew G. Knepley   PetscFunctionBegin;
5527083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5528083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5529064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5530083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5531083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
55329566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
5533083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5534083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5535083401c6SMatthew G. Knepley     }
5536083401c6SMatthew G. Knepley   }
55379566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
55389566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
55399566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
55409566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
5541083401c6SMatthew G. Knepley   if (!label) {
5542083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5543083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5544083401c6SMatthew G. Knepley     Nds = 0;
5545083401c6SMatthew G. Knepley   }
5546083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5547083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5548083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5549083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5550083401c6SMatthew G. Knepley }
5551083401c6SMatthew G. Knepley 
5552083401c6SMatthew G. Knepley /*@
5553e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5554e5e52638SMatthew G. Knepley 
5555e5e52638SMatthew G. Knepley   Not collective
5556e5e52638SMatthew G. Knepley 
5557e5e52638SMatthew G. Knepley   Input Parameters:
5558e5e52638SMatthew G. Knepley + dm  - The DM
5559e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5560e5e52638SMatthew G. Knepley 
5561e5e52638SMatthew G. Knepley   Output Parameters:
5562b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5563b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5564083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5565e5e52638SMatthew G. Knepley 
5566e5e52638SMatthew G. Knepley   Level: advanced
5567e5e52638SMatthew G. Knepley 
5568db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5569e5e52638SMatthew G. Knepley @*/
5570d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5571d71ae5a4SJacob Faibussowitsch {
5572e5e52638SMatthew G. Knepley   PetscInt Nds;
5573e5e52638SMatthew G. Knepley 
5574e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5575e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55769566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
557763a3b9bcSJacob 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);
5578e5e52638SMatthew G. Knepley   if (label) {
5579e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5580e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5581e5e52638SMatthew G. Knepley   }
5582b3cf3223SMatthew G. Knepley   if (fields) {
5583b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5584b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5585b3cf3223SMatthew G. Knepley   }
5586e5e52638SMatthew G. Knepley   if (ds) {
5587b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5588e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5589e5e52638SMatthew G. Knepley   }
5590e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5591e5e52638SMatthew G. Knepley }
5592e5e52638SMatthew G. Knepley 
5593e5e52638SMatthew G. Knepley /*@
5594083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5595e5e52638SMatthew G. Knepley 
5596083401c6SMatthew G. Knepley   Not collective
5597e5e52638SMatthew G. Knepley 
5598e5e52638SMatthew G. Knepley   Input Parameters:
5599e5e52638SMatthew G. Knepley + dm     - The DM
5600083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5601083401c6SMatthew G. Knepley . label  - The region label, or NULL
5602083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5603083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5604e5e52638SMatthew G. Knepley 
5605e5e52638SMatthew G. Knepley   Level: advanced
5606e5e52638SMatthew G. Knepley 
5607db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5608e5e52638SMatthew G. Knepley @*/
5609d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5610d71ae5a4SJacob Faibussowitsch {
5611083401c6SMatthew G. Knepley   PetscInt Nds;
5612e5e52638SMatthew G. Knepley 
5613e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5614e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5615ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
56169566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
561763a3b9bcSJacob 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);
56189566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56199566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5620083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5621083401c6SMatthew G. Knepley   if (fields) {
5622083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
56239566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
56249566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5625083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5626e5e52638SMatthew G. Knepley   }
5627083401c6SMatthew G. Knepley   if (ds) {
5628083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
56299566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
56309566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5631083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5632083401c6SMatthew G. Knepley   }
5633e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5634e5e52638SMatthew G. Knepley }
5635e5e52638SMatthew G. Knepley 
5636e5e52638SMatthew G. Knepley /*@
56371d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
56381d3af9e0SMatthew G. Knepley 
56391d3af9e0SMatthew G. Knepley   Not collective
56401d3af9e0SMatthew G. Knepley 
56411d3af9e0SMatthew G. Knepley   Input Parameters:
56421d3af9e0SMatthew G. Knepley + dm  - The DM
56431d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
56441d3af9e0SMatthew G. Knepley 
56451d3af9e0SMatthew G. Knepley   Output Parameter:
56461d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
56471d3af9e0SMatthew G. Knepley 
56481d3af9e0SMatthew G. Knepley   Level: advanced
56491d3af9e0SMatthew G. Knepley 
5650db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
56511d3af9e0SMatthew G. Knepley @*/
5652d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5653d71ae5a4SJacob Faibussowitsch {
56541d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
56551d3af9e0SMatthew G. Knepley 
56561d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
56571d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56581d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
5659dadcf809SJacob Faibussowitsch   PetscValidIntPointer(num, 3);
56609566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
56619371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
56629371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
56631d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
56641d3af9e0SMatthew G. Knepley   else *num = n;
56651d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
56661d3af9e0SMatthew G. Knepley }
56671d3af9e0SMatthew G. Knepley 
56682df84da0SMatthew G. Knepley /*@C
5669bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
56702df84da0SMatthew G. Knepley 
56712df84da0SMatthew G. Knepley   Not collective
56722df84da0SMatthew G. Knepley 
5673f1a722f8SMatthew G. Knepley   Input Parameters:
5674bb7acecfSBarry Smith + dm     - The `DM`
56752df84da0SMatthew G. Knepley . Nc     - The number of components for the field
5676bb7acecfSBarry Smith . prefix - The options prefix for the output `PetscFE`, or NULL
5677bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
56782df84da0SMatthew G. Knepley 
56792df84da0SMatthew G. Knepley   Output Parameter:
5680bb7acecfSBarry Smith . fem - The `PetscFE`
56812df84da0SMatthew G. Knepley 
5682bb7acecfSBarry Smith   Note:
5683bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
56842df84da0SMatthew G. Knepley 
56852df84da0SMatthew G. Knepley   Level: intermediate
56862df84da0SMatthew G. Knepley 
5687db781477SPatrick Sanan .seealso: `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
56882df84da0SMatthew G. Knepley @*/
5689d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5690d71ae5a4SJacob Faibussowitsch {
56912df84da0SMatthew G. Knepley   DMPolytopeType ct;
56922df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
56932df84da0SMatthew G. Knepley 
56942df84da0SMatthew G. Knepley   PetscFunctionBegin;
56952df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56962df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
56972df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
56982df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
56992df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
57009566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
57019566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
57029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
57039566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
57042df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
57052df84da0SMatthew G. Knepley }
57062df84da0SMatthew G. Knepley 
57071d3af9e0SMatthew G. Knepley /*@
5708bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5709e5e52638SMatthew G. Knepley 
5710d083f849SBarry Smith   Collective on dm
5711e5e52638SMatthew G. Knepley 
5712e5e52638SMatthew G. Knepley   Input Parameter:
5713bb7acecfSBarry Smith . dm - The `DM`
5714e5e52638SMatthew G. Knepley 
571545480ffeSMatthew G. Knepley   Options Database Keys:
5716bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
571745480ffeSMatthew G. Knepley 
5718bb7acecfSBarry Smith   Note:
5719bb7acecfSBarry Smith   If the label has a `PetscDS` defined, it will be replaced. Otherwise, it will be added to the `DM`.
5720e5e52638SMatthew G. Knepley 
5721e5e52638SMatthew G. Knepley   Level: intermediate
5722e5e52638SMatthew G. Knepley 
5723db781477SPatrick Sanan .seealso: `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5724e5e52638SMatthew G. Knepley @*/
5725d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5726d71ae5a4SJacob Faibussowitsch {
5727e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5728083401c6SMatthew G. Knepley   PetscDS   dsDef;
5729083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5730f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5731f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5732e5e52638SMatthew G. Knepley 
5733e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5734e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5735e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
57369566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
57379566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5738083401c6SMatthew G. Knepley   /* Determine how many regions we have */
57399566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5740083401c6SMatthew G. Knepley   Nl   = 0;
5741083401c6SMatthew G. Knepley   Ndef = 0;
5742083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5743083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5744083401c6SMatthew G. Knepley     PetscInt l;
5745083401c6SMatthew G. Knepley 
5746f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5747f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5748f918ec44SMatthew G. Knepley     {
5749f918ec44SMatthew G. Knepley       PetscClassId id;
5750f918ec44SMatthew G. Knepley 
57519566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5752f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5753f918ec44SMatthew G. Knepley         Ceed ceed;
5754f918ec44SMatthew G. Knepley 
57559566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
57569566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5757f918ec44SMatthew G. Knepley       }
5758f918ec44SMatthew G. Knepley     }
5759f918ec44SMatthew G. Knepley #endif
57609371c9d4SSatish Balay     if (!label) {
57619371c9d4SSatish Balay       ++Ndef;
57629371c9d4SSatish Balay       continue;
57639371c9d4SSatish Balay     }
57649371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
57659371c9d4SSatish Balay       if (label == labelSet[l]) break;
5766083401c6SMatthew G. Knepley     if (l < Nl) continue;
5767083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5768083401c6SMatthew G. Knepley   }
5769083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
57709566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
5771083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5772b3cf3223SMatthew G. Knepley     IS        fields;
5773b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5774b3cf3223SMatthew G. Knepley 
57759371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
57769371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
57777a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
57789566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
57799371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
57809371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
57819566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
57829566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
57839566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
57849566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
578588f0c812SMatthew G. Knepley 
57869566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
57879566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef));
57889566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
57899566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
57902df9ee95SMatthew G. Knepley   }
57919566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
57929566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5793083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5794083401c6SMatthew G. Knepley   if (Ndef && Nl) {
57950122748bSMatthew G. Knepley     DM              plex;
5796083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5797083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5798083401c6SMatthew G. Knepley     PetscInt       *fields;
5799083401c6SMatthew G. Knepley     const PetscInt *cells;
5800083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
58010122748bSMatthew G. Knepley 
58029566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
58039566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
58049566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
58059566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
58065fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5807083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5808083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5809083401c6SMatthew G. Knepley       IS      pointIS;
5810083401c6SMatthew G. Knepley 
58119566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
58129566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
58139566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
58149566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5815083401c6SMatthew G. Knepley     }
58169566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5817083401c6SMatthew G. Knepley 
58189566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
58199566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
58209566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
58219566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
58229566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
58239566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
58249566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5825083401c6SMatthew G. Knepley 
58269566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
58279371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
58289371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
58299566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
58309566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
58319566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
58329566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5833083401c6SMatthew G. Knepley 
58349566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
58359566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef));
58369566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
58379566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
58389566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58399566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
58409566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5841083401c6SMatthew G. Knepley   }
5842083401c6SMatthew G. Knepley   /* Create label DSes
5843083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5844083401c6SMatthew G. Knepley   */
5845083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5846083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5847083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5848083401c6SMatthew G. Knepley     PetscDS   ds;
5849083401c6SMatthew G. Knepley     IS        fields;
5850083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5851083401c6SMatthew G. Knepley 
58529566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
58539371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58549371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
58559566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
58569371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58579371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
58589566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58599566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58609566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58619566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
58629566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, label, fields, ds));
58639566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
58649566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5865083401c6SMatthew G. Knepley     {
5866083401c6SMatthew G. Knepley       DMPolytopeType ct;
5867083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
58685fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
58690122748bSMatthew G. Knepley 
58709566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5871665f567fSMatthew G. Knepley       if (lStart >= 0) {
58729566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5873412e9a14SMatthew G. Knepley         switch (ct) {
5874412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5875412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5876412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
5877d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5878d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
5879d71ae5a4SJacob Faibussowitsch           break;
5880d71ae5a4SJacob Faibussowitsch         default:
5881d71ae5a4SJacob Faibussowitsch           break;
5882412e9a14SMatthew G. Knepley         }
5883665f567fSMatthew G. Knepley       }
58849566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
58855fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
58865fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
58875fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
58889566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
58899566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
58905fedec97SMatthew G. Knepley           }
58915fedec97SMatthew G. Knepley           ++nf;
58925fedec97SMatthew G. Knepley         }
58935fedec97SMatthew G. Knepley       }
5894e5e52638SMatthew G. Knepley     }
58959566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5896e5e52638SMatthew G. Knepley   }
58979566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5898e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5899083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5900083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5901083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5902083401c6SMatthew G. Knepley     const PetscInt *fld;
59035fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
59045fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5905e5e52638SMatthew G. Knepley 
59069566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
59079566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
59089566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
59099566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5910083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5911083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
59125fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5913e5e52638SMatthew G. Knepley       PetscClassId id;
5914e5e52638SMatthew G. Knepley 
59155fedec97SMatthew G. Knepley       /* Handle DS with no fields */
59169566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
59175fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
59189566063dSJacob Faibussowitsch       if (isCohesive && !isCohesiveField) PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&disc));
59199566063dSJacob Faibussowitsch       PetscCall(PetscDSSetDiscretization(ds, f, disc));
5920083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
59219566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5922e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5923e5e52638SMatthew G. Knepley     }
59249566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
5925e5e52638SMatthew G. Knepley   }
5926f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
59279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
5928f9244615SMatthew G. Knepley   if (flg) {
59293b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
59303b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
59313b4aee56SMatthew G. Knepley       PetscInt Nf, f;
59323b4aee56SMatthew G. Knepley 
59339566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
59349566063dSJacob Faibussowitsch       for (f = 0; f < Nf; ++f) PetscCall(PetscDSSetJetDegree(ds, f, k));
59353b4aee56SMatthew G. Knepley     }
5936f9244615SMatthew G. Knepley   }
5937e5e52638SMatthew G. Knepley   /* Setup DSes */
5938e5e52638SMatthew G. Knepley   if (doSetup) {
59399566063dSJacob Faibussowitsch     for (s = 0; s < dm->Nds; ++s) PetscCall(PetscDSSetUp(dm->probs[s].ds));
5940e5e52638SMatthew G. Knepley   }
5941e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5942e5e52638SMatthew G. Knepley }
5943e5e52638SMatthew G. Knepley 
5944e5e52638SMatthew G. Knepley /*@
5945bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
59467f96f943SMatthew G. Knepley 
5947bb7acecfSBarry Smith   Collective on `DM`
5948f2cacb80SMatthew G. Knepley 
59497f96f943SMatthew G. Knepley   Input Parameters:
5950bb7acecfSBarry Smith + dm   - The `DM`
59517f96f943SMatthew G. Knepley - time - The time
59527f96f943SMatthew G. Knepley 
59537f96f943SMatthew G. Knepley   Output Parameters:
5954f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5955f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
59567f96f943SMatthew G. Knepley 
5957bb7acecfSBarry Smith   Note:
5958bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
59597f96f943SMatthew G. Knepley 
59607f96f943SMatthew G. Knepley   Level: developer
59617f96f943SMatthew G. Knepley 
5962db781477SPatrick Sanan .seealso: `PetscDSSetExactSolution()`
59637f96f943SMatthew G. Knepley @*/
5964d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
5965d71ae5a4SJacob Faibussowitsch {
59667f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
59677f96f943SMatthew G. Knepley   void   **ectxs;
5968f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
59697f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
59707f96f943SMatthew G. Knepley 
59717f96f943SMatthew G. Knepley   PetscFunctionBegin;
5972f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5973f60fa741SMatthew G. Knepley   if (u) {
5974f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5975f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
5976f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
5977f60fa741SMatthew G. Knepley   }
5978f60fa741SMatthew G. Knepley   if (u_t) {
5979f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
5980f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
5981f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
5982f60fa741SMatthew G. Knepley   }
59839566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
59849566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
59859566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
59867f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
59877f96f943SMatthew G. Knepley     PetscDS         ds;
59887f96f943SMatthew G. Knepley     DMLabel         label;
59897f96f943SMatthew G. Knepley     IS              fieldIS;
59907f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
59917f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
59927f96f943SMatthew G. Knepley 
59939566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
59949566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
59959566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
59969566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
59979566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
5998f2cacb80SMatthew G. Knepley     if (u) {
5999f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6000f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6001f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
60027f96f943SMatthew G. Knepley     }
6003f2cacb80SMatthew G. Knepley     if (u_t) {
60049566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
60059566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6006f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6007f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6008f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6009f2cacb80SMatthew G. Knepley     }
60109566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6011f2cacb80SMatthew G. Knepley   }
6012f2cacb80SMatthew G. Knepley   if (u) {
60139566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
60149566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6015f2cacb80SMatthew G. Knepley   }
6016f2cacb80SMatthew G. Knepley   if (u_t) {
60179566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
60189566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6019f2cacb80SMatthew G. Knepley   }
60209566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6021f60fa741SMatthew G. Knepley   if (u) {
6022f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6023f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6024f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6025f60fa741SMatthew G. Knepley   }
6026f60fa741SMatthew G. Knepley   if (u_t) {
6027f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6028f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6029f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6030f60fa741SMatthew G. Knepley   }
60317f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
60327f96f943SMatthew G. Knepley }
60337f96f943SMatthew G. Knepley 
6034d71ae5a4SJacob Faibussowitsch PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
6035d71ae5a4SJacob Faibussowitsch {
603645480ffeSMatthew G. Knepley   PetscDS    dsNew;
603745480ffeSMatthew G. Knepley   DSBoundary b;
60386a02485aSMatthew G. Knepley   PetscInt   cdim, Nf, f, d;
60395fedec97SMatthew G. Knepley   PetscBool  isCohesive;
604045480ffeSMatthew G. Knepley   void      *ctx;
604145480ffeSMatthew G. Knepley 
604245480ffeSMatthew G. Knepley   PetscFunctionBegin;
60439566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
60449566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyConstants(ds, dsNew));
60459566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyExactSolutions(ds, dsNew));
60469566063dSJacob Faibussowitsch   PetscCall(PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew));
60479566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyEquations(ds, dsNew));
60489566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
604945480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
60509566063dSJacob Faibussowitsch     PetscCall(PetscDSGetContext(ds, f, &ctx));
60519566063dSJacob Faibussowitsch     PetscCall(PetscDSSetContext(dsNew, f, ctx));
60529566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCohesive(ds, f, &isCohesive));
60539566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCohesive(dsNew, f, isCohesive));
60546a02485aSMatthew G. Knepley     PetscCall(PetscDSGetJetDegree(ds, f, &d));
60556a02485aSMatthew G. Knepley     PetscCall(PetscDSSetJetDegree(dsNew, f, d));
605645480ffeSMatthew G. Knepley   }
605745480ffeSMatthew G. Knepley   if (Nf) {
60589566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCoordinateDimension(ds, &cdim));
60599566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsNew, cdim));
606045480ffeSMatthew G. Knepley   }
60619566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew));
606245480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
60639566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, b->lname, &b->label));
606445480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
60657a8be351SBarry Smith     //PetscCheck(b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
606645480ffeSMatthew G. Knepley   }
606745480ffeSMatthew G. Knepley 
60689566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(dm, label, fields, dsNew));
60699566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
607045480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
607145480ffeSMatthew G. Knepley }
607245480ffeSMatthew G. Knepley 
60737f96f943SMatthew G. Knepley /*@
6074bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6075e5e52638SMatthew G. Knepley 
6076d083f849SBarry Smith   Collective on dm
6077e5e52638SMatthew G. Knepley 
6078e5e52638SMatthew G. Knepley   Input Parameter:
6079bb7acecfSBarry Smith . dm - The `DM`
6080e5e52638SMatthew G. Knepley 
6081e5e52638SMatthew G. Knepley   Output Parameter:
6082bb7acecfSBarry Smith . newdm - The `DM`
6083e5e52638SMatthew G. Knepley 
6084e5e52638SMatthew G. Knepley   Level: advanced
6085e5e52638SMatthew G. Knepley 
6086db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6087e5e52638SMatthew G. Knepley @*/
6088d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6089d71ae5a4SJacob Faibussowitsch {
6090e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6091e5e52638SMatthew G. Knepley 
6092e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6093e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
60949566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
60959566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6096e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6097e5e52638SMatthew G. Knepley     DMLabel  label;
6098b3cf3223SMatthew G. Knepley     IS       fields;
609945480ffeSMatthew G. Knepley     PetscDS  ds, newds;
6100783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6101e5e52638SMatthew G. Knepley 
61029566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds));
6103b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
61049566063dSJacob Faibussowitsch     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds));
6105d5b43468SJose E. Roman     /* Complete new labels in the new DS */
61069566063dSJacob Faibussowitsch     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds));
61079566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6108783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6109b8025e53SMatthew G. Knepley       PetscWeakForm wf;
611045480ffeSMatthew G. Knepley       DMLabel       label;
6111783e2ec8SMatthew G. Knepley       PetscInt      field;
6112783e2ec8SMatthew G. Knepley 
61139566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
61149566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6115783e2ec8SMatthew G. Knepley     }
6116e5e52638SMatthew G. Knepley   }
6117799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
6118e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6119e5e52638SMatthew G. Knepley }
6120e5e52638SMatthew G. Knepley 
6121e5e52638SMatthew G. Knepley /*@
6122bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6123e5e52638SMatthew G. Knepley 
6124d083f849SBarry Smith   Collective on dm
6125e5e52638SMatthew G. Knepley 
6126e5e52638SMatthew G. Knepley   Input Parameter:
6127bb7acecfSBarry Smith . dm - The `DM`
6128e5e52638SMatthew G. Knepley 
6129e5e52638SMatthew G. Knepley   Output Parameter:
6130bb7acecfSBarry Smith . newdm - The `DM`
6131e5e52638SMatthew G. Knepley 
6132e5e52638SMatthew G. Knepley   Level: advanced
6133e5e52638SMatthew G. Knepley 
6134bb7acecfSBarry Smith   Developer Note:
6135bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6136bb7acecfSBarry Smith 
6137db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMCopyDS()`
6138e5e52638SMatthew G. Knepley @*/
6139d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6140d71ae5a4SJacob Faibussowitsch {
6141e5e52638SMatthew G. Knepley   PetscFunctionBegin;
61429566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
61439566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
6144e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6145e5e52638SMatthew G. Knepley }
6146e5e52638SMatthew G. Knepley 
6147c73cfb54SMatthew G. Knepley /*@
6148bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6149c73cfb54SMatthew G. Knepley 
6150c73cfb54SMatthew G. Knepley   Not collective
6151c73cfb54SMatthew G. Knepley 
6152c73cfb54SMatthew G. Knepley   Input Parameter:
6153bb7acecfSBarry Smith . dm - The `DM`
6154c73cfb54SMatthew G. Knepley 
6155c73cfb54SMatthew G. Knepley   Output Parameter:
6156c73cfb54SMatthew G. Knepley . dim - The topological dimension
6157c73cfb54SMatthew G. Knepley 
6158c73cfb54SMatthew G. Knepley   Level: beginner
6159c73cfb54SMatthew G. Knepley 
6160db781477SPatrick Sanan .seealso: `DMSetDimension()`, `DMCreate()`
6161c73cfb54SMatthew G. Knepley @*/
6162d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6163d71ae5a4SJacob Faibussowitsch {
6164c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6165c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6166534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6167c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6168c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6169c73cfb54SMatthew G. Knepley }
6170c73cfb54SMatthew G. Knepley 
6171c73cfb54SMatthew G. Knepley /*@
6172bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6173c73cfb54SMatthew G. Knepley 
6174c73cfb54SMatthew G. Knepley   Collective on dm
6175c73cfb54SMatthew G. Knepley 
6176c73cfb54SMatthew G. Knepley   Input Parameters:
6177bb7acecfSBarry Smith + dm - The `DM`
6178c73cfb54SMatthew G. Knepley - dim - The topological dimension
6179c73cfb54SMatthew G. Knepley 
6180c73cfb54SMatthew G. Knepley   Level: beginner
6181c73cfb54SMatthew G. Knepley 
6182db781477SPatrick Sanan .seealso: `DMGetDimension()`, `DMCreate()`
6183c73cfb54SMatthew G. Knepley @*/
6184d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6185d71ae5a4SJacob Faibussowitsch {
6186e5e52638SMatthew G. Knepley   PetscDS  ds;
618745480ffeSMatthew G. Knepley   PetscInt Nds, n;
6188f17e8794SMatthew G. Knepley 
6189c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6190c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6191c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6192c73cfb54SMatthew G. Knepley   dm->dim = dim;
6193d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
61949566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
619545480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
61969566063dSJacob Faibussowitsch       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
61979566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
619845480ffeSMatthew G. Knepley     }
6199d17bd122SMatthew G. Knepley   }
6200c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6201c73cfb54SMatthew G. Knepley }
6202c73cfb54SMatthew G. Knepley 
6203793f3fe5SMatthew G. Knepley /*@
6204793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6205793f3fe5SMatthew G. Knepley 
6206d083f849SBarry Smith   Collective on dm
6207793f3fe5SMatthew G. Knepley 
6208793f3fe5SMatthew G. Knepley   Input Parameters:
6209bb7acecfSBarry Smith + dm - the `DM`
6210793f3fe5SMatthew G. Knepley - dim - the dimension
6211793f3fe5SMatthew G. Knepley 
6212793f3fe5SMatthew G. Knepley   Output Parameters:
6213793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6214aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6215793f3fe5SMatthew G. Knepley 
6216793f3fe5SMatthew G. Knepley   Note:
6217793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6218a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6219793f3fe5SMatthew G. Knepley   then the interval is empty.
6220793f3fe5SMatthew G. Knepley 
6221793f3fe5SMatthew G. Knepley   Level: intermediate
6222793f3fe5SMatthew G. Knepley 
6223db781477SPatrick Sanan .seealso: `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6224793f3fe5SMatthew G. Knepley @*/
6225d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6226d71ae5a4SJacob Faibussowitsch {
6227793f3fe5SMatthew G. Knepley   PetscInt d;
6228793f3fe5SMatthew G. Knepley 
6229793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6230793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62319566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
62327a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6233dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
6234793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6235793f3fe5SMatthew G. Knepley }
6236793f3fe5SMatthew G. Knepley 
62376636e97aSMatthew G Knepley /*@
6238bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6239f4d763aaSMatthew G. Knepley 
62408f700142SStefano Zampini   Collective on dm
62418f700142SStefano Zampini 
6242f4d763aaSMatthew G. Knepley   Input Parameter:
6243bb7acecfSBarry Smith . dm - The original `DM`
6244f4d763aaSMatthew G. Knepley 
6245f4d763aaSMatthew G. Knepley   Output Parameter:
6246bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6247f4d763aaSMatthew G. Knepley 
6248f4d763aaSMatthew G. Knepley   Level: intermediate
6249f4d763aaSMatthew G. Knepley 
6250bb7acecfSBarry Smith   Note:
6251bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6252bb7acecfSBarry 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
6253bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6254bb7acecfSBarry Smith 
6255bb7acecfSBarry Smith .seealso: `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6256f4d763aaSMatthew G. Knepley @*/
6257d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6258d71ae5a4SJacob Faibussowitsch {
6259c26acbdeSMatthew G. Knepley   PetscSection section;
62602d4e4a49SMatthew G. Knepley   PetscBool    hasConstraints, ghasConstraints;
626114f150ffSMatthew G. Knepley 
626214f150ffSMatthew G. Knepley   PetscFunctionBegin;
626314f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
626414f150ffSMatthew G. Knepley   PetscValidPointer(odm, 2);
62659566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
62669566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
62679566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
62682d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6269c26acbdeSMatthew G. Knepley     *odm = dm;
6270c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6271c26acbdeSMatthew G. Knepley   }
627214f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6273c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
627414f150ffSMatthew G. Knepley     PetscSF      sf;
627514f150ffSMatthew G. Knepley 
62769566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
62779566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
62789566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
62799566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
62809566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
62819566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
62829566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
62839566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
62849566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
628514f150ffSMatthew G. Knepley   }
628614f150ffSMatthew G. Knepley   *odm = dm->dmBC;
628714f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
628814f150ffSMatthew G. Knepley }
6289f4d763aaSMatthew G. Knepley 
6290f4d763aaSMatthew G. Knepley /*@
6291cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6292f4d763aaSMatthew G. Knepley 
6293f4d763aaSMatthew G. Knepley   Input Parameter:
6294bb7acecfSBarry Smith . dm - The original `DM`
6295f4d763aaSMatthew G. Knepley 
6296cdb7a50dSMatthew G. Knepley   Output Parameters:
6297cdb7a50dSMatthew G. Knepley + num - The output sequence number
6298cdb7a50dSMatthew G. Knepley - val - The output sequence value
6299f4d763aaSMatthew G. Knepley 
6300f4d763aaSMatthew G. Knepley   Level: intermediate
6301f4d763aaSMatthew G. Knepley 
6302bb7acecfSBarry Smith   Note:
6303bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6304bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6305bb7acecfSBarry Smith 
6306bb7acecfSBarry Smith   Developer Note:
6307bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6308bb7acecfSBarry Smith   not directly related to the `DM`.
6309f4d763aaSMatthew G. Knepley 
6310db781477SPatrick Sanan .seealso: `VecView()`
6311f4d763aaSMatthew G. Knepley @*/
6312d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6313d71ae5a4SJacob Faibussowitsch {
6314f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6315f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63169371c9d4SSatish Balay   if (num) {
63179371c9d4SSatish Balay     PetscValidIntPointer(num, 2);
63189371c9d4SSatish Balay     *num = dm->outputSequenceNum;
63199371c9d4SSatish Balay   }
63209371c9d4SSatish Balay   if (val) {
63219371c9d4SSatish Balay     PetscValidRealPointer(val, 3);
63229371c9d4SSatish Balay     *val = dm->outputSequenceVal;
63239371c9d4SSatish Balay   }
6324f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6325f4d763aaSMatthew G. Knepley }
6326f4d763aaSMatthew G. Knepley 
6327f4d763aaSMatthew G. Knepley /*@
6328cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6329f4d763aaSMatthew G. Knepley 
6330f4d763aaSMatthew G. Knepley   Input Parameters:
6331bb7acecfSBarry Smith + dm - The original `DM`
6332cdb7a50dSMatthew G. Knepley . num - The output sequence number
6333cdb7a50dSMatthew G. Knepley - val - The output sequence value
6334f4d763aaSMatthew G. Knepley 
6335f4d763aaSMatthew G. Knepley   Level: intermediate
6336f4d763aaSMatthew G. Knepley 
6337bb7acecfSBarry Smith   Note:
6338bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6339bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6340f4d763aaSMatthew G. Knepley 
6341db781477SPatrick Sanan .seealso: `VecView()`
6342f4d763aaSMatthew G. Knepley @*/
6343d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6344d71ae5a4SJacob Faibussowitsch {
6345f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6346f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6347f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6348cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6349cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6350cdb7a50dSMatthew G. Knepley }
6351cdb7a50dSMatthew G. Knepley 
6352cdb7a50dSMatthew G. Knepley /*@C
6353bb7acecfSBarry Smith  DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6354cdb7a50dSMatthew G. Knepley 
6355cdb7a50dSMatthew G. Knepley   Input Parameters:
6356bb7acecfSBarry Smith + dm   - The original `DM`
6357cdb7a50dSMatthew G. Knepley . name - The sequence name
6358cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6359cdb7a50dSMatthew G. Knepley 
6360cdb7a50dSMatthew G. Knepley   Output Parameter:
6361cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6362cdb7a50dSMatthew G. Knepley 
6363cdb7a50dSMatthew G. Knepley   Level: intermediate
6364cdb7a50dSMatthew G. Knepley 
6365bb7acecfSBarry Smith   Note:
6366bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6367bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6368bb7acecfSBarry Smith 
6369bb7acecfSBarry Smith   Developer Note:
6370bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6371cdb7a50dSMatthew G. Knepley 
6372db781477SPatrick Sanan .seealso: `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6373cdb7a50dSMatthew G. Knepley @*/
6374d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6375d71ae5a4SJacob Faibussowitsch {
6376cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6377cdb7a50dSMatthew G. Knepley 
6378cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6379cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6380cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6381064a246eSJacob Faibussowitsch   PetscValidRealPointer(val, 5);
63829566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6383cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6384cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6385cdb7a50dSMatthew G. Knepley     PetscScalar value;
6386cdb7a50dSMatthew G. Knepley 
63879566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
63884aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6389cdb7a50dSMatthew G. Knepley #endif
6390cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6391f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6392f4d763aaSMatthew G. Knepley }
63938e4ac7eaSMatthew G. Knepley 
63948e4ac7eaSMatthew G. Knepley /*@
6395bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
63968e4ac7eaSMatthew G. Knepley 
63978e4ac7eaSMatthew G. Knepley   Not collective
63988e4ac7eaSMatthew G. Knepley 
63998e4ac7eaSMatthew G. Knepley   Input Parameter:
6400bb7acecfSBarry Smith . dm - The `DM`
64018e4ac7eaSMatthew G. Knepley 
64028e4ac7eaSMatthew G. Knepley   Output Parameter:
6403bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64048e4ac7eaSMatthew G. Knepley 
64058e4ac7eaSMatthew G. Knepley   Level: beginner
64068e4ac7eaSMatthew G. Knepley 
6407db781477SPatrick Sanan .seealso: `DMSetUseNatural()`, `DMCreate()`
64088e4ac7eaSMatthew G. Knepley @*/
6409d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6410d71ae5a4SJacob Faibussowitsch {
64118e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64128e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6413534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
64148e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
64158e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
64168e4ac7eaSMatthew G. Knepley }
64178e4ac7eaSMatthew G. Knepley 
64188e4ac7eaSMatthew G. Knepley /*@
6419bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64208e4ac7eaSMatthew G. Knepley 
64218e4ac7eaSMatthew G. Knepley   Collective on dm
64228e4ac7eaSMatthew G. Knepley 
64238e4ac7eaSMatthew G. Knepley   Input Parameters:
6424bb7acecfSBarry Smith  + dm - The `DM`
6425bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64268e4ac7eaSMatthew G. Knepley 
6427bb7acecfSBarry Smith   Note:
6428bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
64295d3b26e6SMatthew G. Knepley 
64308e4ac7eaSMatthew G. Knepley   Level: beginner
64318e4ac7eaSMatthew G. Knepley 
6432db781477SPatrick Sanan .seealso: `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
64338e4ac7eaSMatthew G. Knepley @*/
6434d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6435d71ae5a4SJacob Faibussowitsch {
64368e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64378e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64388833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
64398e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
64408e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
64418e4ac7eaSMatthew G. Knepley }
6442c58f1c22SToby Isaac 
6443c58f1c22SToby Isaac /*@C
6444bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6445c58f1c22SToby Isaac 
6446c58f1c22SToby Isaac   Not Collective
6447c58f1c22SToby Isaac 
6448c58f1c22SToby Isaac   Input Parameters:
6449bb7acecfSBarry Smith + dm   - The `DM` object
6450c58f1c22SToby Isaac - name - The label name
6451c58f1c22SToby Isaac 
6452c58f1c22SToby Isaac   Level: intermediate
6453c58f1c22SToby Isaac 
6454db781477SPatrick Sanan .seealso: `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6455c58f1c22SToby Isaac @*/
6456d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6457d71ae5a4SJacob Faibussowitsch {
64585d80c0bfSVaclav Hapla   PetscBool flg;
64595d80c0bfSVaclav Hapla   DMLabel   label;
6460c58f1c22SToby Isaac 
6461c58f1c22SToby Isaac   PetscFunctionBegin;
6462c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6463c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
64649566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6465c58f1c22SToby Isaac   if (!flg) {
64669566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
64679566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
64689566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6469c58f1c22SToby Isaac   }
6470c58f1c22SToby Isaac   PetscFunctionReturn(0);
6471c58f1c22SToby Isaac }
6472c58f1c22SToby Isaac 
6473c58f1c22SToby Isaac /*@C
6474bb7acecfSBarry 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.
64750fdc7489SMatthew Knepley 
64760fdc7489SMatthew Knepley   Not Collective
64770fdc7489SMatthew Knepley 
64780fdc7489SMatthew Knepley   Input Parameters:
6479bb7acecfSBarry Smith + dm   - The `DM` object
64800fdc7489SMatthew Knepley . l    - The index for the label
64810fdc7489SMatthew Knepley - name - The label name
64820fdc7489SMatthew Knepley 
64830fdc7489SMatthew Knepley   Level: intermediate
64840fdc7489SMatthew Knepley 
6485db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
64860fdc7489SMatthew Knepley @*/
6487d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6488d71ae5a4SJacob Faibussowitsch {
64890fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
64900fdc7489SMatthew Knepley   DMLabel     label;
64910fdc7489SMatthew Knepley   PetscInt    Nl, m;
64920fdc7489SMatthew Knepley   PetscBool   flg, match;
64930fdc7489SMatthew Knepley   const char *lname;
64940fdc7489SMatthew Knepley 
64950fdc7489SMatthew Knepley   PetscFunctionBegin;
64960fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6497064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
64989566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
64990fdc7489SMatthew Knepley   if (!flg) {
65009566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
65019566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
65029566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
65030fdc7489SMatthew Knepley   }
65049566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
650563a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
65060fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
65079566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
65089566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
65090fdc7489SMatthew Knepley     if (match) break;
65100fdc7489SMatthew Knepley   }
65110fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
65120fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
65130fdc7489SMatthew Knepley   else prev->next = orig->next;
65140fdc7489SMatthew Knepley   if (!l) {
65150fdc7489SMatthew Knepley     orig->next = dm->labels;
65160fdc7489SMatthew Knepley     dm->labels = orig;
65170fdc7489SMatthew Knepley   } else {
65189371c9d4SSatish Balay     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next)
65199371c9d4SSatish Balay       ;
65200fdc7489SMatthew Knepley     orig->next = prev->next;
65210fdc7489SMatthew Knepley     prev->next = orig;
65220fdc7489SMatthew Knepley   }
65230fdc7489SMatthew Knepley   PetscFunctionReturn(0);
65240fdc7489SMatthew Knepley }
65250fdc7489SMatthew Knepley 
65260fdc7489SMatthew Knepley /*@C
6527bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6528c58f1c22SToby Isaac 
6529c58f1c22SToby Isaac   Not Collective
6530c58f1c22SToby Isaac 
6531c58f1c22SToby Isaac   Input Parameters:
6532bb7acecfSBarry Smith + dm   - The `DM` object
6533c58f1c22SToby Isaac . name - The label name
6534c58f1c22SToby Isaac - point - The mesh point
6535c58f1c22SToby Isaac 
6536c58f1c22SToby Isaac   Output Parameter:
6537c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6538c58f1c22SToby Isaac 
6539c58f1c22SToby Isaac   Level: beginner
6540c58f1c22SToby Isaac 
6541db781477SPatrick Sanan .seealso: `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6542c58f1c22SToby Isaac @*/
6543d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6544d71ae5a4SJacob Faibussowitsch {
6545c58f1c22SToby Isaac   DMLabel label;
6546c58f1c22SToby Isaac 
6547c58f1c22SToby Isaac   PetscFunctionBegin;
6548c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6549c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
65509566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
65517a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
65529566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
6553c58f1c22SToby Isaac   PetscFunctionReturn(0);
6554c58f1c22SToby Isaac }
6555c58f1c22SToby Isaac 
6556c58f1c22SToby Isaac /*@C
6557bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6558c58f1c22SToby Isaac 
6559c58f1c22SToby Isaac   Not Collective
6560c58f1c22SToby Isaac 
6561c58f1c22SToby Isaac   Input Parameters:
6562bb7acecfSBarry Smith + dm   - The `DM` object
6563c58f1c22SToby Isaac . name - The label name
6564c58f1c22SToby Isaac . point - The mesh point
6565c58f1c22SToby Isaac - value - The label value for this point
6566c58f1c22SToby Isaac 
6567c58f1c22SToby Isaac   Output Parameter:
6568c58f1c22SToby Isaac 
6569c58f1c22SToby Isaac   Level: beginner
6570c58f1c22SToby Isaac 
6571db781477SPatrick Sanan .seealso: `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6572c58f1c22SToby Isaac @*/
6573d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6574d71ae5a4SJacob Faibussowitsch {
6575c58f1c22SToby Isaac   DMLabel label;
6576c58f1c22SToby Isaac 
6577c58f1c22SToby Isaac   PetscFunctionBegin;
6578c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6579c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
65809566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6581c58f1c22SToby Isaac   if (!label) {
65829566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
65839566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6584c58f1c22SToby Isaac   }
65859566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
6586c58f1c22SToby Isaac   PetscFunctionReturn(0);
6587c58f1c22SToby Isaac }
6588c58f1c22SToby Isaac 
6589c58f1c22SToby Isaac /*@C
6590bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6591c58f1c22SToby Isaac 
6592c58f1c22SToby Isaac   Not Collective
6593c58f1c22SToby Isaac 
6594c58f1c22SToby Isaac   Input Parameters:
6595bb7acecfSBarry Smith + dm   - The `DM` object
6596c58f1c22SToby Isaac . name - The label name
6597c58f1c22SToby Isaac . point - The mesh point
6598c58f1c22SToby Isaac - value - The label value for this point
6599c58f1c22SToby Isaac 
6600c58f1c22SToby Isaac   Level: beginner
6601c58f1c22SToby Isaac 
6602db781477SPatrick Sanan .seealso: `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6603c58f1c22SToby Isaac @*/
6604d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6605d71ae5a4SJacob Faibussowitsch {
6606c58f1c22SToby Isaac   DMLabel label;
6607c58f1c22SToby Isaac 
6608c58f1c22SToby Isaac   PetscFunctionBegin;
6609c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6610c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66119566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6612c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
66139566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
6614c58f1c22SToby Isaac   PetscFunctionReturn(0);
6615c58f1c22SToby Isaac }
6616c58f1c22SToby Isaac 
6617c58f1c22SToby Isaac /*@C
6618bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6619c58f1c22SToby Isaac 
6620c58f1c22SToby Isaac   Not Collective
6621c58f1c22SToby Isaac 
6622c58f1c22SToby Isaac   Input Parameters:
6623bb7acecfSBarry Smith + dm   - The `DM` object
6624c58f1c22SToby Isaac - name - The label name
6625c58f1c22SToby Isaac 
6626c58f1c22SToby Isaac   Output Parameter:
6627c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6628c58f1c22SToby Isaac 
6629c58f1c22SToby Isaac   Level: beginner
6630c58f1c22SToby Isaac 
6631bb7acecfSBarry Smith   Developer Note:
6632bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6633bb7acecfSBarry Smith 
6634bb7acecfSBarry Smith .seealso: `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6635c58f1c22SToby Isaac @*/
6636d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6637d71ae5a4SJacob Faibussowitsch {
6638c58f1c22SToby Isaac   DMLabel label;
6639c58f1c22SToby Isaac 
6640c58f1c22SToby Isaac   PetscFunctionBegin;
6641c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6642c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6643534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
66449566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6645c58f1c22SToby Isaac   *size = 0;
6646c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
66479566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
6648c58f1c22SToby Isaac   PetscFunctionReturn(0);
6649c58f1c22SToby Isaac }
6650c58f1c22SToby Isaac 
6651c58f1c22SToby Isaac /*@C
6652bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6653c58f1c22SToby Isaac 
6654c58f1c22SToby Isaac   Not Collective
6655c58f1c22SToby Isaac 
6656c58f1c22SToby Isaac   Input Parameters:
6657bb7acecfSBarry Smith + mesh - The `DM` object
6658c58f1c22SToby Isaac - name - The label name
6659c58f1c22SToby Isaac 
6660c58f1c22SToby Isaac   Output Parameter:
6661c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6662c58f1c22SToby Isaac 
6663c58f1c22SToby Isaac   Level: beginner
6664c58f1c22SToby Isaac 
6665db781477SPatrick Sanan .seealso: `DMLabelGetValueIS()`, `DMGetLabelSize()`
6666c58f1c22SToby Isaac @*/
6667d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6668d71ae5a4SJacob Faibussowitsch {
6669c58f1c22SToby Isaac   DMLabel label;
6670c58f1c22SToby Isaac 
6671c58f1c22SToby Isaac   PetscFunctionBegin;
6672c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6673c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6674c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
66759566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6676c58f1c22SToby Isaac   *ids = NULL;
6677dab2e251SBlaise Bourdin   if (label) {
66789566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6679dab2e251SBlaise Bourdin   } else {
6680dab2e251SBlaise Bourdin     /* returning an empty IS */
66819566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6682dab2e251SBlaise Bourdin   }
6683c58f1c22SToby Isaac   PetscFunctionReturn(0);
6684c58f1c22SToby Isaac }
6685c58f1c22SToby Isaac 
6686c58f1c22SToby Isaac /*@C
6687c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6688c58f1c22SToby Isaac 
6689c58f1c22SToby Isaac   Not Collective
6690c58f1c22SToby Isaac 
6691c58f1c22SToby Isaac   Input Parameters:
6692bb7acecfSBarry Smith + dm - The `DM` object
6693c58f1c22SToby Isaac . name - The label name
6694c58f1c22SToby Isaac - value - The stratum value
6695c58f1c22SToby Isaac 
6696c58f1c22SToby Isaac   Output Parameter:
6697bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6698c58f1c22SToby Isaac 
6699c58f1c22SToby Isaac   Level: beginner
6700c58f1c22SToby Isaac 
6701db781477SPatrick Sanan .seealso: `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6702c58f1c22SToby Isaac @*/
6703d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6704d71ae5a4SJacob Faibussowitsch {
6705c58f1c22SToby Isaac   DMLabel label;
6706c58f1c22SToby Isaac 
6707c58f1c22SToby Isaac   PetscFunctionBegin;
6708c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6709c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6710534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
67119566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6712c58f1c22SToby Isaac   *size = 0;
6713c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
67149566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
6715c58f1c22SToby Isaac   PetscFunctionReturn(0);
6716c58f1c22SToby Isaac }
6717c58f1c22SToby Isaac 
6718c58f1c22SToby Isaac /*@C
6719c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6720c58f1c22SToby Isaac 
6721c58f1c22SToby Isaac   Not Collective
6722c58f1c22SToby Isaac 
6723c58f1c22SToby Isaac   Input Parameters:
6724bb7acecfSBarry Smith + dm - The `DM` object
6725c58f1c22SToby Isaac . name - The label name
6726c58f1c22SToby Isaac - value - The stratum value
6727c58f1c22SToby Isaac 
6728c58f1c22SToby Isaac   Output Parameter:
6729c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6730c58f1c22SToby Isaac 
6731c58f1c22SToby Isaac   Level: beginner
6732c58f1c22SToby Isaac 
6733db781477SPatrick Sanan .seealso: `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6734c58f1c22SToby Isaac @*/
6735d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6736d71ae5a4SJacob Faibussowitsch {
6737c58f1c22SToby Isaac   DMLabel label;
6738c58f1c22SToby Isaac 
6739c58f1c22SToby Isaac   PetscFunctionBegin;
6740c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6741c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6742c58f1c22SToby Isaac   PetscValidPointer(points, 4);
67439566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6744c58f1c22SToby Isaac   *points = NULL;
6745c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
67469566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
6747c58f1c22SToby Isaac   PetscFunctionReturn(0);
6748c58f1c22SToby Isaac }
6749c58f1c22SToby Isaac 
67504de306b1SToby Isaac /*@C
67519044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
67524de306b1SToby Isaac 
67534de306b1SToby Isaac   Not Collective
67544de306b1SToby Isaac 
67554de306b1SToby Isaac   Input Parameters:
6756bb7acecfSBarry Smith + dm - The `DM` object
67574de306b1SToby Isaac . name - The label name
67584de306b1SToby Isaac . value - The stratum value
67594de306b1SToby Isaac - points - The stratum points
67604de306b1SToby Isaac 
67614de306b1SToby Isaac   Level: beginner
67624de306b1SToby Isaac 
6763bb7acecfSBarry Smith .seealso: `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
67644de306b1SToby Isaac @*/
6765d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6766d71ae5a4SJacob Faibussowitsch {
67674de306b1SToby Isaac   DMLabel label;
67684de306b1SToby Isaac 
67694de306b1SToby Isaac   PetscFunctionBegin;
67704de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67714de306b1SToby Isaac   PetscValidCharPointer(name, 2);
67724de306b1SToby Isaac   PetscValidPointer(points, 4);
67739566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67744de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
67759566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
67764de306b1SToby Isaac   PetscFunctionReturn(0);
67774de306b1SToby Isaac }
67784de306b1SToby Isaac 
6779c58f1c22SToby Isaac /*@C
6780bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6781c58f1c22SToby Isaac 
6782c58f1c22SToby Isaac   Not Collective
6783c58f1c22SToby Isaac 
6784c58f1c22SToby Isaac   Input Parameters:
6785bb7acecfSBarry Smith + dm   - The `DM` object
6786c58f1c22SToby Isaac . name - The label name
6787c58f1c22SToby Isaac - value - The label value for this point
6788c58f1c22SToby Isaac 
6789c58f1c22SToby Isaac   Output Parameter:
6790c58f1c22SToby Isaac 
6791c58f1c22SToby Isaac   Level: beginner
6792c58f1c22SToby Isaac 
6793bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6794c58f1c22SToby Isaac @*/
6795d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6796d71ae5a4SJacob Faibussowitsch {
6797c58f1c22SToby Isaac   DMLabel label;
6798c58f1c22SToby Isaac 
6799c58f1c22SToby Isaac   PetscFunctionBegin;
6800c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6801c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
68029566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6803c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
68049566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
6805c58f1c22SToby Isaac   PetscFunctionReturn(0);
6806c58f1c22SToby Isaac }
6807c58f1c22SToby Isaac 
6808c58f1c22SToby Isaac /*@
6809bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6810c58f1c22SToby Isaac 
6811c58f1c22SToby Isaac   Not Collective
6812c58f1c22SToby Isaac 
6813c58f1c22SToby Isaac   Input Parameter:
6814bb7acecfSBarry Smith . dm   - The `DM` object
6815c58f1c22SToby Isaac 
6816c58f1c22SToby Isaac   Output Parameter:
6817c58f1c22SToby Isaac . numLabels - the number of Labels
6818c58f1c22SToby Isaac 
6819c58f1c22SToby Isaac   Level: intermediate
6820c58f1c22SToby Isaac 
6821bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6822c58f1c22SToby Isaac @*/
6823d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6824d71ae5a4SJacob Faibussowitsch {
68255d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6826c58f1c22SToby Isaac   PetscInt    n    = 0;
6827c58f1c22SToby Isaac 
6828c58f1c22SToby Isaac   PetscFunctionBegin;
6829c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6830534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
68319371c9d4SSatish Balay   while (next) {
68329371c9d4SSatish Balay     ++n;
68339371c9d4SSatish Balay     next = next->next;
68349371c9d4SSatish Balay   }
6835c58f1c22SToby Isaac   *numLabels = n;
6836c58f1c22SToby Isaac   PetscFunctionReturn(0);
6837c58f1c22SToby Isaac }
6838c58f1c22SToby Isaac 
6839c58f1c22SToby Isaac /*@C
6840c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6841c58f1c22SToby Isaac 
6842c58f1c22SToby Isaac   Not Collective
6843c58f1c22SToby Isaac 
6844c58f1c22SToby Isaac   Input Parameters:
6845bb7acecfSBarry Smith + dm - The `DM` object
6846c58f1c22SToby Isaac - n  - the label number
6847c58f1c22SToby Isaac 
6848c58f1c22SToby Isaac   Output Parameter:
6849c58f1c22SToby Isaac . name - the label name
6850c58f1c22SToby Isaac 
6851c58f1c22SToby Isaac   Level: intermediate
6852c58f1c22SToby Isaac 
6853bb7acecfSBarry Smith   Developer Note:
6854bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
6855bb7acecfSBarry Smith 
6856bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6857c58f1c22SToby Isaac @*/
6858d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6859d71ae5a4SJacob Faibussowitsch {
68605d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6861c58f1c22SToby Isaac   PetscInt    l    = 0;
6862c58f1c22SToby Isaac 
6863c58f1c22SToby Isaac   PetscFunctionBegin;
6864c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6865c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6866c58f1c22SToby Isaac   while (next) {
6867c58f1c22SToby Isaac     if (l == n) {
68689566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
6869c58f1c22SToby Isaac       PetscFunctionReturn(0);
6870c58f1c22SToby Isaac     }
6871c58f1c22SToby Isaac     ++l;
6872c58f1c22SToby Isaac     next = next->next;
6873c58f1c22SToby Isaac   }
687463a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6875c58f1c22SToby Isaac }
6876c58f1c22SToby Isaac 
6877c58f1c22SToby Isaac /*@C
6878bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
6879c58f1c22SToby Isaac 
6880c58f1c22SToby Isaac   Not Collective
6881c58f1c22SToby Isaac 
6882c58f1c22SToby Isaac   Input Parameters:
6883bb7acecfSBarry Smith + dm   - The `DM` object
6884c58f1c22SToby Isaac - name - The label name
6885c58f1c22SToby Isaac 
6886c58f1c22SToby Isaac   Output Parameter:
6887bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
6888c58f1c22SToby Isaac 
6889c58f1c22SToby Isaac   Level: intermediate
6890c58f1c22SToby Isaac 
6891bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6892c58f1c22SToby Isaac @*/
6893d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6894d71ae5a4SJacob Faibussowitsch {
68955d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6896d67d17b1SMatthew G. Knepley   const char *lname;
6897c58f1c22SToby Isaac 
6898c58f1c22SToby Isaac   PetscFunctionBegin;
6899c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6900c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6901534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
6902c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6903c58f1c22SToby Isaac   while (next) {
69049566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
69059566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
6906c58f1c22SToby Isaac     if (*hasLabel) break;
6907c58f1c22SToby Isaac     next = next->next;
6908c58f1c22SToby Isaac   }
6909c58f1c22SToby Isaac   PetscFunctionReturn(0);
6910c58f1c22SToby Isaac }
6911c58f1c22SToby Isaac 
6912c58f1c22SToby Isaac /*@C
6913bb7acecfSBarry Smith   DMGetLabel - Return the label of a given name, or NULL, from a `DM`
6914c58f1c22SToby Isaac 
6915c58f1c22SToby Isaac   Not Collective
6916c58f1c22SToby Isaac 
6917c58f1c22SToby Isaac   Input Parameters:
6918bb7acecfSBarry Smith + dm   - The `DM` object
6919c58f1c22SToby Isaac - name - The label name
6920c58f1c22SToby Isaac 
6921c58f1c22SToby Isaac   Output Parameter:
6922bb7acecfSBarry Smith . label - The `DMLabel`, or NULL if the label is absent
6923c58f1c22SToby Isaac 
6924bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
6925bb7acecfSBarry Smith +   "depth"       - Holds the depth (co-dimension) of each mesh point
6926bb7acecfSBarry Smith .   "celltype"    - Holds the topological type of each cell
6927bb7acecfSBarry Smith .   "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
6928bb7acecfSBarry Smith .   "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
6929bb7acecfSBarry Smith .   "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
6930bb7acecfSBarry Smith -  "Vertex Sets" - Mirrors the vertex sets defined by GMsh
69316d7c9049SMatthew G. Knepley 
6932c58f1c22SToby Isaac   Level: intermediate
6933c58f1c22SToby Isaac 
6934bb7acecfSBarry Smith .seealso: `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
6935c58f1c22SToby Isaac @*/
6936d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6937d71ae5a4SJacob Faibussowitsch {
69385d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6939c58f1c22SToby Isaac   PetscBool   hasLabel;
6940d67d17b1SMatthew G. Knepley   const char *lname;
6941c58f1c22SToby Isaac 
6942c58f1c22SToby Isaac   PetscFunctionBegin;
6943c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6944c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6945c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6946c58f1c22SToby Isaac   *label = NULL;
6947c58f1c22SToby Isaac   while (next) {
69489566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
69499566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
6950c58f1c22SToby Isaac     if (hasLabel) {
6951c58f1c22SToby Isaac       *label = next->label;
6952c58f1c22SToby Isaac       break;
6953c58f1c22SToby Isaac     }
6954c58f1c22SToby Isaac     next = next->next;
6955c58f1c22SToby Isaac   }
6956c58f1c22SToby Isaac   PetscFunctionReturn(0);
6957c58f1c22SToby Isaac }
6958c58f1c22SToby Isaac 
6959c58f1c22SToby Isaac /*@C
6960bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
6961c58f1c22SToby Isaac 
6962c58f1c22SToby Isaac   Not Collective
6963c58f1c22SToby Isaac 
6964c58f1c22SToby Isaac   Input Parameters:
6965bb7acecfSBarry Smith + dm - The `DM` object
6966c58f1c22SToby Isaac - n  - the label number
6967c58f1c22SToby Isaac 
6968c58f1c22SToby Isaac   Output Parameter:
6969c58f1c22SToby Isaac . label - the label
6970c58f1c22SToby Isaac 
6971c58f1c22SToby Isaac   Level: intermediate
6972c58f1c22SToby Isaac 
6973bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6974c58f1c22SToby Isaac @*/
6975d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
6976d71ae5a4SJacob Faibussowitsch {
69775d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6978c58f1c22SToby Isaac   PetscInt    l    = 0;
6979c58f1c22SToby Isaac 
6980c58f1c22SToby Isaac   PetscFunctionBegin;
6981c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6982c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6983c58f1c22SToby Isaac   while (next) {
6984c58f1c22SToby Isaac     if (l == n) {
6985c58f1c22SToby Isaac       *label = next->label;
6986c58f1c22SToby Isaac       PetscFunctionReturn(0);
6987c58f1c22SToby Isaac     }
6988c58f1c22SToby Isaac     ++l;
6989c58f1c22SToby Isaac     next = next->next;
6990c58f1c22SToby Isaac   }
699163a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6992c58f1c22SToby Isaac }
6993c58f1c22SToby Isaac 
6994c58f1c22SToby Isaac /*@C
6995bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
6996c58f1c22SToby Isaac 
6997c58f1c22SToby Isaac   Not Collective
6998c58f1c22SToby Isaac 
6999c58f1c22SToby Isaac   Input Parameters:
7000bb7acecfSBarry Smith + dm   - The `DM` object
7001bb7acecfSBarry Smith - label - The `DMLabel`
7002c58f1c22SToby Isaac 
7003c58f1c22SToby Isaac   Level: developer
7004c58f1c22SToby Isaac 
7005bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7006c58f1c22SToby Isaac @*/
7007d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7008d71ae5a4SJacob Faibussowitsch {
70095d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7010c58f1c22SToby Isaac   PetscBool   hasLabel;
7011d67d17b1SMatthew G. Knepley   const char *lname;
70125d80c0bfSVaclav Hapla   PetscBool   flg;
7013c58f1c22SToby Isaac 
7014c58f1c22SToby Isaac   PetscFunctionBegin;
7015c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70169566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
70179566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
70187a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
70199566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7020c58f1c22SToby Isaac   tmpLabel->label  = label;
7021c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
70225d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
70235d80c0bfSVaclav Hapla   *p = tmpLabel;
70249566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
70259566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
70265d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
70279566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7028ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7029c58f1c22SToby Isaac   PetscFunctionReturn(0);
7030c58f1c22SToby Isaac }
7031c58f1c22SToby Isaac 
7032c58f1c22SToby Isaac /*@C
70334a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
70344a7ee7d0SMatthew G. Knepley 
70354a7ee7d0SMatthew G. Knepley   Not Collective
70364a7ee7d0SMatthew G. Knepley 
70374a7ee7d0SMatthew G. Knepley   Input Parameters:
7038bb7acecfSBarry Smith + dm    - The `DM` object
7039bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
70404a7ee7d0SMatthew G. Knepley 
7041bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7042bb7acecfSBarry Smith +  "depth"       - Holds the depth (co-dimension) of each mesh point
7043bb7acecfSBarry Smith .  "celltype"    - Holds the topological type of each cell
7044bb7acecfSBarry Smith .  "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7045bb7acecfSBarry Smith .  "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7046bb7acecfSBarry Smith .  "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7047bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
70484a7ee7d0SMatthew G. Knepley 
70494a7ee7d0SMatthew G. Knepley   Level: intermediate
70504a7ee7d0SMatthew G. Knepley 
7051bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
70524a7ee7d0SMatthew G. Knepley @*/
7053d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7054d71ae5a4SJacob Faibussowitsch {
70554a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
70564a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
70574a7ee7d0SMatthew G. Knepley   const char *name, *lname;
70584a7ee7d0SMatthew G. Knepley 
70594a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
70604a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70614a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
70629566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
70634a7ee7d0SMatthew G. Knepley   while (next) {
70649566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70659566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
70664a7ee7d0SMatthew G. Knepley     if (hasLabel) {
70679566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
70689566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
70694a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
70709566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
70714a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
70729566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
70734a7ee7d0SMatthew G. Knepley       next->label = label;
70744a7ee7d0SMatthew G. Knepley       break;
70754a7ee7d0SMatthew G. Knepley     }
70764a7ee7d0SMatthew G. Knepley     next = next->next;
70774a7ee7d0SMatthew G. Knepley   }
70784a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
70794a7ee7d0SMatthew G. Knepley }
70804a7ee7d0SMatthew G. Knepley 
70814a7ee7d0SMatthew G. Knepley /*@C
7082bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7083c58f1c22SToby Isaac 
7084c58f1c22SToby Isaac   Not Collective
7085c58f1c22SToby Isaac 
7086c58f1c22SToby Isaac   Input Parameters:
7087bb7acecfSBarry Smith + dm   - The `DM` object
7088c58f1c22SToby Isaac - name - The label name
7089c58f1c22SToby Isaac 
7090c58f1c22SToby Isaac   Output Parameter:
7091bb7acecfSBarry Smith . label - The `DMLabel`, or NULL if the label is absent. Pass in NULL to call `DMLabelDestroy()` on the label, otherwise the
7092bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7093c58f1c22SToby Isaac 
7094c58f1c22SToby Isaac   Level: developer
7095c58f1c22SToby Isaac 
7096bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7097c58f1c22SToby Isaac @*/
7098d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7099d71ae5a4SJacob Faibussowitsch {
710095d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7101c58f1c22SToby Isaac   PetscBool   hasLabel;
7102d67d17b1SMatthew G. Knepley   const char *lname;
7103c58f1c22SToby Isaac 
7104c58f1c22SToby Isaac   PetscFunctionBegin;
7105c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7106e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7107e5472504SVaclav Hapla   if (label) {
7108e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7109c58f1c22SToby Isaac     *label = NULL;
7110e5472504SVaclav Hapla   }
71115d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
71129566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
71139566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7114c58f1c22SToby Isaac     if (hasLabel) {
711595d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
71169566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
711795d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
71189566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7119ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
712095d578d6SVaclav Hapla       if (label) *label = link->label;
71219566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
71229566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7123c58f1c22SToby Isaac       break;
7124c58f1c22SToby Isaac     }
7125c58f1c22SToby Isaac   }
7126c58f1c22SToby Isaac   PetscFunctionReturn(0);
7127c58f1c22SToby Isaac }
7128c58f1c22SToby Isaac 
7129306894acSVaclav Hapla /*@
7130bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7131306894acSVaclav Hapla 
7132306894acSVaclav Hapla   Not Collective
7133306894acSVaclav Hapla 
7134306894acSVaclav Hapla   Input Parameters:
7135bb7acecfSBarry Smith + dm   - The `DM` object
7136bb7acecfSBarry Smith . label - The `DMLabel` to be removed from the `DM`
7137306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7138306894acSVaclav Hapla 
7139306894acSVaclav Hapla   Level: developer
7140306894acSVaclav Hapla 
7141bb7acecfSBarry Smith   Note:
7142306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7143bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7144306894acSVaclav Hapla   *label nullified.
7145306894acSVaclav Hapla 
7146bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7147306894acSVaclav Hapla @*/
7148d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7149d71ae5a4SJacob Faibussowitsch {
715043e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7151306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7152306894acSVaclav Hapla 
7153306894acSVaclav Hapla   PetscFunctionBegin;
7154306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7155306894acSVaclav Hapla   PetscValidPointer(label, 2);
7156f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7157306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7158306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
71595d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
716043e45a93SVaclav Hapla     if (*label == link->label) {
7161306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
716243e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7163306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7164ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
716543e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
71669566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
71679566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7168306894acSVaclav Hapla       break;
7169306894acSVaclav Hapla     }
7170306894acSVaclav Hapla   }
71717a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7172306894acSVaclav Hapla   PetscFunctionReturn(0);
7173306894acSVaclav Hapla }
7174306894acSVaclav Hapla 
7175c58f1c22SToby Isaac /*@C
7176c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7177c58f1c22SToby Isaac 
7178c58f1c22SToby Isaac   Not Collective
7179c58f1c22SToby Isaac 
7180c58f1c22SToby Isaac   Input Parameters:
7181bb7acecfSBarry Smith + dm   - The `DM` object
7182c58f1c22SToby Isaac - name - The label name
7183c58f1c22SToby Isaac 
7184c58f1c22SToby Isaac   Output Parameter:
7185c58f1c22SToby Isaac . output - The flag for output
7186c58f1c22SToby Isaac 
7187c58f1c22SToby Isaac   Level: developer
7188c58f1c22SToby Isaac 
7189bb7acecfSBarry Smith .seealso: `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7190c58f1c22SToby Isaac @*/
7191d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7192d71ae5a4SJacob Faibussowitsch {
71935d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7194d67d17b1SMatthew G. Knepley   const char *lname;
7195c58f1c22SToby Isaac 
7196c58f1c22SToby Isaac   PetscFunctionBegin;
7197c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7198dadcf809SJacob Faibussowitsch   PetscValidCharPointer(name, 2);
7199dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(output, 3);
7200c58f1c22SToby Isaac   while (next) {
7201c58f1c22SToby Isaac     PetscBool flg;
7202c58f1c22SToby Isaac 
72039566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72049566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
72059371c9d4SSatish Balay     if (flg) {
72069371c9d4SSatish Balay       *output = next->output;
72079371c9d4SSatish Balay       PetscFunctionReturn(0);
72089371c9d4SSatish Balay     }
7209c58f1c22SToby Isaac     next = next->next;
7210c58f1c22SToby Isaac   }
721198921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7212c58f1c22SToby Isaac }
7213c58f1c22SToby Isaac 
7214c58f1c22SToby Isaac /*@C
7215bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7216c58f1c22SToby Isaac 
7217c58f1c22SToby Isaac   Not Collective
7218c58f1c22SToby Isaac 
7219c58f1c22SToby Isaac   Input Parameters:
7220bb7acecfSBarry Smith + dm     - The `DM` object
7221c58f1c22SToby Isaac . name   - The label name
7222bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7223c58f1c22SToby Isaac 
7224c58f1c22SToby Isaac   Level: developer
7225c58f1c22SToby Isaac 
7226bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7227c58f1c22SToby Isaac @*/
7228d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7229d71ae5a4SJacob Faibussowitsch {
72305d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7231d67d17b1SMatthew G. Knepley   const char *lname;
7232c58f1c22SToby Isaac 
7233c58f1c22SToby Isaac   PetscFunctionBegin;
7234c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7235534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7236c58f1c22SToby Isaac   while (next) {
7237c58f1c22SToby Isaac     PetscBool flg;
7238c58f1c22SToby Isaac 
72399566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72409566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
72419371c9d4SSatish Balay     if (flg) {
72429371c9d4SSatish Balay       next->output = output;
72439371c9d4SSatish Balay       PetscFunctionReturn(0);
72449371c9d4SSatish Balay     }
7245c58f1c22SToby Isaac     next = next->next;
7246c58f1c22SToby Isaac   }
724798921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7248c58f1c22SToby Isaac }
7249c58f1c22SToby Isaac 
7250c58f1c22SToby Isaac /*@
7251bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7252c58f1c22SToby Isaac 
7253d083f849SBarry Smith   Collective on dmA
7254c58f1c22SToby Isaac 
7255d8d19677SJose E. Roman   Input Parameters:
7256bb7acecfSBarry Smith + dmA - The `DM` object with initial labels
7257bb7acecfSBarry Smith . dmB - The `DM` object to which labels are copied
7258bb7acecfSBarry Smith . mode - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7259bb7acecfSBarry Smith . all  - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7260bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7261c58f1c22SToby Isaac 
7262c58f1c22SToby Isaac   Level: intermediate
7263c58f1c22SToby Isaac 
7264bb7acecfSBarry Smith   Note:
72652cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7266c58f1c22SToby Isaac 
7267bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7268c58f1c22SToby Isaac @*/
7269d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7270d71ae5a4SJacob Faibussowitsch {
72712cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7272c58f1c22SToby Isaac   const char *name;
7273c58f1c22SToby Isaac   PetscBool   flg;
72745d80c0bfSVaclav Hapla   DMLabelLink link;
7275c58f1c22SToby Isaac 
72765d80c0bfSVaclav Hapla   PetscFunctionBegin;
72775d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
72785d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
72795d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
72805d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
72817a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
72825d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
72835d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
72845d80c0bfSVaclav Hapla     label = link->label;
72859566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
72865d80c0bfSVaclav Hapla     if (!all) {
72879566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7288c58f1c22SToby Isaac       if (flg) continue;
72899566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
72907d5acc75SStefano Zampini       if (flg) continue;
72919566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7292ba2698f1SMatthew G. Knepley       if (flg) continue;
72935d80c0bfSVaclav Hapla     }
72949566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
72952cbb9b06SVaclav Hapla     if (labelOld) {
72962cbb9b06SVaclav Hapla       switch (emode) {
7297d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7298d71ae5a4SJacob Faibussowitsch         continue;
7299d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7300d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7301d71ae5a4SJacob Faibussowitsch         break;
7302d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7303d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7304d71ae5a4SJacob Faibussowitsch       default:
7305d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
73062cbb9b06SVaclav Hapla       }
73072cbb9b06SVaclav Hapla     }
73085d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
73099566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
73105d80c0bfSVaclav Hapla     } else {
73115d80c0bfSVaclav Hapla       labelNew = label;
73125d80c0bfSVaclav Hapla     }
73139566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
73149566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7315c58f1c22SToby Isaac   }
7316c58f1c22SToby Isaac   PetscFunctionReturn(0);
7317c58f1c22SToby Isaac }
7318461a15a0SLisandro Dalcin 
7319609dae6eSVaclav Hapla /*@C
7320bb7acecfSBarry Smith   DMCompareLabels - Compare labels of two `DMPLEX` meshes
7321609dae6eSVaclav Hapla 
73225efe38ccSVaclav Hapla   Collective
7323609dae6eSVaclav Hapla 
7324609dae6eSVaclav Hapla   Input Parameters:
7325bb7acecfSBarry Smith + dm0 - First `DM` object
7326bb7acecfSBarry Smith - dm1 - Second `DM` object
7327609dae6eSVaclav Hapla 
7328609dae6eSVaclav Hapla   Output Parameters
73295efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
7330609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
7331609dae6eSVaclav Hapla 
7332609dae6eSVaclav Hapla   Level: intermediate
7333609dae6eSVaclav Hapla 
7334609dae6eSVaclav Hapla   Notes:
7335bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7336bb7acecfSBarry Smith 
7337bb7acecfSBarry Smith   If equal is passed as NULL and difference is found, an error is thrown on all processes.
7338bb7acecfSBarry Smith 
7339bb7acecfSBarry Smith   Make sure to pass equal is NULL on all processes or none of them.
7340609dae6eSVaclav Hapla 
73415efe38ccSVaclav Hapla   The output message is set independently on each rank.
7342bb7acecfSBarry Smith 
7343bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7344bb7acecfSBarry Smith 
7345bb7acecfSBarry Smith   If message is passed as NULL and a difference is found, the difference description is printed to stderr in synchronized manner.
7346bb7acecfSBarry Smith 
7347bb7acecfSBarry Smith   Make sure to pass message as NULL on all processes or no processes.
7348609dae6eSVaclav Hapla 
7349609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7350bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7351609dae6eSVaclav Hapla 
7352bb7acecfSBarry Smith   Fortran Note:
7353bb7acecfSBarry Smith   This function is not available from Fortran.
7354609dae6eSVaclav Hapla 
7355bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7356609dae6eSVaclav Hapla @*/
7357d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7358d71ae5a4SJacob Faibussowitsch {
73595efe38ccSVaclav Hapla   PetscInt    n, i;
7360609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
73615efe38ccSVaclav Hapla   PetscBool   eq;
7362609dae6eSVaclav Hapla   MPI_Comm    comm;
73635efe38ccSVaclav Hapla   PetscMPIInt rank;
7364609dae6eSVaclav Hapla 
7365609dae6eSVaclav Hapla   PetscFunctionBegin;
7366609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7367609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7368609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
73695efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal, 3);
7370609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
73719566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
73729566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
73735efe38ccSVaclav Hapla   {
73745efe38ccSVaclav Hapla     PetscInt n1;
73755efe38ccSVaclav Hapla 
73769566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
73779566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
73785efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
737948a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
73809566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
73815efe38ccSVaclav Hapla     if (!eq) goto finish;
73825efe38ccSVaclav Hapla   }
73835efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7384609dae6eSVaclav Hapla     DMLabel     l0, l1;
7385609dae6eSVaclav Hapla     const char *name;
7386609dae6eSVaclav Hapla     char       *msgInner;
7387609dae6eSVaclav Hapla 
7388609dae6eSVaclav Hapla     /* Ignore label order */
73899566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
73909566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
73919566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7392609dae6eSVaclav Hapla     if (!l1) {
739363a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
73945efe38ccSVaclav Hapla       eq = PETSC_FALSE;
73955efe38ccSVaclav Hapla       break;
7396609dae6eSVaclav Hapla     }
73979566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
73989566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
73999566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
74005efe38ccSVaclav Hapla     if (!eq) break;
7401609dae6eSVaclav Hapla   }
74029566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7403609dae6eSVaclav Hapla finish:
74045efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7405609dae6eSVaclav Hapla   if (message) {
7406609dae6eSVaclav Hapla     *message = NULL;
740748a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
74085efe38ccSVaclav Hapla   } else {
740948a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
74109566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
74115efe38ccSVaclav Hapla   }
74125efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
74135efe38ccSVaclav Hapla   if (equal) *equal = eq;
74147a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
7415609dae6eSVaclav Hapla   PetscFunctionReturn(0);
7416609dae6eSVaclav Hapla }
7417609dae6eSVaclav Hapla 
7418d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7419d71ae5a4SJacob Faibussowitsch {
7420461a15a0SLisandro Dalcin   PetscFunctionBegin;
7421461a15a0SLisandro Dalcin   PetscValidPointer(label, 2);
7422461a15a0SLisandro Dalcin   if (!*label) {
74239566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
74249566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7425461a15a0SLisandro Dalcin   }
74269566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
7427461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
7428461a15a0SLisandro Dalcin }
7429461a15a0SLisandro Dalcin 
74300fdc7489SMatthew Knepley /*
74310fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
74320fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
74330fdc7489SMatthew Knepley   (label, id) pair in the DM.
74340fdc7489SMatthew Knepley 
74350fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
74360fdc7489SMatthew Knepley   each label.
74370fdc7489SMatthew Knepley */
7438d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7439d71ae5a4SJacob Faibussowitsch {
74400fdc7489SMatthew Knepley   DMUniversalLabel ul;
74410fdc7489SMatthew Knepley   PetscBool       *active;
74420fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
74430fdc7489SMatthew Knepley 
74440fdc7489SMatthew Knepley   PetscFunctionBegin;
74459566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
74469566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
74479566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
74489566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
74490fdc7489SMatthew Knepley   ul->Nl = 0;
74500fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
74510fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
74520fdc7489SMatthew Knepley     const char *name;
74530fdc7489SMatthew Knepley 
74549566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
74559566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
74569566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
74570fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
74580fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
74590fdc7489SMatthew Knepley   }
74609566063dSJacob 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));
74610fdc7489SMatthew Knepley   ul->Nv = 0;
74620fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
74630fdc7489SMatthew Knepley     DMLabel     label;
74640fdc7489SMatthew Knepley     PetscInt    nv;
74650fdc7489SMatthew Knepley     const char *name;
74660fdc7489SMatthew Knepley 
74670fdc7489SMatthew Knepley     if (!active[l]) continue;
74689566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
74699566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
74709566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
74719566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
74720fdc7489SMatthew Knepley     ul->indices[m] = l;
74730fdc7489SMatthew Knepley     ul->Nv += nv;
74740fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
74750fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
74760fdc7489SMatthew Knepley     ++m;
74770fdc7489SMatthew Knepley   }
74780fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
74790fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
74800fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
74810fdc7489SMatthew Knepley   }
74820fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
74830fdc7489SMatthew Knepley     PetscInt b;
74840fdc7489SMatthew Knepley 
74850fdc7489SMatthew Knepley     ul->masks[l] = 0;
74860fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
74870fdc7489SMatthew Knepley   }
74889566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
74890fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
74900fdc7489SMatthew Knepley     DMLabel         label;
74910fdc7489SMatthew Knepley     IS              valueIS;
74920fdc7489SMatthew Knepley     const PetscInt *varr;
74930fdc7489SMatthew Knepley     PetscInt        nv, v;
74940fdc7489SMatthew Knepley 
74950fdc7489SMatthew Knepley     if (!active[l]) continue;
74969566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
74979566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
74989566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
74999566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7500ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
75019566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
75029566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
75039566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
75040fdc7489SMatthew Knepley     ++m;
75050fdc7489SMatthew Knepley   }
75069566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
75070fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
75080fdc7489SMatthew Knepley     PetscInt  uval   = 0;
75090fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
75100fdc7489SMatthew Knepley 
75110fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
75120fdc7489SMatthew Knepley       DMLabel  label;
75130649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
75140fdc7489SMatthew Knepley 
75150fdc7489SMatthew Knepley       if (!active[l]) continue;
75169566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
75179566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
75189566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
75199371c9d4SSatish Balay       if (val == defval) {
75209371c9d4SSatish Balay         ++m;
75219371c9d4SSatish Balay         continue;
75229371c9d4SSatish Balay       }
75230649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
75240fdc7489SMatthew Knepley       marked = PETSC_TRUE;
75259566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
752663a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
75270fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
75280fdc7489SMatthew Knepley       ++m;
75290fdc7489SMatthew Knepley     }
75309566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
75310fdc7489SMatthew Knepley   }
75329566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
75330fdc7489SMatthew Knepley   *universal = ul;
75340fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75350fdc7489SMatthew Knepley }
75360fdc7489SMatthew Knepley 
7537d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7538d71ae5a4SJacob Faibussowitsch {
75390fdc7489SMatthew Knepley   PetscInt l;
75400fdc7489SMatthew Knepley 
75410fdc7489SMatthew Knepley   PetscFunctionBegin;
75429566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
75439566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
75449566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
75459566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
75469566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
75470fdc7489SMatthew Knepley   *universal = NULL;
75480fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75490fdc7489SMatthew Knepley }
75500fdc7489SMatthew Knepley 
7551d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7552d71ae5a4SJacob Faibussowitsch {
75530fdc7489SMatthew Knepley   PetscFunctionBegin;
75540fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
75550fdc7489SMatthew Knepley   *ulabel = ul->label;
75560fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75570fdc7489SMatthew Knepley }
75580fdc7489SMatthew Knepley 
7559d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7560d71ae5a4SJacob Faibussowitsch {
75610fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
75620fdc7489SMatthew Knepley 
75630fdc7489SMatthew Knepley   PetscFunctionBegin;
7564064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
75650fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
75669566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
75679566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
75680fdc7489SMatthew Knepley   }
75690fdc7489SMatthew Knepley   if (preserveOrder) {
75700fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
75710fdc7489SMatthew Knepley       const char *name;
75720fdc7489SMatthew Knepley       PetscBool   match;
75730fdc7489SMatthew Knepley 
75749566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
75759566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
757663a3b9bcSJacob 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]);
75770fdc7489SMatthew Knepley     }
75780fdc7489SMatthew Knepley   }
75790fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75800fdc7489SMatthew Knepley }
75810fdc7489SMatthew Knepley 
7582d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7583d71ae5a4SJacob Faibussowitsch {
75840fdc7489SMatthew Knepley   PetscInt l;
75850fdc7489SMatthew Knepley 
75860fdc7489SMatthew Knepley   PetscFunctionBegin;
75870fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
75880fdc7489SMatthew Knepley     DMLabel  label;
75890fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
75900fdc7489SMatthew Knepley 
75910fdc7489SMatthew Knepley     if (lval) {
75929566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
75939566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
75949566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
75950fdc7489SMatthew Knepley     }
75960fdc7489SMatthew Knepley   }
75970fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75980fdc7489SMatthew Knepley }
7599a8fb8f29SToby Isaac 
7600a8fb8f29SToby Isaac /*@
7601bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7602bb7acecfSBarry Smith 
7603bb7acecfSBarry Smith   Not collective
7604a8fb8f29SToby Isaac 
7605a8fb8f29SToby Isaac   Input Parameter:
7606bb7acecfSBarry Smith . dm - The `DM` object
7607a8fb8f29SToby Isaac 
7608a8fb8f29SToby Isaac   Output Parameter:
7609bb7acecfSBarry Smith . cdm - The coarse `DM`
7610a8fb8f29SToby Isaac 
7611a8fb8f29SToby Isaac   Level: intermediate
7612a8fb8f29SToby Isaac 
7613bb7acecfSBarry Smith .seealso: `DMSetCoarseDM()`, `DMCoarsen()`
7614a8fb8f29SToby Isaac @*/
7615d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7616d71ae5a4SJacob Faibussowitsch {
7617a8fb8f29SToby Isaac   PetscFunctionBegin;
7618a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7619a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7620a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7621a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7622a8fb8f29SToby Isaac }
7623a8fb8f29SToby Isaac 
7624a8fb8f29SToby Isaac /*@
7625bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7626a8fb8f29SToby Isaac 
7627a8fb8f29SToby Isaac   Input Parameters:
7628bb7acecfSBarry Smith + dm - The `DM` object
7629bb7acecfSBarry Smith - cdm - The coarse `DM`
7630a8fb8f29SToby Isaac 
7631a8fb8f29SToby Isaac   Level: intermediate
7632a8fb8f29SToby Isaac 
7633bb7acecfSBarry Smith   Note:
7634bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7635bb7acecfSBarry Smith 
7636bb7acecfSBarry Smith .seealso: `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7637a8fb8f29SToby Isaac @*/
7638d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7639d71ae5a4SJacob Faibussowitsch {
7640a8fb8f29SToby Isaac   PetscFunctionBegin;
7641a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7642a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
764389d734beSBarry Smith   if (dm == cdm) cdm = NULL;
76449566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
76459566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7646a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7647a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7648a8fb8f29SToby Isaac }
7649a8fb8f29SToby Isaac 
765088bdff64SToby Isaac /*@
7651bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
765288bdff64SToby Isaac 
765388bdff64SToby Isaac   Input Parameter:
7654bb7acecfSBarry Smith . dm - The `DM` object
765588bdff64SToby Isaac 
765688bdff64SToby Isaac   Output Parameter:
7657bb7acecfSBarry Smith . fdm - The fine `DM`
765888bdff64SToby Isaac 
765988bdff64SToby Isaac   Level: intermediate
766088bdff64SToby Isaac 
7661bb7acecfSBarry Smith .seealso: `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
766288bdff64SToby Isaac @*/
7663d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7664d71ae5a4SJacob Faibussowitsch {
766588bdff64SToby Isaac   PetscFunctionBegin;
766688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
766788bdff64SToby Isaac   PetscValidPointer(fdm, 2);
766888bdff64SToby Isaac   *fdm = dm->fineMesh;
766988bdff64SToby Isaac   PetscFunctionReturn(0);
767088bdff64SToby Isaac }
767188bdff64SToby Isaac 
767288bdff64SToby Isaac /*@
7673bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
767488bdff64SToby Isaac 
767588bdff64SToby Isaac   Input Parameters:
7676bb7acecfSBarry Smith + dm - The `DM` object
7677bb7acecfSBarry Smith - fdm - The fine `DM`
767888bdff64SToby Isaac 
7679bb7acecfSBarry Smith   Level: developer
768088bdff64SToby Isaac 
7681bb7acecfSBarry Smith   Note:
7682bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7683bb7acecfSBarry Smith 
7684bb7acecfSBarry Smith .seealso: `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
768588bdff64SToby Isaac @*/
7686d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7687d71ae5a4SJacob Faibussowitsch {
768888bdff64SToby Isaac   PetscFunctionBegin;
768988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
769088bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
769189d734beSBarry Smith   if (dm == fdm) fdm = NULL;
76929566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
76939566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
769488bdff64SToby Isaac   dm->fineMesh = fdm;
769588bdff64SToby Isaac   PetscFunctionReturn(0);
769688bdff64SToby Isaac }
769788bdff64SToby Isaac 
7698a6ba4734SToby Isaac /*@C
7699bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7700a6ba4734SToby Isaac 
7701783e2ec8SMatthew G. Knepley   Collective on dm
7702783e2ec8SMatthew G. Knepley 
7703a6ba4734SToby Isaac   Input Parameters:
7704bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7705bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7706a6ba4734SToby Isaac . name     - The BC name
770745480ffeSMatthew G. Knepley . label    - The label defining constrained points
7708bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
770945480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7710a6ba4734SToby Isaac . field    - The field to constrain
771145480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7712a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7713a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
771456cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7715a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7716a6ba4734SToby Isaac 
771745480ffeSMatthew G. Knepley   Output Parameter:
771845480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
771945480ffeSMatthew G. Knepley 
7720a6ba4734SToby Isaac   Options Database Keys:
7721a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7722a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7723a6ba4734SToby Isaac 
7724bb7acecfSBarry Smith   Notes:
7725bb7acecfSBarry 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:
772656cf3b9cSMatthew G. Knepley 
772756cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
772856cf3b9cSMatthew G. Knepley 
7729bb7acecfSBarry Smith   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is:
773056cf3b9cSMatthew G. Knepley 
773156cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
773256cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
773356cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
773456cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
773556cf3b9cSMatthew G. Knepley 
773656cf3b9cSMatthew G. Knepley + dim - the spatial dimension
773756cf3b9cSMatthew G. Knepley . Nf - the number of fields
773856cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
773956cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
774056cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
774156cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
774256cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
774356cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
774456cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
774556cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
774656cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
774756cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
774856cf3b9cSMatthew G. Knepley . t - current time
774956cf3b9cSMatthew G. Knepley . x - coordinates of the current point
775056cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
775156cf3b9cSMatthew G. Knepley . constants - constant parameters
775256cf3b9cSMatthew G. Knepley - bcval - output values at the current point
775356cf3b9cSMatthew G. Knepley 
7754ed808b8fSJed Brown   Level: intermediate
7755a6ba4734SToby Isaac 
7756db781477SPatrick Sanan .seealso: `DSGetBoundary()`, `PetscDSAddBoundary()`
7757a6ba4734SToby Isaac @*/
7758d71ae5a4SJacob 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)
7759d71ae5a4SJacob Faibussowitsch {
7760e5e52638SMatthew G. Knepley   PetscDS ds;
7761a6ba4734SToby Isaac 
7762a6ba4734SToby Isaac   PetscFunctionBegin;
7763a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7764783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
776545480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
776645480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
776745480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
776845480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
776901a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
77709566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7771799db056SMatthew G. Knepley   /* Complete label */
7772799db056SMatthew G. Knepley   if (label) {
7773799db056SMatthew G. Knepley     PetscObject  obj;
7774799db056SMatthew G. Knepley     PetscClassId id;
7775799db056SMatthew G. Knepley 
7776799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7777799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7778799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7779799db056SMatthew G. Knepley       DM plex;
7780799db056SMatthew G. Knepley 
7781799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7782799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7783799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7784799db056SMatthew G. Knepley     }
7785799db056SMatthew G. Knepley   }
77869566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
7787a6ba4734SToby Isaac   PetscFunctionReturn(0);
7788a6ba4734SToby Isaac }
7789a6ba4734SToby Isaac 
779045480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7791d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
7792d71ae5a4SJacob Faibussowitsch {
7793e5e52638SMatthew G. Knepley   PetscDS     ds;
7794dff059c6SToby Isaac   DMBoundary *lastnext;
7795e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7796e6f8dbb6SToby Isaac 
7797e6f8dbb6SToby Isaac   PetscFunctionBegin;
77989566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7799e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
780047a1f5adSToby Isaac   if (dm->boundary) {
780147a1f5adSToby Isaac     DMBoundary next = dm->boundary;
780247a1f5adSToby Isaac 
780347a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
780447a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
780547a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
780647a1f5adSToby Isaac     while (next) {
780747a1f5adSToby Isaac       DMBoundary b = next;
780847a1f5adSToby Isaac 
780947a1f5adSToby Isaac       next = b->next;
78109566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7811a6ba4734SToby Isaac     }
781247a1f5adSToby Isaac     dm->boundary = NULL;
7813a6ba4734SToby Isaac   }
781447a1f5adSToby Isaac 
7815dff059c6SToby Isaac   lastnext = &(dm->boundary);
7816e6f8dbb6SToby Isaac   while (dsbound) {
7817e6f8dbb6SToby Isaac     DMBoundary dmbound;
7818e6f8dbb6SToby Isaac 
78199566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7820e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
782145480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
782247a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7823dff059c6SToby Isaac     *lastnext = dmbound;
7824dff059c6SToby Isaac     lastnext  = &(dmbound->next);
7825dff059c6SToby Isaac     dsbound   = dsbound->next;
7826a6ba4734SToby Isaac   }
7827a6ba4734SToby Isaac   PetscFunctionReturn(0);
7828a6ba4734SToby Isaac }
7829a6ba4734SToby Isaac 
7830bb7acecfSBarry Smith /* TODO: missing manual page */
7831d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7832d71ae5a4SJacob Faibussowitsch {
7833b95f2879SToby Isaac   DMBoundary b;
7834a6ba4734SToby Isaac 
7835a6ba4734SToby Isaac   PetscFunctionBegin;
7836a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7837534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7838a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
78399566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7840b95f2879SToby Isaac   b = dm->boundary;
7841a6ba4734SToby Isaac   while (b && !(*isBd)) {
7842e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7843e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7844a6ba4734SToby Isaac     PetscInt   i;
7845a6ba4734SToby Isaac 
784645480ffeSMatthew G. Knepley     if (label) {
78479566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7848a6ba4734SToby Isaac     }
7849a6ba4734SToby Isaac     b = b->next;
7850a6ba4734SToby Isaac   }
7851a6ba4734SToby Isaac   PetscFunctionReturn(0);
7852a6ba4734SToby Isaac }
78534d6f44ffSToby Isaac 
78544d6f44ffSToby Isaac /*@C
7855bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
7856a6e0b375SMatthew G. Knepley 
7857bb7acecfSBarry Smith   Collective on dm
78584d6f44ffSToby Isaac 
78594d6f44ffSToby Isaac   Input Parameters:
7860bb7acecfSBarry Smith + dm      - The `DM`
78610709b2feSToby Isaac . time    - The time
78624d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
78634d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
78644d6f44ffSToby Isaac - mode    - The insertion mode for values
78654d6f44ffSToby Isaac 
78664d6f44ffSToby Isaac   Output Parameter:
78674d6f44ffSToby Isaac . X - vector
78684d6f44ffSToby Isaac 
78694d6f44ffSToby Isaac    Calling sequence of func:
787077b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
78714d6f44ffSToby Isaac 
78724d6f44ffSToby Isaac +  dim - The spatial dimension
78738ec8862eSJed Brown .  time - The time at which to sample
78744d6f44ffSToby Isaac .  x   - The coordinates
787577b739a6SMatthew Knepley .  Nc  - The number of components
78764d6f44ffSToby Isaac .  u   - The output field values
78774d6f44ffSToby Isaac -  ctx - optional user-defined function context
78784d6f44ffSToby Isaac 
78794d6f44ffSToby Isaac   Level: developer
78804d6f44ffSToby Isaac 
7881bb7acecfSBarry Smith   Developer Notes:
7882bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7883bb7acecfSBarry Smith 
7884bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7885bb7acecfSBarry Smith 
7886db781477SPatrick Sanan .seealso: `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
78874d6f44ffSToby Isaac @*/
7888d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
7889d71ae5a4SJacob Faibussowitsch {
78904d6f44ffSToby Isaac   Vec localX;
78914d6f44ffSToby Isaac 
78924d6f44ffSToby Isaac   PetscFunctionBegin;
78934d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78949566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
7895f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
78969566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
78979566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
78989566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
78999566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
79004d6f44ffSToby Isaac   PetscFunctionReturn(0);
79014d6f44ffSToby Isaac }
79024d6f44ffSToby Isaac 
7903a6e0b375SMatthew G. Knepley /*@C
7904bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
7905a6e0b375SMatthew G. Knepley 
7906a6e0b375SMatthew G. Knepley   Not collective
7907a6e0b375SMatthew G. Knepley 
7908a6e0b375SMatthew G. Knepley   Input Parameters:
7909bb7acecfSBarry Smith + dm      - The `DM`
7910a6e0b375SMatthew G. Knepley . time    - The time
7911a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7912a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7913a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7914a6e0b375SMatthew G. Knepley 
7915a6e0b375SMatthew G. Knepley   Output Parameter:
7916a6e0b375SMatthew G. Knepley . localX - vector
7917a6e0b375SMatthew G. Knepley 
7918a6e0b375SMatthew G. Knepley    Calling sequence of func:
791977b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7920a6e0b375SMatthew G. Knepley 
7921a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7922a6e0b375SMatthew G. Knepley .  x   - The coordinates
792377b739a6SMatthew Knepley .  Nc  - The number of components
7924a6e0b375SMatthew G. Knepley .  u   - The output field values
7925a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7926a6e0b375SMatthew G. Knepley 
7927a6e0b375SMatthew G. Knepley   Level: developer
7928a6e0b375SMatthew G. Knepley 
7929bb7acecfSBarry Smith   Developer Notes:
7930bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7931bb7acecfSBarry Smith 
7932bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7933bb7acecfSBarry Smith 
7934db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7935a6e0b375SMatthew G. Knepley @*/
7936d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
7937d71ae5a4SJacob Faibussowitsch {
79384d6f44ffSToby Isaac   PetscFunctionBegin;
79394d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7940064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
79419566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal)(dm, time, funcs, ctxs, mode, localX));
79424d6f44ffSToby Isaac   PetscFunctionReturn(0);
79434d6f44ffSToby Isaac }
79444d6f44ffSToby Isaac 
7945a6e0b375SMatthew G. Knepley /*@C
7946bb7acecfSBarry 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.
7947a6e0b375SMatthew G. Knepley 
7948bb7acecfSBarry Smith   Collective on dm
7949a6e0b375SMatthew G. Knepley 
7950a6e0b375SMatthew G. Knepley   Input Parameters:
7951bb7acecfSBarry Smith + dm      - The `DM`
7952a6e0b375SMatthew G. Knepley . time    - The time
7953bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
7954a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7955bb7acecfSBarry Smith . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
7956a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7957a6e0b375SMatthew G. Knepley 
7958a6e0b375SMatthew G. Knepley   Output Parameter:
7959a6e0b375SMatthew G. Knepley . X - vector
7960a6e0b375SMatthew G. Knepley 
7961a6e0b375SMatthew G. Knepley    Calling sequence of func:
796277b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7963a6e0b375SMatthew G. Knepley 
7964a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7965a6e0b375SMatthew G. Knepley .  x   - The coordinates
796677b739a6SMatthew Knepley .  Nc  - The number of components
7967a6e0b375SMatthew G. Knepley .  u   - The output field values
7968a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7969a6e0b375SMatthew G. Knepley 
7970a6e0b375SMatthew G. Knepley   Level: developer
7971a6e0b375SMatthew G. Knepley 
7972bb7acecfSBarry Smith   Developer Notes:
7973bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7974bb7acecfSBarry Smith 
7975bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7976bb7acecfSBarry Smith 
7977db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
7978a6e0b375SMatthew G. Knepley @*/
7979d71ae5a4SJacob 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)
7980d71ae5a4SJacob Faibussowitsch {
79812c53366bSMatthew G. Knepley   Vec localX;
79822c53366bSMatthew G. Knepley 
79832c53366bSMatthew G. Knepley   PetscFunctionBegin;
79842c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79859566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
7986f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
79879566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
79889566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
79899566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
79909566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
79912c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
79922c53366bSMatthew G. Knepley }
79932c53366bSMatthew G. Knepley 
7994a6e0b375SMatthew G. Knepley /*@C
7995bb7acecfSBarry 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.
7996a6e0b375SMatthew G. Knepley 
7997a6e0b375SMatthew G. Knepley   Not collective
7998a6e0b375SMatthew G. Knepley 
7999a6e0b375SMatthew G. Knepley   Input Parameters:
8000bb7acecfSBarry Smith + dm      - The `DM`
8001a6e0b375SMatthew G. Knepley . time    - The time
8002bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
8003a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8004a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8005a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8006a6e0b375SMatthew G. Knepley 
8007a6e0b375SMatthew G. Knepley   Output Parameter:
8008a6e0b375SMatthew G. Knepley . localX - vector
8009a6e0b375SMatthew G. Knepley 
8010a6e0b375SMatthew G. Knepley    Calling sequence of func:
801177b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8012a6e0b375SMatthew G. Knepley 
8013a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8014a6e0b375SMatthew G. Knepley .  x   - The coordinates
801577b739a6SMatthew Knepley .  Nc  - The number of components
8016a6e0b375SMatthew G. Knepley .  u   - The output field values
8017a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8018a6e0b375SMatthew G. Knepley 
8019a6e0b375SMatthew G. Knepley   Level: developer
8020a6e0b375SMatthew G. Knepley 
8021bb7acecfSBarry Smith   Developer Notes:
8022bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8023bb7acecfSBarry Smith 
8024bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8025bb7acecfSBarry Smith 
8026db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8027a6e0b375SMatthew G. Knepley @*/
8028d71ae5a4SJacob 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)
8029d71ae5a4SJacob Faibussowitsch {
80304d6f44ffSToby Isaac   PetscFunctionBegin;
80314d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8032064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
80339566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal)(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
80344d6f44ffSToby Isaac   PetscFunctionReturn(0);
80354d6f44ffSToby Isaac }
80362716604bSToby Isaac 
8037a6e0b375SMatthew G. Knepley /*@C
8038bb7acecfSBarry 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.
8039a6e0b375SMatthew G. Knepley 
8040a6e0b375SMatthew G. Knepley   Not collective
8041a6e0b375SMatthew G. Knepley 
8042a6e0b375SMatthew G. Knepley   Input Parameters:
8043bb7acecfSBarry Smith + dm      - The `DM`
8044a6e0b375SMatthew G. Knepley . time    - The time
8045a6e0b375SMatthew G. Knepley . localU  - The input field vector
8046a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8047a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8048a6e0b375SMatthew G. Knepley 
8049a6e0b375SMatthew G. Knepley   Output Parameter:
8050a6e0b375SMatthew G. Knepley . localX  - The output vector
8051a6e0b375SMatthew G. Knepley 
8052a6e0b375SMatthew G. Knepley    Calling sequence of func:
8053a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8054a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8055a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8056a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8057a6e0b375SMatthew G. Knepley 
8058a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8059a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8060a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8061a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8062a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8063a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8064a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8065a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8066a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8067a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8068a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8069a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8070a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8071a6e0b375SMatthew G. Knepley .  t            - The current time
8072a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8073a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8074a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8075a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8076a6e0b375SMatthew G. Knepley 
8077bb7acecfSBarry Smith   Note:
8078bb7acecfSBarry 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.
8079bb7acecfSBarry 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
8080bb7acecfSBarry 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
8081a6e0b375SMatthew 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.
8082a6e0b375SMatthew G. Knepley 
8083a6e0b375SMatthew G. Knepley   Level: intermediate
8084a6e0b375SMatthew G. Knepley 
8085bb7acecfSBarry Smith   Developer Notes:
8086bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8087bb7acecfSBarry Smith 
8088bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8089bb7acecfSBarry Smith 
8090db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8091a6e0b375SMatthew G. Knepley @*/
8092d71ae5a4SJacob 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)
8093d71ae5a4SJacob Faibussowitsch {
80948c6c5593SMatthew G. Knepley   PetscFunctionBegin;
80958c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80968c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
80978c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
80989566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal)(dm, time, localU, funcs, mode, localX));
80998c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
81008c6c5593SMatthew G. Knepley }
81018c6c5593SMatthew G. Knepley 
8102a6e0b375SMatthew G. Knepley /*@C
8103a6e0b375SMatthew 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.
8104a6e0b375SMatthew G. Knepley 
8105a6e0b375SMatthew G. Knepley   Not collective
8106a6e0b375SMatthew G. Knepley 
8107a6e0b375SMatthew G. Knepley   Input Parameters:
8108bb7acecfSBarry Smith + dm      - The `DM`
8109a6e0b375SMatthew G. Knepley . time    - The time
8110bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8111a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8112a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8113bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8114a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8115a6e0b375SMatthew G. Knepley . localU  - The input field vector
8116a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8117a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8118a6e0b375SMatthew G. Knepley 
8119a6e0b375SMatthew G. Knepley   Output Parameter:
8120a6e0b375SMatthew G. Knepley . localX  - The output vector
8121a6e0b375SMatthew G. Knepley 
8122a6e0b375SMatthew G. Knepley    Calling sequence of func:
8123a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8124a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8125a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8126a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8127a6e0b375SMatthew G. Knepley 
8128a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8129a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8130a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8131a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8132a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8133a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8134a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8135a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8136a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8137a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8138a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8139a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8140a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8141a6e0b375SMatthew G. Knepley .  t            - The current time
8142a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8143a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8144a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8145a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8146a6e0b375SMatthew G. Knepley 
8147bb7acecfSBarry Smith   Note:
8148bb7acecfSBarry 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.
8149bb7acecfSBarry 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
8150bb7acecfSBarry 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
8151a6e0b375SMatthew 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.
8152a6e0b375SMatthew G. Knepley 
8153a6e0b375SMatthew G. Knepley   Level: intermediate
8154a6e0b375SMatthew G. Knepley 
8155bb7acecfSBarry Smith   Developer Notes:
8156bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8157bb7acecfSBarry Smith 
8158bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8159bb7acecfSBarry Smith 
8160d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8161a6e0b375SMatthew G. Knepley @*/
8162d71ae5a4SJacob 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)
8163d71ae5a4SJacob Faibussowitsch {
81648c6c5593SMatthew G. Knepley   PetscFunctionBegin;
81658c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8166064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8167064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
81689566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
81698c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
81708c6c5593SMatthew G. Knepley }
81718c6c5593SMatthew G. Knepley 
81722716604bSToby Isaac /*@C
8173d29d7c6eSMatthew 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.
8174d29d7c6eSMatthew G. Knepley 
8175d29d7c6eSMatthew G. Knepley   Not collective
8176d29d7c6eSMatthew G. Knepley 
8177d29d7c6eSMatthew G. Knepley   Input Parameters:
8178bb7acecfSBarry Smith + dm      - The `DM`
8179d29d7c6eSMatthew G. Knepley . time    - The time
8180bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8181d29d7c6eSMatthew G. Knepley . numIds  - The number of label ids to use
8182d29d7c6eSMatthew G. Knepley . ids     - The label ids to use for marking
8183bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8184d29d7c6eSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8185d29d7c6eSMatthew G. Knepley . U       - The input field vector
8186d29d7c6eSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8187d29d7c6eSMatthew G. Knepley - mode    - The insertion mode for values
8188d29d7c6eSMatthew G. Knepley 
8189d29d7c6eSMatthew G. Knepley   Output Parameter:
8190d29d7c6eSMatthew G. Knepley . X       - The output vector
8191d29d7c6eSMatthew G. Knepley 
8192d29d7c6eSMatthew G. Knepley    Calling sequence of func:
8193d29d7c6eSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8194d29d7c6eSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8195d29d7c6eSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8196d29d7c6eSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8197d29d7c6eSMatthew G. Knepley 
8198d29d7c6eSMatthew G. Knepley +  dim          - The spatial dimension
8199d29d7c6eSMatthew G. Knepley .  Nf           - The number of input fields
8200d29d7c6eSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8201d29d7c6eSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8202d29d7c6eSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8203d29d7c6eSMatthew G. Knepley .  u            - The field values at this point in space
8204d29d7c6eSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8205d29d7c6eSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8206d29d7c6eSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8207d29d7c6eSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8208d29d7c6eSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8209d29d7c6eSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8210d29d7c6eSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8211d29d7c6eSMatthew G. Knepley .  t            - The current time
8212d29d7c6eSMatthew G. Knepley .  x            - The coordinates of this point
8213d29d7c6eSMatthew G. Knepley .  numConstants - The number of constants
8214d29d7c6eSMatthew G. Knepley .  constants    - The value of each constant
8215d29d7c6eSMatthew G. Knepley -  f            - The value of the function at this point in space
8216d29d7c6eSMatthew G. Knepley 
8217bb7acecfSBarry Smith   Note:
8218bb7acecfSBarry 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.
8219bb7acecfSBarry 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
8220bb7acecfSBarry 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
8221d29d7c6eSMatthew 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.
8222d29d7c6eSMatthew G. Knepley 
8223d29d7c6eSMatthew G. Knepley   Level: intermediate
8224d29d7c6eSMatthew G. Knepley 
8225bb7acecfSBarry Smith   Developer Notes:
8226bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8227bb7acecfSBarry Smith 
8228bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8229bb7acecfSBarry Smith 
8230d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8231d29d7c6eSMatthew G. Knepley @*/
8232d71ae5a4SJacob 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)
8233d71ae5a4SJacob Faibussowitsch {
8234d29d7c6eSMatthew G. Knepley   DM  dmIn;
8235d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8236d29d7c6eSMatthew G. Knepley 
8237d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8238d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8239d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8240d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8241d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8242f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
824372fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
824472fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8245d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8246d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8247d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8248d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8249d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
8250d29d7c6eSMatthew G. Knepley   PetscFunctionReturn(0);
8251d29d7c6eSMatthew G. Knepley }
8252d29d7c6eSMatthew G. Knepley 
8253d29d7c6eSMatthew G. Knepley /*@C
8254ece3a9fcSMatthew 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.
8255ece3a9fcSMatthew G. Knepley 
8256ece3a9fcSMatthew G. Knepley   Not collective
8257ece3a9fcSMatthew G. Knepley 
8258ece3a9fcSMatthew G. Knepley   Input Parameters:
8259bb7acecfSBarry Smith + dm      - The `DM`
8260ece3a9fcSMatthew G. Knepley . time    - The time
8261bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain boundary to output
8262ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8263ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8264bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8265ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8266ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8267ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8268ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8269ece3a9fcSMatthew G. Knepley 
8270ece3a9fcSMatthew G. Knepley   Output Parameter:
8271ece3a9fcSMatthew G. Knepley . localX  - The output vector
8272ece3a9fcSMatthew G. Knepley 
8273ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8274ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8275ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8276ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8277ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8278ece3a9fcSMatthew G. Knepley 
8279ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8280ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8281ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8282ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8283ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8284ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8285ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8286ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8287ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8288ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8289ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8290ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8291ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8292ece3a9fcSMatthew G. Knepley .  t            - The current time
8293ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8294ece3a9fcSMatthew G. Knepley .  n            - The face normal
8295ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8296ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8297ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8298ece3a9fcSMatthew G. Knepley 
8299ece3a9fcSMatthew G. Knepley   Note:
8300bb7acecfSBarry 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.
8301bb7acecfSBarry 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
8302bb7acecfSBarry 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
8303ece3a9fcSMatthew 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.
8304ece3a9fcSMatthew G. Knepley 
8305ece3a9fcSMatthew G. Knepley   Level: intermediate
8306ece3a9fcSMatthew G. Knepley 
8307bb7acecfSBarry Smith   Developer Notes:
8308bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8309bb7acecfSBarry Smith 
8310bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8311bb7acecfSBarry Smith 
8312db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8313ece3a9fcSMatthew G. Knepley @*/
8314d71ae5a4SJacob 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)
8315d71ae5a4SJacob Faibussowitsch {
8316ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8317ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8318064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8319064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
83209566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8321ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8322ece3a9fcSMatthew G. Knepley }
8323ece3a9fcSMatthew G. Knepley 
8324ece3a9fcSMatthew G. Knepley /*@C
83252716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
83262716604bSToby Isaac 
8327bb7acecfSBarry Smith   Collective on dm
8328bb7acecfSBarry Smith 
83292716604bSToby Isaac   Input Parameters:
8330bb7acecfSBarry Smith + dm    - The `DM`
83310709b2feSToby Isaac . time  - The time
83322716604bSToby Isaac . funcs - The functions to evaluate for each field component
83332716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8334574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
83352716604bSToby Isaac 
83362716604bSToby Isaac   Output Parameter:
83372716604bSToby Isaac . diff - The diff ||u - u_h||_2
83382716604bSToby Isaac 
83392716604bSToby Isaac   Level: developer
83402716604bSToby Isaac 
8341bb7acecfSBarry Smith   Developer Notes:
8342bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8343bb7acecfSBarry Smith 
8344bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8345bb7acecfSBarry Smith 
8346db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
83472716604bSToby Isaac @*/
8348d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8349d71ae5a4SJacob Faibussowitsch {
83502716604bSToby Isaac   PetscFunctionBegin;
83512716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8352b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
83539566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm, time, funcs, ctxs, X, diff));
83542716604bSToby Isaac   PetscFunctionReturn(0);
83552716604bSToby Isaac }
8356b698f381SToby Isaac 
8357b698f381SToby Isaac /*@C
8358b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8359b698f381SToby Isaac 
8360d083f849SBarry Smith   Collective on dm
8361d083f849SBarry Smith 
8362b698f381SToby Isaac   Input Parameters:
8363bb7acecfSBarry Smith + dm    - The `DM`
8364b698f381SToby Isaac , time  - The time
8365b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8366b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8367574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8368b698f381SToby Isaac - n     - The vector to project along
8369b698f381SToby Isaac 
8370b698f381SToby Isaac   Output Parameter:
8371b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8372b698f381SToby Isaac 
8373b698f381SToby Isaac   Level: developer
8374b698f381SToby Isaac 
8375bb7acecfSBarry Smith   Developer Notes:
8376bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8377bb7acecfSBarry Smith 
8378bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8379bb7acecfSBarry Smith 
8380bb7acecfSBarry Smith .seealso: `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8381b698f381SToby Isaac @*/
8382d71ae5a4SJacob 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)
8383d71ae5a4SJacob Faibussowitsch {
8384b698f381SToby Isaac   PetscFunctionBegin;
8385b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8386b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
83879566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm, time, funcs, ctxs, X, n, diff));
8388b698f381SToby Isaac   PetscFunctionReturn(0);
8389b698f381SToby Isaac }
8390b698f381SToby Isaac 
83912a16baeaSToby Isaac /*@C
83922a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
83932a16baeaSToby Isaac 
8394d083f849SBarry Smith   Collective on dm
8395d083f849SBarry Smith 
83962a16baeaSToby Isaac   Input Parameters:
8397bb7acecfSBarry Smith + dm    - The `DM`
83982a16baeaSToby Isaac . time  - The time
83992a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
84002a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8401574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84022a16baeaSToby Isaac 
84032a16baeaSToby Isaac   Output Parameter:
84042a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
84052a16baeaSToby Isaac 
84062a16baeaSToby Isaac   Level: developer
84072a16baeaSToby Isaac 
8408bb7acecfSBarry Smith   Developer Notes:
8409bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8410bb7acecfSBarry Smith 
8411bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8412bb7acecfSBarry Smith 
8413db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
84142a16baeaSToby Isaac @*/
8415d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8416d71ae5a4SJacob Faibussowitsch {
84172a16baeaSToby Isaac   PetscFunctionBegin;
84182a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84192a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84209566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm, time, funcs, ctxs, X, diff));
84212a16baeaSToby Isaac   PetscFunctionReturn(0);
84222a16baeaSToby Isaac }
84232a16baeaSToby Isaac 
8424df0b854cSToby Isaac /*@C
8425bb7acecfSBarry Smith  DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8426502a2867SDave May 
8427502a2867SDave May  Not Collective
8428502a2867SDave May 
8429502a2867SDave May  Input Parameter:
8430bb7acecfSBarry Smith .  dm    - The `DM`
8431502a2867SDave May 
84320a19bb7dSprj-  Output Parameters:
84330a19bb7dSprj- +  nranks - the number of neighbours
84340a19bb7dSprj- -  ranks - the neighbors ranks
8435502a2867SDave May 
8436bb7acecfSBarry Smith  Note:
8437bb7acecfSBarry Smith  Do not free the array, it is freed when the `DM` is destroyed.
8438502a2867SDave May 
8439502a2867SDave May  Level: beginner
8440502a2867SDave May 
8441db781477SPatrick Sanan  .seealso: `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8442502a2867SDave May @*/
8443d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8444d71ae5a4SJacob Faibussowitsch {
8445502a2867SDave May   PetscFunctionBegin;
8446502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84479566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm, nranks, ranks));
8448502a2867SDave May   PetscFunctionReturn(0);
8449502a2867SDave May }
8450502a2867SDave May 
8451531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8452531c7667SBarry Smith 
8453531c7667SBarry Smith /*
8454531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8455531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8456531c7667SBarry Smith */
8457d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8458d71ae5a4SJacob Faibussowitsch {
8459531c7667SBarry Smith   PetscFunctionBegin;
8460531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8461531c7667SBarry Smith     Vec x1local;
8462531c7667SBarry Smith     DM  dm;
84639566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
84647a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
84659566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
84669566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
84679566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8468531c7667SBarry Smith     x1 = x1local;
8469531c7667SBarry Smith   }
84709566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8471531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8472531c7667SBarry Smith     DM dm;
84739566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
84749566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8475531c7667SBarry Smith   }
8476531c7667SBarry Smith   PetscFunctionReturn(0);
8477531c7667SBarry Smith }
8478531c7667SBarry Smith 
8479531c7667SBarry Smith /*@
8480bb7acecfSBarry Smith     MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8481531c7667SBarry Smith 
8482531c7667SBarry Smith     Input Parameter:
8483bb7acecfSBarry Smith .    coloring - the `MatFDColoring` object
8484531c7667SBarry Smith 
8485bb7acecfSBarry Smith     Developer Note:
8486bb7acecfSBarry Smith     this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8487531c7667SBarry Smith 
84881b266c99SBarry Smith     Level: advanced
84891b266c99SBarry Smith 
8490db781477SPatrick Sanan .seealso: `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8491531c7667SBarry Smith @*/
8492d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8493d71ae5a4SJacob Faibussowitsch {
8494531c7667SBarry Smith   PetscFunctionBegin;
8495531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8496531c7667SBarry Smith   PetscFunctionReturn(0);
8497531c7667SBarry Smith }
84988320bc6fSPatrick Sanan 
84998320bc6fSPatrick Sanan /*@
8500bb7acecfSBarry Smith     DMGetCompatibility - determine if two `DM`s are compatible
85018320bc6fSPatrick Sanan 
85028320bc6fSPatrick Sanan     Collective
85038320bc6fSPatrick Sanan 
85048320bc6fSPatrick Sanan     Input Parameters:
8505bb7acecfSBarry Smith +    dm1 - the first `DM`
8506bb7acecfSBarry Smith -    dm2 - the second `DM`
85078320bc6fSPatrick Sanan 
85088320bc6fSPatrick Sanan     Output Parameters:
8509bb7acecfSBarry Smith +    compatible - whether or not the two `DM`s are compatible
8510bb7acecfSBarry Smith -    set - whether or not the compatible value was actually determined and set
85118320bc6fSPatrick Sanan 
85128320bc6fSPatrick Sanan     Notes:
8513bb7acecfSBarry Smith     Two `DM`s are deemed compatible if they represent the same parallel decomposition
85143d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
85158320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
8516bb7acecfSBarry Smith     Loosely speaking, compatible `DM`s represent the same domain and parallel
85173d862458SPatrick Sanan     decomposition, but hold different data.
85188320bc6fSPatrick Sanan 
85198320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
8520bb7acecfSBarry Smith     over a pair of vectors obtained from different `DM`s.
85218320bc6fSPatrick Sanan 
8522bb7acecfSBarry Smith     For example, two `DMDA` objects are compatible if they have the same local
85238320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
85248320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
8525bb7acecfSBarry Smith     either `DM` in bounds for a loop over vectors derived from either `DM`.
85268320bc6fSPatrick Sanan 
8527bb7acecfSBarry Smith     Consider the operation of summing data living on a 2-dof `DMDA` to data living
8528bb7acecfSBarry Smith     on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
85298320bc6fSPatrick Sanan .vb
85308320bc6fSPatrick Sanan   ...
85319566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
85328320bc6fSPatrick Sanan   if (set && compatible)  {
85339566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
85349566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
85359566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
85368320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
85378320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
85388320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
85398320bc6fSPatrick Sanan       }
85408320bc6fSPatrick Sanan     }
85419566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
85429566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
85438320bc6fSPatrick Sanan   } else {
85448320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
85458320bc6fSPatrick Sanan   }
85468320bc6fSPatrick Sanan   ...
85478320bc6fSPatrick Sanan .ve
85488320bc6fSPatrick Sanan 
8549bb7acecfSBarry Smith     Checking compatibility might be expensive for a given implementation of `DM`,
85508320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
85518320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
85528320bc6fSPatrick Sanan     always check the "set" output parameter.
85538320bc6fSPatrick Sanan 
8554bb7acecfSBarry Smith     A `DM` is always compatible with itself.
85558320bc6fSPatrick Sanan 
8556bb7acecfSBarry Smith     In the current implementation, `DM`s which live on "unequal" communicators
85578320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
85588320bc6fSPatrick Sanan     incompatible.
85598320bc6fSPatrick Sanan 
85608320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
8561bb7acecfSBarry Smith     is required on each rank. However, in `DM` implementations which store all this
85628320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
85638320bc6fSPatrick Sanan 
8564bb7acecfSBarry Smith     Developer Note:
8565bb7acecfSBarry Smith     Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
85663d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8567a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
8568bb7acecfSBarry Smith     compatibility. It is left to `DM` implementers to ensure that symmetry is
85698320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
85703d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
8571bb7acecfSBarry Smith     of other `DM` types and let *set = PETSC_FALSE if found.
85728320bc6fSPatrick Sanan 
85738320bc6fSPatrick Sanan     Level: advanced
85748320bc6fSPatrick Sanan 
8575db781477SPatrick Sanan .seealso: `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
85768320bc6fSPatrick Sanan @*/
8577d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8578d71ae5a4SJacob Faibussowitsch {
85798320bc6fSPatrick Sanan   PetscMPIInt compareResult;
85808320bc6fSPatrick Sanan   DMType      type, type2;
85818320bc6fSPatrick Sanan   PetscBool   sameType;
85828320bc6fSPatrick Sanan 
85838320bc6fSPatrick Sanan   PetscFunctionBegin;
8584a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
85858320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
85868320bc6fSPatrick Sanan 
85878320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8588a5bc1bf3SBarry Smith   if (dm1 == dm2) {
85898320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
85908320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
85918320bc6fSPatrick Sanan     PetscFunctionReturn(0);
85928320bc6fSPatrick Sanan   }
85938320bc6fSPatrick Sanan 
85948320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
85958320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
85968320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
85978320bc6fSPatrick Sanan      determined by the implementation-specific logic */
85989566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
85998320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
86008320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86018320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
86028320bc6fSPatrick Sanan     PetscFunctionReturn(0);
86038320bc6fSPatrick Sanan   }
86048320bc6fSPatrick Sanan 
86058320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8606a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8607dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
8608b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
86098320bc6fSPatrick Sanan   }
86108320bc6fSPatrick Sanan 
8611a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
86128320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
86139566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
86149566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
86159566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
86168320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8617dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
86188320bc6fSPatrick Sanan   } else {
86198320bc6fSPatrick Sanan     *set = PETSC_FALSE;
86208320bc6fSPatrick Sanan   }
86218320bc6fSPatrick Sanan   PetscFunctionReturn(0);
86228320bc6fSPatrick Sanan }
8623c0f0dcc3SMatthew G. Knepley 
8624c0f0dcc3SMatthew G. Knepley /*@C
8625bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8626c0f0dcc3SMatthew G. Knepley 
8627bb7acecfSBarry Smith   Logically Collective on dm
8628c0f0dcc3SMatthew G. Knepley 
8629c0f0dcc3SMatthew G. Knepley   Input Parameters:
8630bb7acecfSBarry Smith + DM - the `DM`
8631c0f0dcc3SMatthew G. Knepley . f - the monitor function
8632c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8633c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8634c0f0dcc3SMatthew G. Knepley 
8635c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8636bb7acecfSBarry Smith - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8637c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8638c0f0dcc3SMatthew G. Knepley 
8639bb7acecfSBarry Smith   Note:
8640c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8641bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8642c0f0dcc3SMatthew G. Knepley   order in which they were set.
8643c0f0dcc3SMatthew G. Knepley 
8644bb7acecfSBarry Smith   Fortran Note:
8645bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8646bb7acecfSBarry Smith 
8647bb7acecfSBarry Smith   Developer Note:
8648bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8649c0f0dcc3SMatthew G. Knepley 
8650c0f0dcc3SMatthew G. Knepley   Level: intermediate
8651c0f0dcc3SMatthew G. Knepley 
8652bb7acecfSBarry Smith .seealso: `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8653c0f0dcc3SMatthew G. Knepley @*/
8654d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8655d71ae5a4SJacob Faibussowitsch {
8656c0f0dcc3SMatthew G. Knepley   PetscInt m;
8657c0f0dcc3SMatthew G. Knepley 
8658c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8659c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8660c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8661c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8662c0f0dcc3SMatthew G. Knepley 
86639566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
8664c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8665c0f0dcc3SMatthew G. Knepley   }
86667a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8667c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8668c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8669c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
8670c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8671c0f0dcc3SMatthew G. Knepley }
8672c0f0dcc3SMatthew G. Knepley 
8673c0f0dcc3SMatthew G. Knepley /*@
8674bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8675c0f0dcc3SMatthew G. Knepley 
8676bb7acecfSBarry Smith   Logically Collective on dm
8677c0f0dcc3SMatthew G. Knepley 
8678c0f0dcc3SMatthew G. Knepley   Input Parameter:
8679c0f0dcc3SMatthew G. Knepley . dm - the DM
8680c0f0dcc3SMatthew G. Knepley 
8681c0f0dcc3SMatthew G. Knepley   Options Database Key:
8682c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8683bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8684c0f0dcc3SMatthew G. Knepley   set via the options database
8685c0f0dcc3SMatthew G. Knepley 
8686bb7acecfSBarry Smith   Note:
8687bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8688c0f0dcc3SMatthew G. Knepley 
8689c0f0dcc3SMatthew G. Knepley   Level: intermediate
8690c0f0dcc3SMatthew G. Knepley 
8691bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8692c0f0dcc3SMatthew G. Knepley @*/
8693d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8694d71ae5a4SJacob Faibussowitsch {
8695c0f0dcc3SMatthew G. Knepley   PetscInt m;
8696c0f0dcc3SMatthew G. Knepley 
8697c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8698c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8699c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
87009566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8701c0f0dcc3SMatthew G. Knepley   }
8702c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8703c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8704c0f0dcc3SMatthew G. Knepley }
8705c0f0dcc3SMatthew G. Knepley 
8706c0f0dcc3SMatthew G. Knepley /*@C
8707c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8708c0f0dcc3SMatthew G. Knepley 
8709bb7acecfSBarry Smith   Collective on dm
8710c0f0dcc3SMatthew G. Knepley 
8711c0f0dcc3SMatthew G. Knepley   Input Parameters:
8712bb7acecfSBarry Smith + dm   - `DM` object you wish to monitor
8713c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8714c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8715c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8716c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8717bb7acecfSBarry 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
8718c0f0dcc3SMatthew G. Knepley 
8719c0f0dcc3SMatthew G. Knepley   Output Parameter:
8720c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8721c0f0dcc3SMatthew G. Knepley 
8722c0f0dcc3SMatthew G. Knepley   Level: developer
8723c0f0dcc3SMatthew G. Knepley 
8724db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8725db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
8726db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
8727db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8728c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8729db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8730bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8731c0f0dcc3SMatthew G. Knepley @*/
8732d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8733d71ae5a4SJacob Faibussowitsch {
8734c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8735c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8736c0f0dcc3SMatthew G. Knepley 
8737c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8738c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87399566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8740c0f0dcc3SMatthew G. Knepley   if (*flg) {
8741c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8742c0f0dcc3SMatthew G. Knepley 
87439566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
87449566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)viewer));
87459566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
87469566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8747c0f0dcc3SMatthew G. Knepley   }
8748c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8749c0f0dcc3SMatthew G. Knepley }
8750c0f0dcc3SMatthew G. Knepley 
8751c0f0dcc3SMatthew G. Knepley /*@
8752c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8753c0f0dcc3SMatthew G. Knepley 
8754bb7acecfSBarry Smith    Collective on dm
8755c0f0dcc3SMatthew G. Knepley 
8756c0f0dcc3SMatthew G. Knepley    Input Parameters:
8757bb7acecfSBarry Smith .  dm - The `DM`
8758c0f0dcc3SMatthew G. Knepley 
8759c0f0dcc3SMatthew G. Knepley    Level: developer
8760c0f0dcc3SMatthew G. Knepley 
8761bb7acecfSBarry Smith    Question:
8762bb7acecfSBarry 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
8763bb7acecfSBarry Smith    since some `DM` have no concept of discretization
8764bb7acecfSBarry Smith 
8765bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8766c0f0dcc3SMatthew G. Knepley @*/
8767d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8768d71ae5a4SJacob Faibussowitsch {
8769c0f0dcc3SMatthew G. Knepley   PetscInt m;
8770c0f0dcc3SMatthew G. Knepley 
8771c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8772c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8773c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
877448a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
8775c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8776c0f0dcc3SMatthew G. Knepley }
87772e4af2aeSMatthew G. Knepley 
87782e4af2aeSMatthew G. Knepley /*@
8779bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
87802e4af2aeSMatthew G. Knepley 
8781bb7acecfSBarry Smith   Collective on dm
87822e4af2aeSMatthew G. Knepley 
87832e4af2aeSMatthew G. Knepley   Input Parameters:
8784bb7acecfSBarry Smith + dm     - The `DM`
87856b867d5aSJose E. Roman - sol    - The solution vector
87862e4af2aeSMatthew G. Knepley 
87876b867d5aSJose E. Roman   Input/Output Parameter:
87886b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
87896b867d5aSJose E. Roman            contains the error in each field
87906b867d5aSJose E. Roman 
87916b867d5aSJose E. Roman   Output Parameter:
87926b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
87932e4af2aeSMatthew G. Knepley 
8794bb7acecfSBarry Smith   Note:
8795bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
87962e4af2aeSMatthew G. Knepley 
87972e4af2aeSMatthew G. Knepley   Level: developer
87982e4af2aeSMatthew G. Knepley 
8799db781477SPatrick Sanan .seealso: `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
88002e4af2aeSMatthew G. Knepley @*/
8801d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
8802d71ae5a4SJacob Faibussowitsch {
88032e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
88042e4af2aeSMatthew G. Knepley   void    **ctxs;
88052e4af2aeSMatthew G. Knepley   PetscReal time;
88062e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
88072e4af2aeSMatthew G. Knepley 
88082e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
88099566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
88109566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
88119566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
88122e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
88132e4af2aeSMatthew G. Knepley     PetscDS         ds;
88142e4af2aeSMatthew G. Knepley     DMLabel         label;
88152e4af2aeSMatthew G. Knepley     IS              fieldIS;
88162e4af2aeSMatthew G. Knepley     const PetscInt *fields;
88172e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
88182e4af2aeSMatthew G. Knepley 
88199566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
88209566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
88219566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
88222e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
88232e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
88249566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
88252e4af2aeSMatthew G. Knepley     }
88269566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
88272e4af2aeSMatthew G. Knepley   }
8828ad540459SPierre 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);
88299566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
88309566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
88312e4af2aeSMatthew G. Knepley   if (errorVec) {
88322e4af2aeSMatthew G. Knepley     DM             edm;
88332e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
88342e4af2aeSMatthew G. Knepley     PetscBool      simplex;
88352e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
88362e4af2aeSMatthew G. Knepley 
88379566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
88389566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
88399566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
88409566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
88412e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
88429566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
88432e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
88442e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
88452e4af2aeSMatthew G. Knepley       PetscQuadrature q;
88462e4af2aeSMatthew G. Knepley       const char     *name;
88472e4af2aeSMatthew G. Knepley 
88489566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
88499566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
88509566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
88519566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
88529566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
88539566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
88549566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
88559566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
88562e4af2aeSMatthew G. Knepley     }
88579566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
88582e4af2aeSMatthew G. Knepley 
88599566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
88609566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
88619566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
88629566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
88632e4af2aeSMatthew G. Knepley   }
88649566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
88652e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
88662e4af2aeSMatthew G. Knepley }
88679a2a23afSMatthew G. Knepley 
88689a2a23afSMatthew G. Knepley /*@
8869bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
88709a2a23afSMatthew G. Knepley 
88719a2a23afSMatthew G. Knepley   Not collective
88729a2a23afSMatthew G. Knepley 
88739a2a23afSMatthew G. Knepley   Input Parameter:
8874bb7acecfSBarry Smith . dm     - The `DM`
88759a2a23afSMatthew G. Knepley 
88769a2a23afSMatthew G. Knepley   Output Parameter:
8877a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
88789a2a23afSMatthew G. Knepley 
88799a2a23afSMatthew G. Knepley   Level: advanced
88809a2a23afSMatthew G. Knepley 
8881bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
88829a2a23afSMatthew G. Knepley @*/
8883d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
8884d71ae5a4SJacob Faibussowitsch {
88859a2a23afSMatthew G. Knepley   PetscFunctionBegin;
88869a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88879566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
88889a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
88899a2a23afSMatthew G. Knepley }
88909a2a23afSMatthew G. Knepley 
88919a2a23afSMatthew G. Knepley /*@
8892ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
88939a2a23afSMatthew G. Knepley 
88949a2a23afSMatthew G. Knepley   Not collective
88959a2a23afSMatthew G. Knepley 
88969a2a23afSMatthew G. Knepley   Input Parameters:
8897bb7acecfSBarry Smith + dm     - The `DM`
8898bb7acecfSBarry Smith . label  - The `DMLabel`
8899ac17215fSMatthew G. Knepley . value  - The label value indicating the region
8900ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
89019a2a23afSMatthew G. Knepley 
89029a2a23afSMatthew G. Knepley   Output Parameter:
8903bb7acecfSBarry Smith . aux    - The `Vec` holding auxiliary field data
89049a2a23afSMatthew G. Knepley 
8905bb7acecfSBarry Smith   Note:
8906bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
890704c51a94SMatthew G. Knepley 
89089a2a23afSMatthew G. Knepley   Level: advanced
89099a2a23afSMatthew G. Knepley 
8910bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
89119a2a23afSMatthew G. Knepley @*/
8912d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
8913d71ae5a4SJacob Faibussowitsch {
8914ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
891504c51a94SMatthew G. Knepley   PetscBool       has;
89169a2a23afSMatthew G. Knepley 
89179a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89189a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89199a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
89209a2a23afSMatthew G. Knepley   key.label = label;
89219a2a23afSMatthew G. Knepley   key.value = value;
8922ac17215fSMatthew G. Knepley   key.part  = part;
89239566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
89249566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
89259566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
89269a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
89279a2a23afSMatthew G. Knepley }
89289a2a23afSMatthew G. Knepley 
89299a2a23afSMatthew G. Knepley /*@
8930bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
89319a2a23afSMatthew G. Knepley 
8932bb7acecfSBarry Smith   Not collective because auxilary vectors are not parallel
89339a2a23afSMatthew G. Knepley 
89349a2a23afSMatthew G. Knepley   Input Parameters:
8935bb7acecfSBarry Smith + dm     - The `DM`
8936bb7acecfSBarry Smith . label  - The `DMLabel`
89379a2a23afSMatthew G. Knepley . value  - The label value indicating the region
8938ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
8939bb7acecfSBarry Smith - aux    - The `Vec` holding auxiliary field data
89409a2a23afSMatthew G. Knepley 
89419a2a23afSMatthew G. Knepley   Level: advanced
89429a2a23afSMatthew G. Knepley 
8943bb7acecfSBarry Smith .seealso: `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
89449a2a23afSMatthew G. Knepley @*/
8945d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
8946d71ae5a4SJacob Faibussowitsch {
89479a2a23afSMatthew G. Knepley   Vec             old;
89489a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
89499a2a23afSMatthew G. Knepley 
89509a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89519a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89529a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
89539a2a23afSMatthew G. Knepley   key.label = label;
89549a2a23afSMatthew G. Knepley   key.value = value;
8955ac17215fSMatthew G. Knepley   key.part  = part;
89569566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
89579566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
89589566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)old));
89599566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
89609566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
89619a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
89629a2a23afSMatthew G. Knepley }
89639a2a23afSMatthew G. Knepley 
89649a2a23afSMatthew G. Knepley /*@C
8965bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
89669a2a23afSMatthew G. Knepley 
89679a2a23afSMatthew G. Knepley   Not collective
89689a2a23afSMatthew G. Knepley 
89699a2a23afSMatthew G. Knepley   Input Parameter:
8970bb7acecfSBarry Smith . dm      - The `DM`
89719a2a23afSMatthew G. Knepley 
89729a2a23afSMatthew G. Knepley   Output Parameters:
8973bb7acecfSBarry Smith + labels  - The `DMLabel`s for each `Vec`
8974bb7acecfSBarry Smith . values  - The label values for each `Vec`
8975bb7acecfSBarry Smith - parts   - The equation parts for each `Vec`
89769a2a23afSMatthew G. Knepley 
8977bb7acecfSBarry Smith   Note:
8978bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
89799a2a23afSMatthew G. Knepley 
89809a2a23afSMatthew G. Knepley   Level: advanced
89819a2a23afSMatthew G. Knepley 
8982bb7acecfSBarry Smith .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMSetAuxiliaryVec()`, DMCopyAuxiliaryVec()`
89839a2a23afSMatthew G. Knepley @*/
8984d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
8985d71ae5a4SJacob Faibussowitsch {
89869a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
89879a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
89889a2a23afSMatthew G. Knepley 
89899a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89909a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89919a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
8992dadcf809SJacob Faibussowitsch   PetscValidIntPointer(values, 3);
8993dadcf809SJacob Faibussowitsch   PetscValidIntPointer(parts, 4);
89949566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
89959566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
89969566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
89979371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
89989371c9d4SSatish Balay     labels[i] = keys[i].label;
89999371c9d4SSatish Balay     values[i] = keys[i].value;
90009371c9d4SSatish Balay     parts[i]  = keys[i].part;
90019371c9d4SSatish Balay   }
90029566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
90039a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
90049a2a23afSMatthew G. Knepley }
90059a2a23afSMatthew G. Knepley 
90069a2a23afSMatthew G. Knepley /*@
9007bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
90089a2a23afSMatthew G. Knepley 
90099a2a23afSMatthew G. Knepley   Not collective
90109a2a23afSMatthew G. Knepley 
90119a2a23afSMatthew G. Knepley   Input Parameter:
9012bb7acecfSBarry Smith . dm    - The `DM`
90139a2a23afSMatthew G. Knepley 
90149a2a23afSMatthew G. Knepley   Output Parameter:
9015bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
90169a2a23afSMatthew G. Knepley 
90179a2a23afSMatthew G. Knepley   Level: advanced
90189a2a23afSMatthew G. Knepley 
9019bb7acecfSBarry Smith   Note:
9020bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9021bb7acecfSBarry Smith 
9022db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
90239a2a23afSMatthew G. Knepley @*/
9024d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9025d71ae5a4SJacob Faibussowitsch {
90269a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90279a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90289566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
90299566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
90309a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
90319a2a23afSMatthew G. Knepley }
9032b5a892a1SMatthew G. Knepley 
9033b5a892a1SMatthew G. Knepley /*@C
9034bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9035b5a892a1SMatthew G. Knepley 
9036b5a892a1SMatthew G. Knepley   Not collective
9037b5a892a1SMatthew G. Knepley 
9038b5a892a1SMatthew G. Knepley   Input Parameters:
9039bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9040b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9041b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9042b5a892a1SMatthew G. Knepley 
9043b5a892a1SMatthew G. Knepley   Output Parameters:
9044bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9045b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9046b5a892a1SMatthew G. Knepley 
9047b5a892a1SMatthew G. Knepley   Level: advanced
9048b5a892a1SMatthew G. Knepley 
9049bb7acecfSBarry Smith   Note:
9050bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9051bb7acecfSBarry Smith 
9052bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9053bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9054bb7acecfSBarry Smith 
9055bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9056bb7acecfSBarry Smith 
9057bb7acecfSBarry Smith .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9058b5a892a1SMatthew G. Knepley @*/
9059d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9060d71ae5a4SJacob Faibussowitsch {
9061b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9062b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9063b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9064b5a892a1SMatthew G. Knepley 
9065b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
90669371c9d4SSatish Balay   if (!nO) {
90679371c9d4SSatish Balay     *ornt  = 0;
90689371c9d4SSatish Balay     *found = PETSC_TRUE;
90699371c9d4SSatish Balay     PetscFunctionReturn(0);
90709371c9d4SSatish Balay   }
9071b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9072b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9073b5a892a1SMatthew G. Knepley 
90749371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
90759371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
90769371c9d4SSatish Balay     if (c == cS) {
90779371c9d4SSatish Balay       *ornt = o;
90789371c9d4SSatish Balay       break;
90799371c9d4SSatish Balay     }
9080b5a892a1SMatthew G. Knepley   }
9081b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9082b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9083b5a892a1SMatthew G. Knepley }
9084b5a892a1SMatthew G. Knepley 
9085b5a892a1SMatthew G. Knepley /*@C
9086bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9087b5a892a1SMatthew G. Knepley 
9088b5a892a1SMatthew G. Knepley   Not collective
9089b5a892a1SMatthew G. Knepley 
9090b5a892a1SMatthew G. Knepley   Input Parameters:
9091bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9092b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9093b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9094b5a892a1SMatthew G. Knepley 
9095b5a892a1SMatthew G. Knepley   Output Parameters:
9096bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9097b5a892a1SMatthew G. Knepley 
9098b5a892a1SMatthew G. Knepley   Level: advanced
9099b5a892a1SMatthew G. Knepley 
9100bb7acecfSBarry Smith   Note:
9101bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9102bb7acecfSBarry Smith 
9103bb7acecfSBarry Smith   Developer Note:
9104bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9105bb7acecfSBarry Smith 
9106bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9107b5a892a1SMatthew G. Knepley @*/
9108d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9109d71ae5a4SJacob Faibussowitsch {
9110b5a892a1SMatthew G. Knepley   PetscBool found;
9111b5a892a1SMatthew G. Knepley 
9112b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91139566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
91147a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9115b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9116b5a892a1SMatthew G. Knepley }
9117b5a892a1SMatthew G. Knepley 
9118b5a892a1SMatthew G. Knepley /*@C
9119bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9120b5a892a1SMatthew G. Knepley 
9121b5a892a1SMatthew G. Knepley   Not collective
9122b5a892a1SMatthew G. Knepley 
9123b5a892a1SMatthew G. Knepley   Input Parameters:
9124bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9125b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9126b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9127b5a892a1SMatthew G. Knepley 
9128b5a892a1SMatthew G. Knepley   Output Parameters:
9129bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9130b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9131b5a892a1SMatthew G. Knepley 
9132b5a892a1SMatthew G. Knepley   Level: advanced
9133b5a892a1SMatthew G. Knepley 
9134bb7acecfSBarry Smith   Note:
9135bb7acecfSBarry Smith   An arrangement is a vertex order
9136bb7acecfSBarry Smith 
9137bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9138bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9139bb7acecfSBarry Smith 
9140bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9141bb7acecfSBarry Smith 
9142bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangment()`
9143b5a892a1SMatthew G. Knepley @*/
9144d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9145d71ae5a4SJacob Faibussowitsch {
9146b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9147b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9148b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9149b5a892a1SMatthew G. Knepley 
9150b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91519371c9d4SSatish Balay   if (!nO) {
91529371c9d4SSatish Balay     *ornt  = 0;
91539371c9d4SSatish Balay     *found = PETSC_TRUE;
91549371c9d4SSatish Balay     PetscFunctionReturn(0);
91559371c9d4SSatish Balay   }
9156b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9157b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9158b5a892a1SMatthew G. Knepley 
91599371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
91609371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
91619371c9d4SSatish Balay     if (c == cS) {
91629371c9d4SSatish Balay       *ornt = o;
91639371c9d4SSatish Balay       break;
91649371c9d4SSatish Balay     }
9165b5a892a1SMatthew G. Knepley   }
9166b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9167b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9168b5a892a1SMatthew G. Knepley }
9169b5a892a1SMatthew G. Knepley 
9170b5a892a1SMatthew G. Knepley /*@C
9171bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9172b5a892a1SMatthew G. Knepley 
9173b5a892a1SMatthew G. Knepley   Not collective
9174b5a892a1SMatthew G. Knepley 
9175b5a892a1SMatthew G. Knepley   Input Parameters:
9176bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9177b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9178b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9179b5a892a1SMatthew G. Knepley 
9180b5a892a1SMatthew G. Knepley   Output Parameters:
9181bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9182b5a892a1SMatthew G. Knepley 
9183b5a892a1SMatthew G. Knepley   Level: advanced
9184b5a892a1SMatthew G. Knepley 
9185bb7acecfSBarry Smith   Note:
9186bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9187bb7acecfSBarry Smith 
9188bb7acecfSBarry Smith   Developer Note:
9189bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9190bb7acecfSBarry Smith 
9191bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9192b5a892a1SMatthew G. Knepley @*/
9193d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9194d71ae5a4SJacob Faibussowitsch {
9195b5a892a1SMatthew G. Knepley   PetscBool found;
9196b5a892a1SMatthew G. Knepley 
9197b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91989566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
91997a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9200b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9201b5a892a1SMatthew G. Knepley }
9202012bc364SMatthew G. Knepley 
9203012bc364SMatthew G. Knepley /*@C
9204012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9205012bc364SMatthew G. Knepley 
9206012bc364SMatthew G. Knepley   Not collective
9207012bc364SMatthew G. Knepley 
9208012bc364SMatthew G. Knepley   Input Parameters:
9209bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9210012bc364SMatthew G. Knepley - point - Coordinates of the point
9211012bc364SMatthew G. Knepley 
9212012bc364SMatthew G. Knepley   Output Parameters:
9213012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
9214012bc364SMatthew G. Knepley 
9215012bc364SMatthew G. Knepley   Level: advanced
9216012bc364SMatthew G. Knepley 
9217bb7acecfSBarry Smith .seealso: `DM`, `DMPolytopeType`, `DMLocatePoints()`
9218012bc364SMatthew G. Knepley @*/
9219d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9220d71ae5a4SJacob Faibussowitsch {
9221012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9222012bc364SMatthew G. Knepley   PetscInt  d;
9223012bc364SMatthew G. Knepley 
9224012bc364SMatthew G. Knepley   PetscFunctionBegin;
9225012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9226012bc364SMatthew G. Knepley   switch (ct) {
9227012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9228012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9229012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
92309371c9d4SSatish Balay       if (point[d] < -1.0) {
92319371c9d4SSatish Balay         *inside = PETSC_FALSE;
92329371c9d4SSatish Balay         break;
92339371c9d4SSatish Balay       }
9234012bc364SMatthew G. Knepley       sum += point[d];
9235012bc364SMatthew G. Knepley     }
92369371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
92379371c9d4SSatish Balay       *inside = PETSC_FALSE;
92389371c9d4SSatish Balay       break;
92399371c9d4SSatish Balay     }
9240012bc364SMatthew G. Knepley     break;
9241012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9242012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9243012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
92449371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
92459371c9d4SSatish Balay         *inside = PETSC_FALSE;
9246012bc364SMatthew G. Knepley         break;
92479371c9d4SSatish Balay       }
92489371c9d4SSatish Balay     break;
9249d71ae5a4SJacob Faibussowitsch   default:
9250d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9251012bc364SMatthew G. Knepley   }
9252012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
9253012bc364SMatthew G. Knepley }
9254