xref: /petsc/src/dm/interface/dm.c (revision 73ff1848c0400416fcc176f9b48bfbf2ec9308e0)
1d0295fc0SJunchao Zhang #include <petscvec.h>
2af0996ceSBarry Smith #include <petsc/private/dmimpl.h>      /*I      "petscdm.h"          I*/
3c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h> /*I      "petscdmlabel.h"     I*/
4e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h> /*I      "petscds.h"     I*/
53e922f36SToby Isaac #include <petscdmplex.h>
6d2b2dc1eSMatthew G. Knepley #include <petscdmceed.h>
7f19dbd58SToby Isaac #include <petscdmfield.h>
80c312b8eSJed Brown #include <petscsf.h>
92764a2aaSMatthew G. Knepley #include <petscds.h>
1047c6ae99SBarry Smith 
11f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
12f918ec44SMatthew G. Knepley   #include <petscfeceed.h>
13f918ec44SMatthew G. Knepley #endif
14f918ec44SMatthew G. Knepley 
15cea3dcb8SSatish Balay #if !defined(PETSC_HAVE_WINDOWS_COMPILERS)
16cea3dcb8SSatish Balay   #include <petsc/private/valgrind/memcheck.h>
1700d952a4SJed Brown #endif
1800d952a4SJed Brown 
19732e2eb9SMatthew G Knepley PetscClassId DM_CLASSID;
20d67d17b1SMatthew G. Knepley PetscClassId DMLABEL_CLASSID;
21708be2fdSJed Brown PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction, DM_CreateInjection, DM_CreateMatrix, DM_CreateMassMatrix, DM_Load, DM_AdaptInterpolator, DM_ProjectFunction;
2267a56275SMatthew G Knepley 
23ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[]          = {"NONE", "GHOSTED", "MIRROR", "PERIODIC", "TWIST", "DMBoundaryType", "DM_BOUNDARY_", NULL};
24d1b3049bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID", "ESSENTIAL", "NATURAL", "INVALID", "INVALID", "ESSENTIAL_FIELD", "NATURAL_FIELD", "INVALID", "INVALID", "ESSENTIAL_BD_FIELD", "NATURAL_RIEMANN", "DMBoundaryConditionType", "DM_BC_", NULL};
250e762ea3SJed Brown const char *const DMBlockingTypes[]          = {"TOPOLOGICAL_POINT", "FIELD_NODE", "DMBlockingType", "DM_BLOCKING_", NULL};
26476787b7SMatthew G. Knepley const char *const DMPolytopeTypes[] =
27476787b7SMatthew G. Knepley   {"vertex",  "segment",      "tensor_segment", "triangle", "quadrilateral",  "tensor_quad",  "tetrahedron", "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism", "pyramid", "FV_ghost_cell", "interior_ghost_cell",
28476787b7SMatthew G. Knepley    "unknown", "unknown_cell", "unknown_face",   "invalid",  "DMPolytopeType", "DM_POLYTOPE_", NULL};
292cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace", "keep", "fail", "DMCopyLabelsMode", "DM_COPY_LABELS_", NULL};
3060c22052SBarry Smith 
31a4121054SBarry Smith /*@
32bb7acecfSBarry Smith   DMCreate - Creates an empty `DM` object. `DM`s are the abstract objects in PETSc that mediate between meshes and discretizations and the
33bb7acecfSBarry Smith   algebraic solvers, time integrators, and optimization algorithms.
34a4121054SBarry Smith 
35d083f849SBarry Smith   Collective
36a4121054SBarry Smith 
37a4121054SBarry Smith   Input Parameter:
38bb7acecfSBarry Smith . comm - The communicator for the `DM` object
39a4121054SBarry Smith 
40a4121054SBarry Smith   Output Parameter:
41bb7acecfSBarry Smith . dm - The `DM` object
42a4121054SBarry Smith 
43a4121054SBarry Smith   Level: beginner
44a4121054SBarry Smith 
45bb7acecfSBarry Smith   Notes:
46bb7acecfSBarry Smith   See `DMType` for a brief summary of available `DM`.
47bb7acecfSBarry Smith 
48bb7acecfSBarry Smith   The type must then be set with `DMSetType()`. If you never call `DMSetType()` it will generate an
49bb7acecfSBarry Smith   error when you try to use the dm.
50bb7acecfSBarry Smith 
511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMType`, `DMDACreate()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK`
52a4121054SBarry Smith @*/
53d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreate(MPI_Comm comm, DM *dm)
54d71ae5a4SJacob Faibussowitsch {
55a4121054SBarry Smith   DM      v;
56e5e52638SMatthew G. Knepley   PetscDS ds;
57a4121054SBarry Smith 
58a4121054SBarry Smith   PetscFunctionBegin;
594f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
600298fd71SBarry Smith   *dm = NULL;
619566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
62a4121054SBarry Smith 
639566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
64e7c4fc90SDmitry Karpeev 
6562e5d2d2SJDBetteridge   ((PetscObject)v)->non_cyclic_references = &DMCountNonCyclicReferences;
6662e5d2d2SJDBetteridge 
6749be4549SMatthew G. Knepley   v->setupcalled          = PETSC_FALSE;
6849be4549SMatthew G. Knepley   v->setfromoptionscalled = PETSC_FALSE;
690298fd71SBarry Smith   v->ltogmap              = NULL;
70a4ea9b21SRichard Tran Mills   v->bind_below           = 0;
711411c6eeSJed Brown   v->bs                   = 1;
72171400e9SBarry Smith   v->coloringtype         = IS_COLORING_GLOBAL;
739566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
749566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
75c58f1c22SToby Isaac   v->labels                    = NULL;
7634aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7734aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
78c58f1c22SToby Isaac   v->depthLabel                = NULL;
79ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
801bb6d2a8SBarry Smith   v->localSection              = NULL;
811bb6d2a8SBarry Smith   v->globalSection             = NULL;
823b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
833b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
8479769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
856858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
866858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
876858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8896173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
89435a35e8SMatthew G Knepley   {
90435a35e8SMatthew G Knepley     PetscInt i;
91435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
920298fd71SBarry Smith       v->nullspaceConstructors[i]     = NULL;
93f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
94435a35e8SMatthew G Knepley     }
95435a35e8SMatthew G Knepley   }
969566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
9707218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(v, NULL, NULL, ds, NULL));
989566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
999566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
10014f150ffSMatthew G. Knepley   v->dmBC              = NULL;
101a8fb8f29SToby Isaac   v->coarseMesh        = NULL;
102f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
103cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
1049566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v, VECSTANDARD));
1059566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v, MATAIJ));
1064a7a4c06SLawrence Mitchell 
1071411c6eeSJed Brown   *dm = v;
1083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
109a4121054SBarry Smith }
110a4121054SBarry Smith 
11138221697SMatthew G. Knepley /*@
112bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
11338221697SMatthew G. Knepley 
114d083f849SBarry Smith   Collective
11538221697SMatthew G. Knepley 
11638221697SMatthew G. Knepley   Input Parameter:
117bb7acecfSBarry Smith . dm - The original `DM` object
11838221697SMatthew G. Knepley 
11938221697SMatthew G. Knepley   Output Parameter:
120bb7acecfSBarry Smith . newdm - The new `DM` object
12138221697SMatthew G. Knepley 
12238221697SMatthew G. Knepley   Level: beginner
12338221697SMatthew G. Knepley 
1241cb8cacdSPatrick Sanan   Notes:
125bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
126bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
127bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1281bb6d2a8SBarry Smith 
129bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
13089706ed2SPatrick Sanan 
131bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
132bb7acecfSBarry Smith 
13360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMConvert()`
13438221697SMatthew G. Knepley @*/
135d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm)
136d71ae5a4SJacob Faibussowitsch {
13738221697SMatthew G. Knepley   PetscSF              sf;
13838221697SMatthew G. Knepley   Vec                  coords;
13938221697SMatthew G. Knepley   void                *ctx;
140ec196627SMatthew G. Knepley   MatOrderingType      otype;
141ec196627SMatthew G. Knepley   DMReorderDefaultFlag flg;
1426858538eSMatthew G. Knepley   PetscInt             dim, cdim, i;
14338221697SMatthew G. Knepley 
14438221697SMatthew G. Knepley   PetscFunctionBegin;
14538221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1464f572ea9SToby Isaac   PetscAssertPointer(newdm, 2);
1479566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1489566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
149ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
150ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
151c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
152fc214432SJed Brown   (*newdm)->prealloc_skip = dm->prealloc_skip;
1539566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1549566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1559566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1569566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1579566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1589566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
159dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1603f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1619566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1629566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1639566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1649566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
165ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionGetDefault(dm, &flg));
166ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionSetDefault(*newdm, flg));
167ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionGetType(dm, &otype));
168ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionSetType(*newdm, otype));
1696858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1706858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
171be4c1c3eSMatthew G. Knepley       DM           ncdm;
172be4c1c3eSMatthew G. Knepley       PetscSection cs;
1735a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
174be4c1c3eSMatthew G. Knepley 
1756858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1769566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
177712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1785a0206caSToby Isaac       if (pEndMax >= 0) {
1796858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1806858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1819566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1826858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1836858538eSMatthew G. Knepley         else PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1849566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
185be4c1c3eSMatthew G. Knepley       }
186be4c1c3eSMatthew G. Knepley     }
1876858538eSMatthew G. Knepley   }
1889566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1899566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1909566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
19138221697SMatthew G. Knepley   if (coords) {
1929566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
19338221697SMatthew G. Knepley   } else {
1949566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1959566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
19638221697SMatthew G. Knepley   }
1976858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
1986858538eSMatthew G. Knepley   if (coords) {
1996858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
2006858538eSMatthew G. Knepley   } else {
2016858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
2026858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
2036858538eSMatthew G. Knepley   }
20490b157c4SStefano Zampini   {
2054fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
2066858538eSMatthew G. Knepley 
2074fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
2084fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L));
209c6b900c6SMatthew G. Knepley   }
21034aa8a36SMatthew G. Knepley   {
21134aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
21234aa8a36SMatthew G. Knepley 
2139566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2149566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
21534aa8a36SMatthew G. Knepley   }
2163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21738221697SMatthew G. Knepley }
21838221697SMatthew G. Knepley 
2199a42bb27SBarry Smith /*@C
220bb7acecfSBarry Smith   DMSetVecType - Sets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2219a42bb27SBarry Smith 
22220f4b53cSBarry Smith   Logically Collective
2239a42bb27SBarry Smith 
224147403d9SBarry Smith   Input Parameters:
22532546409SMatthew G. Knepley + dm    - initial distributed array
226bb7acecfSBarry Smith - ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2279a42bb27SBarry Smith 
22820f4b53cSBarry Smith   Options Database Key:
229147403d9SBarry Smith . -dm_vec_type ctype - the type of vector to create
2309a42bb27SBarry Smith 
2319a42bb27SBarry Smith   Level: intermediate
2329a42bb27SBarry Smith 
23360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
234bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2359a42bb27SBarry Smith @*/
23632546409SMatthew G. Knepley PetscErrorCode DMSetVecType(DM dm, VecType ctype)
237d71ae5a4SJacob Faibussowitsch {
23832546409SMatthew G. Knepley   char *tmp;
23932546409SMatthew G. Knepley 
2409a42bb27SBarry Smith   PetscFunctionBegin;
24132546409SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24232546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
24332546409SMatthew G. Knepley   tmp = (char *)dm->vectype;
24432546409SMatthew G. Knepley   PetscCall(PetscStrallocpy(ctype, (char **)&dm->vectype));
24532546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
2463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2479a42bb27SBarry Smith }
2489a42bb27SBarry Smith 
249c0dedaeaSBarry Smith /*@C
250bb7acecfSBarry Smith   DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
251c0dedaeaSBarry Smith 
25220f4b53cSBarry Smith   Logically Collective
253c0dedaeaSBarry Smith 
254c0dedaeaSBarry Smith   Input Parameter:
255c0dedaeaSBarry Smith . da - initial distributed array
256c0dedaeaSBarry Smith 
257c0dedaeaSBarry Smith   Output Parameter:
258c0dedaeaSBarry Smith . ctype - the vector type
259c0dedaeaSBarry Smith 
260c0dedaeaSBarry Smith   Level: intermediate
261c0dedaeaSBarry Smith 
26260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
263c0dedaeaSBarry Smith @*/
264d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
265d71ae5a4SJacob Faibussowitsch {
266c0dedaeaSBarry Smith   PetscFunctionBegin;
267c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
268c0dedaeaSBarry Smith   *ctype = da->vectype;
2693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
270c0dedaeaSBarry Smith }
271c0dedaeaSBarry Smith 
2725f1ad066SMatthew G Knepley /*@
273bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2745f1ad066SMatthew G Knepley 
27520f4b53cSBarry Smith   Not Collective
2765f1ad066SMatthew G Knepley 
2775f1ad066SMatthew G Knepley   Input Parameter:
278bb7acecfSBarry Smith . v - The `Vec`
2795f1ad066SMatthew G Knepley 
2805f1ad066SMatthew G Knepley   Output Parameter:
281bb7acecfSBarry Smith . dm - The `DM`
2825f1ad066SMatthew G Knepley 
2835f1ad066SMatthew G Knepley   Level: intermediate
2845f1ad066SMatthew G Knepley 
285bb7acecfSBarry Smith   Note:
286bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
287bb7acecfSBarry Smith 
2881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2895f1ad066SMatthew G Knepley @*/
290d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
291d71ae5a4SJacob Faibussowitsch {
2925f1ad066SMatthew G Knepley   PetscFunctionBegin;
2935f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2944f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
2959566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
2963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2975f1ad066SMatthew G Knepley }
2985f1ad066SMatthew G Knepley 
2995f1ad066SMatthew G Knepley /*@
300bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
3015f1ad066SMatthew G Knepley 
30220f4b53cSBarry Smith   Not Collective
3035f1ad066SMatthew G Knepley 
3045f1ad066SMatthew G Knepley   Input Parameters:
305bb7acecfSBarry Smith + v  - The `Vec`
306bb7acecfSBarry Smith - dm - The `DM`
3075f1ad066SMatthew G Knepley 
30820f4b53cSBarry Smith   Level: developer
30920f4b53cSBarry Smith 
310*73ff1848SBarry Smith   Notes:
311bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
312d9805387SMatthew G. Knepley 
313bb7acecfSBarry 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.
314bb7acecfSBarry Smith 
3151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3165f1ad066SMatthew G Knepley @*/
317d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
318d71ae5a4SJacob Faibussowitsch {
3195f1ad066SMatthew G Knepley   PetscFunctionBegin;
3205f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
321d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3229566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3245f1ad066SMatthew G Knepley }
3255f1ad066SMatthew G Knepley 
326521d9a4cSLisandro Dalcin /*@C
327bb7acecfSBarry Smith   DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3288f1509bcSBarry Smith 
32920f4b53cSBarry Smith   Logically Collective
3308f1509bcSBarry Smith 
3318f1509bcSBarry Smith   Input Parameters:
332bb7acecfSBarry Smith + dm    - the `DM` context
3338f1509bcSBarry Smith - ctype - the matrix type
3348f1509bcSBarry Smith 
33520f4b53cSBarry Smith   Options Database Key:
3368f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3378f1509bcSBarry Smith 
3388f1509bcSBarry Smith   Level: intermediate
3398f1509bcSBarry Smith 
3401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
341bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3428f1509bcSBarry Smith @*/
343d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
344d71ae5a4SJacob Faibussowitsch {
3458f1509bcSBarry Smith   PetscFunctionBegin;
3468f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3478f1509bcSBarry Smith   dm->coloringtype = ctype;
3483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3498f1509bcSBarry Smith }
3508f1509bcSBarry Smith 
3518f1509bcSBarry Smith /*@C
352bb7acecfSBarry Smith   DMGetISColoringType - Gets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
353521d9a4cSLisandro Dalcin 
35420f4b53cSBarry Smith   Logically Collective
355521d9a4cSLisandro Dalcin 
356521d9a4cSLisandro Dalcin   Input Parameter:
357bb7acecfSBarry Smith . dm - the `DM` context
3588f1509bcSBarry Smith 
3598f1509bcSBarry Smith   Output Parameter:
3608f1509bcSBarry Smith . ctype - the matrix type
3618f1509bcSBarry Smith 
36220f4b53cSBarry Smith   Options Database Key:
3638f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3648f1509bcSBarry Smith 
3658f1509bcSBarry Smith   Level: intermediate
3668f1509bcSBarry Smith 
3671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
36842747ad1SJacob Faibussowitsch           `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3698f1509bcSBarry Smith @*/
370d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
371d71ae5a4SJacob Faibussowitsch {
3728f1509bcSBarry Smith   PetscFunctionBegin;
3738f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3748f1509bcSBarry Smith   *ctype = dm->coloringtype;
3753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3768f1509bcSBarry Smith }
3778f1509bcSBarry Smith 
3788f1509bcSBarry Smith /*@C
379bb7acecfSBarry Smith   DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3808f1509bcSBarry Smith 
38120f4b53cSBarry Smith   Logically Collective
3828f1509bcSBarry Smith 
3838f1509bcSBarry Smith   Input Parameters:
384bb7acecfSBarry Smith + dm    - the `DM` context
385bb7acecfSBarry Smith - ctype - the matrix type, for example `MATMPIAIJ`
386521d9a4cSLisandro Dalcin 
38720f4b53cSBarry Smith   Options Database Key:
388bb7acecfSBarry Smith . -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
389521d9a4cSLisandro Dalcin 
390521d9a4cSLisandro Dalcin   Level: intermediate
391521d9a4cSLisandro Dalcin 
39242747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
393521d9a4cSLisandro Dalcin @*/
394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
395d71ae5a4SJacob Faibussowitsch {
39632546409SMatthew G. Knepley   char *tmp;
39732546409SMatthew G. Knepley 
398521d9a4cSLisandro Dalcin   PetscFunctionBegin;
399521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40032546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
40132546409SMatthew G. Knepley   tmp = (char *)dm->mattype;
4029566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
40332546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
4043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
405521d9a4cSLisandro Dalcin }
406521d9a4cSLisandro Dalcin 
407c0dedaeaSBarry Smith /*@C
40820f4b53cSBarry Smith   DMGetMatType - Gets the type of matrix that would be created with `DMCreateMatrix()`
409c0dedaeaSBarry Smith 
41020f4b53cSBarry Smith   Logically Collective
411c0dedaeaSBarry Smith 
412c0dedaeaSBarry Smith   Input Parameter:
413bb7acecfSBarry Smith . dm - the `DM` context
414c0dedaeaSBarry Smith 
415c0dedaeaSBarry Smith   Output Parameter:
416c0dedaeaSBarry Smith . ctype - the matrix type
417c0dedaeaSBarry Smith 
418c0dedaeaSBarry Smith   Level: intermediate
419c0dedaeaSBarry Smith 
42042747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`
421c0dedaeaSBarry Smith @*/
422d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
423d71ae5a4SJacob Faibussowitsch {
424c0dedaeaSBarry Smith   PetscFunctionBegin;
425c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
426c0dedaeaSBarry Smith   *ctype = dm->mattype;
4273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
428c0dedaeaSBarry Smith }
429c0dedaeaSBarry Smith 
430c688c046SMatthew G Knepley /*@
431bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
432c688c046SMatthew G Knepley 
43320f4b53cSBarry Smith   Not Collective
434c688c046SMatthew G Knepley 
435c688c046SMatthew G Knepley   Input Parameter:
436bb7acecfSBarry Smith . A - The `Mat`
437c688c046SMatthew G Knepley 
438c688c046SMatthew G Knepley   Output Parameter:
439bb7acecfSBarry Smith . dm - The `DM`
440c688c046SMatthew G Knepley 
441c688c046SMatthew G Knepley   Level: intermediate
442c688c046SMatthew G Knepley 
443bb7acecfSBarry Smith   Note:
444bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
445bb7acecfSBarry Smith 
446*73ff1848SBarry Smith   Developer Note:
447bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4488f1509bcSBarry Smith 
4491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
450c688c046SMatthew G Knepley @*/
451d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
452d71ae5a4SJacob Faibussowitsch {
453c688c046SMatthew G Knepley   PetscFunctionBegin;
454c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4554f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
4569566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
4573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
458c688c046SMatthew G Knepley }
459c688c046SMatthew G Knepley 
460c688c046SMatthew G Knepley /*@
461bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
462c688c046SMatthew G Knepley 
46320f4b53cSBarry Smith   Not Collective
464c688c046SMatthew G Knepley 
465c688c046SMatthew G Knepley   Input Parameters:
46620f4b53cSBarry Smith + A  - The `Mat`
46720f4b53cSBarry Smith - dm - The `DM`
468c688c046SMatthew G Knepley 
469bb7acecfSBarry Smith   Level: developer
470c688c046SMatthew G Knepley 
471bb7acecfSBarry Smith   Note:
472bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
473bb7acecfSBarry Smith 
474*73ff1848SBarry Smith   Developer Note:
475bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
476bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4778f1509bcSBarry Smith 
4781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
479c688c046SMatthew G Knepley @*/
480d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
481d71ae5a4SJacob Faibussowitsch {
482c688c046SMatthew G Knepley   PetscFunctionBegin;
483c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4848865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4859566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
4863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
487c688c046SMatthew G Knepley }
488c688c046SMatthew G Knepley 
4899a42bb27SBarry Smith /*@C
490bb7acecfSBarry Smith   DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4919a42bb27SBarry Smith 
49220f4b53cSBarry Smith   Logically Collective
4939a42bb27SBarry Smith 
494d8d19677SJose E. Roman   Input Parameters:
49560225df5SJacob Faibussowitsch + dm     - the `DM` context
496bb7acecfSBarry Smith - prefix - the prefix to prepend
4979a42bb27SBarry Smith 
49820f4b53cSBarry Smith   Level: advanced
49920f4b53cSBarry Smith 
50020f4b53cSBarry Smith   Note:
5019a42bb27SBarry Smith   A hyphen (-) must NOT be given at the beginning of the prefix name.
5029a42bb27SBarry Smith   The first character of all runtime options is AUTOMATICALLY the hyphen.
5039a42bb27SBarry Smith 
5041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
5059a42bb27SBarry Smith @*/
506d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
507d71ae5a4SJacob Faibussowitsch {
5089a42bb27SBarry Smith   PetscFunctionBegin;
5099a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5109566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
5111baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
5121baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
5133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5149a42bb27SBarry Smith }
5159a42bb27SBarry Smith 
51631697293SDave May /*@C
517da81f932SPierre Jolivet   DMAppendOptionsPrefix - Appends an additional string to an already existing prefix used for searching for
518bb7acecfSBarry Smith   `DM` options in the options database.
51931697293SDave May 
52020f4b53cSBarry Smith   Logically Collective
52131697293SDave May 
52231697293SDave May   Input Parameters:
523bb7acecfSBarry Smith + dm     - the `DM` context
524bb7acecfSBarry Smith - prefix - the string to append to the current prefix
52531697293SDave May 
52620f4b53cSBarry Smith   Level: advanced
52720f4b53cSBarry Smith 
52820f4b53cSBarry Smith   Note:
529bb7acecfSBarry 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.
53031697293SDave May   A hyphen (-) must NOT be given at the beginning of the prefix name.
53131697293SDave May   The first character of all runtime options is AUTOMATICALLY the hyphen.
53231697293SDave May 
5331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
53431697293SDave May @*/
535d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
536d71ae5a4SJacob Faibussowitsch {
53731697293SDave May   PetscFunctionBegin;
53831697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5399566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
5403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
54131697293SDave May }
54231697293SDave May 
54331697293SDave May /*@C
54431697293SDave May   DMGetOptionsPrefix - Gets the prefix used for searching for all
545bb7acecfSBarry Smith   DM options in the options database.
54631697293SDave May 
54731697293SDave May   Not Collective
54831697293SDave May 
5492fe279fdSBarry Smith   Input Parameter:
550bb7acecfSBarry Smith . dm - the `DM` context
55131697293SDave May 
5522fe279fdSBarry Smith   Output Parameter:
55331697293SDave May . prefix - pointer to the prefix string used is returned
55431697293SDave May 
55531697293SDave May   Level: advanced
55631697293SDave May 
557*73ff1848SBarry Smith   Fortran Note:
55820f4b53cSBarry Smith   Pass in a string 'prefix' of
55920f4b53cSBarry Smith   sufficient length to hold the prefix.
56020f4b53cSBarry Smith 
5611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
56231697293SDave May @*/
563d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
564d71ae5a4SJacob Faibussowitsch {
56531697293SDave May   PetscFunctionBegin;
56631697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5679566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
5683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
56931697293SDave May }
57031697293SDave May 
57162e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
572d71ae5a4SJacob Faibussowitsch {
5736eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
57488bdff64SToby Isaac 
57588bdff64SToby Isaac   PetscFunctionBegin;
576aab5bcd8SJed Brown   *ncrefct = 0;
57788bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
57888bdff64SToby Isaac     refct--;
57988bdff64SToby Isaac     if (recurseCoarse) {
58088bdff64SToby Isaac       PetscInt coarseCount;
58188bdff64SToby Isaac 
58262e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
58388bdff64SToby Isaac       refct += coarseCount;
58488bdff64SToby Isaac     }
58588bdff64SToby Isaac   }
58688bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
58788bdff64SToby Isaac     refct--;
58888bdff64SToby Isaac     if (recurseFine) {
58988bdff64SToby Isaac       PetscInt fineCount;
59088bdff64SToby Isaac 
59162e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
59288bdff64SToby Isaac       refct += fineCount;
59388bdff64SToby Isaac     }
59488bdff64SToby Isaac   }
59588bdff64SToby Isaac   *ncrefct = refct;
5963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59788bdff64SToby Isaac }
59888bdff64SToby Isaac 
59962e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */
60062e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct)
60162e5d2d2SJDBetteridge {
60262e5d2d2SJDBetteridge   PetscFunctionBegin;
60362e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct));
6043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
60562e5d2d2SJDBetteridge }
60662e5d2d2SJDBetteridge 
607d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
608d71ae5a4SJacob Faibussowitsch {
6095d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
610354557abSToby Isaac 
611354557abSToby Isaac   PetscFunctionBegin;
612354557abSToby Isaac   /* destroy the labels */
613354557abSToby Isaac   while (next) {
614354557abSToby Isaac     DMLabelLink tmp = next->next;
615354557abSToby Isaac 
6165d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
617ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
6189566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
6199566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
620354557abSToby Isaac     next = tmp;
621354557abSToby Isaac   }
6225d80c0bfSVaclav Hapla   dm->labels = NULL;
6233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
624354557abSToby Isaac }
625354557abSToby Isaac 
62666976f2fSJacob Faibussowitsch static PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
627d71ae5a4SJacob Faibussowitsch {
6286858538eSMatthew G. Knepley   PetscFunctionBegin;
6296858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6306858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6316858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6326858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6336858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6356858538eSMatthew G. Knepley }
6366858538eSMatthew G. Knepley 
6371fb7b255SJunchao Zhang /*@C
638bb7acecfSBarry Smith   DMDestroy - Destroys a `DM`.
63947c6ae99SBarry Smith 
64020f4b53cSBarry Smith   Collective
64147c6ae99SBarry Smith 
64247c6ae99SBarry Smith   Input Parameter:
643bb7acecfSBarry Smith . dm - the `DM` object to destroy
64447c6ae99SBarry Smith 
64547c6ae99SBarry Smith   Level: developer
64647c6ae99SBarry Smith 
6471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
64847c6ae99SBarry Smith @*/
649d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
650d71ae5a4SJacob Faibussowitsch {
6516eb26441SStefano Zampini   PetscInt cnt;
65247c6ae99SBarry Smith 
65347c6ae99SBarry Smith   PetscFunctionBegin;
6543ba16761SJacob Faibussowitsch   if (!*dm) PetscFunctionReturn(PETSC_SUCCESS);
655f4f49eeaSPierre Jolivet   PetscValidHeaderSpecific(*dm, DM_CLASSID, 1);
65687e657c6SBarry Smith 
65788bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
65862e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
659f4f49eeaSPierre Jolivet   --((PetscObject)*dm)->refct;
6609371c9d4SSatish Balay   if (--cnt > 0) {
6619371c9d4SSatish Balay     *dm = NULL;
6623ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6639371c9d4SSatish Balay   }
664f4f49eeaSPierre Jolivet   if (((PetscObject)*dm)->refct < 0) PetscFunctionReturn(PETSC_SUCCESS);
665f4f49eeaSPierre Jolivet   ((PetscObject)*dm)->refct = 0;
6666eb26441SStefano Zampini 
6679566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6689566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
669974ca4ecSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(*dm));
670974ca4ecSStefano Zampini   PetscCall(DMClearNamedLocalVectors(*dm));
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;
716936381afSPierre Jolivet     PetscCheck(!(*dm)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out %p %p", (void *)(*dm)->workout, (void *)(*dm)->workout->mem);
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));
751adc21957SMatthew G. Knepley   PetscCall(PetscFree((*dm)->reorderSectionType));
7529566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7539566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7549566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7559566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7569566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
75748a46eb9SPierre Jolivet   if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7589566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
759e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(*dm));
7609566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
76148a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7626eb26441SStefano Zampini 
7639566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
76448a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7659566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7664fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7679566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7689566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7696858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7706858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7719566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7729566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7739566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7746725e60dSJed Brown   PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local));
7756725e60dSJed Brown   PetscCall(VecDestroy(&(*dm)->periodic.affine));
7766636e97aSMatthew G Knepley 
7779566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7789566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
779e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7809566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
781732e2eb9SMatthew G Knepley 
782213acdd3SPierre Jolivet   PetscTryTypeMethod(*dm, destroy);
7839566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
784d2b2dc1eSMatthew G. Knepley   PetscCall(DMCeedDestroy(&(*dm)->dmceed));
785f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7869566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7879566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
788f918ec44SMatthew G. Knepley #endif
789435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7909566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
7913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79247c6ae99SBarry Smith }
79347c6ae99SBarry Smith 
794d7bf68aeSBarry Smith /*@
795bb7acecfSBarry Smith   DMSetUp - sets up the data structures inside a `DM` object
796d7bf68aeSBarry Smith 
79720f4b53cSBarry Smith   Collective
798d7bf68aeSBarry Smith 
799d7bf68aeSBarry Smith   Input Parameter:
800bb7acecfSBarry Smith . dm - the `DM` object to setup
801d7bf68aeSBarry Smith 
802bb7acecfSBarry Smith   Level: intermediate
803d7bf68aeSBarry Smith 
804bb7acecfSBarry Smith   Note:
805bb7acecfSBarry Smith   This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
806bb7acecfSBarry Smith 
8071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
808d7bf68aeSBarry Smith @*/
809d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
810d71ae5a4SJacob Faibussowitsch {
811d7bf68aeSBarry Smith   PetscFunctionBegin;
812171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8133ba16761SJacob Faibussowitsch   if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
814dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8158387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
8163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
817d7bf68aeSBarry Smith }
818d7bf68aeSBarry Smith 
819d7bf68aeSBarry Smith /*@
820bb7acecfSBarry Smith   DMSetFromOptions - sets parameters in a `DM` from the options database
821d7bf68aeSBarry Smith 
82220f4b53cSBarry Smith   Collective
823d7bf68aeSBarry Smith 
824d7bf68aeSBarry Smith   Input Parameter:
825bb7acecfSBarry Smith . dm - the `DM` object to set options for
826d7bf68aeSBarry Smith 
82720f4b53cSBarry Smith   Options Database Keys:
828bb7acecfSBarry Smith + -dm_preallocate_only                               - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
829bb7acecfSBarry Smith . -dm_vec_type <type>                                - type of vector to create inside `DM`
830bb7acecfSBarry Smith . -dm_mat_type <type>                                - type of matrix to create inside `DM`
831a4ea9b21SRichard Tran Mills . -dm_is_coloring_type                               - <global or local>
83220f4b53cSBarry 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`
83320f4b53cSBarry Smith . -dm_plex_filename <str>                            - File containing a mesh
8349318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>                   - File containing a mesh boundary
835cd7e8a5eSksagiyam . -dm_plex_name <str>                                - Name of the mesh in the file
8365dca41c3SJed Brown . -dm_plex_shape <shape>                             - The domain shape, such as `BOX`, `SPHERE`, etc.
8379318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                                 - Cell shape
8389318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool>              - Use a reference cell domain
8399318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                                 - Set the topological dimension
840bb7acecfSBarry Smith . -dm_plex_simplex <bool>                            - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
841bb7acecfSBarry Smith . -dm_plex_interpolate <bool>                        - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8429318fe57SMatthew G. Knepley . -dm_plex_scale <sc>                                - Scale factor for mesh coordinates
843be664eb1SMatthew G. Knepley . -dm_coord_remap <bool>                             - Map coordinates using a function
844be664eb1SMatthew G. Knepley . -dm_coord_map <mapname>                            - Select a builtin coordinate map
845be664eb1SMatthew G. Knepley . -dm_coord_map_params <p0,p1,p2,...>                - Set coordinate mapping parameters
8469318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>                         - Number of faces along each dimension
8479318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>                         - Specify lower-left-bottom coordinates for the box
8489318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>                         - Specify upper-right-top coordinates for the box
849bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>                         - Specify the `DMBoundaryType` for each direction
8509318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>                         - The sphere radius
8519318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>                           - Radius of the ball
8529318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>                          - Boundary type in the z direction
8539318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>                   - Number of wedges around the cylinder
854bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>                           - Reorder the mesh using the specified algorithm
8559318fe57SMatthew G. Knepley . -dm_refine_pre <n>                                 - The number of refinements before distribution
8569318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>                      - Flag for uniform refinement before distribution
8579318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>                    - The maximum cell volume after refinement before distribution
8589318fe57SMatthew G. Knepley . -dm_refine <n>                                     - The number of refinements after distribution
859bdf63967SMatthew G. Knepley . -dm_extrude <l>                                    - Activate extrusion and specify the number of layers to extrude
860d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
861d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
862d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
863d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
864d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
865909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells                     - Flag to create finite volume ghost cells on the boundary
866909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name>               - Label name for ghost cells boundary
8679318fe57SMatthew G. Knepley . -dm_distribute <bool>                              - Flag to redistribute a mesh among processes
8689318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>                         - The size of the overlap halo
8699318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>                           - Set adjacency direction
87020f4b53cSBarry Smith . -dm_plex_adj_closure <bool>                        - Set adjacency size
871d2b2dc1eSMatthew G. Knepley . -dm_plex_use_ceed <bool>                           - Use LibCEED as the FEM backend
87220f4b53cSBarry Smith . -dm_plex_check_symmetry                            - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
873bb7acecfSBarry Smith . -dm_plex_check_skeleton                            - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
874bb7acecfSBarry Smith . -dm_plex_check_faces                               - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type - `DMPlexCheckFaces()`
875bb7acecfSBarry Smith . -dm_plex_check_geometry                            - Check that cells have positive volume - `DMPlexCheckGeometry()`
876bb7acecfSBarry Smith . -dm_plex_check_pointsf                             - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
877bb7acecfSBarry Smith . -dm_plex_check_interface_cones                     - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
878384a6580SVaclav Hapla - -dm_plex_check_all                                 - Perform all the checks above
879d7bf68aeSBarry Smith 
88095eb5ee5SVaclav Hapla   Level: intermediate
88195eb5ee5SVaclav Hapla 
8821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
883bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
88460225df5SJacob Faibussowitsch          `DMSetOptionsPrefix()`, `DMType`, `DMPLEX`, `DMDA`
885d7bf68aeSBarry Smith @*/
886d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
887d71ae5a4SJacob Faibussowitsch {
8887781c08eSBarry Smith   char      typeName[256];
889ca266f36SBarry Smith   PetscBool flg;
890d7bf68aeSBarry Smith 
891d7bf68aeSBarry Smith   PetscFunctionBegin;
892171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89349be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
8949566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
8959566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
896dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
897d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
8989566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
8999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9001baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9021baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
903863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9049566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-dm_bind_below", "Set the size threshold (in entries) below which the Vec is bound to the CPU", "VecBindToCPU", dm->bind_below, &dm->bind_below, &flg));
906eb9d3e4dSMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_ignore_perm_output", "Ignore the local section permutation on output", "DMGetOutputDM", dm->ignorePermOutput, &dm->ignorePermOutput, NULL));
907dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
908f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
909dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
910d0609cedSBarry Smith   PetscOptionsEnd();
9113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
912d7bf68aeSBarry Smith }
913d7bf68aeSBarry Smith 
914fc9bc008SSatish Balay /*@C
915bb7acecfSBarry Smith   DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
916fe2efc57SMark 
91720f4b53cSBarry Smith   Collective
918fe2efc57SMark 
919fe2efc57SMark   Input Parameters:
920bb7acecfSBarry Smith + dm   - the `DM` object
92120f4b53cSBarry Smith . obj  - optional object that provides the prefix for the options database (if `NULL` then the prefix in obj is used)
92260225df5SJacob Faibussowitsch - name - option string that is used to activate viewing
923fe2efc57SMark 
924fe2efc57SMark   Level: intermediate
925bb7acecfSBarry Smith 
926bb7acecfSBarry Smith   Note:
927bb7acecfSBarry Smith   See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
928bb7acecfSBarry Smith 
92960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`
930fe2efc57SMark @*/
931d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
932d71ae5a4SJacob Faibussowitsch {
933fe2efc57SMark   PetscFunctionBegin;
934fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9359566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
937fe2efc57SMark }
938fe2efc57SMark 
939fe2efc57SMark /*@C
940bb7acecfSBarry Smith   DMView - Views a `DM`. Depending on the `PetscViewer` and its `PetscViewerFormat` it may print some ASCII information about the `DM` to the screen or a file or
941bb7acecfSBarry Smith   save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
94247c6ae99SBarry Smith 
94320f4b53cSBarry Smith   Collective
94447c6ae99SBarry Smith 
945d8d19677SJose E. Roman   Input Parameters:
946bb7acecfSBarry Smith + dm - the `DM` object to view
94747c6ae99SBarry Smith - v  - the viewer
94847c6ae99SBarry Smith 
94920f4b53cSBarry Smith   Level: beginner
95020f4b53cSBarry Smith 
951*73ff1848SBarry Smith   Note:
952bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
953bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
954bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
955cd7e8a5eSksagiyam 
9561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
95747c6ae99SBarry Smith @*/
958d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
959d71ae5a4SJacob Faibussowitsch {
96032c0f0efSBarry Smith   PetscBool         isbinary;
96176a8abe0SBarry Smith   PetscMPIInt       size;
96276a8abe0SBarry Smith   PetscViewerFormat format;
96347c6ae99SBarry Smith 
96447c6ae99SBarry Smith   PetscFunctionBegin;
965171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
96648a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
967b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
96874903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
96974903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
97074903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
97174903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
97274903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
97374903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
97474903a4fSStefano Zampini      in an error here */
97574903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9769566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
977b1b135c8SBarry Smith 
9789566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9799566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
9803ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
9819566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9829566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
98332c0f0efSBarry Smith   if (isbinary) {
98455849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
98532c0f0efSBarry Smith     char     type[256];
98632c0f0efSBarry Smith 
9879566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
988c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
9899566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
99032c0f0efSBarry Smith   }
991dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
9923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
99347c6ae99SBarry Smith }
99447c6ae99SBarry Smith 
99547c6ae99SBarry Smith /*@
996bb7acecfSBarry 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,
997bb7acecfSBarry Smith   that is it has no ghost locations.
99847c6ae99SBarry Smith 
99920f4b53cSBarry Smith   Collective
100047c6ae99SBarry Smith 
100147c6ae99SBarry Smith   Input Parameter:
1002bb7acecfSBarry Smith . dm - the `DM` object
100347c6ae99SBarry Smith 
100447c6ae99SBarry Smith   Output Parameter:
100547c6ae99SBarry Smith . vec - the global vector
100647c6ae99SBarry Smith 
1007073dac72SJed Brown   Level: beginner
100847c6ae99SBarry Smith 
10091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1010bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
101147c6ae99SBarry Smith @*/
1012d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1013d71ae5a4SJacob Faibussowitsch {
101447c6ae99SBarry Smith   PetscFunctionBegin;
1015171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10164f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1017dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
101876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1019c6b011d8SStefano Zampini     DM vdm;
1020c6b011d8SStefano Zampini 
10219566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10227a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1023c6b011d8SStefano Zampini   }
10243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
102547c6ae99SBarry Smith }
102647c6ae99SBarry Smith 
102747c6ae99SBarry Smith /*@
1028bb7acecfSBarry Smith   DMCreateLocalVector - Creates a local vector from a `DM` object.
102947c6ae99SBarry Smith 
103047c6ae99SBarry Smith   Not Collective
103147c6ae99SBarry Smith 
103247c6ae99SBarry Smith   Input Parameter:
1033bb7acecfSBarry Smith . dm - the `DM` object
103447c6ae99SBarry Smith 
103547c6ae99SBarry Smith   Output Parameter:
103647c6ae99SBarry Smith . vec - the local vector
103747c6ae99SBarry Smith 
1038073dac72SJed Brown   Level: beginner
103947c6ae99SBarry Smith 
104020f4b53cSBarry Smith   Note:
1041bb7acecfSBarry Smith   A local vector usually has ghost locations that contain values that are owned by different MPI ranks. A global vector has no ghost locations.
1042bb7acecfSBarry Smith 
10431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1044bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
104547c6ae99SBarry Smith @*/
1046d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1047d71ae5a4SJacob Faibussowitsch {
104847c6ae99SBarry Smith   PetscFunctionBegin;
1049171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10504f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1051dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
105276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1053c6b011d8SStefano Zampini     DM vdm;
1054c6b011d8SStefano Zampini 
10559566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10567a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1057c6b011d8SStefano Zampini   }
10583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
105947c6ae99SBarry Smith }
106047c6ae99SBarry Smith 
10611411c6eeSJed Brown /*@
1062bb7acecfSBarry Smith   DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10631411c6eeSJed Brown 
106420f4b53cSBarry Smith   Collective
10651411c6eeSJed Brown 
10661411c6eeSJed Brown   Input Parameter:
1067bb7acecfSBarry Smith . dm - the `DM` that provides the mapping
10681411c6eeSJed Brown 
10691411c6eeSJed Brown   Output Parameter:
10701411c6eeSJed Brown . ltog - the mapping
10711411c6eeSJed Brown 
1072bb7acecfSBarry Smith   Level: advanced
10731411c6eeSJed Brown 
10741411c6eeSJed Brown   Notes:
1075bb7acecfSBarry Smith   The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10761411c6eeSJed Brown 
1077bb7acecfSBarry Smith   Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1078bb7acecfSBarry Smith   need to use this function with those objects.
1079bb7acecfSBarry Smith 
1080bb7acecfSBarry Smith   This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1081bb7acecfSBarry Smith 
108260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1083bb7acecfSBarry Smith           `DMCreateMatrix()`
10841411c6eeSJed Brown @*/
1085d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1086d71ae5a4SJacob Faibussowitsch {
10870be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10881411c6eeSJed Brown 
10891411c6eeSJed Brown   PetscFunctionBegin;
10901411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10914f572ea9SToby Isaac   PetscAssertPointer(ltog, 2);
10921411c6eeSJed Brown   if (!dm->ltogmap) {
109337d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
109437d0c07bSMatthew G Knepley 
10959566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
109637d0c07bSMatthew G Knepley     if (section) {
1097a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
109837d0c07bSMatthew G Knepley       PetscInt       *ltog;
1099ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
110037d0c07bSMatthew G Knepley 
11019566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11029566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11039566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11049566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
110537d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1106e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
110737d0c07bSMatthew G Knepley 
110837d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11099566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11109566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11119566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11129566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11131a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11141a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1115ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11165227eafbSStefano Zampini 
1117e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11185227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1119e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1120e6befd46SJed Brown             cind++;
1121e6befd46SJed Brown           } else {
11225227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1123e6befd46SJed Brown           }
112437d0c07bSMatthew G Knepley         }
112537d0c07bSMatthew G Knepley       }
1126bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11279371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11289371c9d4SSatish Balay       bsLocal[1] = bs;
11299566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11309371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11319371c9d4SSatish Balay         bs = 1;
11329371c9d4SSatish Balay       } else {
11339371c9d4SSatish Balay         bs = bsMinMax[0];
11349371c9d4SSatish Balay       }
11357591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11367591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1137ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1138ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1139ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1140ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1141ca469d19SJed Brown         }
1142ccf3bd66SMatthew G. Knepley         n /= bs;
1143ccf3bd66SMatthew G. Knepley       }
11449566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1145dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
114637d0c07bSMatthew G Knepley   }
11471411c6eeSJed Brown   *ltog = dm->ltogmap;
11483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11491411c6eeSJed Brown }
11501411c6eeSJed Brown 
11511411c6eeSJed Brown /*@
1152bb7acecfSBarry Smith   DMGetBlockSize - Gets the inherent block size associated with a `DM`
11531411c6eeSJed Brown 
11541411c6eeSJed Brown   Not Collective
11551411c6eeSJed Brown 
11561411c6eeSJed Brown   Input Parameter:
1157bb7acecfSBarry Smith . dm - the `DM` with block structure
11581411c6eeSJed Brown 
11591411c6eeSJed Brown   Output Parameter:
11601411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure
11611411c6eeSJed Brown 
11621411c6eeSJed Brown   Level: intermediate
11631411c6eeSJed Brown 
1164*73ff1848SBarry Smith   Notes:
1165bb7acecfSBarry Smith   This might be the number of degrees of freedom at each grid point for a structured grid.
1166bb7acecfSBarry Smith 
1167bb7acecfSBarry Smith   Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1168bb7acecfSBarry Smith   rather different locations in the vectors may have a different block size.
1169bb7acecfSBarry Smith 
11701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11711411c6eeSJed Brown @*/
1172d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1173d71ae5a4SJacob Faibussowitsch {
11741411c6eeSJed Brown   PetscFunctionBegin;
11751411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11764f572ea9SToby Isaac   PetscAssertPointer(bs, 2);
11777a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11781411c6eeSJed Brown   *bs = dm->bs;
11793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11801411c6eeSJed Brown }
11811411c6eeSJed Brown 
118248eeb7c8SBarry Smith /*@C
1183bb7acecfSBarry Smith   DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1184bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
118547c6ae99SBarry Smith 
118620f4b53cSBarry Smith   Collective
118747c6ae99SBarry Smith 
1188d8d19677SJose E. Roman   Input Parameters:
1189bb7acecfSBarry Smith + dmc - the `DM` object
1190bb7acecfSBarry Smith - dmf - the second, finer `DM` object
119147c6ae99SBarry Smith 
1192d8d19677SJose E. Roman   Output Parameters:
119347c6ae99SBarry Smith + mat - the interpolation
1194bb7acecfSBarry Smith - vec - the scaling (optional), see `DMCreateInterpolationScale()`
119547c6ae99SBarry Smith 
119647c6ae99SBarry Smith   Level: developer
119747c6ae99SBarry Smith 
119895452b02SPatrick Sanan   Notes:
1199bb7acecfSBarry 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
1200bb7acecfSBarry Smith   DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1201d52bd9f3SBarry Smith 
1202bb7acecfSBarry Smith   For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1203bb7acecfSBarry Smith   vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
120485afcc9aSBarry Smith 
12051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
120647c6ae99SBarry Smith @*/
1207d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1208d71ae5a4SJacob Faibussowitsch {
120947c6ae99SBarry Smith   PetscFunctionBegin;
1210a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1211a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12124f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12139566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1214dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12159566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
121747c6ae99SBarry Smith }
121847c6ae99SBarry Smith 
12193ad4599aSBarry Smith /*@
1220a4e35b19SJacob Faibussowitsch   DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is
1221a4e35b19SJacob Faibussowitsch   the transpose of the interpolation between the `DM`.
12222ed6491fSPatrick Sanan 
12232ed6491fSPatrick Sanan   Input Parameters:
1224bb7acecfSBarry Smith + dac - `DM` that defines a coarse mesh
1225bb7acecfSBarry Smith . daf - `DM` that defines a fine mesh
12262ed6491fSPatrick Sanan - mat - the restriction (or interpolation operator) from fine to coarse
12272ed6491fSPatrick Sanan 
12282ed6491fSPatrick Sanan   Output Parameter:
12292ed6491fSPatrick Sanan . scale - the scaled vector
12302ed6491fSPatrick Sanan 
1231bb7acecfSBarry Smith   Level: advanced
12322ed6491fSPatrick Sanan 
1233*73ff1848SBarry Smith   Note:
1234a4e35b19SJacob Faibussowitsch   xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual)
1235a4e35b19SJacob Faibussowitsch   restriction. In other words xcoarse is the coarse representation of xfine.
1236a4e35b19SJacob Faibussowitsch 
1237*73ff1848SBarry Smith   Developer Note:
1238bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1239e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1240e9c74fd6SRichard Tran Mills 
124160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, `DMCreateRestriction()`, `DMCreateGlobalVector()`
12422ed6491fSPatrick Sanan @*/
1243d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1244d71ae5a4SJacob Faibussowitsch {
12452ed6491fSPatrick Sanan   Vec         fine;
12462ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12479704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1248e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12499704db99SRichard Tran Mills #endif
12502ed6491fSPatrick Sanan 
12512ed6491fSPatrick Sanan   PetscFunctionBegin;
12529566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12539566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12549566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12559704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12569704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12579704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12589704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12599566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1260e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12619566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12629566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12639566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
126483aa49f4SRichard Tran Mills   }
12659704db99SRichard Tran Mills #endif
12669566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12679566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12689566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12702ed6491fSPatrick Sanan }
12712ed6491fSPatrick Sanan 
12722ed6491fSPatrick Sanan /*@
1273bb7acecfSBarry Smith   DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1274bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12753ad4599aSBarry Smith 
127620f4b53cSBarry Smith   Collective
12773ad4599aSBarry Smith 
1278d8d19677SJose E. Roman   Input Parameters:
1279bb7acecfSBarry Smith + dmc - the `DM` object
1280bb7acecfSBarry Smith - dmf - the second, finer `DM` object
12813ad4599aSBarry Smith 
12823ad4599aSBarry Smith   Output Parameter:
12833ad4599aSBarry Smith . mat - the restriction
12843ad4599aSBarry Smith 
12853ad4599aSBarry Smith   Level: developer
12863ad4599aSBarry Smith 
1287bb7acecfSBarry Smith   Note:
1288bb7acecfSBarry Smith   This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1289bb7acecfSBarry Smith   matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12903ad4599aSBarry Smith 
12911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12923ad4599aSBarry Smith @*/
1293d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1294d71ae5a4SJacob Faibussowitsch {
12953ad4599aSBarry Smith   PetscFunctionBegin;
1296a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1297a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12984f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12999566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1300dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13019566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13033ad4599aSBarry Smith }
13043ad4599aSBarry Smith 
130547c6ae99SBarry Smith /*@
1306a4e35b19SJacob Faibussowitsch   DMCreateInjection - Gets injection matrix between two `DM` objects.
130747c6ae99SBarry Smith 
130820f4b53cSBarry Smith   Collective
130947c6ae99SBarry Smith 
1310d8d19677SJose E. Roman   Input Parameters:
1311bb7acecfSBarry Smith + dac - the `DM` object
1312bb7acecfSBarry Smith - daf - the second, finer `DM` object
131347c6ae99SBarry Smith 
131447c6ae99SBarry Smith   Output Parameter:
13156dbf9973SLawrence Mitchell . mat - the injection
131647c6ae99SBarry Smith 
131747c6ae99SBarry Smith   Level: developer
131847c6ae99SBarry Smith 
1319a4e35b19SJacob Faibussowitsch   Notes:
1320a4e35b19SJacob Faibussowitsch   This is an operator that applied to a vector obtained with `DMCreateGlobalVector()` on the
1321a4e35b19SJacob Faibussowitsch   fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting
1322a4e35b19SJacob Faibussowitsch   the values on the coarse grid points. This compares to the operator obtained by
1323a4e35b19SJacob Faibussowitsch   `DMCreateRestriction()` or the transpose of the operator obtained by
1324a4e35b19SJacob Faibussowitsch   `DMCreateInterpolation()` that uses a "local weighted average" of the values around the
1325a4e35b19SJacob Faibussowitsch   coarse grid point as the coarse grid value.
1326a4e35b19SJacob Faibussowitsch 
1327bb7acecfSBarry 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
1328bb7acecfSBarry Smith   `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
132985afcc9aSBarry Smith 
13301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1331bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
133247c6ae99SBarry Smith @*/
1333d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1334d71ae5a4SJacob Faibussowitsch {
133547c6ae99SBarry Smith   PetscFunctionBegin;
1336a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1337a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13384f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13399566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1340dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13419566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
134347c6ae99SBarry Smith }
134447c6ae99SBarry Smith 
1345b412c318SBarry Smith /*@
1346bb7acecfSBarry 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
1347bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1348bd041c0cSMatthew G. Knepley 
134920f4b53cSBarry Smith   Collective
1350bd041c0cSMatthew G. Knepley 
1351d8d19677SJose E. Roman   Input Parameters:
1352bb7acecfSBarry Smith + dmc - the target `DM` object
1353bb7acecfSBarry Smith - dmf - the source `DM` object
1354bd041c0cSMatthew G. Knepley 
1355bd041c0cSMatthew G. Knepley   Output Parameter:
1356b4937a87SMatthew G. Knepley . mat - the mass matrix
1357bd041c0cSMatthew G. Knepley 
1358bd041c0cSMatthew G. Knepley   Level: developer
1359bd041c0cSMatthew G. Knepley 
1360bb7acecfSBarry Smith   Notes:
1361bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1362bb7acecfSBarry Smith 
136320f4b53cSBarry 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()`
1364bb7acecfSBarry Smith 
136542747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1366bd041c0cSMatthew G. Knepley @*/
1367d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1368d71ae5a4SJacob Faibussowitsch {
1369bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1370b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1371b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13724f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13735b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1374dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13755b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
13763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1377b4937a87SMatthew G. Knepley }
1378b4937a87SMatthew G. Knepley 
1379b4937a87SMatthew G. Knepley /*@
1380bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1381b4937a87SMatthew G. Knepley 
138220f4b53cSBarry Smith   Collective
1383b4937a87SMatthew G. Knepley 
1384b4937a87SMatthew G. Knepley   Input Parameter:
1385bb7acecfSBarry Smith . dm - the `DM` object
1386b4937a87SMatthew G. Knepley 
1387b4937a87SMatthew G. Knepley   Output Parameter:
1388bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1389b4937a87SMatthew G. Knepley 
1390b4937a87SMatthew G. Knepley   Level: developer
1391b4937a87SMatthew G. Knepley 
1392bb7acecfSBarry Smith   Note:
1393bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1394bb7acecfSBarry Smith 
139560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1396b4937a87SMatthew G. Knepley @*/
1397d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1398d71ae5a4SJacob Faibussowitsch {
1399b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1400b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14014f572ea9SToby Isaac   PetscAssertPointer(lm, 2);
1402dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
14033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1404bd041c0cSMatthew G. Knepley }
1405bd041c0cSMatthew G. Knepley 
1406bd041c0cSMatthew G. Knepley /*@
1407bb7acecfSBarry Smith   DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1408bb7acecfSBarry Smith   of a PDE on the `DM`.
140947c6ae99SBarry Smith 
141020f4b53cSBarry Smith   Collective
141147c6ae99SBarry Smith 
1412d8d19677SJose E. Roman   Input Parameters:
1413bb7acecfSBarry Smith + dm    - the `DM` object
1414bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
141547c6ae99SBarry Smith 
141647c6ae99SBarry Smith   Output Parameter:
141747c6ae99SBarry Smith . coloring - the coloring
141847c6ae99SBarry Smith 
14191bf8429eSBarry Smith   Level: developer
14201bf8429eSBarry Smith 
1421ec5066bdSBarry Smith   Notes:
1422bb7acecfSBarry 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
1423bb7acecfSBarry Smith   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1424ec5066bdSBarry Smith 
1425bb7acecfSBarry Smith   This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14261bf8429eSBarry Smith   For `DMDA` in three dimensions with periodic boundary conditions the number of grid points in each dimension must be divisible by 2*stencil_width + 1,
14271bf8429eSBarry Smith   otherwise an error will be generated.
1428ec5066bdSBarry Smith 
14291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1430aab9d709SJed Brown @*/
1431d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1432d71ae5a4SJacob Faibussowitsch {
143347c6ae99SBarry Smith   PetscFunctionBegin;
1434171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14354f572ea9SToby Isaac   PetscAssertPointer(coloring, 3);
1436dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
143847c6ae99SBarry Smith }
143947c6ae99SBarry Smith 
1440b412c318SBarry Smith /*@
1441bb7acecfSBarry Smith   DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
144247c6ae99SBarry Smith 
144320f4b53cSBarry Smith   Collective
144447c6ae99SBarry Smith 
144547c6ae99SBarry Smith   Input Parameter:
1446bb7acecfSBarry Smith . dm - the `DM` object
144747c6ae99SBarry Smith 
144847c6ae99SBarry Smith   Output Parameter:
144947c6ae99SBarry Smith . mat - the empty Jacobian
145047c6ae99SBarry Smith 
145120f4b53cSBarry Smith   Options Database Key:
1452bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1453f27dd7c6SMatthew G. Knepley 
145420f4b53cSBarry Smith   Level: beginner
145520f4b53cSBarry Smith 
145695452b02SPatrick Sanan   Notes:
145795452b02SPatrick Sanan   This properly preallocates the number of nonzeros in the sparse matrix so you
145894013140SBarry Smith   do not need to do it yourself.
145994013140SBarry Smith 
146094013140SBarry Smith   By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1461bb7acecfSBarry Smith   the nonzero pattern call `DMSetMatrixPreallocateOnly()`
146294013140SBarry Smith 
1463bb7acecfSBarry Smith   For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
146494013140SBarry Smith   internally by PETSc.
146594013140SBarry Smith 
1466bb7acecfSBarry Smith   For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1467bb7acecfSBarry Smith   `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
146894013140SBarry Smith 
14691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1470aab9d709SJed Brown @*/
1471d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1472d71ae5a4SJacob Faibussowitsch {
147347c6ae99SBarry Smith   PetscFunctionBegin;
1474171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14754f572ea9SToby Isaac   PetscAssertPointer(mat, 2);
14769566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14779566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1478dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
147976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1480c6b011d8SStefano Zampini     DM mdm;
1481c6b011d8SStefano Zampini 
14829566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14837a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1484c6b011d8SStefano Zampini   }
1485e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1486e5e52638SMatthew G. Knepley   if (dm->Nf) {
1487e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1488649ef022SMatthew Knepley     PetscInt     Nf, f;
1489e571a35bSMatthew G. Knepley 
14909566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1491649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1492649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14939566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14949566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14959566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1496649ef022SMatthew Knepley         break;
1497e571a35bSMatthew G. Knepley       }
1498649ef022SMatthew Knepley     }
1499649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1500649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15019566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15029566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15039566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1504e571a35bSMatthew G. Knepley       }
1505e571a35bSMatthew G. Knepley     }
1506e571a35bSMatthew G. Knepley   }
15079566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
150947c6ae99SBarry Smith }
151047c6ae99SBarry Smith 
1511732e2eb9SMatthew G Knepley /*@
1512a4e35b19SJacob Faibussowitsch   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and
1513a4e35b19SJacob Faibussowitsch   `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the
1514a4e35b19SJacob Faibussowitsch   matrices will not be preallocated.
1515aa0f6e3cSJed Brown 
151620f4b53cSBarry Smith   Logically Collective
1517aa0f6e3cSJed Brown 
1518aa0f6e3cSJed Brown   Input Parameters:
1519bb7acecfSBarry Smith + dm   - the `DM`
1520bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1521aa0f6e3cSJed Brown 
1522aa0f6e3cSJed Brown   Level: developer
1523aa0f6e3cSJed Brown 
1524*73ff1848SBarry Smith   Note:
1525a4e35b19SJacob Faibussowitsch   This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and
1526a4e35b19SJacob Faibussowitsch   `MatSetValuesCOO()` will be used.
1527a4e35b19SJacob Faibussowitsch 
15281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1529aa0f6e3cSJed Brown @*/
1530d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1531d71ae5a4SJacob Faibussowitsch {
1532aa0f6e3cSJed Brown   PetscFunctionBegin;
1533aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1534aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1536aa0f6e3cSJed Brown }
1537aa0f6e3cSJed Brown 
1538aa0f6e3cSJed Brown /*@
1539bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1540732e2eb9SMatthew G Knepley   preallocated but the nonzero structure and zero values will not be set.
1541732e2eb9SMatthew G Knepley 
154220f4b53cSBarry Smith   Logically Collective
1543732e2eb9SMatthew G Knepley 
1544d8d19677SJose E. Roman   Input Parameters:
1545bb7acecfSBarry Smith + dm   - the `DM`
1546bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1547732e2eb9SMatthew G Knepley 
154820f4b53cSBarry Smith   Options Database Key:
1549bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1550f27dd7c6SMatthew G. Knepley 
155120f4b53cSBarry Smith   Level: developer
155220f4b53cSBarry Smith 
15531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1554732e2eb9SMatthew G Knepley @*/
1555d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1556d71ae5a4SJacob Faibussowitsch {
1557732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1558732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1559732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1561732e2eb9SMatthew G Knepley }
1562732e2eb9SMatthew G Knepley 
1563b06ff27eSHong Zhang /*@
1564bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1565bb7acecfSBarry Smith   but the array for numerical values will not be allocated.
1566b06ff27eSHong Zhang 
156720f4b53cSBarry Smith   Logically Collective
1568b06ff27eSHong Zhang 
1569d8d19677SJose E. Roman   Input Parameters:
1570bb7acecfSBarry Smith + dm   - the `DM`
1571da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1572b06ff27eSHong Zhang 
1573b06ff27eSHong Zhang   Level: developer
1574bb7acecfSBarry Smith 
15751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1576b06ff27eSHong Zhang @*/
1577d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1578d71ae5a4SJacob Faibussowitsch {
1579b06ff27eSHong Zhang   PetscFunctionBegin;
1580b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1581b06ff27eSHong Zhang   dm->structure_only = only;
15823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1583b06ff27eSHong Zhang }
1584b06ff27eSHong Zhang 
1585863027abSJed Brown /*@
1586863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1587863027abSJed Brown 
158820f4b53cSBarry Smith   Logically Collective
1589863027abSJed Brown 
1590863027abSJed Brown   Input Parameters:
1591863027abSJed Brown + dm    - the `DM`
1592863027abSJed Brown - btype - block by topological point or field node
1593863027abSJed Brown 
159420f4b53cSBarry Smith   Options Database Key:
15950e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1596863027abSJed Brown 
159720f4b53cSBarry Smith   Level: advanced
159820f4b53cSBarry Smith 
15991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1600863027abSJed Brown @*/
1601863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1602863027abSJed Brown {
1603863027abSJed Brown   PetscFunctionBegin;
1604863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1605863027abSJed Brown   dm->blocking_type = btype;
16063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1607863027abSJed Brown }
1608863027abSJed Brown 
1609863027abSJed Brown /*@
1610863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1611863027abSJed Brown 
1612863027abSJed Brown   Not Collective
1613863027abSJed Brown 
16142fe279fdSBarry Smith   Input Parameter:
1615863027abSJed Brown . dm - the `DM`
1616863027abSJed Brown 
16172fe279fdSBarry Smith   Output Parameter:
1618863027abSJed Brown . btype - block by topological point or field node
1619863027abSJed Brown 
1620863027abSJed Brown   Level: advanced
1621863027abSJed Brown 
16221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1623863027abSJed Brown @*/
1624863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1625863027abSJed Brown {
1626863027abSJed Brown   PetscFunctionBegin;
1627863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16284f572ea9SToby Isaac   PetscAssertPointer(btype, 2);
1629863027abSJed Brown   *btype = dm->blocking_type;
16303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1631863027abSJed Brown }
1632863027abSJed Brown 
1633a89ea682SMatthew G Knepley /*@C
1634bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1635a89ea682SMatthew G Knepley 
1636a89ea682SMatthew G Knepley   Not Collective
1637a89ea682SMatthew G Knepley 
1638a89ea682SMatthew G Knepley   Input Parameters:
1639bb7acecfSBarry Smith + dm    - the `DM` object
1640a5b23f4aSJose E. Roman . count - The minimum size
164120f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1642a89ea682SMatthew G Knepley 
1643a89ea682SMatthew G Knepley   Output Parameter:
164460225df5SJacob Faibussowitsch . mem - the work array
1645a89ea682SMatthew G Knepley 
1646a89ea682SMatthew G Knepley   Level: developer
1647a89ea682SMatthew G Knepley 
1648*73ff1848SBarry Smith   Notes:
1649da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1650bb7acecfSBarry Smith 
1651bb7acecfSBarry Smith   The array may contain nonzero values
1652bb7acecfSBarry Smith 
16531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1654a89ea682SMatthew G Knepley @*/
1655d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1656d71ae5a4SJacob Faibussowitsch {
1657aa1993deSMatthew G Knepley   DMWorkLink  link;
165869291d52SBarry Smith   PetscMPIInt dsize;
1659a89ea682SMatthew G Knepley 
1660a89ea682SMatthew G Knepley   PetscFunctionBegin;
1661a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16624f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1663442f3b32SStefano Zampini   if (!count) {
1664442f3b32SStefano Zampini     *(void **)mem = NULL;
1665442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1666442f3b32SStefano Zampini   }
1667aa1993deSMatthew G Knepley   if (dm->workin) {
1668aa1993deSMatthew G Knepley     link       = dm->workin;
1669aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1670aa1993deSMatthew G Knepley   } else {
16714dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1672a89ea682SMatthew G Knepley   }
1673b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1674b77fa653SStefano Zampini      Get size directly */
1675b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1676b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1677b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1678b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1679b77fa653SStefano Zampini #endif
1680b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1681b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1682b77fa653SStefano Zampini #endif
1683b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1684b77fa653SStefano Zampini 
16855056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16869566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16879566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1688854ce69bSBarry Smith     link->bytes = dsize * count;
1689aa1993deSMatthew G Knepley   }
1690aa1993deSMatthew G Knepley   link->next  = dm->workout;
1691aa1993deSMatthew G Knepley   dm->workout = link;
1692cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
169300d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
169400d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
169500d952a4SJed Brown #endif
1696aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
16973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1698a89ea682SMatthew G Knepley }
1699a89ea682SMatthew G Knepley 
1700aa1993deSMatthew G Knepley /*@C
1701bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1702aa1993deSMatthew G Knepley 
1703aa1993deSMatthew G Knepley   Not Collective
1704aa1993deSMatthew G Knepley 
1705aa1993deSMatthew G Knepley   Input Parameters:
1706bb7acecfSBarry Smith + dm    - the `DM` object
1707a5b23f4aSJose E. Roman . count - The minimum size
170820f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1709aa1993deSMatthew G Knepley 
1710aa1993deSMatthew G Knepley   Output Parameter:
171160225df5SJacob Faibussowitsch . mem - the work array
1712aa1993deSMatthew G Knepley 
1713aa1993deSMatthew G Knepley   Level: developer
1714aa1993deSMatthew G Knepley 
1715*73ff1848SBarry Smith   Developer Note:
1716bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1717147403d9SBarry Smith 
17181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1719aa1993deSMatthew G Knepley @*/
1720d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1721d71ae5a4SJacob Faibussowitsch {
1722aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1723aa1993deSMatthew G Knepley 
1724aa1993deSMatthew G Knepley   PetscFunctionBegin;
1725aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17264f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1727a4e35b19SJacob Faibussowitsch   (void)count;
1728a4e35b19SJacob Faibussowitsch   (void)dtype;
1729442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1730aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1731aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1732aa1993deSMatthew G Knepley       *p            = link->next;
1733aa1993deSMatthew G Knepley       link->next    = dm->workin;
1734aa1993deSMatthew G Knepley       dm->workin    = link;
17350298fd71SBarry Smith       *(void **)mem = NULL;
17363ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1737aa1993deSMatthew G Knepley     }
1738aa1993deSMatthew G Knepley   }
1739aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1740aa1993deSMatthew G Knepley }
1741e7c4fc90SDmitry Karpeev 
17428cda7954SMatthew G. Knepley /*@C
1743bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1744bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17458cda7954SMatthew G. Knepley 
174620f4b53cSBarry Smith   Logically Collective; No Fortran Support
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 nullspace
17528cda7954SMatthew G. Knepley 
175320f4b53cSBarry Smith   Calling sequence of `nullsp`:
1754bb7acecfSBarry Smith + dm        - The present `DM`
1755bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1756147403d9SBarry Smith . field     - The field number in dm
1757147403d9SBarry Smith - nullSpace - The nullspace for the given field
17588cda7954SMatthew G. Knepley 
175949762cbcSSatish Balay   Level: intermediate
176049762cbcSSatish Balay 
17611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1762147403d9SBarry Smith @*/
1763a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1764d71ae5a4SJacob Faibussowitsch {
1765435a35e8SMatthew G Knepley   PetscFunctionBegin;
1766435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17677a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1768435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1770435a35e8SMatthew G Knepley }
1771435a35e8SMatthew G Knepley 
17728cda7954SMatthew G. Knepley /*@C
1773bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17748cda7954SMatthew G. Knepley 
177520f4b53cSBarry Smith   Not Collective; No Fortran Support
17768cda7954SMatthew G. Knepley 
17778cda7954SMatthew G. Knepley   Input Parameters:
1778bb7acecfSBarry Smith + dm    - The `DM`
17798cda7954SMatthew G. Knepley - field - The field number for the nullspace
17808cda7954SMatthew G. Knepley 
17818cda7954SMatthew G. Knepley   Output Parameter:
17828cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17838cda7954SMatthew G. Knepley 
178420f4b53cSBarry Smith   Calling sequence of `nullsp`:
1785147403d9SBarry Smith + dm        - The present DM
1786147403d9SBarry Smith . origField - The field number given above, in the original DM
1787147403d9SBarry Smith . field     - The field number in dm
1788147403d9SBarry Smith - nullSpace - The nullspace for the given field
17898cda7954SMatthew G. Knepley 
179049762cbcSSatish Balay   Level: intermediate
179149762cbcSSatish Balay 
17921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1793147403d9SBarry Smith @*/
1794a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1795d71ae5a4SJacob Faibussowitsch {
17960a50eb56SMatthew G. Knepley   PetscFunctionBegin;
17970a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17984f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
17997a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
18000a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
18013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18020a50eb56SMatthew G. Knepley }
18030a50eb56SMatthew G. Knepley 
18048cda7954SMatthew G. Knepley /*@C
1805bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18068cda7954SMatthew G. Knepley 
180720f4b53cSBarry Smith   Logically Collective; No Fortran Support
18088cda7954SMatthew G. Knepley 
18098cda7954SMatthew G. Knepley   Input Parameters:
1810bb7acecfSBarry Smith + dm     - The `DM`
18118cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18128cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18138cda7954SMatthew G. Knepley 
181420f4b53cSBarry Smith   Calling sequence of `nullsp`:
1815bb7acecfSBarry Smith + dm        - The present `DM`
1816bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1817147403d9SBarry Smith . field     - The field number in dm
1818147403d9SBarry Smith - nullSpace - The nullspace for the given field
18198cda7954SMatthew G. Knepley 
182049762cbcSSatish Balay   Level: intermediate
182149762cbcSSatish Balay 
18221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1823bb7acecfSBarry Smith           `MatNullSpace`
1824147403d9SBarry Smith @*/
1825a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1826d71ae5a4SJacob Faibussowitsch {
1827f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1828f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18297a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1830f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1832f9d4088aSMatthew G. Knepley }
1833f9d4088aSMatthew G. Knepley 
18348cda7954SMatthew G. Knepley /*@C
1835bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18368cda7954SMatthew G. Knepley 
183720f4b53cSBarry Smith   Not Collective; No Fortran Support
18388cda7954SMatthew G. Knepley 
18398cda7954SMatthew G. Knepley   Input Parameters:
1840bb7acecfSBarry Smith + dm    - The `DM`
18418cda7954SMatthew G. Knepley - field - The field number for the nullspace
18428cda7954SMatthew G. Knepley 
18438cda7954SMatthew G. Knepley   Output Parameter:
18448cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18458cda7954SMatthew G. Knepley 
184620f4b53cSBarry Smith   Calling sequence of `nullsp`:
1847bb7acecfSBarry Smith + dm        - The present `DM`
1848bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1849147403d9SBarry Smith . field     - The field number in dm
1850147403d9SBarry Smith - nullSpace - The nullspace for the given field
18518cda7954SMatthew G. Knepley 
185249762cbcSSatish Balay   Level: intermediate
185349762cbcSSatish Balay 
18541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1855bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1856147403d9SBarry Smith @*/
1857a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1858d71ae5a4SJacob Faibussowitsch {
1859f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1860f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18614f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18627a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1863f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1865f9d4088aSMatthew G. Knepley }
1866f9d4088aSMatthew G. Knepley 
18674f3b5142SJed Brown /*@C
1868bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18694d343eeaSMatthew G Knepley 
187020f4b53cSBarry Smith   Not Collective; No Fortran Support
18714d343eeaSMatthew G Knepley 
18724d343eeaSMatthew G Knepley   Input Parameter:
1873bb7acecfSBarry Smith . dm - the `DM` object
18744d343eeaSMatthew G Knepley 
18754d343eeaSMatthew G Knepley   Output Parameters:
187620f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
187720f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
187820f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
18794d343eeaSMatthew G Knepley 
18804d343eeaSMatthew G Knepley   Level: intermediate
18814d343eeaSMatthew G Knepley 
1882bb7acecfSBarry Smith   Note:
1883*73ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `fieldNames` should be freed with
1884*73ff1848SBarry Smith   `PetscFree()`, every entry of `fields` should be destroyed with `ISDestroy()`, and both arrays should be freed with
1885bb7acecfSBarry Smith   `PetscFree()`.
188621c9b008SJed Brown 
1887*73ff1848SBarry Smith   Developer Note:
1888bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1889bb7acecfSBarry Smith   likely be removed.
1890bb7acecfSBarry Smith 
18911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1892bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18934d343eeaSMatthew G Knepley @*/
1894d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1895d71ae5a4SJacob Faibussowitsch {
189637d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
18974d343eeaSMatthew G Knepley 
18984d343eeaSMatthew G Knepley   PetscFunctionBegin;
18994d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
190069ca1f37SDmitry Karpeev   if (numFields) {
19014f572ea9SToby Isaac     PetscAssertPointer(numFields, 2);
190269ca1f37SDmitry Karpeev     *numFields = 0;
190369ca1f37SDmitry Karpeev   }
190437d0c07bSMatthew G Knepley   if (fieldNames) {
19054f572ea9SToby Isaac     PetscAssertPointer(fieldNames, 3);
19060298fd71SBarry Smith     *fieldNames = NULL;
190769ca1f37SDmitry Karpeev   }
190869ca1f37SDmitry Karpeev   if (fields) {
19094f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
19100298fd71SBarry Smith     *fields = NULL;
191169ca1f37SDmitry Karpeev   }
19129566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
191337d0c07bSMatthew G Knepley   if (section) {
19143a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
191537d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
191637d0c07bSMatthew G Knepley 
19179566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19189566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19199566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19209566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
192137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
192237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19239566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
192437d0c07bSMatthew G Knepley     }
192537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
192637d0c07bSMatthew G Knepley       PetscInt gdof;
192737d0c07bSMatthew G Knepley 
19289566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
192937d0c07bSMatthew G Knepley       if (gdof > 0) {
193037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19313a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
193237d0c07bSMatthew G Knepley 
19339566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19349566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19353a544194SStefano Zampini           fpdof = fdof - fcdof;
19363a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19373a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19383a544194SStefano Zampini             fieldNc[f] = 1;
19393a544194SStefano Zampini           }
19403a544194SStefano Zampini           fieldSizes[f] += fpdof;
194137d0c07bSMatthew G Knepley         }
194237d0c07bSMatthew G Knepley       }
194337d0c07bSMatthew G Knepley     }
194437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19459566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
194637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
194737d0c07bSMatthew G Knepley     }
194837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
194937d0c07bSMatthew G Knepley       PetscInt gdof, goff;
195037d0c07bSMatthew G Knepley 
19519566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
195237d0c07bSMatthew G Knepley       if (gdof > 0) {
19539566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
195437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
195537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
195637d0c07bSMatthew G Knepley 
19579566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19589566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1959ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
196037d0c07bSMatthew G Knepley         }
196137d0c07bSMatthew G Knepley       }
196237d0c07bSMatthew G Knepley     }
19638865f1eaSKarl Rupp     if (numFields) *numFields = nF;
196437d0c07bSMatthew G Knepley     if (fieldNames) {
19659566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
196637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
196737d0c07bSMatthew G Knepley         const char *fieldName;
196837d0c07bSMatthew G Knepley 
19699566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19709566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
197137d0c07bSMatthew G Knepley       }
197237d0c07bSMatthew G Knepley     }
197337d0c07bSMatthew G Knepley     if (fields) {
19749566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
197537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19763a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19773a544194SStefano Zampini 
19789566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19793a544194SStefano Zampini         in[0] = -fieldNc[f];
19803a544194SStefano Zampini         in[1] = fieldNc[f];
19811c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19823a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19839566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
198437d0c07bSMatthew G Knepley       }
198537d0c07bSMatthew G Knepley     }
19869566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1987dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19894d343eeaSMatthew G Knepley }
19904d343eeaSMatthew G Knepley 
199116621825SDmitry Karpeev /*@C
1992bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1993a4e35b19SJacob Faibussowitsch   corresponding to different fields.
1994e7c4fc90SDmitry Karpeev 
199520f4b53cSBarry Smith   Not Collective; No Fortran Support
1996e7c4fc90SDmitry Karpeev 
1997e7c4fc90SDmitry Karpeev   Input Parameter:
1998bb7acecfSBarry Smith . dm - the `DM` object
1999e7c4fc90SDmitry Karpeev 
2000e7c4fc90SDmitry Karpeev   Output Parameters:
200120f4b53cSBarry Smith + len      - The number of fields (or `NULL` if not requested)
200220f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested)
200320f4b53cSBarry Smith . islist   - The global indices for each field (or `NULL` if not requested)
200420f4b53cSBarry Smith - dmlist   - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
2005e7c4fc90SDmitry Karpeev 
2006e7c4fc90SDmitry Karpeev   Level: intermediate
2007e7c4fc90SDmitry Karpeev 
2008a4e35b19SJacob Faibussowitsch   Notes:
2009a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding field, defined by
2010a4e35b19SJacob Faibussowitsch   `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2011a4e35b19SJacob Faibussowitsch 
2012a4e35b19SJacob Faibussowitsch   The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2013a4e35b19SJacob Faibussowitsch 
2014*73ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
2015*73ff1848SBarry Smith   `PetscFree()`, every entry of `islist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2016bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2017e7c4fc90SDmitry Karpeev 
201860225df5SJacob Faibussowitsch   Developer Notes:
2019bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2020bb7acecfSBarry Smith 
2021*73ff1848SBarry Smith   Unlike  `DMRefine()`, `DMCoarsen()`, and `DMCreateDomainDecomposition()` this provides no mechanism to provide hooks that are called after the
2022*73ff1848SBarry Smith   decomposition is computed.
2023*73ff1848SBarry Smith 
2024*73ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
2025e7c4fc90SDmitry Karpeev @*/
2026d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2027d71ae5a4SJacob Faibussowitsch {
2028e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2029e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20308865f1eaSKarl Rupp   if (len) {
20314f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20328865f1eaSKarl Rupp     *len = 0;
20338865f1eaSKarl Rupp   }
20348865f1eaSKarl Rupp   if (namelist) {
20354f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2036ea78f98cSLisandro Dalcin     *namelist = NULL;
20378865f1eaSKarl Rupp   }
20388865f1eaSKarl Rupp   if (islist) {
20394f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2040ea78f98cSLisandro Dalcin     *islist = NULL;
20418865f1eaSKarl Rupp   }
20428865f1eaSKarl Rupp   if (dmlist) {
20434f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2044ea78f98cSLisandro Dalcin     *dmlist = NULL;
20458865f1eaSKarl Rupp   }
2046f3f0edfdSDmitry Karpeev   /*
2047f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2048f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2049f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2050f3f0edfdSDmitry Karpeev    */
20517a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
205216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2053435a35e8SMatthew G Knepley     PetscSection section;
2054435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2055435a35e8SMatthew G Knepley 
20569566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20579566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2058435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2059f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20609566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20619566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20629566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2063435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2064435a35e8SMatthew G Knepley         const char *fieldName;
2065435a35e8SMatthew G Knepley 
20669566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
206703dc3394SMatthew G. Knepley         if (namelist) {
20689566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20699566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2070435a35e8SMatthew G Knepley         }
207103dc3394SMatthew G. Knepley       }
2072435a35e8SMatthew G Knepley     } else {
20739566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2074e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20750298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2076e7c4fc90SDmitry Karpeev     }
2077dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
207916621825SDmitry Karpeev }
208016621825SDmitry Karpeev 
2081412a4547SAlexis Marboeuf /*@C
208220f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
208320f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2084435a35e8SMatthew G Knepley 
2085435a35e8SMatthew G Knepley   Not collective
2086435a35e8SMatthew G Knepley 
2087435a35e8SMatthew G Knepley   Input Parameters:
2088bb7acecfSBarry Smith + dm        - The `DM` object
2089bb7acecfSBarry Smith . numFields - The number of fields to select
20902adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2091435a35e8SMatthew G Knepley 
2092435a35e8SMatthew G Knepley   Output Parameters:
2093bb7acecfSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`
2094bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2095435a35e8SMatthew G Knepley 
209620f4b53cSBarry Smith   Level: intermediate
209720f4b53cSBarry Smith 
2098bb7acecfSBarry Smith   Note:
2099bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21005d3b26e6SMatthew G. Knepley 
210160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2102435a35e8SMatthew G Knepley @*/
2103d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2104d71ae5a4SJacob Faibussowitsch {
2105435a35e8SMatthew G Knepley   PetscFunctionBegin;
2106435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21074f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
21084f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
21094f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2110dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2112435a35e8SMatthew G Knepley }
2113435a35e8SMatthew G Knepley 
21142adcc780SMatthew G. Knepley /*@C
2115bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21162adcc780SMatthew G. Knepley 
21172adcc780SMatthew G. Knepley   Not collective
21182adcc780SMatthew G. Knepley 
2119d8d19677SJose E. Roman   Input Parameters:
2120bb7acecfSBarry Smith + dms - The `DM` objects
2121bb7acecfSBarry Smith - n   - The number of `DM`s
21222adcc780SMatthew G. Knepley 
21232adcc780SMatthew G. Knepley   Output Parameters:
2124bb7acecfSBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or NULL
2125bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21262adcc780SMatthew G. Knepley 
212720f4b53cSBarry Smith   Level: intermediate
212820f4b53cSBarry Smith 
2129bb7acecfSBarry Smith   Note:
2130bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21315d3b26e6SMatthew G. Knepley 
2132*73ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`, `DMCreateDomainDecomposition()`
21332adcc780SMatthew G. Knepley @*/
2134d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
2135d71ae5a4SJacob Faibussowitsch {
21362adcc780SMatthew G. Knepley   PetscInt i;
21372adcc780SMatthew G. Knepley 
21382adcc780SMatthew G. Knepley   PetscFunctionBegin;
21394f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2140ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21414f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
21424f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2143bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2144bb7acecfSBarry Smith   if (n) {
2145b9d85ea2SLisandro Dalcin     DM dm = dms[0];
214600045ab3SPierre Jolivet     PetscCheck(dm->ops->createsuperdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No method createsuperdm for DM of type %s", ((PetscObject)dm)->type_name);
2147dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21482adcc780SMatthew G. Knepley   }
21493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21502adcc780SMatthew G. Knepley }
21512adcc780SMatthew G. Knepley 
215216621825SDmitry Karpeev /*@C
2153a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2154a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
215516621825SDmitry Karpeev 
215620f4b53cSBarry Smith   Not Collective
215716621825SDmitry Karpeev 
215816621825SDmitry Karpeev   Input Parameter:
2159bb7acecfSBarry Smith . dm - the `DM` object
216016621825SDmitry Karpeev 
216116621825SDmitry Karpeev   Output Parameters:
216220f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
216320f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
2164*73ff1848SBarry Smith . innerislist - The global indices for each inner subdomain (or `NULL`, if not requested)
2165*73ff1848SBarry Smith . outerislist - The global indices for each outer subdomain (or `NULL`, if not requested)
2166*73ff1848SBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
216716621825SDmitry Karpeev 
216816621825SDmitry Karpeev   Level: intermediate
216916621825SDmitry Karpeev 
2170*73ff1848SBarry Smith   Notes:
2171a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2172a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2173a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2174a4e35b19SJacob Faibussowitsch 
2175a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2176a4e35b19SJacob Faibussowitsch 
2177*73ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
2178*73ff1848SBarry Smith   `PetscFree()`, every entry of `innerislist` and `outerislist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2179bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
218016621825SDmitry Karpeev 
2181a4e35b19SJacob Faibussowitsch   Developer Notes:
218220f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2183bb7acecfSBarry Smith 
2184*73ff1848SBarry Smith   The names are inconsistent, the hooks use `DMSubDomainHook` which is nothing like `DMCreateDomainDecomposition()` while `DMRefineHook` is used for `DMRefine()`.
2185*73ff1848SBarry Smith 
2186*73ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`,
2187*73ff1848SBarry Smith           `DMSubDomainHookAdd()`, `DMSubDomainHookRemove()`,`DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
218816621825SDmitry Karpeev @*/
2189d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2190d71ae5a4SJacob Faibussowitsch {
2191be081cd6SPeter Brune   DMSubDomainHookLink link;
2192be081cd6SPeter Brune   PetscInt            i, l;
219316621825SDmitry Karpeev 
219416621825SDmitry Karpeev   PetscFunctionBegin;
219516621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21969371c9d4SSatish Balay   if (n) {
21974f572ea9SToby Isaac     PetscAssertPointer(n, 2);
21989371c9d4SSatish Balay     *n = 0;
21999371c9d4SSatish Balay   }
22009371c9d4SSatish Balay   if (namelist) {
22014f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
22029371c9d4SSatish Balay     *namelist = NULL;
22039371c9d4SSatish Balay   }
22049371c9d4SSatish Balay   if (innerislist) {
22054f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
22069371c9d4SSatish Balay     *innerislist = NULL;
22079371c9d4SSatish Balay   }
22089371c9d4SSatish Balay   if (outerislist) {
22094f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
22109371c9d4SSatish Balay     *outerislist = NULL;
22119371c9d4SSatish Balay   }
22129371c9d4SSatish Balay   if (dmlist) {
22134f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
22149371c9d4SSatish Balay     *dmlist = NULL;
22159371c9d4SSatish Balay   }
2216f3f0edfdSDmitry Karpeev   /*
2217f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2218f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2219f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2220f3f0edfdSDmitry Karpeev    */
22217a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
222216621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2223dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
222414a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2225f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2226be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2227be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22289566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2229be081cd6SPeter Brune         }
2230648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2231e7c4fc90SDmitry Karpeev       }
223214a18fd3SPeter Brune     }
2233bb7acecfSBarry Smith     if (n) *n = l;
223414a18fd3SPeter Brune   }
22353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2236e30e807fSPeter Brune }
2237e30e807fSPeter Brune 
2238e30e807fSPeter Brune /*@C
2239*73ff1848SBarry Smith   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector for subdomains created with
2240*73ff1848SBarry Smith   `DMCreateDomainDecomposition()`
2241e30e807fSPeter Brune 
224220f4b53cSBarry Smith   Not Collective
2243e30e807fSPeter Brune 
2244e30e807fSPeter Brune   Input Parameters:
2245bb7acecfSBarry Smith + dm     - the `DM` object
2246*73ff1848SBarry Smith . n      - the number of subdomains
2247e30e807fSPeter Brune - subdms - the local subdomains
2248e30e807fSPeter Brune 
2249e30e807fSPeter Brune   Output Parameters:
22506b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2251e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2252e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2253e30e807fSPeter Brune 
225420f4b53cSBarry Smith   Level: developer
225520f4b53cSBarry Smith 
2256bb7acecfSBarry Smith   Note:
2257bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2258e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2259e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2260e30e807fSPeter Brune   solution and residual data.
2261e30e807fSPeter Brune 
2262*73ff1848SBarry Smith   Developer Note:
2263a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2264a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2265bb7acecfSBarry Smith 
22661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2267e30e807fSPeter Brune @*/
2268d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat)
2269d71ae5a4SJacob Faibussowitsch {
2270e30e807fSPeter Brune   PetscFunctionBegin;
2271e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22724f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2273dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2275e7c4fc90SDmitry Karpeev }
2276e7c4fc90SDmitry Karpeev 
227747c6ae99SBarry Smith /*@
2278bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
227947c6ae99SBarry Smith 
228020f4b53cSBarry Smith   Collective
228147c6ae99SBarry Smith 
2282d8d19677SJose E. Roman   Input Parameters:
2283bb7acecfSBarry Smith + dm   - the `DM` object
2284bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
228547c6ae99SBarry Smith 
228647c6ae99SBarry Smith   Output Parameter:
228720f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2288ae0a1c52SMatthew G Knepley 
228920f4b53cSBarry Smith   Options Database Key:
2290412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2291412e9a14SMatthew G. Knepley 
229247c6ae99SBarry Smith   Level: developer
229347c6ae99SBarry Smith 
229420f4b53cSBarry Smith   Note:
229520f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
229620f4b53cSBarry Smith 
2297*73ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
2298*73ff1848SBarry Smith           `DMRefineHookAdd()`, `DMRefineHookRemove()`
229947c6ae99SBarry Smith @*/
2300d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2301d71ae5a4SJacob Faibussowitsch {
2302c833c3b5SJed Brown   DMRefineHookLink link;
230347c6ae99SBarry Smith 
230447c6ae99SBarry Smith   PetscFunctionBegin;
2305732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23069566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2307dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
23084057135bSMatthew G Knepley   if (*dmf) {
230943842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
23108865f1eaSKarl Rupp 
23119566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23128865f1eaSKarl Rupp 
2313644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23140598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2315656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23168865f1eaSKarl Rupp 
23179566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2318c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23191baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2320c833c3b5SJed Brown     }
2321c833c3b5SJed Brown   }
23229566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2324c833c3b5SJed Brown }
2325c833c3b5SJed Brown 
2326bb9467b5SJed Brown /*@C
2327c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2328c833c3b5SJed Brown 
232920f4b53cSBarry Smith   Logically Collective; No Fortran Support
2330c833c3b5SJed Brown 
23314165533cSJose E. Roman   Input Parameters:
2332bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2333bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2334f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
233520f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2336c833c3b5SJed Brown 
233720f4b53cSBarry Smith   Calling sequence of `refinehook`:
2338bb7acecfSBarry Smith + coarse - coarse level `DM`
2339bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2340c833c3b5SJed Brown - ctx    - optional user-defined function context
2341c833c3b5SJed Brown 
234220f4b53cSBarry Smith   Calling sequence of `interphook`:
2343bb7acecfSBarry Smith + coarse - coarse level `DM`
2344c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2345bb7acecfSBarry Smith . fine   - fine level `DM` to update
2346c833c3b5SJed Brown - ctx    - optional user-defined function context
2347c833c3b5SJed Brown 
2348c833c3b5SJed Brown   Level: advanced
2349c833c3b5SJed Brown 
2350c833c3b5SJed Brown   Notes:
2351bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2352bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2353bb7acecfSBarry Smith 
2354bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2355c833c3b5SJed Brown 
2356c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2357c833c3b5SJed Brown 
23581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2359c833c3b5SJed Brown @*/
2360a4e35b19SJacob Faibussowitsch PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM coarse, DM fine, void *ctx), PetscErrorCode (*interphook)(DM coarse, Mat interp, DM fine, void *ctx), void *ctx)
2361d71ae5a4SJacob Faibussowitsch {
2362c833c3b5SJed Brown   DMRefineHookLink link, *p;
2363c833c3b5SJed Brown 
2364c833c3b5SJed Brown   PetscFunctionBegin;
2365c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23663d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23673ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23683d8e3701SJed Brown   }
23699566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2370c833c3b5SJed Brown   link->refinehook = refinehook;
2371c833c3b5SJed Brown   link->interphook = interphook;
2372c833c3b5SJed Brown   link->ctx        = ctx;
23730298fd71SBarry Smith   link->next       = NULL;
2374c833c3b5SJed Brown   *p               = link;
23753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2376c833c3b5SJed Brown }
2377c833c3b5SJed Brown 
23783d8e3701SJed Brown /*@C
2379bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2380bb7acecfSBarry Smith   a nonlinear problem to a finer grid
23813d8e3701SJed Brown 
238220f4b53cSBarry Smith   Logically Collective; No Fortran Support
23833d8e3701SJed Brown 
23844165533cSJose E. Roman   Input Parameters:
2385bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2386bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2387bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
238820f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
23893d8e3701SJed Brown 
23903d8e3701SJed Brown   Level: advanced
23913d8e3701SJed Brown 
2392bb7acecfSBarry Smith   Note:
23933d8e3701SJed Brown   This function does nothing if the hook is not in the list.
23943d8e3701SJed Brown 
23951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
23963d8e3701SJed Brown @*/
2397d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2398d71ae5a4SJacob Faibussowitsch {
23993d8e3701SJed Brown   DMRefineHookLink link, *p;
24003d8e3701SJed Brown 
24013d8e3701SJed Brown   PetscFunctionBegin;
24023d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24033d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
24043d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
24053d8e3701SJed Brown       link = *p;
24063d8e3701SJed Brown       *p   = link->next;
24079566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
24083d8e3701SJed Brown       break;
24093d8e3701SJed Brown     }
24103d8e3701SJed Brown   }
24113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24123d8e3701SJed Brown }
24133d8e3701SJed Brown 
2414c833c3b5SJed Brown /*@
2415bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2416c833c3b5SJed Brown 
2417c833c3b5SJed Brown   Collective if any hooks are
2418c833c3b5SJed Brown 
24194165533cSJose E. Roman   Input Parameters:
2420bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2421bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2422bb7acecfSBarry Smith - fine   - finer `DM` to update
2423c833c3b5SJed Brown 
2424c833c3b5SJed Brown   Level: developer
2425c833c3b5SJed Brown 
2426*73ff1848SBarry Smith   Developer Note:
2427bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2428bb7acecfSBarry Smith   an API with consistent terminology.
2429bb7acecfSBarry Smith 
24301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2431c833c3b5SJed Brown @*/
2432d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2433d71ae5a4SJacob Faibussowitsch {
2434c833c3b5SJed Brown   DMRefineHookLink link;
2435c833c3b5SJed Brown 
2436c833c3b5SJed Brown   PetscFunctionBegin;
2437c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24381baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24394057135bSMatthew G Knepley   }
24403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
244147c6ae99SBarry Smith }
244247c6ae99SBarry Smith 
2443eb3f98d2SBarry Smith /*@
24441f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24451f3379b2SToby Isaac 
244620f4b53cSBarry Smith   Collective
24471f3379b2SToby Isaac 
24484165533cSJose E. Roman   Input Parameters:
2449bb7acecfSBarry Smith + coarse    - coarse `DM`
2450bb7acecfSBarry Smith . fine      - fine `DM`
2451bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2452bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2453bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24541f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
24551f3379b2SToby Isaac 
24564165533cSJose E. Roman   Output Parameter:
24571f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
24581f3379b2SToby Isaac 
24591f3379b2SToby Isaac   Level: developer
24601f3379b2SToby Isaac 
2461bb7acecfSBarry Smith   Note:
2462bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
24631f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24641f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24651f3379b2SToby Isaac   slope-limiting reconstruction.
24661f3379b2SToby Isaac 
2467*73ff1848SBarry Smith   Developer Note:
2468bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2469bb7acecfSBarry Smith 
24701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24711f3379b2SToby Isaac @*/
2472d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2473d71ae5a4SJacob Faibussowitsch {
24741f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24751f3379b2SToby Isaac 
24761f3379b2SToby Isaac   PetscFunctionBegin;
24771f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24781f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24791f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24801f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24811f3379b2SToby Isaac 
24829566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24831f3379b2SToby Isaac   if (interpsol) {
24849566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24851f3379b2SToby Isaac   } else if (interp) {
24869566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
248798921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24891f3379b2SToby Isaac }
24901f3379b2SToby Isaac 
24911f3379b2SToby Isaac /*@
2492bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2493eb3f98d2SBarry Smith 
2494eb3f98d2SBarry Smith   Not Collective
2495eb3f98d2SBarry Smith 
2496eb3f98d2SBarry Smith   Input Parameter:
2497bb7acecfSBarry Smith . dm - the `DM` object
2498eb3f98d2SBarry Smith 
2499eb3f98d2SBarry Smith   Output Parameter:
2500eb3f98d2SBarry Smith . level - number of refinements
2501eb3f98d2SBarry Smith 
2502eb3f98d2SBarry Smith   Level: developer
2503eb3f98d2SBarry Smith 
2504bb7acecfSBarry Smith   Note:
2505bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2506bb7acecfSBarry Smith 
25071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2508eb3f98d2SBarry Smith @*/
2509d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2510d71ae5a4SJacob Faibussowitsch {
2511eb3f98d2SBarry Smith   PetscFunctionBegin;
2512eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2513eb3f98d2SBarry Smith   *level = dm->levelup;
25143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2515eb3f98d2SBarry Smith }
2516eb3f98d2SBarry Smith 
2517fef3a512SBarry Smith /*@
2518bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2519fef3a512SBarry Smith 
2520fef3a512SBarry Smith   Not Collective
2521fef3a512SBarry Smith 
2522d8d19677SJose E. Roman   Input Parameters:
2523bb7acecfSBarry Smith + dm    - the `DM` object
2524fef3a512SBarry Smith - level - number of refinements
2525fef3a512SBarry Smith 
2526fef3a512SBarry Smith   Level: advanced
2527fef3a512SBarry Smith 
252895452b02SPatrick Sanan   Notes:
2529bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2530fef3a512SBarry Smith 
2531bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2532bb7acecfSBarry Smith 
25331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2534fef3a512SBarry Smith @*/
2535d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2536d71ae5a4SJacob Faibussowitsch {
2537fef3a512SBarry Smith   PetscFunctionBegin;
2538fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2539fef3a512SBarry Smith   dm->levelup = level;
25403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2541fef3a512SBarry Smith }
2542fef3a512SBarry Smith 
2543d410b0cfSMatthew G. Knepley /*@
2544bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2545d410b0cfSMatthew G. Knepley 
254620f4b53cSBarry Smith   Collective
2547d410b0cfSMatthew G. Knepley 
2548f1a722f8SMatthew G. Knepley   Input Parameters:
2549bb7acecfSBarry Smith + dm     - the `DM` object
2550d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2551d410b0cfSMatthew G. Knepley 
2552d410b0cfSMatthew G. Knepley   Output Parameter:
255320f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2554d410b0cfSMatthew G. Knepley 
2555d410b0cfSMatthew G. Knepley   Level: developer
2556d410b0cfSMatthew G. Knepley 
255720f4b53cSBarry Smith   Note:
255820f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
255920f4b53cSBarry Smith 
25601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2561d410b0cfSMatthew G. Knepley @*/
2562d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2563d71ae5a4SJacob Faibussowitsch {
2564d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2565d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2566dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2567d410b0cfSMatthew G. Knepley   if (*dme) {
2568d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25699566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2570d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25719566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2572d410b0cfSMatthew G. Knepley   }
25733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2574d410b0cfSMatthew G. Knepley }
2575d410b0cfSMatthew G. Knepley 
2576d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2577d71ae5a4SJacob Faibussowitsch {
2578ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2579ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25804f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2581ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
25823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2583ca3d3a14SMatthew G. Knepley }
2584ca3d3a14SMatthew G. Knepley 
2585d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2586d71ae5a4SJacob Faibussowitsch {
2587ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2588ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25894f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2590ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
25913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2592ca3d3a14SMatthew G. Knepley }
2593ca3d3a14SMatthew G. Knepley 
2594ca3d3a14SMatthew G. Knepley /*@
2595bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2596ca3d3a14SMatthew G. Knepley 
2597ca3d3a14SMatthew G. Knepley   Input Parameter:
259820f4b53cSBarry Smith . dm - The `DM`
2599ca3d3a14SMatthew G. Knepley 
2600ca3d3a14SMatthew G. Knepley   Output Parameter:
260120f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2602ca3d3a14SMatthew G. Knepley 
2603ca3d3a14SMatthew G. Knepley   Level: developer
2604ca3d3a14SMatthew G. Knepley 
26051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2606ca3d3a14SMatthew G. Knepley @*/
2607d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2608d71ae5a4SJacob Faibussowitsch {
2609ca3d3a14SMatthew G. Knepley   Vec tv;
2610ca3d3a14SMatthew G. Knepley 
2611ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2612ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26134f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
26149566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2615ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2617ca3d3a14SMatthew G. Knepley }
2618ca3d3a14SMatthew G. Knepley 
2619d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2620d71ae5a4SJacob Faibussowitsch {
2621ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2622ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2623ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2624ca3d3a14SMatthew G. Knepley 
2625ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26269566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26279566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26289566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26299566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26309566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26319566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26329566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26339566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2634ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26359566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2636ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2637ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2638ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26399566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2640ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26419566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26429566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2643ca3d3a14SMatthew G. Knepley     }
2644ca3d3a14SMatthew G. Knepley   }
26459566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26469566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26479566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2648ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2649ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26509566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2651ca3d3a14SMatthew G. Knepley       if (dof) {
2652ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2653ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2654ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2655ca3d3a14SMatthew G. Knepley 
2656ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26579566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26589566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26599566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2660ca3d3a14SMatthew G. Knepley       }
2661ca3d3a14SMatthew G. Knepley     }
2662ca3d3a14SMatthew G. Knepley   }
26639566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2665ca3d3a14SMatthew G. Knepley }
2666ca3d3a14SMatthew G. Knepley 
2667d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2668d71ae5a4SJacob Faibussowitsch {
2669ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2670ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2671ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2672ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2673ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2674ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2675ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26769566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2678ca3d3a14SMatthew G. Knepley }
2679ca3d3a14SMatthew G. Knepley 
2680bb9467b5SJed Brown /*@C
2681bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2682baf369e7SPeter Brune 
268320f4b53cSBarry Smith   Logically Collective
2684baf369e7SPeter Brune 
26854165533cSJose E. Roman   Input Parameters:
2686bb7acecfSBarry Smith + dm        - the `DM`
2687bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2688bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
268920f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2690baf369e7SPeter Brune 
269120f4b53cSBarry Smith   Calling sequence of `beginhook`:
2692a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2693baf369e7SPeter Brune . g    - global vector
2694baf369e7SPeter Brune . mode - mode
2695baf369e7SPeter Brune . l    - local vector
2696baf369e7SPeter Brune - ctx  - optional user-defined function context
2697baf369e7SPeter Brune 
269820f4b53cSBarry Smith   Calling sequence of `endhook`:
2699a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2700a4e35b19SJacob Faibussowitsch . g    - global vector
2701a4e35b19SJacob Faibussowitsch . mode - mode
2702a4e35b19SJacob Faibussowitsch . l    - local vector
2703baf369e7SPeter Brune - ctx  - optional user-defined function context
2704baf369e7SPeter Brune 
2705baf369e7SPeter Brune   Level: advanced
2706baf369e7SPeter Brune 
2707bb7acecfSBarry Smith   Note:
2708bb7acecfSBarry 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.
2709bb7acecfSBarry Smith 
27101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2711baf369e7SPeter Brune @*/
2712a4e35b19SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM dm, Vec g, InsertMode mode, Vec l, void *ctx), PetscErrorCode (*endhook)(DM dm, Vec g, InsertMode mode, Vec l, void *ctx), void *ctx)
2713d71ae5a4SJacob Faibussowitsch {
2714baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2715baf369e7SPeter Brune 
2716baf369e7SPeter Brune   PetscFunctionBegin;
2717baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2718baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27199566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2720baf369e7SPeter Brune   link->beginhook = beginhook;
2721baf369e7SPeter Brune   link->endhook   = endhook;
2722baf369e7SPeter Brune   link->ctx       = ctx;
27230298fd71SBarry Smith   link->next      = NULL;
2724baf369e7SPeter Brune   *p              = link;
27253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2726baf369e7SPeter Brune }
2727baf369e7SPeter Brune 
2728d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2729d71ae5a4SJacob Faibussowitsch {
27304c274da1SToby Isaac   Mat          cMat;
273179769bd5SJed Brown   Vec          cVec, cBias;
27324c274da1SToby Isaac   PetscSection section, cSec;
27334c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27344c274da1SToby Isaac 
27354c274da1SToby Isaac   PetscFunctionBegin;
2736a4e35b19SJacob Faibussowitsch   (void)g;
2737a4e35b19SJacob Faibussowitsch   (void)ctx;
27384c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27399566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27404c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27415db9a05bSToby Isaac     PetscInt nRows;
27425db9a05bSToby Isaac 
27439566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27443ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27459566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27469566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27479566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27489566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27504c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27519566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27524c274da1SToby Isaac       if (dof) {
27534c274da1SToby Isaac         PetscScalar *vals;
27549566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27559566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27564c274da1SToby Isaac       }
27574c274da1SToby Isaac     }
27589566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27594c274da1SToby Isaac   }
27603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27614c274da1SToby Isaac }
27624c274da1SToby Isaac 
276347c6ae99SBarry Smith /*@
276401729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
276501729b5cSPatrick Sanan 
276620f4b53cSBarry Smith   Neighbor-wise Collective
276701729b5cSPatrick Sanan 
276801729b5cSPatrick Sanan   Input Parameters:
2769bb7acecfSBarry Smith + dm   - the `DM` object
277001729b5cSPatrick Sanan . g    - the global vector
2771bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
277201729b5cSPatrick Sanan - l    - the local vector
277301729b5cSPatrick Sanan 
277420f4b53cSBarry Smith   Level: beginner
277520f4b53cSBarry Smith 
277601729b5cSPatrick Sanan   Notes:
2777bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2778bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2779bb7acecfSBarry Smith 
2780bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
278101729b5cSPatrick Sanan 
27821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
278360225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2784bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
278501729b5cSPatrick Sanan @*/
2786d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2787d71ae5a4SJacob Faibussowitsch {
278801729b5cSPatrick Sanan   PetscFunctionBegin;
27899566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27909566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
27913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
279201729b5cSPatrick Sanan }
279301729b5cSPatrick Sanan 
279401729b5cSPatrick Sanan /*@
279547c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
279647c6ae99SBarry Smith 
279720f4b53cSBarry Smith   Neighbor-wise Collective
279847c6ae99SBarry Smith 
279947c6ae99SBarry Smith   Input Parameters:
2800bb7acecfSBarry Smith + dm   - the `DM` object
280147c6ae99SBarry Smith . g    - the global vector
2802bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
280347c6ae99SBarry Smith - l    - the local vector
280447c6ae99SBarry Smith 
280501729b5cSPatrick Sanan   Level: intermediate
280647c6ae99SBarry Smith 
2807bb7acecfSBarry Smith   Notes:
2808bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2809bb7acecfSBarry Smith 
2810bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2811bb7acecfSBarry Smith 
2812bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2813bb7acecfSBarry Smith 
2814bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2815bb7acecfSBarry Smith 
281660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
281747c6ae99SBarry Smith @*/
2818d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2819d71ae5a4SJacob Faibussowitsch {
28207128ae9fSMatthew G Knepley   PetscSF                 sf;
2821baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
282247c6ae99SBarry Smith 
282347c6ae99SBarry Smith   PetscFunctionBegin;
2824171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2825baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28261baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2827baf369e7SPeter Brune   }
28289566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28297128ae9fSMatthew G Knepley   if (sf) {
2830ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2831ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2832d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28337128ae9fSMatthew G Knepley 
28347a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28359566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28369566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28379566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28389566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28399566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28407128ae9fSMatthew G Knepley   } else {
2841fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28427128ae9fSMatthew G Knepley   }
28433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
284447c6ae99SBarry Smith }
284547c6ae99SBarry Smith 
284647c6ae99SBarry Smith /*@
284747c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
284847c6ae99SBarry Smith 
284920f4b53cSBarry Smith   Neighbor-wise Collective
285047c6ae99SBarry Smith 
285147c6ae99SBarry Smith   Input Parameters:
2852bb7acecfSBarry Smith + dm   - the `DM` object
285347c6ae99SBarry Smith . g    - the global vector
2854bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
285547c6ae99SBarry Smith - l    - the local vector
285647c6ae99SBarry Smith 
285701729b5cSPatrick Sanan   Level: intermediate
285847c6ae99SBarry Smith 
2859bb7acecfSBarry Smith   Note:
2860bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2861bb7acecfSBarry Smith 
286260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
286347c6ae99SBarry Smith @*/
2864d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2865d71ae5a4SJacob Faibussowitsch {
28667128ae9fSMatthew G Knepley   PetscSF                 sf;
2867ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2868ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2869ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2870baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2871d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
287247c6ae99SBarry Smith 
287347c6ae99SBarry Smith   PetscFunctionBegin;
2874171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28759566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28769566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28777128ae9fSMatthew G Knepley   if (sf) {
28787a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28797128ae9fSMatthew G Knepley 
28809566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28819566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28829566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28839566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28849566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28859566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28867128ae9fSMatthew G Knepley   } else {
2887fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28887128ae9fSMatthew G Knepley   }
28899566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2890baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28919566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2892baf369e7SPeter Brune   }
28933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
289447c6ae99SBarry Smith }
289547c6ae99SBarry Smith 
2896d4d07f1eSToby Isaac /*@C
2897d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2898d4d07f1eSToby Isaac 
289920f4b53cSBarry Smith   Logically Collective
2900d4d07f1eSToby Isaac 
29014165533cSJose E. Roman   Input Parameters:
2902bb7acecfSBarry Smith + dm        - the `DM`
2903bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2904bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
290520f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2906d4d07f1eSToby Isaac 
290720f4b53cSBarry Smith   Calling sequence of `beginhook`:
2908a4e35b19SJacob Faibussowitsch + global - global `DM`
2909d4d07f1eSToby Isaac . l      - local vector
2910d4d07f1eSToby Isaac . mode   - mode
2911d4d07f1eSToby Isaac . g      - global vector
2912d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2913d4d07f1eSToby Isaac 
291420f4b53cSBarry Smith   Calling sequence of `endhook`:
2915bb7acecfSBarry Smith + global - global `DM`
2916d4d07f1eSToby Isaac . l      - local vector
2917d4d07f1eSToby Isaac . mode   - mode
2918d4d07f1eSToby Isaac . g      - global vector
2919d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2920d4d07f1eSToby Isaac 
2921d4d07f1eSToby Isaac   Level: advanced
2922d4d07f1eSToby Isaac 
29231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2924d4d07f1eSToby Isaac @*/
2925a4e35b19SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM global, Vec l, InsertMode mode, Vec g, void *ctx), PetscErrorCode (*endhook)(DM global, Vec l, InsertMode mode, Vec g, void *ctx), void *ctx)
2926d71ae5a4SJacob Faibussowitsch {
2927d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2928d4d07f1eSToby Isaac 
2929d4d07f1eSToby Isaac   PetscFunctionBegin;
2930d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2931d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29329566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2933d4d07f1eSToby Isaac   link->beginhook = beginhook;
2934d4d07f1eSToby Isaac   link->endhook   = endhook;
2935d4d07f1eSToby Isaac   link->ctx       = ctx;
2936d4d07f1eSToby Isaac   link->next      = NULL;
2937d4d07f1eSToby Isaac   *p              = link;
29383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2939d4d07f1eSToby Isaac }
2940d4d07f1eSToby Isaac 
2941d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2942d71ae5a4SJacob Faibussowitsch {
29434c274da1SToby Isaac   PetscFunctionBegin;
2944a4e35b19SJacob Faibussowitsch   (void)g;
2945a4e35b19SJacob Faibussowitsch   (void)ctx;
29464c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29472b6b7d09SToby Isaac   if (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES) {
29482b6b7d09SToby Isaac     Mat          cMat;
29492b6b7d09SToby Isaac     Vec          cVec;
29505db9a05bSToby Isaac     PetscInt     nRows;
29512b6b7d09SToby Isaac     PetscSection section, cSec;
29522b6b7d09SToby Isaac     PetscInt     pStart, pEnd, p, dof;
29532b6b7d09SToby Isaac 
29542b6b7d09SToby Isaac     PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29552b6b7d09SToby Isaac     if (!cMat) PetscFunctionReturn(PETSC_SUCCESS);
29565db9a05bSToby Isaac 
29579566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29583ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29599566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29609566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29619566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29624c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29639566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29644c274da1SToby Isaac       if (dof) {
29654c274da1SToby Isaac         PetscInt     d;
29664c274da1SToby Isaac         PetscScalar *vals;
29679566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29689566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29694c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29704c274da1SToby Isaac          * we just extracted */
2971ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29724c274da1SToby Isaac       }
29734c274da1SToby Isaac     }
29749566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29759566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29764c274da1SToby Isaac   }
29773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29784c274da1SToby Isaac }
297901729b5cSPatrick Sanan /*@
298001729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
298101729b5cSPatrick Sanan 
298220f4b53cSBarry Smith   Neighbor-wise Collective
298301729b5cSPatrick Sanan 
298401729b5cSPatrick Sanan   Input Parameters:
2985bb7acecfSBarry Smith + dm   - the `DM` object
298601729b5cSPatrick Sanan . l    - the local vector
2987bb7acecfSBarry 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.
298801729b5cSPatrick Sanan - g    - the global vector
298901729b5cSPatrick Sanan 
299020f4b53cSBarry Smith   Level: beginner
299120f4b53cSBarry Smith 
299201729b5cSPatrick Sanan   Notes:
299301729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
2994bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
299501729b5cSPatrick Sanan 
2996bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2997bb7acecfSBarry Smith 
2998bb7acecfSBarry 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.
2999bb7acecfSBarry Smith 
3000bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
300101729b5cSPatrick Sanan 
30021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
300301729b5cSPatrick Sanan @*/
3004d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
3005d71ae5a4SJacob Faibussowitsch {
300601729b5cSPatrick Sanan   PetscFunctionBegin;
30079566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
30089566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
30093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
301001729b5cSPatrick Sanan }
30114c274da1SToby Isaac 
301247c6ae99SBarry Smith /*@
301301729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
30149a42bb27SBarry Smith 
301520f4b53cSBarry Smith   Neighbor-wise Collective
30169a42bb27SBarry Smith 
30179a42bb27SBarry Smith   Input Parameters:
3018bb7acecfSBarry Smith + dm   - the `DM` object
3019f6813fd5SJed Brown . l    - the local vector
3020aa624791SPierre Jolivet . mode - if `INSERT_VALUES` then no parallel communication is used, if `ADD_VALUES` then all ghost points from the same base point accumulate into that base point.
30211eb28f2eSBarry Smith - g    - the global vector
30229a42bb27SBarry Smith 
302320f4b53cSBarry Smith   Level: intermediate
302420f4b53cSBarry Smith 
302595452b02SPatrick Sanan   Notes:
3026bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3027bb7acecfSBarry Smith 
3028bb7acecfSBarry 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.
3029bb7acecfSBarry Smith 
3030bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3031bb7acecfSBarry Smith 
3032bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3033bb7acecfSBarry Smith 
3034bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30359a42bb27SBarry Smith 
30361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30379a42bb27SBarry Smith @*/
3038d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3039d71ae5a4SJacob Faibussowitsch {
30407128ae9fSMatthew G Knepley   PetscSF                 sf;
304184330215SMatthew G. Knepley   PetscSection            s, gs;
3042d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3043ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3044ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3045ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3046fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3047d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30489a42bb27SBarry Smith 
30499a42bb27SBarry Smith   PetscFunctionBegin;
3050171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3051d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30521baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3053d4d07f1eSToby Isaac   }
30549566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30559566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30569566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30577128ae9fSMatthew G Knepley   switch (mode) {
30587128ae9fSMatthew G Knepley   case INSERT_VALUES:
30597128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3060d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3061d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3062d71ae5a4SJacob Faibussowitsch     break;
30637128ae9fSMatthew G Knepley   case ADD_VALUES:
30647128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3065d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3066d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3067d71ae5a4SJacob Faibussowitsch     break;
3068d71ae5a4SJacob Faibussowitsch   default:
3069d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30707128ae9fSMatthew G Knepley   }
3071ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30729566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3073ca3d3a14SMatthew G. Knepley     if (transform) {
30749566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30759566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30769566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30779566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3078fa88e482SJed Brown     } else if (isInsert) {
30799566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3080fa88e482SJed Brown     } else {
30819566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3082fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3083ca3d3a14SMatthew G. Knepley     }
3084fa88e482SJed Brown     if (s && isInsert) {
30859566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3086fa88e482SJed Brown     } else {
30879566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3088fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3089fa88e482SJed Brown     }
3090ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30919566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
309284330215SMatthew G. Knepley     } else if (s && isInsert) {
309384330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
309484330215SMatthew G. Knepley 
30959566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
30969566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
30979566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
309884330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3099b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
310084330215SMatthew G. Knepley 
31019566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
31029566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
31039566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
31049566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
31059566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
31069566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3107b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
310803442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
31097a8be351SBarry 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);
3110b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3111b3b16f48SMatthew G. Knepley         if (!gcdof) {
311284330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3113b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3114b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
311584330215SMatthew G. Knepley           const PetscInt *cdofs;
311684330215SMatthew G. Knepley           PetscInt        cind = 0;
311784330215SMatthew G. Knepley 
31189566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3119b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31209371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31219371c9d4SSatish Balay               ++cind;
31229371c9d4SSatish Balay               continue;
31239371c9d4SSatish Balay             }
3124b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
312584330215SMatthew G. Knepley           }
31267a8be351SBarry 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);
312784330215SMatthew G. Knepley       }
3128ca3d3a14SMatthew G. Knepley     }
3129fa88e482SJed Brown     if (g_inplace) {
31309566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3131fa88e482SJed Brown     } else {
31329566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3133fa88e482SJed Brown     }
3134ca3d3a14SMatthew G. Knepley     if (transform) {
31359566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31369566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3137fa88e482SJed Brown     } else if (l_inplace) {
31389566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3139ca3d3a14SMatthew G. Knepley     } else {
31409566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3141ca3d3a14SMatthew G. Knepley     }
31427128ae9fSMatthew G Knepley   } else {
3143fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalbegin, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
31447128ae9fSMatthew G Knepley   }
31453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31469a42bb27SBarry Smith }
31479a42bb27SBarry Smith 
31489a42bb27SBarry Smith /*@
31499a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
315047c6ae99SBarry Smith 
315120f4b53cSBarry Smith   Neighbor-wise Collective
315247c6ae99SBarry Smith 
315347c6ae99SBarry Smith   Input Parameters:
3154bb7acecfSBarry Smith + dm   - the `DM` object
3155f6813fd5SJed Brown . l    - the local vector
3156bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3157f6813fd5SJed Brown - g    - the global vector
315847c6ae99SBarry Smith 
315901729b5cSPatrick Sanan   Level: intermediate
316047c6ae99SBarry Smith 
3161bb7acecfSBarry Smith   Note:
3162bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3163bb7acecfSBarry Smith 
316460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
316547c6ae99SBarry Smith @*/
3166d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3167d71ae5a4SJacob Faibussowitsch {
31687128ae9fSMatthew G Knepley   PetscSF                 sf;
316984330215SMatthew G. Knepley   PetscSection            s;
3170d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3171ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
317247c6ae99SBarry Smith 
317347c6ae99SBarry Smith   PetscFunctionBegin;
3174171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31759566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31769566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31777128ae9fSMatthew G Knepley   switch (mode) {
31787128ae9fSMatthew G Knepley   case INSERT_VALUES:
3179d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3180d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3181d71ae5a4SJacob Faibussowitsch     break;
31827128ae9fSMatthew G Knepley   case ADD_VALUES:
3183d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3184d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3185d71ae5a4SJacob Faibussowitsch     break;
3186d71ae5a4SJacob Faibussowitsch   default:
3187d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31887128ae9fSMatthew G Knepley   }
318984330215SMatthew G. Knepley   if (sf && !isInsert) {
3190ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3191ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3192ca3d3a14SMatthew G. Knepley     Vec                tmpl;
319384330215SMatthew G. Knepley 
31949566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3195ca3d3a14SMatthew G. Knepley     if (transform) {
31969566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31979566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3198ca3d3a14SMatthew G. Knepley     } else {
31999566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3200ca3d3a14SMatthew G. Knepley     }
32019566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
32029566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3203ca3d3a14SMatthew G. Knepley     if (transform) {
32049566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
32059566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3206ca3d3a14SMatthew G. Knepley     } else {
32079566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3208ca3d3a14SMatthew G. Knepley     }
32099566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
321084330215SMatthew G. Knepley   } else if (s && isInsert) {
32117128ae9fSMatthew G Knepley   } else {
3212fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalend, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
32137128ae9fSMatthew G Knepley   }
3214d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32159566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3216d4d07f1eSToby Isaac   }
32173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
321847c6ae99SBarry Smith }
321947c6ae99SBarry Smith 
3220f089877aSRichard Tran Mills /*@
3221a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3222a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3223a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3224f089877aSRichard Tran Mills 
322520f4b53cSBarry Smith   Neighbor-wise Collective
3226f089877aSRichard Tran Mills 
3227f089877aSRichard Tran Mills   Input Parameters:
3228bb7acecfSBarry Smith + dm   - the `DM` object
3229bc0a1609SRichard Tran Mills . g    - the original local vector
3230bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3231f089877aSRichard Tran Mills 
3232bc0a1609SRichard Tran Mills   Output Parameter:
3233bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3234f089877aSRichard Tran Mills 
3235f089877aSRichard Tran Mills   Level: intermediate
3236f089877aSRichard Tran Mills 
3237*73ff1848SBarry Smith   Note:
3238a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3239a4e35b19SJacob Faibussowitsch 
324060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3241f089877aSRichard Tran Mills @*/
3242d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3243d71ae5a4SJacob Faibussowitsch {
3244f089877aSRichard Tran Mills   PetscFunctionBegin;
3245f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32469f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32479f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32489f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3250f089877aSRichard Tran Mills }
3251f089877aSRichard Tran Mills 
3252f089877aSRichard Tran Mills /*@
3253bb7acecfSBarry Smith   DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3254bb7acecfSBarry Smith   points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3255f089877aSRichard Tran Mills 
325620f4b53cSBarry Smith   Neighbor-wise Collective
3257f089877aSRichard Tran Mills 
3258f089877aSRichard Tran Mills   Input Parameters:
325960225df5SJacob Faibussowitsch + dm   - the `DM` object
3260bc0a1609SRichard Tran Mills . g    - the original local vector
3261bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3262f089877aSRichard Tran Mills 
3263bc0a1609SRichard Tran Mills   Output Parameter:
3264bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3265f089877aSRichard Tran Mills 
3266f089877aSRichard Tran Mills   Level: intermediate
3267f089877aSRichard Tran Mills 
326860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3269f089877aSRichard Tran Mills @*/
3270d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3271d71ae5a4SJacob Faibussowitsch {
3272f089877aSRichard Tran Mills   PetscFunctionBegin;
3273f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32749f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32759f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32769f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3278f089877aSRichard Tran Mills }
3279f089877aSRichard Tran Mills 
328047c6ae99SBarry Smith /*@
3281bb7acecfSBarry Smith   DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
328247c6ae99SBarry Smith 
328320f4b53cSBarry Smith   Collective
328447c6ae99SBarry Smith 
3285d8d19677SJose E. Roman   Input Parameters:
3286bb7acecfSBarry Smith + dm   - the `DM` object
328720f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
328847c6ae99SBarry Smith 
328947c6ae99SBarry Smith   Output Parameter:
3290bb7acecfSBarry Smith . dmc - the coarsened `DM`
329147c6ae99SBarry Smith 
329247c6ae99SBarry Smith   Level: developer
329347c6ae99SBarry Smith 
3294*73ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
3295*73ff1848SBarry Smith           `DMCoarsenHookAdd()`, `DMCoarsenHookRemove()`
329647c6ae99SBarry Smith @*/
3297d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3298d71ae5a4SJacob Faibussowitsch {
3299b17ce1afSJed Brown   DMCoarsenHookLink link;
330047c6ae99SBarry Smith 
330147c6ae99SBarry Smith   PetscFunctionBegin;
3302171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33039566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3304dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3305b9d85ea2SLisandro Dalcin   if (*dmc) {
3306a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
33079566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
330843842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
33099566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3310644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
33110598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3312656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
33139566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3314b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33159566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3316b17ce1afSJed Brown     }
3317b9d85ea2SLisandro Dalcin   }
33189566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33197a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3321b17ce1afSJed Brown }
3322b17ce1afSJed Brown 
3323bb9467b5SJed Brown /*@C
3324b17ce1afSJed Brown   DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3325b17ce1afSJed Brown 
332620f4b53cSBarry Smith   Logically Collective; No Fortran Support
3327b17ce1afSJed Brown 
33284165533cSJose E. Roman   Input Parameters:
3329bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3330b17ce1afSJed Brown . coarsenhook  - function to run when setting up a coarser level
3331bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
333220f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3333b17ce1afSJed Brown 
333420f4b53cSBarry Smith   Calling sequence of `coarsenhook`:
3335bb7acecfSBarry Smith + fine   - fine level `DM`
3336bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3337b17ce1afSJed Brown - ctx    - optional user-defined function context
3338b17ce1afSJed Brown 
333920f4b53cSBarry Smith   Calling sequence of `restricthook`:
3340bb7acecfSBarry Smith + fine      - fine level `DM`
3341bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3342c833c3b5SJed Brown . rscale    - scaling vector for restriction
3343c833c3b5SJed Brown . inject    - matrix restricting by injection
3344b17ce1afSJed Brown . coarse    - coarse level DM to update
3345b17ce1afSJed Brown - ctx       - optional user-defined function context
3346b17ce1afSJed Brown 
3347b17ce1afSJed Brown   Level: advanced
3348b17ce1afSJed Brown 
3349b17ce1afSJed Brown   Notes:
3350bb7acecfSBarry 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`.
3351b17ce1afSJed Brown 
3352b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3353b17ce1afSJed Brown 
3354b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3355bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3356b17ce1afSJed Brown 
3357bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3358bb7acecfSBarry Smith 
33591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3360b17ce1afSJed Brown @*/
3361a4e35b19SJacob Faibussowitsch PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM fine, DM coarse, void *ctx), PetscErrorCode (*restricthook)(DM fine, Mat mrestrict, Vec rscale, Mat inject, DM coarse, void *ctx), void *ctx)
3362d71ae5a4SJacob Faibussowitsch {
3363b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3364b17ce1afSJed Brown 
3365b17ce1afSJed Brown   PetscFunctionBegin;
3366b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33671e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33683ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33691e3d8eccSJed Brown   }
33709566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3371b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3372b17ce1afSJed Brown   link->restricthook = restricthook;
3373b17ce1afSJed Brown   link->ctx          = ctx;
33740298fd71SBarry Smith   link->next         = NULL;
3375b17ce1afSJed Brown   *p                 = link;
33763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3377b17ce1afSJed Brown }
3378b17ce1afSJed Brown 
3379dc822a44SJed Brown /*@C
3380bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3381dc822a44SJed Brown 
338220f4b53cSBarry Smith   Logically Collective; No Fortran Support
3383dc822a44SJed Brown 
33844165533cSJose E. Roman   Input Parameters:
3385bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3386dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3387bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
338820f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3389dc822a44SJed Brown 
3390dc822a44SJed Brown   Level: advanced
3391dc822a44SJed Brown 
3392*73ff1848SBarry Smith   Notes:
3393*73ff1848SBarry Smith   This function does nothing if the `coarsenhook` is not in the list.
3394*73ff1848SBarry Smith 
3395*73ff1848SBarry Smith   See `DMCoarsenHookAdd()` for the calling sequence of `coarsenhook` and `restricthook`
3396dc822a44SJed Brown 
33971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3398dc822a44SJed Brown @*/
3399d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3400d71ae5a4SJacob Faibussowitsch {
3401dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3402dc822a44SJed Brown 
3403dc822a44SJed Brown   PetscFunctionBegin;
3404dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3405dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3406dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3407dc822a44SJed Brown       link = *p;
3408dc822a44SJed Brown       *p   = link->next;
34099566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3410dc822a44SJed Brown       break;
3411dc822a44SJed Brown     }
3412dc822a44SJed Brown   }
34133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3414dc822a44SJed Brown }
3415dc822a44SJed Brown 
3416b17ce1afSJed Brown /*@
3417bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3418b17ce1afSJed Brown 
3419b17ce1afSJed Brown   Collective if any hooks are
3420b17ce1afSJed Brown 
34214165533cSJose E. Roman   Input Parameters:
3422bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3423bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3424e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3425bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
342620f4b53cSBarry Smith - coarse  - coarser `DM` to update
3427b17ce1afSJed Brown 
3428b17ce1afSJed Brown   Level: developer
3429b17ce1afSJed Brown 
3430*73ff1848SBarry Smith   Developer Note:
3431bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3432bb7acecfSBarry Smith 
34331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3434b17ce1afSJed Brown @*/
3435d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3436d71ae5a4SJacob Faibussowitsch {
3437b17ce1afSJed Brown   DMCoarsenHookLink link;
3438b17ce1afSJed Brown 
3439b17ce1afSJed Brown   PetscFunctionBegin;
3440b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34411baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3442b17ce1afSJed Brown   }
34433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
344447c6ae99SBarry Smith }
344547c6ae99SBarry Smith 
3446bb9467b5SJed Brown /*@C
3447*73ff1848SBarry Smith   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
34485dbd56e3SPeter Brune 
344920f4b53cSBarry Smith   Logically Collective; No Fortran Support
34505dbd56e3SPeter Brune 
34514165533cSJose E. Roman   Input Parameters:
3452bb7acecfSBarry Smith + global       - global `DM`
3453bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
34545dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
345520f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34565dbd56e3SPeter Brune 
345720f4b53cSBarry Smith   Calling sequence of `ddhook`:
3458bb7acecfSBarry Smith + global - global `DM`
3459*73ff1848SBarry Smith . block  - subdomain `DM`
3460ec4806b8SPeter Brune - ctx    - optional user-defined function context
3461ec4806b8SPeter Brune 
346220f4b53cSBarry Smith   Calling sequence of `restricthook`:
3463bb7acecfSBarry Smith + global - global `DM`
3464*73ff1848SBarry Smith . out    - scatter to the outer (with ghost and overlap points) sub vector
3465*73ff1848SBarry Smith . in     - scatter to sub vector values only owned locally
3466*73ff1848SBarry Smith . block  - subdomain `DM`
34675dbd56e3SPeter Brune - ctx    - optional user-defined function context
34685dbd56e3SPeter Brune 
34695dbd56e3SPeter Brune   Level: advanced
34705dbd56e3SPeter Brune 
34715dbd56e3SPeter Brune   Notes:
3472*73ff1848SBarry Smith   This function can be used if auxiliary data needs to be set up on subdomain `DM`s.
34735dbd56e3SPeter Brune 
34745dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
34755dbd56e3SPeter Brune 
34765dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3477bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
34785dbd56e3SPeter Brune 
3479*73ff1848SBarry Smith   Developer Note:
3480*73ff1848SBarry Smith   It is unclear what "block solve" means within the definition of `restricthook`
3481*73ff1848SBarry Smith 
3482*73ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`, `DMCreateDomainDecomposition()`
34835dbd56e3SPeter Brune @*/
3484a4e35b19SJacob Faibussowitsch PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM global, DM block, void *ctx), PetscErrorCode (*restricthook)(DM global, VecScatter out, VecScatter in, DM block, void *ctx), void *ctx)
3485d71ae5a4SJacob Faibussowitsch {
3486be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34875dbd56e3SPeter Brune 
34885dbd56e3SPeter Brune   PetscFunctionBegin;
34895dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3490b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
34913ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3492b3a6b972SJed Brown   }
34939566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
34945dbd56e3SPeter Brune   link->restricthook = restricthook;
3495be081cd6SPeter Brune   link->ddhook       = ddhook;
34965dbd56e3SPeter Brune   link->ctx          = ctx;
34970298fd71SBarry Smith   link->next         = NULL;
34985dbd56e3SPeter Brune   *p                 = link;
34993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35005dbd56e3SPeter Brune }
35015dbd56e3SPeter Brune 
3502b3a6b972SJed Brown /*@C
3503*73ff1848SBarry Smith   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
3504b3a6b972SJed Brown 
350520f4b53cSBarry Smith   Logically Collective; No Fortran Support
3506b3a6b972SJed Brown 
35074165533cSJose E. Roman   Input Parameters:
3508bb7acecfSBarry Smith + global       - global `DM`
3509bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3510b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
351120f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3512b3a6b972SJed Brown 
3513b3a6b972SJed Brown   Level: advanced
3514b3a6b972SJed Brown 
3515*73ff1848SBarry Smith   Note:
3516*73ff1848SBarry Smith   See `DMSubDomainHookAdd()` for the calling sequences of `ddhook` and `restricthook`
3517*73ff1848SBarry Smith 
3518*73ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`,
3519*73ff1848SBarry Smith           `DMCreateDomainDecomposition()`
3520b3a6b972SJed Brown @*/
3521d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3522d71ae5a4SJacob Faibussowitsch {
3523b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3524b3a6b972SJed Brown 
3525b3a6b972SJed Brown   PetscFunctionBegin;
3526b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3527b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3528b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3529b3a6b972SJed Brown       link = *p;
3530b3a6b972SJed Brown       *p   = link->next;
35319566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3532b3a6b972SJed Brown       break;
3533b3a6b972SJed Brown     }
3534b3a6b972SJed Brown   }
35353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3536b3a6b972SJed Brown }
3537b3a6b972SJed Brown 
35385dbd56e3SPeter Brune /*@
3539*73ff1848SBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a subdomain `DM` by running hooks registered by `DMSubDomainHookAdd()`
35405dbd56e3SPeter Brune 
35415dbd56e3SPeter Brune   Collective if any hooks are
35425dbd56e3SPeter Brune 
35434165533cSJose E. Roman   Input Parameters:
3544a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3545a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3546a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3547a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
35485dbd56e3SPeter Brune 
35495dbd56e3SPeter Brune   Level: developer
35505dbd56e3SPeter Brune 
3551*73ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMCreateDomainDecomposition()`
35525dbd56e3SPeter Brune @*/
3553d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3554d71ae5a4SJacob Faibussowitsch {
3555be081cd6SPeter Brune   DMSubDomainHookLink link;
35565dbd56e3SPeter Brune 
35575dbd56e3SPeter Brune   PetscFunctionBegin;
3558be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35591baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35605dbd56e3SPeter Brune   }
35613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35625dbd56e3SPeter Brune }
35635dbd56e3SPeter Brune 
35645fe1f584SPeter Brune /*@
3565bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35665fe1f584SPeter Brune 
35675fe1f584SPeter Brune   Not Collective
35685fe1f584SPeter Brune 
35695fe1f584SPeter Brune   Input Parameter:
3570bb7acecfSBarry Smith . dm - the `DM` object
35715fe1f584SPeter Brune 
35725fe1f584SPeter Brune   Output Parameter:
35736a7d9d85SPeter Brune . level - number of coarsenings
35745fe1f584SPeter Brune 
35755fe1f584SPeter Brune   Level: developer
35765fe1f584SPeter Brune 
35771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35785fe1f584SPeter Brune @*/
3579d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3580d71ae5a4SJacob Faibussowitsch {
35815fe1f584SPeter Brune   PetscFunctionBegin;
35825fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35834f572ea9SToby Isaac   PetscAssertPointer(level, 2);
35845fe1f584SPeter Brune   *level = dm->leveldown;
35853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35865fe1f584SPeter Brune }
35875fe1f584SPeter Brune 
35889a64c4a8SMatthew G. Knepley /*@
3589bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
35909a64c4a8SMatthew G. Knepley 
359120f4b53cSBarry Smith   Collective
35929a64c4a8SMatthew G. Knepley 
35939a64c4a8SMatthew G. Knepley   Input Parameters:
3594bb7acecfSBarry Smith + dm    - the `DM` object
35959a64c4a8SMatthew G. Knepley - level - number of coarsenings
35969a64c4a8SMatthew G. Knepley 
35979a64c4a8SMatthew G. Knepley   Level: developer
35989a64c4a8SMatthew G. Knepley 
3599bb7acecfSBarry Smith   Note:
3600bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3601bb7acecfSBarry Smith 
360242747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36039a64c4a8SMatthew G. Knepley @*/
3604d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3605d71ae5a4SJacob Faibussowitsch {
36069a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
36079a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36089a64c4a8SMatthew G. Knepley   dm->leveldown = level;
36093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36109a64c4a8SMatthew G. Knepley }
36119a64c4a8SMatthew G. Knepley 
361247c6ae99SBarry Smith /*@C
3613bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
361447c6ae99SBarry Smith 
361520f4b53cSBarry Smith   Collective
361647c6ae99SBarry Smith 
3617d8d19677SJose E. Roman   Input Parameters:
3618bb7acecfSBarry Smith + dm      - the `DM` object
361947c6ae99SBarry Smith - nlevels - the number of levels of refinement
362047c6ae99SBarry Smith 
362147c6ae99SBarry Smith   Output Parameter:
3622bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
362347c6ae99SBarry Smith 
362447c6ae99SBarry Smith   Level: developer
362547c6ae99SBarry Smith 
36261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
362747c6ae99SBarry Smith @*/
3628d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3629d71ae5a4SJacob Faibussowitsch {
363047c6ae99SBarry Smith   PetscFunctionBegin;
3631171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36327a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36333ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36344f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
3635213acdd3SPierre Jolivet   if (dm->ops->refine && !dm->ops->refinehierarchy) {
363647c6ae99SBarry Smith     PetscInt i;
363747c6ae99SBarry Smith 
36389566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
363948a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3640213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
36413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
364247c6ae99SBarry Smith }
364347c6ae99SBarry Smith 
364447c6ae99SBarry Smith /*@C
3645bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
364647c6ae99SBarry Smith 
364720f4b53cSBarry Smith   Collective
364847c6ae99SBarry Smith 
3649d8d19677SJose E. Roman   Input Parameters:
3650bb7acecfSBarry Smith + dm      - the `DM` object
365147c6ae99SBarry Smith - nlevels - the number of levels of coarsening
365247c6ae99SBarry Smith 
365347c6ae99SBarry Smith   Output Parameter:
3654bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
365547c6ae99SBarry Smith 
365647c6ae99SBarry Smith   Level: developer
365747c6ae99SBarry Smith 
36581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
365947c6ae99SBarry Smith @*/
3660d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3661d71ae5a4SJacob Faibussowitsch {
366247c6ae99SBarry Smith   PetscFunctionBegin;
3663171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36647a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36653ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36664f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
3667213acdd3SPierre Jolivet   if (dm->ops->coarsen && !dm->ops->coarsenhierarchy) {
366847c6ae99SBarry Smith     PetscInt i;
366947c6ae99SBarry Smith 
36709566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
367148a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3672213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
36733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
367447c6ae99SBarry Smith }
367547c6ae99SBarry Smith 
36761a266240SBarry Smith /*@C
3677bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36781a266240SBarry Smith 
3679bb7acecfSBarry Smith   Logically Collective if the function is collective
36801a266240SBarry Smith 
36811a266240SBarry Smith   Input Parameters:
3682bb7acecfSBarry Smith + dm      - the `DM` object
36831a266240SBarry Smith - destroy - the destroy function
36841a266240SBarry Smith 
36851a266240SBarry Smith   Level: intermediate
36861a266240SBarry Smith 
36871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3688f07f9ceaSJed Brown @*/
3689d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3690d71ae5a4SJacob Faibussowitsch {
36911a266240SBarry Smith   PetscFunctionBegin;
3692171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36931a266240SBarry Smith   dm->ctxdestroy = destroy;
36943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36951a266240SBarry Smith }
36961a266240SBarry Smith 
3697b07ff414SBarry Smith /*@
3698bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
369947c6ae99SBarry Smith 
370047c6ae99SBarry Smith   Not Collective
370147c6ae99SBarry Smith 
370247c6ae99SBarry Smith   Input Parameters:
3703bb7acecfSBarry Smith + dm  - the `DM` object
370447c6ae99SBarry Smith - ctx - the user context
370547c6ae99SBarry Smith 
370647c6ae99SBarry Smith   Level: intermediate
370747c6ae99SBarry Smith 
3708bb7acecfSBarry Smith   Note:
370935cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3710bb7acecfSBarry Smith 
371160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
371247c6ae99SBarry Smith @*/
3713d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3714d71ae5a4SJacob Faibussowitsch {
371547c6ae99SBarry Smith   PetscFunctionBegin;
3716171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
371747c6ae99SBarry Smith   dm->ctx = ctx;
37183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
371947c6ae99SBarry Smith }
372047c6ae99SBarry Smith 
372147c6ae99SBarry Smith /*@
3722bb7acecfSBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object
372347c6ae99SBarry Smith 
372447c6ae99SBarry Smith   Not Collective
372547c6ae99SBarry Smith 
372647c6ae99SBarry Smith   Input Parameter:
3727bb7acecfSBarry Smith . dm - the `DM` object
372847c6ae99SBarry Smith 
372947c6ae99SBarry Smith   Output Parameter:
373047c6ae99SBarry Smith . ctx - the user context
373147c6ae99SBarry Smith 
373247c6ae99SBarry Smith   Level: intermediate
373347c6ae99SBarry Smith 
3734bb7acecfSBarry Smith   Note:
373535cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3736bb7acecfSBarry Smith 
373742747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
373847c6ae99SBarry Smith @*/
3739d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3740d71ae5a4SJacob Faibussowitsch {
374147c6ae99SBarry Smith   PetscFunctionBegin;
3742171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37431b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
374547c6ae99SBarry Smith }
374647c6ae99SBarry Smith 
374708da532bSDmitry Karpeev /*@C
3748bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
374908da532bSDmitry Karpeev 
375020f4b53cSBarry Smith   Logically Collective
375108da532bSDmitry Karpeev 
3752d8d19677SJose E. Roman   Input Parameters:
375308da532bSDmitry Karpeev + dm - the DM object
375420f4b53cSBarry Smith - f  - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
375508da532bSDmitry Karpeev 
375608da532bSDmitry Karpeev   Level: intermediate
375708da532bSDmitry Karpeev 
37581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3759db781477SPatrick Sanan          `DMSetJacobian()`
376008da532bSDmitry Karpeev @*/
3761d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3762d71ae5a4SJacob Faibussowitsch {
376308da532bSDmitry Karpeev   PetscFunctionBegin;
37645a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
376508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
376708da532bSDmitry Karpeev }
376808da532bSDmitry Karpeev 
376908da532bSDmitry Karpeev /*@
3770bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
377108da532bSDmitry Karpeev 
377208da532bSDmitry Karpeev   Not Collective
377308da532bSDmitry Karpeev 
377408da532bSDmitry Karpeev   Input Parameter:
3775bb7acecfSBarry Smith . dm - the `DM` object to destroy
377608da532bSDmitry Karpeev 
377708da532bSDmitry Karpeev   Output Parameter:
3778bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
377908da532bSDmitry Karpeev 
378008da532bSDmitry Karpeev   Level: developer
378108da532bSDmitry Karpeev 
37821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
378308da532bSDmitry Karpeev @*/
3784d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3785d71ae5a4SJacob Faibussowitsch {
378608da532bSDmitry Karpeev   PetscFunctionBegin;
37875a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37884f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
378908da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
37903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
379108da532bSDmitry Karpeev }
379208da532bSDmitry Karpeev 
379308da532bSDmitry Karpeev /*@C
3794bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
379508da532bSDmitry Karpeev 
379620f4b53cSBarry Smith   Logically Collective
379708da532bSDmitry Karpeev 
3798f899ff85SJose E. Roman   Input Parameter:
3799bb7acecfSBarry Smith . dm - the `DM` object
380008da532bSDmitry Karpeev 
380160225df5SJacob Faibussowitsch   Output Parameters:
380208da532bSDmitry Karpeev + xl - lower bound
380308da532bSDmitry Karpeev - xu - upper bound
380408da532bSDmitry Karpeev 
3805907376e6SBarry Smith   Level: advanced
3806907376e6SBarry Smith 
380720f4b53cSBarry Smith   Note:
380895452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
380908da532bSDmitry Karpeev 
38101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
381108da532bSDmitry Karpeev @*/
3812d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3813d71ae5a4SJacob Faibussowitsch {
381408da532bSDmitry Karpeev   PetscFunctionBegin;
38155a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
381608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
38175a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3818dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
382008da532bSDmitry Karpeev }
382108da532bSDmitry Karpeev 
3822b0ae01b7SPeter Brune /*@
3823bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3824b0ae01b7SPeter Brune 
3825b0ae01b7SPeter Brune   Not Collective
3826b0ae01b7SPeter Brune 
3827b0ae01b7SPeter Brune   Input Parameter:
3828b0ae01b7SPeter Brune . dm - the DM object
3829b0ae01b7SPeter Brune 
3830b0ae01b7SPeter Brune   Output Parameter:
3831bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3832b0ae01b7SPeter Brune 
3833b0ae01b7SPeter Brune   Level: developer
3834b0ae01b7SPeter Brune 
38351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3836b0ae01b7SPeter Brune @*/
3837d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3838d71ae5a4SJacob Faibussowitsch {
3839b0ae01b7SPeter Brune   PetscFunctionBegin;
38405a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38414f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3842b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3844b0ae01b7SPeter Brune }
3845b0ae01b7SPeter Brune 
38463ad4599aSBarry Smith /*@
3847bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38483ad4599aSBarry Smith 
38493ad4599aSBarry Smith   Not Collective
38503ad4599aSBarry Smith 
38513ad4599aSBarry Smith   Input Parameter:
3852bb7acecfSBarry Smith . dm - the `DM` object
38533ad4599aSBarry Smith 
38543ad4599aSBarry Smith   Output Parameter:
3855bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38563ad4599aSBarry Smith 
38573ad4599aSBarry Smith   Level: developer
38583ad4599aSBarry Smith 
38591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38603ad4599aSBarry Smith @*/
3861d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3862d71ae5a4SJacob Faibussowitsch {
38633ad4599aSBarry Smith   PetscFunctionBegin;
38645a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38654f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
38663ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38683ad4599aSBarry Smith }
38693ad4599aSBarry Smith 
3870a7058e45SLawrence Mitchell /*@
3871bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3872a7058e45SLawrence Mitchell 
3873a7058e45SLawrence Mitchell   Not Collective
3874a7058e45SLawrence Mitchell 
3875a7058e45SLawrence Mitchell   Input Parameter:
3876bb7acecfSBarry Smith . dm - the `DM` object
3877a7058e45SLawrence Mitchell 
3878a7058e45SLawrence Mitchell   Output Parameter:
3879bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3880a7058e45SLawrence Mitchell 
3881a7058e45SLawrence Mitchell   Level: developer
3882a7058e45SLawrence Mitchell 
38831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3884a7058e45SLawrence Mitchell @*/
3885d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3886d71ae5a4SJacob Faibussowitsch {
3887a7058e45SLawrence Mitchell   PetscFunctionBegin;
38885a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38894f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3890dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3891ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
38923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3893a7058e45SLawrence Mitchell }
3894a7058e45SLawrence Mitchell 
38950298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3896264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3897264ace61SBarry Smith 
3898264ace61SBarry Smith /*@C
3899bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3900264ace61SBarry Smith 
390120f4b53cSBarry Smith   Collective
3902264ace61SBarry Smith 
3903264ace61SBarry Smith   Input Parameters:
3904bb7acecfSBarry Smith + dm     - The `DM` object
3905bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3906264ace61SBarry Smith 
3907264ace61SBarry Smith   Options Database Key:
3908bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3909264ace61SBarry Smith 
3910264ace61SBarry Smith   Level: intermediate
3911264ace61SBarry Smith 
3912bb7acecfSBarry Smith   Note:
39130462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3914bb7acecfSBarry Smith 
39151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3916264ace61SBarry Smith @*/
3917d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3918d71ae5a4SJacob Faibussowitsch {
3919264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3920264ace61SBarry Smith   PetscBool match;
3921264ace61SBarry Smith 
3922264ace61SBarry Smith   PetscFunctionBegin;
3923264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39249566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39253ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3926264ace61SBarry Smith 
39279566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39289566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39297a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3930264ace61SBarry Smith 
3931dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39329566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39339566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39349566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3936264ace61SBarry Smith }
3937264ace61SBarry Smith 
3938264ace61SBarry Smith /*@C
3939bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3940264ace61SBarry Smith 
3941264ace61SBarry Smith   Not Collective
3942264ace61SBarry Smith 
3943264ace61SBarry Smith   Input Parameter:
3944bb7acecfSBarry Smith . dm - The `DM`
3945264ace61SBarry Smith 
3946264ace61SBarry Smith   Output Parameter:
3947bb7acecfSBarry Smith . type - The `DMType` name
3948264ace61SBarry Smith 
3949264ace61SBarry Smith   Level: intermediate
3950264ace61SBarry Smith 
39511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3952264ace61SBarry Smith @*/
3953d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3954d71ae5a4SJacob Faibussowitsch {
3955264ace61SBarry Smith   PetscFunctionBegin;
3956264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39574f572ea9SToby Isaac   PetscAssertPointer(type, 2);
39589566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3959264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3961264ace61SBarry Smith }
3962264ace61SBarry Smith 
396367a56275SMatthew G Knepley /*@C
3964bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
396567a56275SMatthew G Knepley 
396620f4b53cSBarry Smith   Collective
396767a56275SMatthew G Knepley 
396867a56275SMatthew G Knepley   Input Parameters:
3969bb7acecfSBarry Smith + dm      - the `DM`
3970bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
397167a56275SMatthew G Knepley 
397267a56275SMatthew G Knepley   Output Parameter:
3973bb7acecfSBarry Smith . M - pointer to new `DM`
397467a56275SMatthew G Knepley 
397520f4b53cSBarry Smith   Level: intermediate
397620f4b53cSBarry Smith 
3977*73ff1848SBarry Smith   Note:
3978bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3979bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3980bb7acecfSBarry Smith   of the input `DM`.
398167a56275SMatthew G Knepley 
39821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
398367a56275SMatthew G Knepley @*/
3984d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3985d71ae5a4SJacob Faibussowitsch {
398667a56275SMatthew G Knepley   DM        B;
398767a56275SMatthew G Knepley   char      convname[256];
3988c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
398967a56275SMatthew G Knepley 
399067a56275SMatthew G Knepley   PetscFunctionBegin;
399167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
399267a56275SMatthew G Knepley   PetscValidType(dm, 1);
39934f572ea9SToby Isaac   PetscAssertPointer(M, 3);
39949566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
39959566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3996c067b6caSMatthew G. Knepley   if (sametype) {
3997c067b6caSMatthew G. Knepley     *M = dm;
39989566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
39993ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
4000c067b6caSMatthew G. Knepley   } else {
40010298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
400267a56275SMatthew G Knepley 
400367a56275SMatthew G Knepley     /*
400467a56275SMatthew G Knepley        Order of precedence:
400567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
400667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
400767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
400867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
400967a56275SMatthew G Knepley        5) Use a really basic converter.
401067a56275SMatthew G Knepley     */
401167a56275SMatthew G Knepley 
401267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
40139566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40149566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40159566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40169566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40179566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40189566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
401967a56275SMatthew G Knepley     if (conv) goto foundconv;
402067a56275SMatthew G Knepley 
402167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40229566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40239566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40249566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40259566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40269566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40279566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40289566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40299566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
403067a56275SMatthew G Knepley     if (conv) {
40319566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
403267a56275SMatthew G Knepley       goto foundconv;
403367a56275SMatthew G Knepley     }
403467a56275SMatthew G Knepley 
403567a56275SMatthew G Knepley #if 0
403667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
403767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40389566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
403967a56275SMatthew G Knepley     if (conv) goto foundconv;
404067a56275SMatthew G Knepley 
404167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
404267a56275SMatthew G Knepley     if (dm->ops->convert) {
404367a56275SMatthew G Knepley       conv = dm->ops->convert;
404467a56275SMatthew G Knepley     }
404567a56275SMatthew G Knepley     if (conv) goto foundconv;
404667a56275SMatthew G Knepley #endif
404767a56275SMatthew G Knepley 
404867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
404998921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
405067a56275SMatthew G Knepley 
405167a56275SMatthew G Knepley   foundconv:
40529566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40539566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
405412fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
405590b157c4SStefano Zampini     {
40564fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40576858538eSMatthew G. Knepley 
40584fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40594fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4060c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40619566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40629566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40639566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40649566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
406512fa691eSMatthew G. Knepley     }
40669566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
406767a56275SMatthew G Knepley   }
40689566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
407067a56275SMatthew G Knepley }
4071264ace61SBarry Smith 
4072264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4073264ace61SBarry Smith 
4074264ace61SBarry Smith /*@C
4075bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40761c84c290SBarry Smith 
40771c84c290SBarry Smith   Not Collective
40781c84c290SBarry Smith 
40791c84c290SBarry Smith   Input Parameters:
408020f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
408120f4b53cSBarry Smith - function - The creation routine itself
408220f4b53cSBarry Smith 
408320f4b53cSBarry Smith   Level: advanced
40841c84c290SBarry Smith 
4085*73ff1848SBarry Smith   Note:
408620f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
40871c84c290SBarry Smith 
408860225df5SJacob Faibussowitsch   Example Usage:
40891c84c290SBarry Smith .vb
4090bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40911c84c290SBarry Smith .ve
40921c84c290SBarry Smith 
409320f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
40941c84c290SBarry Smith .vb
40951c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
40961c84c290SBarry Smith     DMSetType(DM,"my_da");
40971c84c290SBarry Smith .ve
40981c84c290SBarry Smith   or at runtime via the option
40991c84c290SBarry Smith .vb
41001c84c290SBarry Smith     -da_type my_da
41011c84c290SBarry Smith .ve
4102264ace61SBarry Smith 
41031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4104264ace61SBarry Smith @*/
4105d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4106d71ae5a4SJacob Faibussowitsch {
4107264ace61SBarry Smith   PetscFunctionBegin;
41089566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
41099566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
41103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4111264ace61SBarry Smith }
4112264ace61SBarry Smith 
4113b859378eSBarry Smith /*@C
4114bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4115b859378eSBarry Smith 
411620f4b53cSBarry Smith   Collective
4117b859378eSBarry Smith 
4118b859378eSBarry Smith   Input Parameters:
4119bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4120bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4121bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4122bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4123b859378eSBarry Smith 
4124b859378eSBarry Smith   Level: intermediate
4125b859378eSBarry Smith 
4126b859378eSBarry Smith   Notes:
412755849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4128b859378eSBarry Smith 
4129bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4130bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4131bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4132cd7e8a5eSksagiyam 
41331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4134b859378eSBarry Smith @*/
4135d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4136d71ae5a4SJacob Faibussowitsch {
41379331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4138b859378eSBarry Smith 
4139b859378eSBarry Smith   PetscFunctionBegin;
4140b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4141b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41429566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41439566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41449566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41459566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41469331c7a4SMatthew G. Knepley   if (isbinary) {
41479331c7a4SMatthew G. Knepley     PetscInt classid;
41489331c7a4SMatthew G. Knepley     char     type[256];
4149b859378eSBarry Smith 
41509566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41517a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41529566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41539566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4154dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41559331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4156dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41579331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41589566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4160b859378eSBarry Smith }
4161b859378eSBarry Smith 
41627da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41637da65231SMatthew G Knepley 
41642ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[])
41652ecf6ec3SMatthew G. Knepley {
41662ecf6ec3SMatthew G. Knepley   PetscInt f;
41672ecf6ec3SMatthew G. Knepley 
41682ecf6ec3SMatthew G. Knepley   PetscFunctionBegin;
41692ecf6ec3SMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41702ecf6ec3SMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %" PetscInt_FMT " |\n", x[f]));
41712ecf6ec3SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41722ecf6ec3SMatthew G. Knepley }
41732ecf6ec3SMatthew G. Knepley 
4174d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4175d71ae5a4SJacob Faibussowitsch {
41761d47ebbbSSatish Balay   PetscInt f;
41771b30c384SMatthew G Knepley 
41787da65231SMatthew G Knepley   PetscFunctionBegin;
417963a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
418048a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41827da65231SMatthew G Knepley }
41837da65231SMatthew G Knepley 
41845962854dSMatthew G. Knepley PetscErrorCode DMPrintCellVectorReal(PetscInt c, const char name[], PetscInt len, const PetscReal x[])
41855962854dSMatthew G. Knepley {
41865962854dSMatthew G. Knepley   PetscInt f;
41875962854dSMatthew G. Knepley 
41885962854dSMatthew G. Knepley   PetscFunctionBegin;
41895962854dSMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41905962854dSMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)x[f]));
41915962854dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41925962854dSMatthew G. Knepley }
41935962854dSMatthew G. Knepley 
4194d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4195d71ae5a4SJacob Faibussowitsch {
41961b30c384SMatthew G Knepley   PetscInt f, g;
41977da65231SMatthew G Knepley 
41987da65231SMatthew G Knepley   PetscFunctionBegin;
419963a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42001d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
42019566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
420248a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
42039566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
42047da65231SMatthew G Knepley   }
42053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42067da65231SMatthew G Knepley }
4207e7c4fc90SDmitry Karpeev 
4208d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4209d71ae5a4SJacob Faibussowitsch {
42100c5b8624SToby Isaac   PetscInt           localSize, bs;
42110c5b8624SToby Isaac   PetscMPIInt        size;
42120c5b8624SToby Isaac   Vec                x, xglob;
42130c5b8624SToby Isaac   const PetscScalar *xarray;
4214e759306cSMatthew G. Knepley 
4215e759306cSMatthew G. Knepley   PetscFunctionBegin;
42169566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
42179566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
42189566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
421993ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
42209566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
42210c5b8624SToby Isaac   if (size > 1) {
42229566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
42239566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
42249566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
42259566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42260c5b8624SToby Isaac   } else {
42270c5b8624SToby Isaac     xglob = x;
42280c5b8624SToby Isaac   }
42299566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42300c5b8624SToby Isaac   if (size > 1) {
42319566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42329566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42330c5b8624SToby Isaac   }
42349566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
42353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4236e759306cSMatthew G. Knepley }
4237e759306cSMatthew G. Knepley 
423888ed4aceSMatthew G Knepley /*@
4239bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4240061576a5SJed Brown 
4241061576a5SJed Brown   Input Parameter:
4242bb7acecfSBarry Smith . dm - The `DM`
4243061576a5SJed Brown 
4244061576a5SJed Brown   Output Parameter:
4245bb7acecfSBarry Smith . section - The `PetscSection`
4246061576a5SJed Brown 
424720f4b53cSBarry Smith   Options Database Key:
4248bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4249061576a5SJed Brown 
4250061576a5SJed Brown   Level: advanced
4251061576a5SJed Brown 
4252061576a5SJed Brown   Notes:
4253bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4254061576a5SJed Brown 
4255bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4256061576a5SJed Brown 
42571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4258061576a5SJed Brown @*/
4259d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4260d71ae5a4SJacob Faibussowitsch {
4261061576a5SJed Brown   PetscFunctionBegin;
42629566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4264061576a5SJed Brown }
4265061576a5SJed Brown 
4266061576a5SJed Brown /*@
4267bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
426888ed4aceSMatthew G Knepley 
426988ed4aceSMatthew G Knepley   Input Parameter:
4270bb7acecfSBarry Smith . dm - The `DM`
427188ed4aceSMatthew G Knepley 
427288ed4aceSMatthew G Knepley   Output Parameter:
4273bb7acecfSBarry Smith . section - The `PetscSection`
427488ed4aceSMatthew G Knepley 
427520f4b53cSBarry Smith   Options Database Key:
4276bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4277e5893cccSMatthew G. Knepley 
427888ed4aceSMatthew G Knepley   Level: intermediate
427988ed4aceSMatthew G Knepley 
4280bb7acecfSBarry Smith   Note:
4281bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
428288ed4aceSMatthew G Knepley 
42831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
428488ed4aceSMatthew G Knepley @*/
4285d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4286d71ae5a4SJacob Faibussowitsch {
428788ed4aceSMatthew G Knepley   PetscFunctionBegin;
428888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42894f572ea9SToby Isaac   PetscAssertPointer(section, 2);
42901bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4291e5e52638SMatthew G. Knepley     PetscInt d;
4292e5e52638SMatthew G. Knepley 
429345480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
429445480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
429545480ffeSMatthew G. Knepley       PetscViewer       viewer;
429645480ffeSMatthew G. Knepley       PetscViewerFormat format;
429745480ffeSMatthew G. Knepley       PetscBool         flg;
429845480ffeSMatthew G. Knepley 
42999566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
43009566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
430145480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
43029566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
43039566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
430445480ffeSMatthew G. Knepley       }
430545480ffeSMatthew G. Knepley       if (flg) {
43069566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
43079566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
4308cd791dc2SBarry Smith         PetscCall(PetscOptionsRestoreViewer(&viewer));
430945480ffeSMatthew G. Knepley       }
431045480ffeSMatthew G. Knepley     }
4311dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
43129566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
43132f0f8703SMatthew G. Knepley   }
43141bb6d2a8SBarry Smith   *section = dm->localSection;
43153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
431688ed4aceSMatthew G Knepley }
431788ed4aceSMatthew G Knepley 
431888ed4aceSMatthew G Knepley /*@
4319bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4320061576a5SJed Brown 
4321061576a5SJed Brown   Input Parameters:
4322bb7acecfSBarry Smith + dm      - The `DM`
4323bb7acecfSBarry Smith - section - The `PetscSection`
4324061576a5SJed Brown 
4325061576a5SJed Brown   Level: advanced
4326061576a5SJed Brown 
4327061576a5SJed Brown   Notes:
4328bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4329061576a5SJed Brown 
4330bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4331061576a5SJed Brown 
43321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4333061576a5SJed Brown @*/
4334d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4335d71ae5a4SJacob Faibussowitsch {
4336061576a5SJed Brown   PetscFunctionBegin;
43379566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
43383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4339061576a5SJed Brown }
4340061576a5SJed Brown 
4341061576a5SJed Brown /*@
4342bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
434388ed4aceSMatthew G Knepley 
434488ed4aceSMatthew G Knepley   Input Parameters:
4345bb7acecfSBarry Smith + dm      - The `DM`
4346bb7acecfSBarry Smith - section - The `PetscSection`
434788ed4aceSMatthew G Knepley 
434888ed4aceSMatthew G Knepley   Level: intermediate
434988ed4aceSMatthew G Knepley 
4350bb7acecfSBarry Smith   Note:
4351bb7acecfSBarry Smith   Any existing Section will be destroyed
435288ed4aceSMatthew G Knepley 
43531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
435488ed4aceSMatthew G Knepley @*/
4355d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4356d71ae5a4SJacob Faibussowitsch {
4357c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4358af122d2aSMatthew G Knepley   PetscInt f;
435988ed4aceSMatthew G Knepley 
436088ed4aceSMatthew G Knepley   PetscFunctionBegin;
436188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4362b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43639566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43649566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43651bb6d2a8SBarry Smith   dm->localSection = section;
43669566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4367af122d2aSMatthew G Knepley   if (numFields) {
43689566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4369af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43700f21e855SMatthew G. Knepley       PetscObject disc;
4371af122d2aSMatthew G Knepley       const char *name;
4372af122d2aSMatthew G Knepley 
43739566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43749566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43759566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4376af122d2aSMatthew G Knepley     }
4377af122d2aSMatthew G Knepley   }
437850350c8eSStefano Zampini   /* The global section and the SectionSF will be rebuilt
437950350c8eSStefano Zampini      in the next call to DMGetGlobalSection() and DMGetSectionSF(). */
43809566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
438150350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
438252947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
438350350c8eSStefano Zampini 
438450350c8eSStefano Zampini   /* Clear scratch vectors */
438550350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
438650350c8eSStefano Zampini   PetscCall(DMClearLocalVectors(dm));
438750350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
438850350c8eSStefano Zampini   PetscCall(DMClearNamedLocalVectors(dm));
43893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
439088ed4aceSMatthew G Knepley }
439188ed4aceSMatthew G Knepley 
4392adc21957SMatthew G. Knepley /*@C
4393adc21957SMatthew G. Knepley   DMCreateSectionPermutation - Create a permutation of the `PetscSection` chart and optionally a blokc structure.
4394adc21957SMatthew G. Knepley 
4395adc21957SMatthew G. Knepley   Input Parameter:
4396adc21957SMatthew G. Knepley . dm - The `DM`
4397adc21957SMatthew G. Knepley 
4398adc21957SMatthew G. Knepley   Output Parameter:
4399adc21957SMatthew G. Knepley + perm        - A permutation of the mesh points in the chart
4400adc21957SMatthew G. Knepley - blockStarts - A high bit is set for the point that begins every block, or NULL for default blocking
4401adc21957SMatthew G. Knepley 
4402adc21957SMatthew G. Knepley   Level: developer
4403adc21957SMatthew G. Knepley 
4404adc21957SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMGetGlobalSection()`
4405adc21957SMatthew G. Knepley @*/
4406adc21957SMatthew G. Knepley PetscErrorCode DMCreateSectionPermutation(DM dm, IS *perm, PetscBT *blockStarts)
4407adc21957SMatthew G. Knepley {
4408adc21957SMatthew G. Knepley   PetscFunctionBegin;
4409adc21957SMatthew G. Knepley   *perm        = NULL;
4410adc21957SMatthew G. Knepley   *blockStarts = NULL;
4411adc21957SMatthew G. Knepley   PetscTryTypeMethod(dm, createsectionpermutation, perm, blockStarts);
4412adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
4413adc21957SMatthew G. Knepley }
4414adc21957SMatthew G. Knepley 
44159435951eSToby Isaac /*@
4416bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
44179435951eSToby Isaac 
441820f4b53cSBarry Smith   not Collective
4419e228b242SToby Isaac 
44209435951eSToby Isaac   Input Parameter:
4421bb7acecfSBarry Smith . dm - The `DM`
44229435951eSToby Isaac 
4423d8d19677SJose E. Roman   Output Parameters:
442420f4b53cSBarry 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.
442520f4b53cSBarry 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.
442679769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
44279435951eSToby Isaac 
44289435951eSToby Isaac   Level: advanced
44299435951eSToby Isaac 
4430bb7acecfSBarry Smith   Note:
4431bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
44329435951eSToby Isaac 
44331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
44349435951eSToby Isaac @*/
4435d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4436d71ae5a4SJacob Faibussowitsch {
44379435951eSToby Isaac   PetscFunctionBegin;
44389435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4439dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44403b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44413b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
444279769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44449435951eSToby Isaac }
44459435951eSToby Isaac 
44469435951eSToby Isaac /*@
4447bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44489435951eSToby Isaac 
444920f4b53cSBarry Smith   Collective
4450e228b242SToby Isaac 
44519435951eSToby Isaac   Input Parameters:
4452bb7acecfSBarry Smith + dm      - The `DM`
4453bb7acecfSBarry 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).
445420f4b53cSBarry 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).
445520f4b53cSBarry 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).
44569435951eSToby Isaac 
44579435951eSToby Isaac   Level: advanced
44589435951eSToby Isaac 
445920f4b53cSBarry Smith   Notes:
446020f4b53cSBarry 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()`.
446120f4b53cSBarry Smith 
446220f4b53cSBarry 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.
446320f4b53cSBarry Smith 
4464bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44659435951eSToby Isaac 
44661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
44679435951eSToby Isaac @*/
4468d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4469d71ae5a4SJacob Faibussowitsch {
4470e228b242SToby Isaac   PetscMPIInt result;
44719435951eSToby Isaac 
44729435951eSToby Isaac   PetscFunctionBegin;
44739435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4474e228b242SToby Isaac   if (section) {
4475e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44769566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44777a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4478e228b242SToby Isaac   }
4479e228b242SToby Isaac   if (mat) {
4480e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44819566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44827a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4483e228b242SToby Isaac   }
448479769bd5SJed Brown   if (bias) {
448579769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44869566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
448779769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
448879769bd5SJed Brown   }
44899566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44909566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44913b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44929566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44939566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44943b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44959566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44969566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
449779769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44999435951eSToby Isaac }
45009435951eSToby Isaac 
4501497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4502507e4973SMatthew G. Knepley /*
4503bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4504507e4973SMatthew G. Knepley 
4505507e4973SMatthew G. Knepley   Input Parameters:
4506bb7acecfSBarry Smith + dm - The `DM`
4507bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4508bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4509507e4973SMatthew G. Knepley 
4510507e4973SMatthew G. Knepley   Level: intermediate
4511507e4973SMatthew G. Knepley 
45121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4513507e4973SMatthew G. Knepley */
4514d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4515d71ae5a4SJacob Faibussowitsch {
4516507e4973SMatthew G. Knepley   MPI_Comm        comm;
4517507e4973SMatthew G. Knepley   PetscLayout     layout;
4518507e4973SMatthew G. Knepley   const PetscInt *ranges;
4519507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4520507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4521507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4522507e4973SMatthew G. Knepley 
4523507e4973SMatthew G. Knepley   PetscFunctionBegin;
45249566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4525507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45269566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
45279566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
45289566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
45299566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
45309566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
45319566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
45329566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
45339566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45349566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4535507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4536f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4537507e4973SMatthew G. Knepley 
45389566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45399566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45409566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45419566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45429566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45439566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4544507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45459371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45469371c9d4SSatish 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));
45479371c9d4SSatish Balay       valid = PETSC_FALSE;
45489371c9d4SSatish Balay     }
45499371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45509371c9d4SSatish 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));
45519371c9d4SSatish Balay       valid = PETSC_FALSE;
45529371c9d4SSatish Balay     }
4553507e4973SMatthew G. Knepley     if (gdof < 0) {
4554507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4555507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4556507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4557507e4973SMatthew G. Knepley 
45589566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4559507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45609371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45619371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45629371c9d4SSatish Balay           valid = PETSC_FALSE;
45639371c9d4SSatish Balay           break;
45649371c9d4SSatish Balay         }
4565507e4973SMatthew G. Knepley       }
4566507e4973SMatthew G. Knepley     }
4567507e4973SMatthew G. Knepley   }
45689566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45699566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
45701c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4571507e4973SMatthew G. Knepley   if (!gvalid) {
45729566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4573507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4574507e4973SMatthew G. Knepley   }
45753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4576507e4973SMatthew G. Knepley }
4577f741bcd2SMatthew G. Knepley #endif
4578507e4973SMatthew G. Knepley 
45795f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45806725e60dSJed Brown {
45816725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45824d86920dSPierre Jolivet 
45836725e60dSJed Brown   PetscFunctionBegin;
45846725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45854f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
45865f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45876725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
45886725e60dSJed Brown   else *sf = dm->sf;
45893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45906725e60dSJed Brown }
45916725e60dSJed Brown 
459288ed4aceSMatthew G Knepley /*@
4593bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
459488ed4aceSMatthew G Knepley 
459520f4b53cSBarry Smith   Collective
45968b1ab98fSJed Brown 
459788ed4aceSMatthew G Knepley   Input Parameter:
4598bb7acecfSBarry Smith . dm - The `DM`
459988ed4aceSMatthew G Knepley 
460088ed4aceSMatthew G Knepley   Output Parameter:
4601bb7acecfSBarry Smith . section - The `PetscSection`
460288ed4aceSMatthew G Knepley 
460388ed4aceSMatthew G Knepley   Level: intermediate
460488ed4aceSMatthew G Knepley 
4605bb7acecfSBarry Smith   Note:
4606bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
460788ed4aceSMatthew G Knepley 
46081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
460988ed4aceSMatthew G Knepley @*/
4610d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4611d71ae5a4SJacob Faibussowitsch {
461288ed4aceSMatthew G Knepley   PetscFunctionBegin;
461388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46144f572ea9SToby Isaac   PetscAssertPointer(section, 2);
46151bb6d2a8SBarry Smith   if (!dm->globalSection) {
4616fd59a867SMatthew G. Knepley     PetscSection s;
46176725e60dSJed Brown     PetscSF      sf;
4618fd59a867SMatthew G. Knepley 
46199566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
46207a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
46217a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
46225f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
4623eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
46249566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
46259566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
46269566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
462788ed4aceSMatthew G Knepley   }
46281bb6d2a8SBarry Smith   *section = dm->globalSection;
46293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
463088ed4aceSMatthew G Knepley }
463188ed4aceSMatthew G Knepley 
4632b21d0597SMatthew G Knepley /*@
4633bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4634b21d0597SMatthew G Knepley 
4635b21d0597SMatthew G Knepley   Input Parameters:
4636bb7acecfSBarry Smith + dm      - The `DM`
46372fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4638b21d0597SMatthew G Knepley 
4639b21d0597SMatthew G Knepley   Level: intermediate
4640b21d0597SMatthew G Knepley 
4641bb7acecfSBarry Smith   Note:
4642bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4643b21d0597SMatthew G Knepley 
46441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4645b21d0597SMatthew G Knepley @*/
4646d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4647d71ae5a4SJacob Faibussowitsch {
4648b21d0597SMatthew G Knepley   PetscFunctionBegin;
4649b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46505080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46519566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46529566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46531bb6d2a8SBarry Smith   dm->globalSection = section;
4654497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46559566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4656507e4973SMatthew G. Knepley #endif
465750350c8eSStefano Zampini   /* Clear global scratch vectors and sectionSF */
465850350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
465952947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
466050350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
466150350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
46623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4663b21d0597SMatthew G Knepley }
4664b21d0597SMatthew G Knepley 
466588ed4aceSMatthew G Knepley /*@
4666bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4667bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
466888ed4aceSMatthew G Knepley 
466988ed4aceSMatthew G Knepley   Input Parameter:
4670bb7acecfSBarry Smith . dm - The `DM`
467188ed4aceSMatthew G Knepley 
467288ed4aceSMatthew G Knepley   Output Parameter:
4673bb7acecfSBarry Smith . sf - The `PetscSF`
467488ed4aceSMatthew G Knepley 
467588ed4aceSMatthew G Knepley   Level: intermediate
467688ed4aceSMatthew G Knepley 
4677bb7acecfSBarry Smith   Note:
4678bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
467988ed4aceSMatthew G Knepley 
46801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
468188ed4aceSMatthew G Knepley @*/
4682d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4683d71ae5a4SJacob Faibussowitsch {
468488ed4aceSMatthew G Knepley   PetscInt nroots;
468588ed4aceSMatthew G Knepley 
468688ed4aceSMatthew G Knepley   PetscFunctionBegin;
468788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46884f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
468948a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
46909566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
469188ed4aceSMatthew G Knepley   if (nroots < 0) {
469288ed4aceSMatthew G Knepley     PetscSection section, gSection;
469388ed4aceSMatthew G Knepley 
46949566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
469531ea6d37SMatthew G Knepley     if (section) {
46969566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
46979566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
469831ea6d37SMatthew G Knepley     } else {
46990298fd71SBarry Smith       *sf = NULL;
47003ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
470131ea6d37SMatthew G Knepley     }
470288ed4aceSMatthew G Knepley   }
47031bb6d2a8SBarry Smith   *sf = dm->sectionSF;
47043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
470588ed4aceSMatthew G Knepley }
470688ed4aceSMatthew G Knepley 
470788ed4aceSMatthew G Knepley /*@
4708bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
470988ed4aceSMatthew G Knepley 
471088ed4aceSMatthew G Knepley   Input Parameters:
4711bb7acecfSBarry Smith + dm - The `DM`
4712bb7acecfSBarry Smith - sf - The `PetscSF`
471388ed4aceSMatthew G Knepley 
471488ed4aceSMatthew G Knepley   Level: intermediate
471588ed4aceSMatthew G Knepley 
4716bb7acecfSBarry Smith   Note:
4717bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
471888ed4aceSMatthew G Knepley 
47191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
472088ed4aceSMatthew G Knepley @*/
4721d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4722d71ae5a4SJacob Faibussowitsch {
472388ed4aceSMatthew G Knepley   PetscFunctionBegin;
472488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4725b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47269566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47279566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
47281bb6d2a8SBarry Smith   dm->sectionSF = sf;
47293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
473088ed4aceSMatthew G Knepley }
473188ed4aceSMatthew G Knepley 
473288ed4aceSMatthew G Knepley /*@C
4733bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
473488ed4aceSMatthew G Knepley   describing the data layout.
473588ed4aceSMatthew G Knepley 
473688ed4aceSMatthew G Knepley   Input Parameters:
4737bb7acecfSBarry Smith + dm            - The `DM`
4738bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4739bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
474088ed4aceSMatthew G Knepley 
47411bb6d2a8SBarry Smith   Level: developer
47421bb6d2a8SBarry Smith 
4743bb7acecfSBarry Smith   Note:
4744bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4745bb7acecfSBarry Smith 
4746*73ff1848SBarry Smith   Developer Note:
4747bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4748bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4749bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
47501bb6d2a8SBarry Smith 
47511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
475288ed4aceSMatthew G Knepley @*/
4753d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4754d71ae5a4SJacob Faibussowitsch {
475588ed4aceSMatthew G Knepley   PetscFunctionBegin;
475688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47579566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
47583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
475988ed4aceSMatthew G Knepley }
4760af122d2aSMatthew G Knepley 
4761b21d0597SMatthew G Knepley /*@
4762bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4763bb7acecfSBarry Smith 
4764bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4765b21d0597SMatthew G Knepley 
4766b21d0597SMatthew G Knepley   Input Parameter:
4767bb7acecfSBarry Smith . dm - The `DM`
4768b21d0597SMatthew G Knepley 
4769b21d0597SMatthew G Knepley   Output Parameter:
4770bb7acecfSBarry Smith . sf - The `PetscSF`
4771b21d0597SMatthew G Knepley 
4772b21d0597SMatthew G Knepley   Level: intermediate
4773b21d0597SMatthew G Knepley 
4774bb7acecfSBarry Smith   Note:
4775bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4776b21d0597SMatthew G Knepley 
47771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4778b21d0597SMatthew G Knepley @*/
4779d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4780d71ae5a4SJacob Faibussowitsch {
4781b21d0597SMatthew G Knepley   PetscFunctionBegin;
4782b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47834f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4784b21d0597SMatthew G Knepley   *sf = dm->sf;
47853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4786b21d0597SMatthew G Knepley }
4787b21d0597SMatthew G Knepley 
4788057b4bcdSMatthew G Knepley /*@
4789bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4790bb7acecfSBarry Smith 
479120f4b53cSBarry Smith   Collective
4792057b4bcdSMatthew G Knepley 
4793057b4bcdSMatthew G Knepley   Input Parameters:
4794bb7acecfSBarry Smith + dm - The `DM`
4795bb7acecfSBarry Smith - sf - The `PetscSF`
4796057b4bcdSMatthew G Knepley 
4797057b4bcdSMatthew G Knepley   Level: intermediate
4798057b4bcdSMatthew G Knepley 
47991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4800057b4bcdSMatthew G Knepley @*/
4801d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4802d71ae5a4SJacob Faibussowitsch {
4803057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4804057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4805b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48069566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48079566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4808057b4bcdSMatthew G Knepley   dm->sf = sf;
48093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4810057b4bcdSMatthew G Knepley }
4811057b4bcdSMatthew G Knepley 
48124f37162bSMatthew G. Knepley /*@
4813bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
48144f37162bSMatthew G. Knepley 
48154f37162bSMatthew G. Knepley   Input Parameter:
4816bb7acecfSBarry Smith . dm - The `DM`
48174f37162bSMatthew G. Knepley 
48184f37162bSMatthew G. Knepley   Output Parameter:
4819bb7acecfSBarry Smith . sf - The `PetscSF`
48204f37162bSMatthew G. Knepley 
48214f37162bSMatthew G. Knepley   Level: intermediate
48224f37162bSMatthew G. Knepley 
4823bb7acecfSBarry Smith   Note:
4824bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
48254f37162bSMatthew G. Knepley 
48261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48274f37162bSMatthew G. Knepley @*/
4828d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4829d71ae5a4SJacob Faibussowitsch {
48304f37162bSMatthew G. Knepley   PetscFunctionBegin;
48314f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48324f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
48334f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
48343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48354f37162bSMatthew G. Knepley }
48364f37162bSMatthew G. Knepley 
48374f37162bSMatthew G. Knepley /*@
48384f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48394f37162bSMatthew G. Knepley 
48404f37162bSMatthew G. Knepley   Input Parameters:
48414f37162bSMatthew G. Knepley + dm - The DM
48424f37162bSMatthew G. Knepley - sf - The PetscSF
48434f37162bSMatthew G. Knepley 
48444f37162bSMatthew G. Knepley   Level: intermediate
48454f37162bSMatthew G. Knepley 
48461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48474f37162bSMatthew G. Knepley @*/
4848d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4849d71ae5a4SJacob Faibussowitsch {
48504f37162bSMatthew G. Knepley   PetscFunctionBegin;
48514f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48524f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48539566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48549566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48554f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48574f37162bSMatthew G. Knepley }
48584f37162bSMatthew G. Knepley 
4859d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4860d71ae5a4SJacob Faibussowitsch {
486134aa8a36SMatthew G. Knepley   PetscClassId id;
486234aa8a36SMatthew G. Knepley 
486334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48649566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
486534aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48669566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
486734aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48689566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
486917c1d62eSMatthew G. Knepley   } else {
48709566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
487134aa8a36SMatthew G. Knepley   }
48723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
487334aa8a36SMatthew G. Knepley }
487434aa8a36SMatthew G. Knepley 
4875d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4876d71ae5a4SJacob Faibussowitsch {
487744a7f3ddSMatthew G. Knepley   RegionField *tmpr;
487844a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
487944a7f3ddSMatthew G. Knepley 
488044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
48813ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
48829566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
488344a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
48849371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
48859371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48869371c9d4SSatish Balay     tmpr[f].label       = NULL;
48879371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
48889371c9d4SSatish Balay   }
48899566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
489044a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
489144a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
48923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
489344a7f3ddSMatthew G. Knepley }
489444a7f3ddSMatthew G. Knepley 
489544a7f3ddSMatthew G. Knepley /*@
489620f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
489744a7f3ddSMatthew G. Knepley 
489820f4b53cSBarry Smith   Logically Collective
489944a7f3ddSMatthew G. Knepley 
490044a7f3ddSMatthew G. Knepley   Input Parameter:
490120f4b53cSBarry Smith . dm - The `DM`
490244a7f3ddSMatthew G. Knepley 
490344a7f3ddSMatthew G. Knepley   Level: intermediate
490444a7f3ddSMatthew G. Knepley 
49051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
490644a7f3ddSMatthew G. Knepley @*/
4907d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4908d71ae5a4SJacob Faibussowitsch {
490944a7f3ddSMatthew G. Knepley   PetscInt f;
491044a7f3ddSMatthew G. Knepley 
491144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
491244a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
491344a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
49149566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
49159566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
491644a7f3ddSMatthew G. Knepley   }
49179566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
491844a7f3ddSMatthew G. Knepley   dm->fields = NULL;
491944a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
49203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
492144a7f3ddSMatthew G. Knepley }
492244a7f3ddSMatthew G. Knepley 
4923689b5837SMatthew G. Knepley /*@
492420f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4925689b5837SMatthew G. Knepley 
492620f4b53cSBarry Smith   Not Collective
4927689b5837SMatthew G. Knepley 
4928689b5837SMatthew G. Knepley   Input Parameter:
492920f4b53cSBarry Smith . dm - The `DM`
4930689b5837SMatthew G. Knepley 
4931689b5837SMatthew G. Knepley   Output Parameter:
493260225df5SJacob Faibussowitsch . numFields - The number of fields
4933689b5837SMatthew G. Knepley 
4934689b5837SMatthew G. Knepley   Level: intermediate
4935689b5837SMatthew G. Knepley 
49361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4937689b5837SMatthew G. Knepley @*/
4938d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4939d71ae5a4SJacob Faibussowitsch {
49400f21e855SMatthew G. Knepley   PetscFunctionBegin;
49410f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49424f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
494344a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
49443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4945af122d2aSMatthew G Knepley }
4946af122d2aSMatthew G Knepley 
4947689b5837SMatthew G. Knepley /*@
494820f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4949689b5837SMatthew G. Knepley 
495020f4b53cSBarry Smith   Logically Collective
4951689b5837SMatthew G. Knepley 
4952689b5837SMatthew G. Knepley   Input Parameters:
495320f4b53cSBarry Smith + dm        - The `DM`
495460225df5SJacob Faibussowitsch - numFields - The number of fields
4955689b5837SMatthew G. Knepley 
4956689b5837SMatthew G. Knepley   Level: intermediate
4957689b5837SMatthew G. Knepley 
49581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4959689b5837SMatthew G. Knepley @*/
4960d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4961d71ae5a4SJacob Faibussowitsch {
49620f21e855SMatthew G. Knepley   PetscInt Nf, f;
4963af122d2aSMatthew G Knepley 
4964af122d2aSMatthew G Knepley   PetscFunctionBegin;
4965af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49669566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49670f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49680f21e855SMatthew G. Knepley     PetscContainer obj;
49690f21e855SMatthew G. Knepley 
49709566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49719566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
49729566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4973af122d2aSMatthew G Knepley   }
49743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4975af122d2aSMatthew G Knepley }
4976af122d2aSMatthew G Knepley 
4977c1929be8SMatthew G. Knepley /*@
4978bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4979c1929be8SMatthew G. Knepley 
498020f4b53cSBarry Smith   Not Collective
4981c1929be8SMatthew G. Knepley 
4982c1929be8SMatthew G. Knepley   Input Parameters:
4983bb7acecfSBarry Smith + dm - The `DM`
4984c1929be8SMatthew G. Knepley - f  - The field number
4985c1929be8SMatthew G. Knepley 
498644a7f3ddSMatthew G. Knepley   Output Parameters:
498720f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
498820f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
4989c1929be8SMatthew G. Knepley 
499044a7f3ddSMatthew G. Knepley   Level: intermediate
4991c1929be8SMatthew G. Knepley 
49921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
4993c1929be8SMatthew G. Knepley @*/
4994d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4995d71ae5a4SJacob Faibussowitsch {
4996af122d2aSMatthew G Knepley   PetscFunctionBegin;
4997af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49984f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
49997a8be351SBarry 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);
500044a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
5001bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
50023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5003decb47aaSMatthew G. Knepley }
5004decb47aaSMatthew G. Knepley 
5005083401c6SMatthew G. Knepley /* Does not clear the DS */
5006d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5007d71ae5a4SJacob Faibussowitsch {
5008083401c6SMatthew G. Knepley   PetscFunctionBegin;
50099566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
50109566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
50119566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
5012083401c6SMatthew G. Knepley   dm->fields[f].label = label;
5013bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
50149566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5015bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
50163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5017083401c6SMatthew G. Knepley }
5018083401c6SMatthew G. Knepley 
501946560f82SMatthew G. Knepley /*@C
5020bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
5021bb7acecfSBarry Smith   the field numbering.
5022c1929be8SMatthew G. Knepley 
502320f4b53cSBarry Smith   Logically Collective
5024c1929be8SMatthew G. Knepley 
5025c1929be8SMatthew G. Knepley   Input Parameters:
5026bb7acecfSBarry Smith + dm    - The `DM`
5027c1929be8SMatthew G. Knepley . f     - The field number
502820f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5029bb7acecfSBarry Smith - disc  - The discretization object
5030c1929be8SMatthew G. Knepley 
503144a7f3ddSMatthew G. Knepley   Level: intermediate
5032c1929be8SMatthew G. Knepley 
50331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
5034c1929be8SMatthew G. Knepley @*/
5035d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5036d71ae5a4SJacob Faibussowitsch {
5037decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5038decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5039e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
5040bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50417a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5042bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5043bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50449566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
504644a7f3ddSMatthew G. Knepley }
504744a7f3ddSMatthew G. Knepley 
504846560f82SMatthew G. Knepley /*@C
5049bb7acecfSBarry 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)
5050bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
505144a7f3ddSMatthew G. Knepley 
505220f4b53cSBarry Smith   Logically Collective
505344a7f3ddSMatthew G. Knepley 
505444a7f3ddSMatthew G. Knepley   Input Parameters:
5055bb7acecfSBarry Smith + dm    - The `DM`
505620f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5057bb7acecfSBarry Smith - disc  - The discretization object
505844a7f3ddSMatthew G. Knepley 
505944a7f3ddSMatthew G. Knepley   Level: intermediate
506044a7f3ddSMatthew G. Knepley 
5061bb7acecfSBarry Smith   Notes:
5062bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5063bb7acecfSBarry Smith 
5064da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5065bb7acecfSBarry 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
5066bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5067bb7acecfSBarry Smith 
50681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
506944a7f3ddSMatthew G. Knepley @*/
5070d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5071d71ae5a4SJacob Faibussowitsch {
507244a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
507344a7f3ddSMatthew G. Knepley 
507444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
507544a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5076064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5077bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50789566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
507944a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5080bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50819566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5082bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5083bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50849566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5086af122d2aSMatthew G Knepley }
50876636e97aSMatthew G Knepley 
5088e5e52638SMatthew G. Knepley /*@
5089e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5090e0b68406SMatthew Knepley 
509120f4b53cSBarry Smith   Logically Collective
5092e0b68406SMatthew Knepley 
5093e0b68406SMatthew Knepley   Input Parameters:
5094bb7acecfSBarry Smith + dm          - The `DM`
5095e0b68406SMatthew Knepley . f           - The field index
5096bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5097e0b68406SMatthew Knepley 
5098e0b68406SMatthew Knepley   Level: intermediate
5099e0b68406SMatthew Knepley 
51001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5101e0b68406SMatthew Knepley @*/
5102d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5103d71ae5a4SJacob Faibussowitsch {
5104e0b68406SMatthew Knepley   PetscFunctionBegin;
510563a3b9bcSJacob 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);
5106e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
51073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5108e0b68406SMatthew Knepley }
5109e0b68406SMatthew Knepley 
5110e0b68406SMatthew Knepley /*@
5111e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5112e0b68406SMatthew Knepley 
511320f4b53cSBarry Smith   Not Collective
5114e0b68406SMatthew Knepley 
5115e0b68406SMatthew Knepley   Input Parameters:
5116bb7acecfSBarry Smith + dm - The `DM`
5117e0b68406SMatthew Knepley - f  - The field index
5118e0b68406SMatthew Knepley 
5119e0b68406SMatthew Knepley   Output Parameter:
5120e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5121e0b68406SMatthew Knepley 
5122e0b68406SMatthew Knepley   Level: intermediate
5123e0b68406SMatthew Knepley 
512460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5125e0b68406SMatthew Knepley @*/
5126d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5127d71ae5a4SJacob Faibussowitsch {
5128e0b68406SMatthew Knepley   PetscFunctionBegin;
512963a3b9bcSJacob 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);
5130e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
51313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5132e0b68406SMatthew Knepley }
5133e0b68406SMatthew Knepley 
5134e0b68406SMatthew Knepley /*@
5135bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5136e5e52638SMatthew G. Knepley 
513720f4b53cSBarry Smith   Collective
5138e5e52638SMatthew G. Knepley 
5139e5e52638SMatthew G. Knepley   Input Parameter:
5140bb7acecfSBarry Smith . dm - The `DM`
5141e5e52638SMatthew G. Knepley 
5142e5e52638SMatthew G. Knepley   Output Parameter:
5143bb7acecfSBarry Smith . newdm - The `DM`
5144e5e52638SMatthew G. Knepley 
5145e5e52638SMatthew G. Knepley   Level: advanced
5146e5e52638SMatthew G. Knepley 
51471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5148e5e52638SMatthew G. Knepley @*/
5149d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5150d71ae5a4SJacob Faibussowitsch {
5151e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5152e5e52638SMatthew G. Knepley 
5153e5e52638SMatthew G. Knepley   PetscFunctionBegin;
51543ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
51559566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
51569566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5157e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5158e5e52638SMatthew G. Knepley     DMLabel     label;
5159e5e52638SMatthew G. Knepley     PetscObject field;
516034aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5161e5e52638SMatthew G. Knepley 
51629566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
51639566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
51649566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
51659566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
516634aa8a36SMatthew G. Knepley   }
51673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
516834aa8a36SMatthew G. Knepley }
516934aa8a36SMatthew G. Knepley 
517034aa8a36SMatthew G. Knepley /*@
517134aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
517234aa8a36SMatthew G. Knepley 
517320f4b53cSBarry Smith   Not Collective
517434aa8a36SMatthew G. Knepley 
517534aa8a36SMatthew G. Knepley   Input Parameters:
517620f4b53cSBarry Smith + dm - The `DM` object
517720f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
517834aa8a36SMatthew G. Knepley 
5179d8d19677SJose E. Roman   Output Parameters:
518034aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
518134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
518234aa8a36SMatthew G. Knepley 
518334aa8a36SMatthew G. Knepley   Level: developer
518434aa8a36SMatthew G. Knepley 
518520f4b53cSBarry Smith   Notes:
518620f4b53cSBarry Smith .vb
518720f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
518820f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
518920f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
519020f4b53cSBarry Smith .ve
519120f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
519220f4b53cSBarry Smith 
51931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
519434aa8a36SMatthew G. Knepley @*/
5195d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5196d71ae5a4SJacob Faibussowitsch {
519734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
519834aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51994f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
52004f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
520134aa8a36SMatthew G. Knepley   if (f < 0) {
520234aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
520334aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
520434aa8a36SMatthew G. Knepley   } else {
520534aa8a36SMatthew G. Knepley     PetscInt Nf;
520634aa8a36SMatthew G. Knepley 
52079566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52087a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
520934aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
521034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
521134aa8a36SMatthew G. Knepley   }
52123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
521334aa8a36SMatthew G. Knepley }
521434aa8a36SMatthew G. Knepley 
521534aa8a36SMatthew G. Knepley /*@
521634aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
521734aa8a36SMatthew G. Knepley 
521820f4b53cSBarry Smith   Not Collective
521934aa8a36SMatthew G. Knepley 
522034aa8a36SMatthew G. Knepley   Input Parameters:
522120f4b53cSBarry Smith + dm         - The `DM` object
522234aa8a36SMatthew G. Knepley . f          - The field number
522334aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
522434aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
522534aa8a36SMatthew G. Knepley 
522634aa8a36SMatthew G. Knepley   Level: developer
522734aa8a36SMatthew G. Knepley 
522820f4b53cSBarry Smith   Notes:
522920f4b53cSBarry Smith .vb
523020f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
523120f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
523220f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
523320f4b53cSBarry Smith .ve
523420f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
523520f4b53cSBarry Smith 
52361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
523734aa8a36SMatthew G. Knepley @*/
5238d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5239d71ae5a4SJacob Faibussowitsch {
524034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
524134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
524234aa8a36SMatthew G. Knepley   if (f < 0) {
524334aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
524434aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
524534aa8a36SMatthew G. Knepley   } else {
524634aa8a36SMatthew G. Knepley     PetscInt Nf;
524734aa8a36SMatthew G. Knepley 
52489566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52497a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
525034aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
525134aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5252e5e52638SMatthew G. Knepley   }
52533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5254e5e52638SMatthew G. Knepley }
5255e5e52638SMatthew G. Knepley 
5256b0441da4SMatthew G. Knepley /*@
5257b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5258b0441da4SMatthew G. Knepley 
5259b0441da4SMatthew G. Knepley   Not collective
5260b0441da4SMatthew G. Knepley 
5261f899ff85SJose E. Roman   Input Parameter:
526220f4b53cSBarry Smith . dm - The `DM` object
5263b0441da4SMatthew G. Knepley 
5264d8d19677SJose E. Roman   Output Parameters:
5265b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5266b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5267b0441da4SMatthew G. Knepley 
5268b0441da4SMatthew G. Knepley   Level: developer
5269b0441da4SMatthew G. Knepley 
527020f4b53cSBarry Smith   Notes:
527120f4b53cSBarry Smith .vb
527220f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
527320f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
527420f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
527520f4b53cSBarry Smith .ve
527620f4b53cSBarry Smith 
52771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5278b0441da4SMatthew G. Knepley @*/
5279d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5280d71ae5a4SJacob Faibussowitsch {
5281b0441da4SMatthew G. Knepley   PetscInt Nf;
5282b0441da4SMatthew G. Knepley 
5283b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5284b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52854f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
52864f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
52879566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5288b0441da4SMatthew G. Knepley   if (!Nf) {
52899566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5290b0441da4SMatthew G. Knepley   } else {
52919566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5292b0441da4SMatthew G. Knepley   }
52933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5294b0441da4SMatthew G. Knepley }
5295b0441da4SMatthew G. Knepley 
5296b0441da4SMatthew G. Knepley /*@
5297b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5298b0441da4SMatthew G. Knepley 
529920f4b53cSBarry Smith   Not Collective
5300b0441da4SMatthew G. Knepley 
5301b0441da4SMatthew G. Knepley   Input Parameters:
530220f4b53cSBarry Smith + dm         - The `DM` object
5303b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5304b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5305b0441da4SMatthew G. Knepley 
5306b0441da4SMatthew G. Knepley   Level: developer
5307b0441da4SMatthew G. Knepley 
530820f4b53cSBarry Smith   Notes:
530920f4b53cSBarry Smith .vb
531020f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
531120f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
531220f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
531320f4b53cSBarry Smith .ve
531420f4b53cSBarry Smith 
53151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5316b0441da4SMatthew G. Knepley @*/
5317d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5318d71ae5a4SJacob Faibussowitsch {
5319b0441da4SMatthew G. Knepley   PetscInt Nf;
5320b0441da4SMatthew G. Knepley 
5321b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5322b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53239566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5324b0441da4SMatthew G. Knepley   if (!Nf) {
53259566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5326b0441da4SMatthew G. Knepley   } else {
53279566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5328e5e52638SMatthew G. Knepley   }
53293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5330e5e52638SMatthew G. Knepley }
5331e5e52638SMatthew G. Knepley 
5332d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5333d71ae5a4SJacob Faibussowitsch {
5334799db056SMatthew G. Knepley   DM           plex;
5335799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5336799db056SMatthew G. Knepley   const char **names;
5337799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5338799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5339799db056SMatthew G. Knepley   size_t       len;
5340799db056SMatthew G. Knepley   MPI_Comm     comm;
5341799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5342783e2ec8SMatthew G. Knepley 
5343783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5344799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5345799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5346799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5347799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5348799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5349799db056SMatthew G. Knepley     PetscDS  dsBC;
5350799db056SMatthew G. Knepley     PetscInt numBd;
5351799db056SMatthew G. Knepley 
535207218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5353799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5354799db056SMatthew G. Knepley     maxLabels += numBd;
5355799db056SMatthew G. Knepley   }
5356799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5357799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5358799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5359799db056SMatthew G. Knepley     PetscDS  dsBC;
5360799db056SMatthew G. Knepley     PetscInt numBd, bd;
5361799db056SMatthew G. Knepley 
536207218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5363799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5364799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5365799db056SMatthew G. Knepley       DMLabel      label;
5366799db056SMatthew G. Knepley       PetscInt     field;
5367799db056SMatthew G. Knepley       PetscObject  obj;
5368799db056SMatthew G. Knepley       PetscClassId id;
5369799db056SMatthew G. Knepley 
5370799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
53719566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
53729566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5373799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
53749371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
53759371c9d4SSatish Balay         if (labels[l] == label) break;
5376799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5377783e2ec8SMatthew G. Knepley     }
5378799db056SMatthew G. Knepley   }
5379799db056SMatthew G. Knepley   /* Get label names */
5380799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5381799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
53829371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
53839371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
53849371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
53859371c9d4SSatish Balay   }
5386799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5387712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5388799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5389c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5390799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5391799db056SMatthew G. Knepley   /* Put all names on all processes */
5392799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5393799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5394799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5395799db056SMatthew G. Knepley   gNl = displs[size];
53969371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
53979371c9d4SSatish Balay     counts[p] *= gmaxLen;
53989371c9d4SSatish Balay     displs[p] *= gmaxLen;
53999371c9d4SSatish Balay   }
5400799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5401799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5402799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5403799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5404799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5405799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5406799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
54079371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
54089371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
54099566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5410799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
54119566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5412799db056SMatthew G. Knepley     ++gl;
5413783e2ec8SMatthew G. Knepley   }
5414799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
54153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5416783e2ec8SMatthew G. Knepley }
5417783e2ec8SMatthew G. Knepley 
5418d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5419d71ae5a4SJacob Faibussowitsch {
5420e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5421e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5422e5e52638SMatthew G. Knepley 
5423e5e52638SMatthew G. Knepley   PetscFunctionBegin;
54243ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
54259566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5426e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
54279371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
54289371c9d4SSatish Balay     tmpd[s].ds     = NULL;
54299371c9d4SSatish Balay     tmpd[s].label  = NULL;
54309371c9d4SSatish Balay     tmpd[s].fields = NULL;
54319371c9d4SSatish Balay   }
54329566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5433e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5434e5e52638SMatthew G. Knepley   dm->probs = tmpd;
54353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5436e5e52638SMatthew G. Knepley }
5437e5e52638SMatthew G. Knepley 
5438e5e52638SMatthew G. Knepley /*@
543920f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5440e5e52638SMatthew G. Knepley 
544120f4b53cSBarry Smith   Not Collective
5442e5e52638SMatthew G. Knepley 
5443e5e52638SMatthew G. Knepley   Input Parameter:
544420f4b53cSBarry Smith . dm - The `DM`
5445e5e52638SMatthew G. Knepley 
5446e5e52638SMatthew G. Knepley   Output Parameter:
544720f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5448e5e52638SMatthew G. Knepley 
5449e5e52638SMatthew G. Knepley   Level: intermediate
5450e5e52638SMatthew G. Knepley 
54511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5452e5e52638SMatthew G. Knepley @*/
5453d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5454d71ae5a4SJacob Faibussowitsch {
5455e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5456e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54574f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5458e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
54593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5460e5e52638SMatthew G. Knepley }
5461e5e52638SMatthew G. Knepley 
5462e5e52638SMatthew G. Knepley /*@
546320f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5464e5e52638SMatthew G. Knepley 
546520f4b53cSBarry Smith   Logically Collective
5466e5e52638SMatthew G. Knepley 
5467e5e52638SMatthew G. Knepley   Input Parameter:
546820f4b53cSBarry Smith . dm - The `DM`
5469e5e52638SMatthew G. Knepley 
5470e5e52638SMatthew G. Knepley   Level: intermediate
5471e5e52638SMatthew G. Knepley 
54721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5473e5e52638SMatthew G. Knepley @*/
5474d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5475d71ae5a4SJacob Faibussowitsch {
5476e5e52638SMatthew G. Knepley   PetscInt s;
5477e5e52638SMatthew G. Knepley 
5478e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5479e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5480e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
54819566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
548207218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
54839566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
54849566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5485e5e52638SMatthew G. Knepley   }
54869566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5487e5e52638SMatthew G. Knepley   dm->probs = NULL;
5488e5e52638SMatthew G. Knepley   dm->Nds   = 0;
54893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5490e5e52638SMatthew G. Knepley }
5491e5e52638SMatthew G. Knepley 
5492e5e52638SMatthew G. Knepley /*@
549320f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5494e5e52638SMatthew G. Knepley 
549520f4b53cSBarry Smith   Not Collective
5496e5e52638SMatthew G. Knepley 
5497e5e52638SMatthew G. Knepley   Input Parameter:
549820f4b53cSBarry Smith . dm - The `DM`
5499e5e52638SMatthew G. Knepley 
5500e5e52638SMatthew G. Knepley   Output Parameter:
550107218a29SMatthew G. Knepley . ds - The default `PetscDS`
5502e5e52638SMatthew G. Knepley 
5503e5e52638SMatthew G. Knepley   Level: intermediate
5504e5e52638SMatthew G. Knepley 
55051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5506e5e52638SMatthew G. Knepley @*/
550707218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5508d71ae5a4SJacob Faibussowitsch {
5509e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5510e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55114f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
551207218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
551307218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
55143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5515e5e52638SMatthew G. Knepley }
5516e5e52638SMatthew G. Knepley 
5517e5e52638SMatthew G. Knepley /*@
551820f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5519e5e52638SMatthew G. Knepley 
552020f4b53cSBarry Smith   Not Collective
5521e5e52638SMatthew G. Knepley 
5522e5e52638SMatthew G. Knepley   Input Parameters:
552320f4b53cSBarry Smith + dm    - The `DM`
552420f4b53cSBarry Smith - point - Cell for the `PetscDS`
5525e5e52638SMatthew G. Knepley 
552607218a29SMatthew G. Knepley   Output Parameters:
552707218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
552807218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5529e5e52638SMatthew G. Knepley 
5530e5e52638SMatthew G. Knepley   Level: developer
5531e5e52638SMatthew G. Knepley 
55321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5533e5e52638SMatthew G. Knepley @*/
553407218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5535d71ae5a4SJacob Faibussowitsch {
553607218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5537e5e52638SMatthew G. Knepley   PetscInt s;
5538e5e52638SMatthew G. Knepley 
5539e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5540e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55414f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
55424f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
554363a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
554407218a29SMatthew G. Knepley   if (ds) *ds = NULL;
554507218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5546e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5547e5e52638SMatthew G. Knepley     PetscInt val;
5548e5e52638SMatthew G. Knepley 
55499371c9d4SSatish Balay     if (!dm->probs[s].label) {
555007218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
55519371c9d4SSatish Balay     } else {
55529566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
55539371c9d4SSatish Balay       if (val >= 0) {
555407218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
555507218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
55569371c9d4SSatish Balay         break;
55579371c9d4SSatish Balay       }
5558e5e52638SMatthew G. Knepley     }
5559e5e52638SMatthew G. Knepley   }
556007218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
55613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5562e5e52638SMatthew G. Knepley }
5563e5e52638SMatthew G. Knepley 
5564e5e52638SMatthew G. Knepley /*@
556520f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5566e5e52638SMatthew G. Knepley 
556720f4b53cSBarry Smith   Not Collective
5568e5e52638SMatthew G. Knepley 
5569e5e52638SMatthew G. Knepley   Input Parameters:
557020f4b53cSBarry Smith + dm    - The `DM`
557120f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5572e5e52638SMatthew G. Knepley 
5573b3cf3223SMatthew G. Knepley   Output Parameters:
557420f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
557507218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
557607218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5577e5e52638SMatthew G. Knepley 
5578e5e52638SMatthew G. Knepley   Level: advanced
5579e5e52638SMatthew G. Knepley 
558020f4b53cSBarry Smith   Note:
558120f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
558220f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
558307218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
558420f4b53cSBarry Smith 
55851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5586e5e52638SMatthew G. Knepley @*/
558707218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5588d71ae5a4SJacob Faibussowitsch {
5589e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5590e5e52638SMatthew G. Knepley 
5591e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5592e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5593e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
55949371c9d4SSatish Balay   if (fields) {
55954f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
55969371c9d4SSatish Balay     *fields = NULL;
55979371c9d4SSatish Balay   }
55989371c9d4SSatish Balay   if (ds) {
55994f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
56009371c9d4SSatish Balay     *ds = NULL;
56019371c9d4SSatish Balay   }
560207218a29SMatthew G. Knepley   if (dsIn) {
56034f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
560407218a29SMatthew G. Knepley     *dsIn = NULL;
560507218a29SMatthew G. Knepley   }
5606e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5607154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5608b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5609b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
561007218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
56113ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5612b3cf3223SMatthew G. Knepley     }
5613e5e52638SMatthew G. Knepley   }
56143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5615e5e52638SMatthew G. Knepley }
5616e5e52638SMatthew G. Knepley 
5617e5e52638SMatthew G. Knepley /*@
5618bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5619083401c6SMatthew G. Knepley 
562020f4b53cSBarry Smith   Collective
5621083401c6SMatthew G. Knepley 
5622083401c6SMatthew G. Knepley   Input Parameters:
5623bb7acecfSBarry Smith + dm     - The `DM`
562420f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
562507218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
562607218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
562707218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5628083401c6SMatthew G. Knepley 
562920f4b53cSBarry Smith   Level: advanced
563020f4b53cSBarry Smith 
5631bb7acecfSBarry Smith   Note:
5632bb7acecfSBarry 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,
5633083401c6SMatthew G. Knepley   the fields argument is ignored.
5634083401c6SMatthew G. Knepley 
56351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5636083401c6SMatthew G. Knepley @*/
563707218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5638d71ae5a4SJacob Faibussowitsch {
5639083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5640083401c6SMatthew G. Knepley 
5641083401c6SMatthew G. Knepley   PetscFunctionBegin;
5642083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5643083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
564407218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5645064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
564607218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5647083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5648083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
56499566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
565007218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5651083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
565207218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
56533ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5654083401c6SMatthew G. Knepley     }
5655083401c6SMatthew G. Knepley   }
56569566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
56579566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56589566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
56599566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
566007218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5661083401c6SMatthew G. Knepley   if (!label) {
5662083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5663083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5664083401c6SMatthew G. Knepley     Nds = 0;
5665083401c6SMatthew G. Knepley   }
5666083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5667083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5668083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
566907218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
56703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5671083401c6SMatthew G. Knepley }
5672083401c6SMatthew G. Knepley 
5673083401c6SMatthew G. Knepley /*@
567420f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5675e5e52638SMatthew G. Knepley 
567620f4b53cSBarry Smith   Not Collective
5677e5e52638SMatthew G. Knepley 
5678e5e52638SMatthew G. Knepley   Input Parameters:
567920f4b53cSBarry Smith + dm  - The `DM`
5680e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5681e5e52638SMatthew G. Knepley 
5682e5e52638SMatthew G. Knepley   Output Parameters:
568320f4b53cSBarry Smith + label  - The region label, or `NULL`
568420f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
568507218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
568607218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5687e5e52638SMatthew G. Knepley 
5688e5e52638SMatthew G. Knepley   Level: advanced
5689e5e52638SMatthew G. Knepley 
56901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5691e5e52638SMatthew G. Knepley @*/
569207218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5693d71ae5a4SJacob Faibussowitsch {
5694e5e52638SMatthew G. Knepley   PetscInt Nds;
5695e5e52638SMatthew G. Knepley 
5696e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5697e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56989566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
569963a3b9bcSJacob 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);
5700e5e52638SMatthew G. Knepley   if (label) {
57014f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5702e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5703e5e52638SMatthew G. Knepley   }
5704b3cf3223SMatthew G. Knepley   if (fields) {
57054f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5706b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5707b3cf3223SMatthew G. Knepley   }
5708e5e52638SMatthew G. Knepley   if (ds) {
57094f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5710e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5711e5e52638SMatthew G. Knepley   }
571207218a29SMatthew G. Knepley   if (dsIn) {
57134f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
571407218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
571507218a29SMatthew G. Knepley   }
57163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5717e5e52638SMatthew G. Knepley }
5718e5e52638SMatthew G. Knepley 
5719e5e52638SMatthew G. Knepley /*@
572020f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5721e5e52638SMatthew G. Knepley 
572220f4b53cSBarry Smith   Not Collective
5723e5e52638SMatthew G. Knepley 
5724e5e52638SMatthew G. Knepley   Input Parameters:
572520f4b53cSBarry Smith + dm     - The `DM`
5726083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
572720f4b53cSBarry Smith . label  - The region label, or `NULL`
572807218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
572907218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
573007218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5731e5e52638SMatthew G. Knepley 
5732e5e52638SMatthew G. Knepley   Level: advanced
5733e5e52638SMatthew G. Knepley 
57341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5735e5e52638SMatthew G. Knepley @*/
573607218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5737d71ae5a4SJacob Faibussowitsch {
5738083401c6SMatthew G. Knepley   PetscInt Nds;
5739e5e52638SMatthew G. Knepley 
5740e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5741e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5742ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
57439566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
574463a3b9bcSJacob 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);
57459566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57469566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5747083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5748083401c6SMatthew G. Knepley   if (fields) {
5749083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
57509566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
57519566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5752083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5753e5e52638SMatthew G. Knepley   }
5754083401c6SMatthew G. Knepley   if (ds) {
5755083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
57569566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
57579566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5758083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5759083401c6SMatthew G. Knepley   }
576007218a29SMatthew G. Knepley   if (dsIn) {
576107218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
576207218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
576307218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
576407218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
576507218a29SMatthew G. Knepley   }
57663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5767e5e52638SMatthew G. Knepley }
5768e5e52638SMatthew G. Knepley 
5769e5e52638SMatthew G. Knepley /*@
577020f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
57711d3af9e0SMatthew G. Knepley 
577220f4b53cSBarry Smith   Not Collective
57731d3af9e0SMatthew G. Knepley 
57741d3af9e0SMatthew G. Knepley   Input Parameters:
577520f4b53cSBarry Smith + dm - The `DM`
577620f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
57771d3af9e0SMatthew G. Knepley 
57781d3af9e0SMatthew G. Knepley   Output Parameter:
57791d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
57801d3af9e0SMatthew G. Knepley 
57811d3af9e0SMatthew G. Knepley   Level: advanced
57821d3af9e0SMatthew G. Knepley 
57831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
57841d3af9e0SMatthew G. Knepley @*/
5785d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5786d71ae5a4SJacob Faibussowitsch {
57871d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
57881d3af9e0SMatthew G. Knepley 
57891d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
57901d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57911d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
57924f572ea9SToby Isaac   PetscAssertPointer(num, 3);
57939566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
57949371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
57959371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
57961d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
57971d3af9e0SMatthew G. Knepley   else *num = n;
57983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57991d3af9e0SMatthew G. Knepley }
58001d3af9e0SMatthew G. Knepley 
58012df84da0SMatthew G. Knepley /*@C
5802bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
58032df84da0SMatthew G. Knepley 
580420f4b53cSBarry Smith   Not Collective
58052df84da0SMatthew G. Knepley 
5806f1a722f8SMatthew G. Knepley   Input Parameters:
5807bb7acecfSBarry Smith + dm     - The `DM`
58082df84da0SMatthew G. Knepley . Nc     - The number of components for the field
580920f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5810bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
58112df84da0SMatthew G. Knepley 
58122df84da0SMatthew G. Knepley   Output Parameter:
5813bb7acecfSBarry Smith . fem - The `PetscFE`
58142df84da0SMatthew G. Knepley 
581520f4b53cSBarry Smith   Level: intermediate
581620f4b53cSBarry Smith 
5817bb7acecfSBarry Smith   Note:
5818bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
58192df84da0SMatthew G. Knepley 
58201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
58212df84da0SMatthew G. Knepley @*/
5822d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5823d71ae5a4SJacob Faibussowitsch {
58242df84da0SMatthew G. Knepley   DMPolytopeType ct;
58252df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
58262df84da0SMatthew G. Knepley 
58272df84da0SMatthew G. Knepley   PetscFunctionBegin;
58282df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58292df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
58304f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
58312df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
58324f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
58339566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
58349566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
58359566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
58369566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
58373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58382df84da0SMatthew G. Knepley }
58392df84da0SMatthew G. Knepley 
58401d3af9e0SMatthew G. Knepley /*@
5841bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5842e5e52638SMatthew G. Knepley 
584320f4b53cSBarry Smith   Collective
5844e5e52638SMatthew G. Knepley 
5845e5e52638SMatthew G. Knepley   Input Parameter:
5846bb7acecfSBarry Smith . dm - The `DM`
5847e5e52638SMatthew G. Knepley 
584820f4b53cSBarry Smith   Options Database Key:
5849bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
585045480ffeSMatthew G. Knepley 
585120f4b53cSBarry Smith   Level: intermediate
585220f4b53cSBarry Smith 
58531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5854e5e52638SMatthew G. Knepley @*/
5855d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5856d71ae5a4SJacob Faibussowitsch {
5857e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5858083401c6SMatthew G. Knepley   PetscDS   dsDef;
5859083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5860f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5861f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5862e5e52638SMatthew G. Knepley 
5863e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5864e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58653ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
58669566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
58679566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5868083401c6SMatthew G. Knepley   /* Determine how many regions we have */
58699566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5870083401c6SMatthew G. Knepley   Nl   = 0;
5871083401c6SMatthew G. Knepley   Ndef = 0;
5872083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5873083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5874083401c6SMatthew G. Knepley     PetscInt l;
5875083401c6SMatthew G. Knepley 
5876f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5877f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5878f918ec44SMatthew G. Knepley     {
5879f918ec44SMatthew G. Knepley       PetscClassId id;
5880f918ec44SMatthew G. Knepley 
58819566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5882f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5883f918ec44SMatthew G. Knepley         Ceed ceed;
5884f918ec44SMatthew G. Knepley 
58859566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
58869566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5887f918ec44SMatthew G. Knepley       }
5888f918ec44SMatthew G. Knepley     }
5889f918ec44SMatthew G. Knepley #endif
58909371c9d4SSatish Balay     if (!label) {
58919371c9d4SSatish Balay       ++Ndef;
58929371c9d4SSatish Balay       continue;
58939371c9d4SSatish Balay     }
58949371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
58959371c9d4SSatish Balay       if (label == labelSet[l]) break;
5896083401c6SMatthew G. Knepley     if (l < Nl) continue;
5897083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5898083401c6SMatthew G. Knepley   }
5899083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
590007218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5901083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5902b3cf3223SMatthew G. Knepley     IS        fields;
5903b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5904b3cf3223SMatthew G. Knepley 
59059371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59069371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
59077a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
59089566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59099371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59109371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
59119566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59129566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59139566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59149566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
591588f0c812SMatthew G. Knepley 
59169566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
591707218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
59189566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59199566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
59202df9ee95SMatthew G. Knepley   }
592107218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
59229566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5923083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5924083401c6SMatthew G. Knepley   if (Ndef && Nl) {
59250122748bSMatthew G. Knepley     DM              plex;
5926083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5927083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5928083401c6SMatthew G. Knepley     PetscInt       *fields;
5929083401c6SMatthew G. Knepley     const PetscInt *cells;
5930083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
59310122748bSMatthew G. Knepley 
59329566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
59339566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
59349566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
59359566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
59365fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5937083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5938083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5939083401c6SMatthew G. Knepley       IS      pointIS;
5940083401c6SMatthew G. Knepley 
59419566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
59429566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
59439566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
59449566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5945083401c6SMatthew G. Knepley     }
59469566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5947083401c6SMatthew G. Knepley 
59489566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
59499566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
59509566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
59519566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
59529566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
59539566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
59549566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5955083401c6SMatthew G. Knepley 
59569566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
59579371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
59589371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
59599566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
59609566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
59619566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
59629566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5963083401c6SMatthew G. Knepley 
59649566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
596507218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
59669566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
59679566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
59689566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59699566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
59709566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5971083401c6SMatthew G. Knepley   }
5972083401c6SMatthew G. Knepley   /* Create label DSes
5973083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5974083401c6SMatthew G. Knepley   */
5975083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5976083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5977083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
597807218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
5979083401c6SMatthew G. Knepley     IS        fields;
5980083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5981083401c6SMatthew G. Knepley 
59829566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
59839371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59849371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
59859566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59869371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59879371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
59889566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59899566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59909566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59919566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
59929566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5993083401c6SMatthew G. Knepley     {
5994083401c6SMatthew G. Knepley       DMPolytopeType ct;
5995083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
59965fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
59970122748bSMatthew G. Knepley 
59989566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5999665f567fSMatthew G. Knepley       if (lStart >= 0) {
60009566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
6001412e9a14SMatthew G. Knepley         switch (ct) {
6002412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
6003412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
6004412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
6005d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
6006d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
6007d71ae5a4SJacob Faibussowitsch           break;
6008d71ae5a4SJacob Faibussowitsch         default:
6009d71ae5a4SJacob Faibussowitsch           break;
6010412e9a14SMatthew G. Knepley         }
6011665f567fSMatthew G. Knepley       }
6012712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
601307218a29SMatthew G. Knepley       if (isCohesive) {
601407218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
601507218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
601607218a29SMatthew G. Knepley       }
60175fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
60185fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
60195fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
60209566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
60219566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
602207218a29SMatthew G. Knepley             if (dsIn) {
602307218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
602407218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
602507218a29SMatthew G. Knepley             }
60265fedec97SMatthew G. Knepley           }
60275fedec97SMatthew G. Knepley           ++nf;
60285fedec97SMatthew G. Knepley         }
60295fedec97SMatthew G. Knepley       }
6030e5e52638SMatthew G. Knepley     }
603107218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
603207218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
60339566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
603407218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
6035e5e52638SMatthew G. Knepley   }
60369566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
6037e5e52638SMatthew G. Knepley   /* Set fields in DSes */
6038083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
6039083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
604007218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
6041083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
6042083401c6SMatthew G. Knepley     const PetscInt *fld;
60435fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
60445fedec97SMatthew G. Knepley     PetscBool       isCohesive;
6045e5e52638SMatthew G. Knepley 
60469566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
60479566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
60489566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
60499566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
6050083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
6051083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
60525fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
6053e5e52638SMatthew G. Knepley       PetscClassId id;
6054e5e52638SMatthew G. Knepley 
60555fedec97SMatthew G. Knepley       /* Handle DS with no fields */
60569566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
60575fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
605807218a29SMatthew G. Knepley       if (isCohesive) {
605907218a29SMatthew G. Knepley         if (!isCohesiveField) {
606007218a29SMatthew G. Knepley           PetscObject bdDisc;
606107218a29SMatthew G. Knepley 
606207218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
606307218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
606407218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
606507218a29SMatthew G. Knepley         } else {
60669566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
606707218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
606807218a29SMatthew G. Knepley         }
606907218a29SMatthew G. Knepley       } else {
607007218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
607107218a29SMatthew G. Knepley       }
6072083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
60739566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
6074e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
6075e5e52638SMatthew G. Knepley     }
60769566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6077e5e52638SMatthew G. Knepley   }
6078f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
60799566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6080f9244615SMatthew G. Knepley   if (flg) {
60813b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
60823b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
608307218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
60843b4aee56SMatthew G. Knepley       PetscInt Nf, f;
60853b4aee56SMatthew G. Knepley 
60869566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
608707218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
608807218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
608907218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
609007218a29SMatthew G. Knepley       }
60913b4aee56SMatthew G. Knepley     }
6092f9244615SMatthew G. Knepley   }
6093e5e52638SMatthew G. Knepley   /* Setup DSes */
6094e5e52638SMatthew G. Knepley   if (doSetup) {
609507218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
6096cf1363e4SMatthew G. Knepley       if (dm->setfromoptionscalled) {
6097cf1363e4SMatthew G. Knepley         PetscCall(PetscDSSetFromOptions(dm->probs[s].ds));
6098cf1363e4SMatthew G. Knepley         if (dm->probs[s].dsIn) PetscCall(PetscDSSetFromOptions(dm->probs[s].dsIn));
6099cf1363e4SMatthew G. Knepley       }
610007218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
610107218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
610207218a29SMatthew G. Knepley     }
6103e5e52638SMatthew G. Knepley   }
61043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6105e5e52638SMatthew G. Knepley }
6106e5e52638SMatthew G. Knepley 
6107e5e52638SMatthew G. Knepley /*@
6108d2b2dc1eSMatthew G. Knepley   DMUseTensorOrder - Use a tensor product closure ordering for the default section
6109d2b2dc1eSMatthew G. Knepley 
6110d2b2dc1eSMatthew G. Knepley   Input Parameters:
6111d2b2dc1eSMatthew G. Knepley + dm     - The DM
6112d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order
6113d2b2dc1eSMatthew G. Knepley 
6114d2b2dc1eSMatthew G. Knepley   Level: developer
6115d2b2dc1eSMatthew G. Knepley 
6116d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()`
6117d2b2dc1eSMatthew G. Knepley @*/
6118d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor)
6119d2b2dc1eSMatthew G. Knepley {
6120d2b2dc1eSMatthew G. Knepley   PetscInt  Nf;
6121d2b2dc1eSMatthew G. Knepley   PetscBool reorder = PETSC_TRUE, isPlex;
6122d2b2dc1eSMatthew G. Knepley 
6123d2b2dc1eSMatthew G. Knepley   PetscFunctionBegin;
6124d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex));
6125d2b2dc1eSMatthew G. Knepley   PetscCall(DMGetNumFields(dm, &Nf));
6126d2b2dc1eSMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
6127d2b2dc1eSMatthew G. Knepley     PetscObject  obj;
6128d2b2dc1eSMatthew G. Knepley     PetscClassId id;
6129d2b2dc1eSMatthew G. Knepley 
6130d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetField(dm, f, NULL, &obj));
6131d2b2dc1eSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
6132d2b2dc1eSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
6133d2b2dc1eSMatthew G. Knepley       PetscSpace sp;
6134d2b2dc1eSMatthew G. Knepley       PetscBool  tensor;
6135d2b2dc1eSMatthew G. Knepley 
6136d2b2dc1eSMatthew G. Knepley       PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp));
6137d2b2dc1eSMatthew G. Knepley       PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor));
6138d2b2dc1eSMatthew G. Knepley       reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE;
6139d2b2dc1eSMatthew G. Knepley     } else reorder = PETSC_FALSE;
6140d2b2dc1eSMatthew G. Knepley   }
6141d2b2dc1eSMatthew G. Knepley   if (tensor) {
6142d2b2dc1eSMatthew G. Knepley     if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
6143d2b2dc1eSMatthew G. Knepley   } else {
6144d2b2dc1eSMatthew G. Knepley     PetscSection s;
6145d2b2dc1eSMatthew G. Knepley 
6146d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetLocalSection(dm, &s));
6147d2b2dc1eSMatthew G. Knepley     if (s) PetscCall(PetscSectionResetClosurePermutation(s));
6148d2b2dc1eSMatthew G. Knepley   }
6149d2b2dc1eSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6150d2b2dc1eSMatthew G. Knepley }
6151d2b2dc1eSMatthew G. Knepley 
6152d2b2dc1eSMatthew G. Knepley /*@
6153bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
61547f96f943SMatthew G. Knepley 
615520f4b53cSBarry Smith   Collective
6156f2cacb80SMatthew G. Knepley 
61577f96f943SMatthew G. Knepley   Input Parameters:
6158bb7acecfSBarry Smith + dm   - The `DM`
61597f96f943SMatthew G. Knepley - time - The time
61607f96f943SMatthew G. Knepley 
61617f96f943SMatthew G. Knepley   Output Parameters:
616220f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
616320f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
616420f4b53cSBarry Smith 
616520f4b53cSBarry Smith   Level: developer
61667f96f943SMatthew G. Knepley 
6167bb7acecfSBarry Smith   Note:
6168bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
61697f96f943SMatthew G. Knepley 
61701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
61717f96f943SMatthew G. Knepley @*/
6172d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6173d71ae5a4SJacob Faibussowitsch {
61747f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
61757f96f943SMatthew G. Knepley   void   **ectxs;
6176f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
61777f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
61787f96f943SMatthew G. Knepley 
61797f96f943SMatthew G. Knepley   PetscFunctionBegin;
6180f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6181f60fa741SMatthew G. Knepley   if (u) {
6182f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6183f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6184f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6185f60fa741SMatthew G. Knepley   }
6186f60fa741SMatthew G. Knepley   if (u_t) {
6187f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6188f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6189f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6190f60fa741SMatthew G. Knepley   }
61919566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
61929566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
61939566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
61947f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
61957f96f943SMatthew G. Knepley     PetscDS         ds;
61967f96f943SMatthew G. Knepley     DMLabel         label;
61977f96f943SMatthew G. Knepley     IS              fieldIS;
61987f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
61997f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
62007f96f943SMatthew G. Knepley 
620107218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
62029566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
62039566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
62049566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
62059566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6206f2cacb80SMatthew G. Knepley     if (u) {
6207f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6208f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6209f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
62107f96f943SMatthew G. Knepley     }
6211f2cacb80SMatthew G. Knepley     if (u_t) {
62129566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
62139566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6214f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6215f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6216f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6217f2cacb80SMatthew G. Knepley     }
62189566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6219f2cacb80SMatthew G. Knepley   }
6220f2cacb80SMatthew G. Knepley   if (u) {
62219566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
62229566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6223f2cacb80SMatthew G. Knepley   }
6224f2cacb80SMatthew G. Knepley   if (u_t) {
62259566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
62269566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6227f2cacb80SMatthew G. Knepley   }
62289566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6229f60fa741SMatthew G. Knepley   if (u) {
6230f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6231f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6232f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6233f60fa741SMatthew G. Knepley   }
6234f60fa741SMatthew G. Knepley   if (u_t) {
6235f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6236f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6237f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6238f60fa741SMatthew G. Knepley   }
62393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
62407f96f943SMatthew G. Knepley }
62417f96f943SMatthew G. Knepley 
624207218a29SMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
6243d71ae5a4SJacob Faibussowitsch {
624407218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
624545480ffeSMatthew G. Knepley 
624645480ffeSMatthew G. Knepley   PetscFunctionBegin;
62479566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
624807218a29SMatthew G. Knepley   PetscCall(PetscDSCopy(ds, dm, dsNew));
624907218a29SMatthew G. Knepley   if (dsIn) {
625007218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
625107218a29SMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, dm, dsInNew));
625245480ffeSMatthew G. Knepley   }
625307218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
62549566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
625507218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
62563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
625745480ffeSMatthew G. Knepley }
625845480ffeSMatthew G. Knepley 
62597f96f943SMatthew G. Knepley /*@
6260bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6261e5e52638SMatthew G. Knepley 
626220f4b53cSBarry Smith   Collective
6263e5e52638SMatthew G. Knepley 
6264e5e52638SMatthew G. Knepley   Input Parameter:
6265bb7acecfSBarry Smith . dm - The `DM`
6266e5e52638SMatthew G. Knepley 
6267e5e52638SMatthew G. Knepley   Output Parameter:
6268bb7acecfSBarry Smith . newdm - The `DM`
6269e5e52638SMatthew G. Knepley 
6270e5e52638SMatthew G. Knepley   Level: advanced
6271e5e52638SMatthew G. Knepley 
62721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6273e5e52638SMatthew G. Knepley @*/
6274d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6275d71ae5a4SJacob Faibussowitsch {
6276e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6277e5e52638SMatthew G. Knepley 
6278e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62793ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
62809566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62819566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6282e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6283e5e52638SMatthew G. Knepley     DMLabel  label;
6284b3cf3223SMatthew G. Knepley     IS       fields;
628507218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6286783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6287e5e52638SMatthew G. Knepley 
628807218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6289b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
629007218a29SMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds, dsIn));
6291d5b43468SJose E. Roman     /* Complete new labels in the new DS */
629207218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
62939566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6294783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6295b8025e53SMatthew G. Knepley       PetscWeakForm wf;
629645480ffeSMatthew G. Knepley       DMLabel       label;
6297783e2ec8SMatthew G. Knepley       PetscInt      field;
6298783e2ec8SMatthew G. Knepley 
62999566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
63009566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6301783e2ec8SMatthew G. Knepley     }
6302e5e52638SMatthew G. Knepley   }
6303799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
63043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6305e5e52638SMatthew G. Knepley }
6306e5e52638SMatthew G. Knepley 
6307e5e52638SMatthew G. Knepley /*@
6308bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6309e5e52638SMatthew G. Knepley 
631020f4b53cSBarry Smith   Collective
6311e5e52638SMatthew G. Knepley 
6312e5e52638SMatthew G. Knepley   Input Parameter:
6313bb7acecfSBarry Smith . dm - The `DM`
6314e5e52638SMatthew G. Knepley 
6315e5e52638SMatthew G. Knepley   Output Parameter:
6316bb7acecfSBarry Smith . newdm - The `DM`
6317e5e52638SMatthew G. Knepley 
6318e5e52638SMatthew G. Knepley   Level: advanced
6319e5e52638SMatthew G. Knepley 
6320*73ff1848SBarry Smith   Developer Note:
6321bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6322bb7acecfSBarry Smith 
63231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6324e5e52638SMatthew G. Knepley @*/
6325d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6326d71ae5a4SJacob Faibussowitsch {
6327e5e52638SMatthew G. Knepley   PetscFunctionBegin;
63289566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
63299566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
63303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6331e5e52638SMatthew G. Knepley }
6332e5e52638SMatthew G. Knepley 
6333c73cfb54SMatthew G. Knepley /*@
6334bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6335c73cfb54SMatthew G. Knepley 
633620f4b53cSBarry Smith   Not Collective
6337c73cfb54SMatthew G. Knepley 
6338c73cfb54SMatthew G. Knepley   Input Parameter:
6339bb7acecfSBarry Smith . dm - The `DM`
6340c73cfb54SMatthew G. Knepley 
6341c73cfb54SMatthew G. Knepley   Output Parameter:
6342c73cfb54SMatthew G. Knepley . dim - The topological dimension
6343c73cfb54SMatthew G. Knepley 
6344c73cfb54SMatthew G. Knepley   Level: beginner
6345c73cfb54SMatthew G. Knepley 
63461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6347c73cfb54SMatthew G. Knepley @*/
6348d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6349d71ae5a4SJacob Faibussowitsch {
6350c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6351c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63524f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6353c73cfb54SMatthew G. Knepley   *dim = dm->dim;
63543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6355c73cfb54SMatthew G. Knepley }
6356c73cfb54SMatthew G. Knepley 
6357c73cfb54SMatthew G. Knepley /*@
6358bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6359c73cfb54SMatthew G. Knepley 
636020f4b53cSBarry Smith   Collective
6361c73cfb54SMatthew G. Knepley 
6362c73cfb54SMatthew G. Knepley   Input Parameters:
6363bb7acecfSBarry Smith + dm  - The `DM`
6364c73cfb54SMatthew G. Knepley - dim - The topological dimension
6365c73cfb54SMatthew G. Knepley 
6366c73cfb54SMatthew G. Knepley   Level: beginner
6367c73cfb54SMatthew G. Knepley 
63681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6369c73cfb54SMatthew G. Knepley @*/
6370d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6371d71ae5a4SJacob Faibussowitsch {
6372e5e52638SMatthew G. Knepley   PetscDS  ds;
637345480ffeSMatthew G. Knepley   PetscInt Nds, n;
6374f17e8794SMatthew G. Knepley 
6375c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6376c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6377c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6378c73cfb54SMatthew G. Knepley   dm->dim = dim;
6379d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
63809566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
638145480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
638207218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
63839566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
638445480ffeSMatthew G. Knepley     }
6385d17bd122SMatthew G. Knepley   }
63863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6387c73cfb54SMatthew G. Knepley }
6388c73cfb54SMatthew G. Knepley 
6389793f3fe5SMatthew G. Knepley /*@
6390793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6391793f3fe5SMatthew G. Knepley 
639220f4b53cSBarry Smith   Collective
6393793f3fe5SMatthew G. Knepley 
6394793f3fe5SMatthew G. Knepley   Input Parameters:
6395bb7acecfSBarry Smith + dm  - the `DM`
6396793f3fe5SMatthew G. Knepley - dim - the dimension
6397793f3fe5SMatthew G. Knepley 
6398793f3fe5SMatthew G. Knepley   Output Parameters:
6399793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6400aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6401793f3fe5SMatthew G. Knepley 
640220f4b53cSBarry Smith   Level: intermediate
640320f4b53cSBarry Smith 
6404793f3fe5SMatthew G. Knepley   Note:
6405793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6406a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6407793f3fe5SMatthew G. Knepley   then the interval is empty.
6408793f3fe5SMatthew G. Knepley 
64091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6410793f3fe5SMatthew G. Knepley @*/
6411d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6412d71ae5a4SJacob Faibussowitsch {
6413793f3fe5SMatthew G. Knepley   PetscInt d;
6414793f3fe5SMatthew G. Knepley 
6415793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6416793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64179566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
64187a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6419dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
64203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6421793f3fe5SMatthew G. Knepley }
6422793f3fe5SMatthew G. Knepley 
64236636e97aSMatthew G Knepley /*@
6424bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6425f4d763aaSMatthew G. Knepley 
642620f4b53cSBarry Smith   Collective
64278f700142SStefano Zampini 
6428f4d763aaSMatthew G. Knepley   Input Parameter:
6429bb7acecfSBarry Smith . dm - The original `DM`
6430f4d763aaSMatthew G. Knepley 
6431f4d763aaSMatthew G. Knepley   Output Parameter:
6432bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6433f4d763aaSMatthew G. Knepley 
6434f4d763aaSMatthew G. Knepley   Level: intermediate
6435f4d763aaSMatthew G. Knepley 
6436bb7acecfSBarry Smith   Note:
6437bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6438bb7acecfSBarry 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
6439bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6440bb7acecfSBarry Smith 
64411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6442f4d763aaSMatthew G. Knepley @*/
6443d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6444d71ae5a4SJacob Faibussowitsch {
6445c26acbdeSMatthew G. Knepley   PetscSection section;
6446eb9d3e4dSMatthew G. Knepley   IS           perm;
6447eb9d3e4dSMatthew G. Knepley   PetscBool    hasConstraints, newDM, gnewDM;
644814f150ffSMatthew G. Knepley 
644914f150ffSMatthew G. Knepley   PetscFunctionBegin;
645014f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64514f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
64529566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
64539566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6454eb9d3e4dSMatthew G. Knepley   PetscCall(PetscSectionGetPermutation(section, &perm));
6455eb9d3e4dSMatthew G. Knepley   newDM = hasConstraints || perm ? PETSC_TRUE : PETSC_FALSE;
6456eb9d3e4dSMatthew G. Knepley   PetscCall(MPIU_Allreduce(&newDM, &gnewDM, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
6457eb9d3e4dSMatthew G. Knepley   if (!gnewDM) {
6458c26acbdeSMatthew G. Knepley     *odm = dm;
64593ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6460c26acbdeSMatthew G. Knepley   }
646114f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6462c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
646314f150ffSMatthew G. Knepley     PetscSF      sf;
6464eb9d3e4dSMatthew G. Knepley     PetscBool    usePerm = dm->ignorePermOutput ? PETSC_FALSE : PETSC_TRUE;
646514f150ffSMatthew G. Knepley 
64669566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
64679566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
64689566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
64699566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
64709566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
64719566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
6472eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(section, sf, usePerm, PETSC_TRUE, PETSC_FALSE, &gsection));
64739566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
64749566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
647514f150ffSMatthew G. Knepley   }
647614f150ffSMatthew G. Knepley   *odm = dm->dmBC;
64773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
647814f150ffSMatthew G. Knepley }
6479f4d763aaSMatthew G. Knepley 
6480f4d763aaSMatthew G. Knepley /*@
6481cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6482f4d763aaSMatthew G. Knepley 
6483f4d763aaSMatthew G. Knepley   Input Parameter:
6484bb7acecfSBarry Smith . dm - The original `DM`
6485f4d763aaSMatthew G. Knepley 
6486cdb7a50dSMatthew G. Knepley   Output Parameters:
6487cdb7a50dSMatthew G. Knepley + num - The output sequence number
6488cdb7a50dSMatthew G. Knepley - val - The output sequence value
6489f4d763aaSMatthew G. Knepley 
6490f4d763aaSMatthew G. Knepley   Level: intermediate
6491f4d763aaSMatthew G. Knepley 
6492bb7acecfSBarry Smith   Note:
6493bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6494bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6495bb7acecfSBarry Smith 
6496*73ff1848SBarry Smith   Developer Note:
6497bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6498bb7acecfSBarry Smith   not directly related to the `DM`.
6499f4d763aaSMatthew G. Knepley 
65001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6501f4d763aaSMatthew G. Knepley @*/
6502d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6503d71ae5a4SJacob Faibussowitsch {
6504f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6505f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65069371c9d4SSatish Balay   if (num) {
65074f572ea9SToby Isaac     PetscAssertPointer(num, 2);
65089371c9d4SSatish Balay     *num = dm->outputSequenceNum;
65099371c9d4SSatish Balay   }
65109371c9d4SSatish Balay   if (val) {
65114f572ea9SToby Isaac     PetscAssertPointer(val, 3);
65129371c9d4SSatish Balay     *val = dm->outputSequenceVal;
65139371c9d4SSatish Balay   }
65143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6515f4d763aaSMatthew G. Knepley }
6516f4d763aaSMatthew G. Knepley 
6517f4d763aaSMatthew G. Knepley /*@
6518cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6519f4d763aaSMatthew G. Knepley 
6520f4d763aaSMatthew G. Knepley   Input Parameters:
6521bb7acecfSBarry Smith + dm  - The original `DM`
6522cdb7a50dSMatthew G. Knepley . num - The output sequence number
6523cdb7a50dSMatthew G. Knepley - val - The output sequence value
6524f4d763aaSMatthew G. Knepley 
6525f4d763aaSMatthew G. Knepley   Level: intermediate
6526f4d763aaSMatthew G. Knepley 
6527bb7acecfSBarry Smith   Note:
6528bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6529bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6530f4d763aaSMatthew G. Knepley 
65311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6532f4d763aaSMatthew G. Knepley @*/
6533d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6534d71ae5a4SJacob Faibussowitsch {
6535f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6536f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6537f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6538cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
65393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6540cdb7a50dSMatthew G. Knepley }
6541cdb7a50dSMatthew G. Knepley 
6542cdb7a50dSMatthew G. Knepley /*@C
6543bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6544cdb7a50dSMatthew G. Knepley 
6545cdb7a50dSMatthew G. Knepley   Input Parameters:
6546bb7acecfSBarry Smith + dm     - The original `DM`
6547a4e35b19SJacob Faibussowitsch . viewer - The viewer to get it from
6548cdb7a50dSMatthew G. Knepley . name   - The sequence name
6549cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6550cdb7a50dSMatthew G. Knepley 
6551cdb7a50dSMatthew G. Knepley   Output Parameter:
6552cdb7a50dSMatthew G. Knepley . val - The output sequence value
6553cdb7a50dSMatthew G. Knepley 
6554cdb7a50dSMatthew G. Knepley   Level: intermediate
6555cdb7a50dSMatthew G. Knepley 
6556bb7acecfSBarry Smith   Note:
6557bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6558bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6559bb7acecfSBarry Smith 
6560*73ff1848SBarry Smith   Developer Note:
6561bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6562cdb7a50dSMatthew G. Knepley 
65631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6564cdb7a50dSMatthew G. Knepley @*/
6565d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6566d71ae5a4SJacob Faibussowitsch {
6567cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6568cdb7a50dSMatthew G. Knepley 
6569cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6570cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6571cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
65724f572ea9SToby Isaac   PetscAssertPointer(val, 5);
65739566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6574cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6575cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6576cdb7a50dSMatthew G. Knepley     PetscScalar value;
6577cdb7a50dSMatthew G. Knepley 
65789566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
65794aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6580cdb7a50dSMatthew G. Knepley #endif
6581cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
65823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6583f4d763aaSMatthew G. Knepley }
65848e4ac7eaSMatthew G. Knepley 
65858e4ac7eaSMatthew G. Knepley /*@
6586bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
65878e4ac7eaSMatthew G. Knepley 
658820f4b53cSBarry Smith   Not Collective
65898e4ac7eaSMatthew G. Knepley 
65908e4ac7eaSMatthew G. Knepley   Input Parameter:
6591bb7acecfSBarry Smith . dm - The `DM`
65928e4ac7eaSMatthew G. Knepley 
65938e4ac7eaSMatthew G. Knepley   Output Parameter:
6594bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
65958e4ac7eaSMatthew G. Knepley 
65968e4ac7eaSMatthew G. Knepley   Level: beginner
65978e4ac7eaSMatthew G. Knepley 
65981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
65998e4ac7eaSMatthew G. Knepley @*/
6600d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6601d71ae5a4SJacob Faibussowitsch {
66028e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66038e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66044f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
66058e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
66063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66078e4ac7eaSMatthew G. Knepley }
66088e4ac7eaSMatthew G. Knepley 
66098e4ac7eaSMatthew G. Knepley /*@
6610bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66118e4ac7eaSMatthew G. Knepley 
661220f4b53cSBarry Smith   Collective
66138e4ac7eaSMatthew G. Knepley 
66148e4ac7eaSMatthew G. Knepley   Input Parameters:
6615bb7acecfSBarry Smith + dm         - The `DM`
6616bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66178e4ac7eaSMatthew G. Knepley 
6618*73ff1848SBarry Smith   Level: beginner
6619*73ff1848SBarry Smith 
6620bb7acecfSBarry Smith   Note:
6621bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
66225d3b26e6SMatthew G. Knepley 
66231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
66248e4ac7eaSMatthew G. Knepley @*/
6625d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6626d71ae5a4SJacob Faibussowitsch {
66278e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66288e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66298833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
66308e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
66313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66328e4ac7eaSMatthew G. Knepley }
6633c58f1c22SToby Isaac 
6634c58f1c22SToby Isaac /*@C
6635bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6636c58f1c22SToby Isaac 
6637c58f1c22SToby Isaac   Not Collective
6638c58f1c22SToby Isaac 
6639c58f1c22SToby Isaac   Input Parameters:
6640bb7acecfSBarry Smith + dm   - The `DM` object
6641c58f1c22SToby Isaac - name - The label name
6642c58f1c22SToby Isaac 
6643c58f1c22SToby Isaac   Level: intermediate
6644c58f1c22SToby Isaac 
66451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6646c58f1c22SToby Isaac @*/
6647d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6648d71ae5a4SJacob Faibussowitsch {
66495d80c0bfSVaclav Hapla   PetscBool flg;
66505d80c0bfSVaclav Hapla   DMLabel   label;
6651c58f1c22SToby Isaac 
6652c58f1c22SToby Isaac   PetscFunctionBegin;
6653c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66544f572ea9SToby Isaac   PetscAssertPointer(name, 2);
66559566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6656c58f1c22SToby Isaac   if (!flg) {
66579566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
66589566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
66599566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6660c58f1c22SToby Isaac   }
66613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6662c58f1c22SToby Isaac }
6663c58f1c22SToby Isaac 
6664c58f1c22SToby Isaac /*@C
6665bb7acecfSBarry 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.
66660fdc7489SMatthew Knepley 
66670fdc7489SMatthew Knepley   Not Collective
66680fdc7489SMatthew Knepley 
66690fdc7489SMatthew Knepley   Input Parameters:
6670bb7acecfSBarry Smith + dm   - The `DM` object
66710fdc7489SMatthew Knepley . l    - The index for the label
66720fdc7489SMatthew Knepley - name - The label name
66730fdc7489SMatthew Knepley 
66740fdc7489SMatthew Knepley   Level: intermediate
66750fdc7489SMatthew Knepley 
66761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
66770fdc7489SMatthew Knepley @*/
6678d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6679d71ae5a4SJacob Faibussowitsch {
66800fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
66810fdc7489SMatthew Knepley   DMLabel     label;
66820fdc7489SMatthew Knepley   PetscInt    Nl, m;
66830fdc7489SMatthew Knepley   PetscBool   flg, match;
66840fdc7489SMatthew Knepley   const char *lname;
66850fdc7489SMatthew Knepley 
66860fdc7489SMatthew Knepley   PetscFunctionBegin;
66870fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66884f572ea9SToby Isaac   PetscAssertPointer(name, 3);
66899566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
66900fdc7489SMatthew Knepley   if (!flg) {
66919566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
66929566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
66939566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
66940fdc7489SMatthew Knepley   }
66959566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
669663a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
66970fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
66989566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
66999566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
67000fdc7489SMatthew Knepley     if (match) break;
67010fdc7489SMatthew Knepley   }
67023ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
67030fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
67040fdc7489SMatthew Knepley   else prev->next = orig->next;
67050fdc7489SMatthew Knepley   if (!l) {
67060fdc7489SMatthew Knepley     orig->next = dm->labels;
67070fdc7489SMatthew Knepley     dm->labels = orig;
67080fdc7489SMatthew Knepley   } else {
6709fbccb6d4SPierre Jolivet     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next);
67100fdc7489SMatthew Knepley     orig->next = prev->next;
67110fdc7489SMatthew Knepley     prev->next = orig;
67120fdc7489SMatthew Knepley   }
67133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67140fdc7489SMatthew Knepley }
67150fdc7489SMatthew Knepley 
67160fdc7489SMatthew Knepley /*@C
6717bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6718c58f1c22SToby Isaac 
6719c58f1c22SToby Isaac   Not Collective
6720c58f1c22SToby Isaac 
6721c58f1c22SToby Isaac   Input Parameters:
6722bb7acecfSBarry Smith + dm    - The `DM` object
6723c58f1c22SToby Isaac . name  - The label name
6724c58f1c22SToby Isaac - point - The mesh point
6725c58f1c22SToby Isaac 
6726c58f1c22SToby Isaac   Output Parameter:
6727c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6728c58f1c22SToby Isaac 
6729c58f1c22SToby Isaac   Level: beginner
6730c58f1c22SToby Isaac 
67311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6732c58f1c22SToby Isaac @*/
6733d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6734d71ae5a4SJacob Faibussowitsch {
6735c58f1c22SToby Isaac   DMLabel label;
6736c58f1c22SToby Isaac 
6737c58f1c22SToby Isaac   PetscFunctionBegin;
6738c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67394f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67409566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67417a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
67429566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
67433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6744c58f1c22SToby Isaac }
6745c58f1c22SToby Isaac 
6746c58f1c22SToby Isaac /*@C
6747bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6748c58f1c22SToby Isaac 
6749c58f1c22SToby Isaac   Not Collective
6750c58f1c22SToby Isaac 
6751c58f1c22SToby Isaac   Input Parameters:
6752bb7acecfSBarry Smith + dm    - The `DM` object
6753c58f1c22SToby Isaac . name  - The label name
6754c58f1c22SToby Isaac . point - The mesh point
6755c58f1c22SToby Isaac - value - The label value for this point
6756c58f1c22SToby Isaac 
6757c58f1c22SToby Isaac   Output Parameter:
6758c58f1c22SToby Isaac 
6759c58f1c22SToby Isaac   Level: beginner
6760c58f1c22SToby Isaac 
67611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6762c58f1c22SToby Isaac @*/
6763d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6764d71ae5a4SJacob Faibussowitsch {
6765c58f1c22SToby Isaac   DMLabel label;
6766c58f1c22SToby Isaac 
6767c58f1c22SToby Isaac   PetscFunctionBegin;
6768c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67694f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67709566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6771c58f1c22SToby Isaac   if (!label) {
67729566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
67739566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6774c58f1c22SToby Isaac   }
67759566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
67763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6777c58f1c22SToby Isaac }
6778c58f1c22SToby Isaac 
6779c58f1c22SToby Isaac /*@C
6780bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
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 . point - The mesh point
6788c58f1c22SToby Isaac - value - The label value for this point
6789c58f1c22SToby Isaac 
6790c58f1c22SToby Isaac   Level: beginner
6791c58f1c22SToby Isaac 
67921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6793c58f1c22SToby Isaac @*/
6794d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6795d71ae5a4SJacob Faibussowitsch {
6796c58f1c22SToby Isaac   DMLabel label;
6797c58f1c22SToby Isaac 
6798c58f1c22SToby Isaac   PetscFunctionBegin;
6799c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68004f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68019566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68023ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68039566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
68043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6805c58f1c22SToby Isaac }
6806c58f1c22SToby Isaac 
6807c58f1c22SToby Isaac /*@C
6808bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6809c58f1c22SToby Isaac 
6810c58f1c22SToby Isaac   Not Collective
6811c58f1c22SToby Isaac 
6812c58f1c22SToby Isaac   Input Parameters:
6813bb7acecfSBarry Smith + dm   - The `DM` object
6814c58f1c22SToby Isaac - name - The label name
6815c58f1c22SToby Isaac 
6816c58f1c22SToby Isaac   Output Parameter:
6817c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6818c58f1c22SToby Isaac 
6819c58f1c22SToby Isaac   Level: beginner
6820c58f1c22SToby Isaac 
6821*73ff1848SBarry Smith   Developer Note:
6822bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6823bb7acecfSBarry Smith 
68241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6825c58f1c22SToby Isaac @*/
6826d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6827d71ae5a4SJacob Faibussowitsch {
6828c58f1c22SToby Isaac   DMLabel label;
6829c58f1c22SToby Isaac 
6830c58f1c22SToby Isaac   PetscFunctionBegin;
6831c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68324f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68334f572ea9SToby Isaac   PetscAssertPointer(size, 3);
68349566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6835c58f1c22SToby Isaac   *size = 0;
68363ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68379566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
68383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6839c58f1c22SToby Isaac }
6840c58f1c22SToby Isaac 
6841c58f1c22SToby Isaac /*@C
6842bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6843c58f1c22SToby Isaac 
6844c58f1c22SToby Isaac   Not Collective
6845c58f1c22SToby Isaac 
6846c58f1c22SToby Isaac   Input Parameters:
684760225df5SJacob Faibussowitsch + dm   - The `DM` object
6848c58f1c22SToby Isaac - name - The label name
6849c58f1c22SToby Isaac 
6850c58f1c22SToby Isaac   Output Parameter:
685120f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6852c58f1c22SToby Isaac 
6853c58f1c22SToby Isaac   Level: beginner
6854c58f1c22SToby Isaac 
68551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6856c58f1c22SToby Isaac @*/
6857d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6858d71ae5a4SJacob Faibussowitsch {
6859c58f1c22SToby Isaac   DMLabel label;
6860c58f1c22SToby Isaac 
6861c58f1c22SToby Isaac   PetscFunctionBegin;
6862c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68634f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68644f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
68659566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6866c58f1c22SToby Isaac   *ids = NULL;
6867dab2e251SBlaise Bourdin   if (label) {
68689566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6869dab2e251SBlaise Bourdin   } else {
6870dab2e251SBlaise Bourdin     /* returning an empty IS */
68719566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6872dab2e251SBlaise Bourdin   }
68733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6874c58f1c22SToby Isaac }
6875c58f1c22SToby Isaac 
6876c58f1c22SToby Isaac /*@C
6877c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6878c58f1c22SToby Isaac 
6879c58f1c22SToby Isaac   Not Collective
6880c58f1c22SToby Isaac 
6881c58f1c22SToby Isaac   Input Parameters:
6882bb7acecfSBarry Smith + dm    - The `DM` object
6883c58f1c22SToby Isaac . name  - The label name
6884c58f1c22SToby Isaac - value - The stratum value
6885c58f1c22SToby Isaac 
6886c58f1c22SToby Isaac   Output Parameter:
6887bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6888c58f1c22SToby Isaac 
6889c58f1c22SToby Isaac   Level: beginner
6890c58f1c22SToby Isaac 
68911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6892c58f1c22SToby Isaac @*/
6893d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6894d71ae5a4SJacob Faibussowitsch {
6895c58f1c22SToby Isaac   DMLabel label;
6896c58f1c22SToby Isaac 
6897c58f1c22SToby Isaac   PetscFunctionBegin;
6898c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68994f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69004f572ea9SToby Isaac   PetscAssertPointer(size, 4);
69019566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6902c58f1c22SToby Isaac   *size = 0;
69033ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69049566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
69053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6906c58f1c22SToby Isaac }
6907c58f1c22SToby Isaac 
6908c58f1c22SToby Isaac /*@C
6909c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6910c58f1c22SToby Isaac 
6911c58f1c22SToby Isaac   Not Collective
6912c58f1c22SToby Isaac 
6913c58f1c22SToby Isaac   Input Parameters:
6914bb7acecfSBarry Smith + dm    - The `DM` object
6915c58f1c22SToby Isaac . name  - The label name
6916c58f1c22SToby Isaac - value - The stratum value
6917c58f1c22SToby Isaac 
6918c58f1c22SToby Isaac   Output Parameter:
691920f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
6920c58f1c22SToby Isaac 
6921c58f1c22SToby Isaac   Level: beginner
6922c58f1c22SToby Isaac 
69231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6924c58f1c22SToby Isaac @*/
6925d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6926d71ae5a4SJacob Faibussowitsch {
6927c58f1c22SToby Isaac   DMLabel label;
6928c58f1c22SToby Isaac 
6929c58f1c22SToby Isaac   PetscFunctionBegin;
6930c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69314f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69324f572ea9SToby Isaac   PetscAssertPointer(points, 4);
69339566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6934c58f1c22SToby Isaac   *points = NULL;
69353ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69369566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
69373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6938c58f1c22SToby Isaac }
6939c58f1c22SToby Isaac 
69404de306b1SToby Isaac /*@C
69419044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
69424de306b1SToby Isaac 
69434de306b1SToby Isaac   Not Collective
69444de306b1SToby Isaac 
69454de306b1SToby Isaac   Input Parameters:
6946bb7acecfSBarry Smith + dm     - The `DM` object
69474de306b1SToby Isaac . name   - The label name
69484de306b1SToby Isaac . value  - The stratum value
69494de306b1SToby Isaac - points - The stratum points
69504de306b1SToby Isaac 
69514de306b1SToby Isaac   Level: beginner
69524de306b1SToby Isaac 
69531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
69544de306b1SToby Isaac @*/
6955d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6956d71ae5a4SJacob Faibussowitsch {
69574de306b1SToby Isaac   DMLabel label;
69584de306b1SToby Isaac 
69594de306b1SToby Isaac   PetscFunctionBegin;
69604de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69614f572ea9SToby Isaac   PetscAssertPointer(name, 2);
6962292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
69639566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
69643ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69659566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
69663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
69674de306b1SToby Isaac }
69684de306b1SToby Isaac 
6969c58f1c22SToby Isaac /*@C
6970bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6971c58f1c22SToby Isaac 
6972c58f1c22SToby Isaac   Not Collective
6973c58f1c22SToby Isaac 
6974c58f1c22SToby Isaac   Input Parameters:
6975bb7acecfSBarry Smith + dm    - The `DM` object
6976c58f1c22SToby Isaac . name  - The label name
6977c58f1c22SToby Isaac - value - The label value for this point
6978c58f1c22SToby Isaac 
6979c58f1c22SToby Isaac   Output Parameter:
6980c58f1c22SToby Isaac 
6981c58f1c22SToby Isaac   Level: beginner
6982c58f1c22SToby Isaac 
69831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6984c58f1c22SToby Isaac @*/
6985d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6986d71ae5a4SJacob Faibussowitsch {
6987c58f1c22SToby Isaac   DMLabel label;
6988c58f1c22SToby Isaac 
6989c58f1c22SToby Isaac   PetscFunctionBegin;
6990c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69914f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69929566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
69933ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69949566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
69953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6996c58f1c22SToby Isaac }
6997c58f1c22SToby Isaac 
6998c58f1c22SToby Isaac /*@
6999bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
7000c58f1c22SToby Isaac 
7001c58f1c22SToby Isaac   Not Collective
7002c58f1c22SToby Isaac 
7003c58f1c22SToby Isaac   Input Parameter:
7004bb7acecfSBarry Smith . dm - The `DM` object
7005c58f1c22SToby Isaac 
7006c58f1c22SToby Isaac   Output Parameter:
7007c58f1c22SToby Isaac . numLabels - the number of Labels
7008c58f1c22SToby Isaac 
7009c58f1c22SToby Isaac   Level: intermediate
7010c58f1c22SToby Isaac 
70111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7012c58f1c22SToby Isaac @*/
7013d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7014d71ae5a4SJacob Faibussowitsch {
70155d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7016c58f1c22SToby Isaac   PetscInt    n    = 0;
7017c58f1c22SToby Isaac 
7018c58f1c22SToby Isaac   PetscFunctionBegin;
7019c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70204f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
70219371c9d4SSatish Balay   while (next) {
70229371c9d4SSatish Balay     ++n;
70239371c9d4SSatish Balay     next = next->next;
70249371c9d4SSatish Balay   }
7025c58f1c22SToby Isaac   *numLabels = n;
70263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7027c58f1c22SToby Isaac }
7028c58f1c22SToby Isaac 
7029c58f1c22SToby Isaac /*@C
7030c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7031c58f1c22SToby Isaac 
7032c58f1c22SToby Isaac   Not Collective
7033c58f1c22SToby Isaac 
7034c58f1c22SToby Isaac   Input Parameters:
7035bb7acecfSBarry Smith + dm - The `DM` object
7036c58f1c22SToby Isaac - n  - the label number
7037c58f1c22SToby Isaac 
7038c58f1c22SToby Isaac   Output Parameter:
7039c58f1c22SToby Isaac . name - the label name
7040c58f1c22SToby Isaac 
7041c58f1c22SToby Isaac   Level: intermediate
7042c58f1c22SToby Isaac 
7043*73ff1848SBarry Smith   Developer Note:
7044bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
7045bb7acecfSBarry Smith 
70461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7047c58f1c22SToby Isaac @*/
7048d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7049d71ae5a4SJacob Faibussowitsch {
70505d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7051c58f1c22SToby Isaac   PetscInt    l    = 0;
7052c58f1c22SToby Isaac 
7053c58f1c22SToby Isaac   PetscFunctionBegin;
7054c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70554f572ea9SToby Isaac   PetscAssertPointer(name, 3);
7056c58f1c22SToby Isaac   while (next) {
7057c58f1c22SToby Isaac     if (l == n) {
70589566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
70593ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7060c58f1c22SToby Isaac     }
7061c58f1c22SToby Isaac     ++l;
7062c58f1c22SToby Isaac     next = next->next;
7063c58f1c22SToby Isaac   }
706463a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7065c58f1c22SToby Isaac }
7066c58f1c22SToby Isaac 
7067c58f1c22SToby Isaac /*@C
7068bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
7069c58f1c22SToby Isaac 
7070c58f1c22SToby Isaac   Not Collective
7071c58f1c22SToby Isaac 
7072c58f1c22SToby Isaac   Input Parameters:
7073bb7acecfSBarry Smith + dm   - The `DM` object
7074c58f1c22SToby Isaac - name - The label name
7075c58f1c22SToby Isaac 
7076c58f1c22SToby Isaac   Output Parameter:
7077bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
7078c58f1c22SToby Isaac 
7079c58f1c22SToby Isaac   Level: intermediate
7080c58f1c22SToby Isaac 
70811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7082c58f1c22SToby Isaac @*/
7083d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7084d71ae5a4SJacob Faibussowitsch {
70855d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7086d67d17b1SMatthew G. Knepley   const char *lname;
7087c58f1c22SToby Isaac 
7088c58f1c22SToby Isaac   PetscFunctionBegin;
7089c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70904f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70914f572ea9SToby Isaac   PetscAssertPointer(hasLabel, 3);
7092c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7093c58f1c22SToby Isaac   while (next) {
70949566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70959566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7096c58f1c22SToby Isaac     if (*hasLabel) break;
7097c58f1c22SToby Isaac     next = next->next;
7098c58f1c22SToby Isaac   }
70993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7100c58f1c22SToby Isaac }
7101c58f1c22SToby Isaac 
7102a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7103c58f1c22SToby Isaac /*@C
710420f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
7105c58f1c22SToby Isaac 
7106c58f1c22SToby Isaac   Not Collective
7107c58f1c22SToby Isaac 
7108c58f1c22SToby Isaac   Input Parameters:
7109bb7acecfSBarry Smith + dm   - The `DM` object
7110c58f1c22SToby Isaac - name - The label name
7111c58f1c22SToby Isaac 
7112c58f1c22SToby Isaac   Output Parameter:
711320f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
7114c58f1c22SToby Isaac 
7115bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7116bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7117bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7118bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7119bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7120bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7121bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
71226d7c9049SMatthew G. Knepley 
7123c58f1c22SToby Isaac   Level: intermediate
7124c58f1c22SToby Isaac 
712560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7126c58f1c22SToby Isaac @*/
7127d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7128d71ae5a4SJacob Faibussowitsch {
71295d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7130c58f1c22SToby Isaac   PetscBool   hasLabel;
7131d67d17b1SMatthew G. Knepley   const char *lname;
7132c58f1c22SToby Isaac 
7133c58f1c22SToby Isaac   PetscFunctionBegin;
7134c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71354f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71364f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7137c58f1c22SToby Isaac   *label = NULL;
7138c58f1c22SToby Isaac   while (next) {
71399566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71409566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7141c58f1c22SToby Isaac     if (hasLabel) {
7142c58f1c22SToby Isaac       *label = next->label;
7143c58f1c22SToby Isaac       break;
7144c58f1c22SToby Isaac     }
7145c58f1c22SToby Isaac     next = next->next;
7146c58f1c22SToby Isaac   }
71473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7148c58f1c22SToby Isaac }
7149c58f1c22SToby Isaac 
7150c58f1c22SToby Isaac /*@C
7151bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7152c58f1c22SToby Isaac 
7153c58f1c22SToby Isaac   Not Collective
7154c58f1c22SToby Isaac 
7155c58f1c22SToby Isaac   Input Parameters:
7156bb7acecfSBarry Smith + dm - The `DM` object
7157c58f1c22SToby Isaac - n  - the label number
7158c58f1c22SToby Isaac 
7159c58f1c22SToby Isaac   Output Parameter:
7160c58f1c22SToby Isaac . label - the label
7161c58f1c22SToby Isaac 
7162c58f1c22SToby Isaac   Level: intermediate
7163c58f1c22SToby Isaac 
71641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7165c58f1c22SToby Isaac @*/
7166d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7167d71ae5a4SJacob Faibussowitsch {
71685d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7169c58f1c22SToby Isaac   PetscInt    l    = 0;
7170c58f1c22SToby Isaac 
7171c58f1c22SToby Isaac   PetscFunctionBegin;
7172c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71734f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7174c58f1c22SToby Isaac   while (next) {
7175c58f1c22SToby Isaac     if (l == n) {
7176c58f1c22SToby Isaac       *label = next->label;
71773ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7178c58f1c22SToby Isaac     }
7179c58f1c22SToby Isaac     ++l;
7180c58f1c22SToby Isaac     next = next->next;
7181c58f1c22SToby Isaac   }
718263a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7183c58f1c22SToby Isaac }
7184c58f1c22SToby Isaac 
7185c58f1c22SToby Isaac /*@C
7186bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7187c58f1c22SToby Isaac 
7188c58f1c22SToby Isaac   Not Collective
7189c58f1c22SToby Isaac 
7190c58f1c22SToby Isaac   Input Parameters:
7191bb7acecfSBarry Smith + dm    - The `DM` object
7192bb7acecfSBarry Smith - label - The `DMLabel`
7193c58f1c22SToby Isaac 
7194c58f1c22SToby Isaac   Level: developer
7195c58f1c22SToby Isaac 
719660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7197c58f1c22SToby Isaac @*/
7198d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7199d71ae5a4SJacob Faibussowitsch {
72005d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7201c58f1c22SToby Isaac   PetscBool   hasLabel;
7202d67d17b1SMatthew G. Knepley   const char *lname;
72035d80c0bfSVaclav Hapla   PetscBool   flg;
7204c58f1c22SToby Isaac 
7205c58f1c22SToby Isaac   PetscFunctionBegin;
7206c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72079566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
72089566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
72097a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
72109566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7211c58f1c22SToby Isaac   tmpLabel->label  = label;
7212c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
72135d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
72145d80c0bfSVaclav Hapla   *p = tmpLabel;
72159566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
72169566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
72175d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
72189566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7219ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
72203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7221c58f1c22SToby Isaac }
7222c58f1c22SToby Isaac 
7223a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7224c58f1c22SToby Isaac /*@C
72254a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
72264a7ee7d0SMatthew G. Knepley 
72274a7ee7d0SMatthew G. Knepley   Not Collective
72284a7ee7d0SMatthew G. Knepley 
72294a7ee7d0SMatthew G. Knepley   Input Parameters:
7230bb7acecfSBarry Smith + dm    - The `DM` object
7231bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
72324a7ee7d0SMatthew G. Knepley 
7233bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7234bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7235bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7236bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7237bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7238bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7239bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
72404a7ee7d0SMatthew G. Knepley 
72414a7ee7d0SMatthew G. Knepley   Level: intermediate
72424a7ee7d0SMatthew G. Knepley 
72431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
72444a7ee7d0SMatthew G. Knepley @*/
7245d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7246d71ae5a4SJacob Faibussowitsch {
72474a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
72484a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
72494a7ee7d0SMatthew G. Knepley   const char *name, *lname;
72504a7ee7d0SMatthew G. Knepley 
72514a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
72524a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72534a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
72549566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
72554a7ee7d0SMatthew G. Knepley   while (next) {
72569566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72579566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
72584a7ee7d0SMatthew G. Knepley     if (hasLabel) {
72599566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
72609566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
72614a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
72629566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
72634a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
72649566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
72654a7ee7d0SMatthew G. Knepley       next->label = label;
72664a7ee7d0SMatthew G. Knepley       break;
72674a7ee7d0SMatthew G. Knepley     }
72684a7ee7d0SMatthew G. Knepley     next = next->next;
72694a7ee7d0SMatthew G. Knepley   }
72703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
72714a7ee7d0SMatthew G. Knepley }
72724a7ee7d0SMatthew G. Knepley 
72734a7ee7d0SMatthew G. Knepley /*@C
7274bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7275c58f1c22SToby Isaac 
7276c58f1c22SToby Isaac   Not Collective
7277c58f1c22SToby Isaac 
7278c58f1c22SToby Isaac   Input Parameters:
7279bb7acecfSBarry Smith + dm   - The `DM` object
7280c58f1c22SToby Isaac - name - The label name
7281c58f1c22SToby Isaac 
7282c58f1c22SToby Isaac   Output Parameter:
728320f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7284bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7285c58f1c22SToby Isaac 
7286c58f1c22SToby Isaac   Level: developer
7287c58f1c22SToby Isaac 
72881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7289c58f1c22SToby Isaac @*/
7290d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7291d71ae5a4SJacob Faibussowitsch {
729295d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7293c58f1c22SToby Isaac   PetscBool   hasLabel;
7294d67d17b1SMatthew G. Knepley   const char *lname;
7295c58f1c22SToby Isaac 
7296c58f1c22SToby Isaac   PetscFunctionBegin;
7297c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72984f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7299e5472504SVaclav Hapla   if (label) {
73004f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7301c58f1c22SToby Isaac     *label = NULL;
7302e5472504SVaclav Hapla   }
73035d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
73049566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
73059566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7306c58f1c22SToby Isaac     if (hasLabel) {
730795d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
73089566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
730995d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
73109566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7311ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
731295d578d6SVaclav Hapla       if (label) *label = link->label;
73139566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
73149566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7315c58f1c22SToby Isaac       break;
7316c58f1c22SToby Isaac     }
7317c58f1c22SToby Isaac   }
73183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7319c58f1c22SToby Isaac }
7320c58f1c22SToby Isaac 
7321306894acSVaclav Hapla /*@
7322bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7323306894acSVaclav Hapla 
7324306894acSVaclav Hapla   Not Collective
7325306894acSVaclav Hapla 
7326306894acSVaclav Hapla   Input Parameters:
7327bb7acecfSBarry Smith + dm           - The `DM` object
7328bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
732920f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7330306894acSVaclav Hapla 
7331306894acSVaclav Hapla   Level: developer
7332306894acSVaclav Hapla 
7333bb7acecfSBarry Smith   Note:
7334306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7335bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7336306894acSVaclav Hapla   *label nullified.
7337306894acSVaclav Hapla 
73381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7339306894acSVaclav Hapla @*/
7340d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7341d71ae5a4SJacob Faibussowitsch {
734243e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7343306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7344306894acSVaclav Hapla 
7345306894acSVaclav Hapla   PetscFunctionBegin;
7346306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73474f572ea9SToby Isaac   PetscAssertPointer(label, 2);
73483ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7349306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7350306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
73515d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
735243e45a93SVaclav Hapla     if (*label == link->label) {
7353306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
735443e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7355306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7356ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
735743e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
73589566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
73599566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7360306894acSVaclav Hapla       break;
7361306894acSVaclav Hapla     }
7362306894acSVaclav Hapla   }
73637a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
73643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7365306894acSVaclav Hapla }
7366306894acSVaclav Hapla 
7367c58f1c22SToby Isaac /*@C
7368c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7369c58f1c22SToby Isaac 
7370c58f1c22SToby Isaac   Not Collective
7371c58f1c22SToby Isaac 
7372c58f1c22SToby Isaac   Input Parameters:
7373bb7acecfSBarry Smith + dm   - The `DM` object
7374c58f1c22SToby Isaac - name - The label name
7375c58f1c22SToby Isaac 
7376c58f1c22SToby Isaac   Output Parameter:
7377c58f1c22SToby Isaac . output - The flag for output
7378c58f1c22SToby Isaac 
7379c58f1c22SToby Isaac   Level: developer
7380c58f1c22SToby Isaac 
73811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7382c58f1c22SToby Isaac @*/
7383d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7384d71ae5a4SJacob Faibussowitsch {
73855d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7386d67d17b1SMatthew G. Knepley   const char *lname;
7387c58f1c22SToby Isaac 
7388c58f1c22SToby Isaac   PetscFunctionBegin;
7389c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73904f572ea9SToby Isaac   PetscAssertPointer(name, 2);
73914f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7392c58f1c22SToby Isaac   while (next) {
7393c58f1c22SToby Isaac     PetscBool flg;
7394c58f1c22SToby Isaac 
73959566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73969566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
73979371c9d4SSatish Balay     if (flg) {
73989371c9d4SSatish Balay       *output = next->output;
73993ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74009371c9d4SSatish Balay     }
7401c58f1c22SToby Isaac     next = next->next;
7402c58f1c22SToby Isaac   }
740398921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7404c58f1c22SToby Isaac }
7405c58f1c22SToby Isaac 
7406c58f1c22SToby Isaac /*@C
7407bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7408c58f1c22SToby Isaac 
7409c58f1c22SToby Isaac   Not Collective
7410c58f1c22SToby Isaac 
7411c58f1c22SToby Isaac   Input Parameters:
7412bb7acecfSBarry Smith + dm     - The `DM` object
7413c58f1c22SToby Isaac . name   - The label name
7414bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7415c58f1c22SToby Isaac 
7416c58f1c22SToby Isaac   Level: developer
7417c58f1c22SToby Isaac 
74181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7419c58f1c22SToby Isaac @*/
7420d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7421d71ae5a4SJacob Faibussowitsch {
74225d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7423d67d17b1SMatthew G. Knepley   const char *lname;
7424c58f1c22SToby Isaac 
7425c58f1c22SToby Isaac   PetscFunctionBegin;
7426c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74274f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7428c58f1c22SToby Isaac   while (next) {
7429c58f1c22SToby Isaac     PetscBool flg;
7430c58f1c22SToby Isaac 
74319566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74329566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74339371c9d4SSatish Balay     if (flg) {
74349371c9d4SSatish Balay       next->output = output;
74353ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74369371c9d4SSatish Balay     }
7437c58f1c22SToby Isaac     next = next->next;
7438c58f1c22SToby Isaac   }
743998921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7440c58f1c22SToby Isaac }
7441c58f1c22SToby Isaac 
7442c58f1c22SToby Isaac /*@
7443bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7444c58f1c22SToby Isaac 
744520f4b53cSBarry Smith   Collective
7446c58f1c22SToby Isaac 
7447d8d19677SJose E. Roman   Input Parameters:
7448bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7449bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7450bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7451bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7452bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7453c58f1c22SToby Isaac 
7454c58f1c22SToby Isaac   Level: intermediate
7455c58f1c22SToby Isaac 
7456bb7acecfSBarry Smith   Note:
74572cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7458c58f1c22SToby Isaac 
74591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7460c58f1c22SToby Isaac @*/
7461d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7462d71ae5a4SJacob Faibussowitsch {
74632cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7464c58f1c22SToby Isaac   const char *name;
7465c58f1c22SToby Isaac   PetscBool   flg;
74665d80c0bfSVaclav Hapla   DMLabelLink link;
7467c58f1c22SToby Isaac 
74685d80c0bfSVaclav Hapla   PetscFunctionBegin;
74695d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
74705d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
74715d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
74725d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
74737a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
74743ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
74755d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
74765d80c0bfSVaclav Hapla     label = link->label;
74779566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
74785d80c0bfSVaclav Hapla     if (!all) {
74799566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7480c58f1c22SToby Isaac       if (flg) continue;
74819566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
74827d5acc75SStefano Zampini       if (flg) continue;
74839566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7484ba2698f1SMatthew G. Knepley       if (flg) continue;
74855d80c0bfSVaclav Hapla     }
74869566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
74872cbb9b06SVaclav Hapla     if (labelOld) {
74882cbb9b06SVaclav Hapla       switch (emode) {
7489d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7490d71ae5a4SJacob Faibussowitsch         continue;
7491d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7492d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7493d71ae5a4SJacob Faibussowitsch         break;
7494d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7495d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7496d71ae5a4SJacob Faibussowitsch       default:
7497d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
74982cbb9b06SVaclav Hapla       }
74992cbb9b06SVaclav Hapla     }
75005d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
75019566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
75025d80c0bfSVaclav Hapla     } else {
75035d80c0bfSVaclav Hapla       labelNew = label;
75045d80c0bfSVaclav Hapla     }
75059566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
75069566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7507c58f1c22SToby Isaac   }
75083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7509c58f1c22SToby Isaac }
7510461a15a0SLisandro Dalcin 
7511609dae6eSVaclav Hapla /*@C
7512b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7513609dae6eSVaclav Hapla 
751420f4b53cSBarry Smith   Collective; No Fortran Support
7515609dae6eSVaclav Hapla 
7516609dae6eSVaclav Hapla   Input Parameters:
7517bb7acecfSBarry Smith + dm0 - First `DM` object
7518bb7acecfSBarry Smith - dm1 - Second `DM` object
7519609dae6eSVaclav Hapla 
7520a4e35b19SJacob Faibussowitsch   Output Parameters:
75215efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
752220f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7523609dae6eSVaclav Hapla 
7524609dae6eSVaclav Hapla   Level: intermediate
7525609dae6eSVaclav Hapla 
7526609dae6eSVaclav Hapla   Notes:
7527bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7528bb7acecfSBarry Smith 
752920f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7530bb7acecfSBarry Smith 
753120f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7532609dae6eSVaclav Hapla 
75335efe38ccSVaclav Hapla   The output message is set independently on each rank.
7534bb7acecfSBarry Smith 
7535bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7536bb7acecfSBarry Smith 
753720f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7538bb7acecfSBarry Smith 
753920f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7540609dae6eSVaclav Hapla 
7541609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7542bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7543609dae6eSVaclav Hapla 
75441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7545609dae6eSVaclav Hapla @*/
7546d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7547d71ae5a4SJacob Faibussowitsch {
75485efe38ccSVaclav Hapla   PetscInt    n, i;
7549609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
75505efe38ccSVaclav Hapla   PetscBool   eq;
7551609dae6eSVaclav Hapla   MPI_Comm    comm;
75525efe38ccSVaclav Hapla   PetscMPIInt rank;
7553609dae6eSVaclav Hapla 
7554609dae6eSVaclav Hapla   PetscFunctionBegin;
7555609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7556609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7557609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
75584f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
75594f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
75609566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
75619566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
75625efe38ccSVaclav Hapla   {
75635efe38ccSVaclav Hapla     PetscInt n1;
75645efe38ccSVaclav Hapla 
75659566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
75669566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
75675efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
756848a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7569712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
75705efe38ccSVaclav Hapla     if (!eq) goto finish;
75715efe38ccSVaclav Hapla   }
75725efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7573609dae6eSVaclav Hapla     DMLabel     l0, l1;
7574609dae6eSVaclav Hapla     const char *name;
7575609dae6eSVaclav Hapla     char       *msgInner;
7576609dae6eSVaclav Hapla 
7577609dae6eSVaclav Hapla     /* Ignore label order */
75789566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
75799566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
75809566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7581609dae6eSVaclav Hapla     if (!l1) {
758263a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
75835efe38ccSVaclav Hapla       eq = PETSC_FALSE;
75845efe38ccSVaclav Hapla       break;
7585609dae6eSVaclav Hapla     }
75869566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
75879566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
75889566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
75895efe38ccSVaclav Hapla     if (!eq) break;
7590609dae6eSVaclav Hapla   }
7591712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7592609dae6eSVaclav Hapla finish:
75935efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7594609dae6eSVaclav Hapla   if (message) {
7595609dae6eSVaclav Hapla     *message = NULL;
759648a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
75975efe38ccSVaclav Hapla   } else {
759848a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
75999566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
76005efe38ccSVaclav Hapla   }
76015efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
76025efe38ccSVaclav Hapla   if (equal) *equal = eq;
76037a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
76043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7605609dae6eSVaclav Hapla }
7606609dae6eSVaclav Hapla 
7607d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7608d71ae5a4SJacob Faibussowitsch {
7609461a15a0SLisandro Dalcin   PetscFunctionBegin;
76104f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7611461a15a0SLisandro Dalcin   if (!*label) {
76129566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
76139566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7614461a15a0SLisandro Dalcin   }
76159566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
76163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7617461a15a0SLisandro Dalcin }
7618461a15a0SLisandro Dalcin 
76190fdc7489SMatthew Knepley /*
76200fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
76210fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
76220fdc7489SMatthew Knepley   (label, id) pair in the DM.
76230fdc7489SMatthew Knepley 
76240fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
76250fdc7489SMatthew Knepley   each label.
76260fdc7489SMatthew Knepley */
7627d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7628d71ae5a4SJacob Faibussowitsch {
76290fdc7489SMatthew Knepley   DMUniversalLabel ul;
76300fdc7489SMatthew Knepley   PetscBool       *active;
76310fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
76320fdc7489SMatthew Knepley 
76330fdc7489SMatthew Knepley   PetscFunctionBegin;
76349566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
76359566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
76369566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
76379566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
76380fdc7489SMatthew Knepley   ul->Nl = 0;
76390fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
76400fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
76410fdc7489SMatthew Knepley     const char *name;
76420fdc7489SMatthew Knepley 
76439566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
76449566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
76459566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
76460fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
76470fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
76480fdc7489SMatthew Knepley   }
76499566063dSJacob 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));
76500fdc7489SMatthew Knepley   ul->Nv = 0;
76510fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
76520fdc7489SMatthew Knepley     DMLabel     label;
76530fdc7489SMatthew Knepley     PetscInt    nv;
76540fdc7489SMatthew Knepley     const char *name;
76550fdc7489SMatthew Knepley 
76560fdc7489SMatthew Knepley     if (!active[l]) continue;
76579566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
76589566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
76599566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
76609566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
76610fdc7489SMatthew Knepley     ul->indices[m] = l;
76620fdc7489SMatthew Knepley     ul->Nv += nv;
76630fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
76640fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
76650fdc7489SMatthew Knepley     ++m;
76660fdc7489SMatthew Knepley   }
76670fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
76680fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
76690fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
76700fdc7489SMatthew Knepley   }
76710fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
76720fdc7489SMatthew Knepley     PetscInt b;
76730fdc7489SMatthew Knepley 
76740fdc7489SMatthew Knepley     ul->masks[l] = 0;
76750fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
76760fdc7489SMatthew Knepley   }
76779566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
76780fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
76790fdc7489SMatthew Knepley     DMLabel         label;
76800fdc7489SMatthew Knepley     IS              valueIS;
76810fdc7489SMatthew Knepley     const PetscInt *varr;
76820fdc7489SMatthew Knepley     PetscInt        nv, v;
76830fdc7489SMatthew Knepley 
76840fdc7489SMatthew Knepley     if (!active[l]) continue;
76859566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
76869566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
76879566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
76889566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7689ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
76909566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
76919566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
76929566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
76930fdc7489SMatthew Knepley     ++m;
76940fdc7489SMatthew Knepley   }
76959566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
76960fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
76970fdc7489SMatthew Knepley     PetscInt  uval   = 0;
76980fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
76990fdc7489SMatthew Knepley 
77000fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
77010fdc7489SMatthew Knepley       DMLabel  label;
77020649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
77030fdc7489SMatthew Knepley 
77040fdc7489SMatthew Knepley       if (!active[l]) continue;
77059566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
77069566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
77079566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
77089371c9d4SSatish Balay       if (val == defval) {
77099371c9d4SSatish Balay         ++m;
77109371c9d4SSatish Balay         continue;
77119371c9d4SSatish Balay       }
77120649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
77130fdc7489SMatthew Knepley       marked = PETSC_TRUE;
77149566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
771563a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
77160fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
77170fdc7489SMatthew Knepley       ++m;
77180fdc7489SMatthew Knepley     }
77199566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
77200fdc7489SMatthew Knepley   }
77219566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
77220fdc7489SMatthew Knepley   *universal = ul;
77233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77240fdc7489SMatthew Knepley }
77250fdc7489SMatthew Knepley 
7726d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7727d71ae5a4SJacob Faibussowitsch {
77280fdc7489SMatthew Knepley   PetscInt l;
77290fdc7489SMatthew Knepley 
77300fdc7489SMatthew Knepley   PetscFunctionBegin;
77319566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
77329566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
77339566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
77349566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
77359566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
77360fdc7489SMatthew Knepley   *universal = NULL;
77373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77380fdc7489SMatthew Knepley }
77390fdc7489SMatthew Knepley 
7740d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7741d71ae5a4SJacob Faibussowitsch {
77420fdc7489SMatthew Knepley   PetscFunctionBegin;
77434f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
77440fdc7489SMatthew Knepley   *ulabel = ul->label;
77453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77460fdc7489SMatthew Knepley }
77470fdc7489SMatthew Knepley 
7748d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7749d71ae5a4SJacob Faibussowitsch {
77500fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
77510fdc7489SMatthew Knepley 
77520fdc7489SMatthew Knepley   PetscFunctionBegin;
7753064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
77540fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
77559566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
77569566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
77570fdc7489SMatthew Knepley   }
77580fdc7489SMatthew Knepley   if (preserveOrder) {
77590fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
77600fdc7489SMatthew Knepley       const char *name;
77610fdc7489SMatthew Knepley       PetscBool   match;
77620fdc7489SMatthew Knepley 
77639566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
77649566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
776563a3b9bcSJacob 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]);
77660fdc7489SMatthew Knepley     }
77670fdc7489SMatthew Knepley   }
77683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77690fdc7489SMatthew Knepley }
77700fdc7489SMatthew Knepley 
7771d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7772d71ae5a4SJacob Faibussowitsch {
77730fdc7489SMatthew Knepley   PetscInt l;
77740fdc7489SMatthew Knepley 
77750fdc7489SMatthew Knepley   PetscFunctionBegin;
77760fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
77770fdc7489SMatthew Knepley     DMLabel  label;
77780fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
77790fdc7489SMatthew Knepley 
77800fdc7489SMatthew Knepley     if (lval) {
77819566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
77829566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
77839566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
77840fdc7489SMatthew Knepley     }
77850fdc7489SMatthew Knepley   }
77863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77870fdc7489SMatthew Knepley }
7788a8fb8f29SToby Isaac 
7789a8fb8f29SToby Isaac /*@
7790bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7791bb7acecfSBarry Smith 
779220f4b53cSBarry Smith   Not Collective
7793a8fb8f29SToby Isaac 
7794a8fb8f29SToby Isaac   Input Parameter:
7795bb7acecfSBarry Smith . dm - The `DM` object
7796a8fb8f29SToby Isaac 
7797a8fb8f29SToby Isaac   Output Parameter:
7798bb7acecfSBarry Smith . cdm - The coarse `DM`
7799a8fb8f29SToby Isaac 
7800a8fb8f29SToby Isaac   Level: intermediate
7801a8fb8f29SToby Isaac 
78021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7803a8fb8f29SToby Isaac @*/
7804d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7805d71ae5a4SJacob Faibussowitsch {
7806a8fb8f29SToby Isaac   PetscFunctionBegin;
7807a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78084f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7809a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
78103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7811a8fb8f29SToby Isaac }
7812a8fb8f29SToby Isaac 
7813a8fb8f29SToby Isaac /*@
7814bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7815a8fb8f29SToby Isaac 
7816a8fb8f29SToby Isaac   Input Parameters:
7817bb7acecfSBarry Smith + dm  - The `DM` object
7818bb7acecfSBarry Smith - cdm - The coarse `DM`
7819a8fb8f29SToby Isaac 
7820a8fb8f29SToby Isaac   Level: intermediate
7821a8fb8f29SToby Isaac 
7822bb7acecfSBarry Smith   Note:
7823bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7824bb7acecfSBarry Smith 
78251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7826a8fb8f29SToby Isaac @*/
7827d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7828d71ae5a4SJacob Faibussowitsch {
7829a8fb8f29SToby Isaac   PetscFunctionBegin;
7830a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7831a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
783289d734beSBarry Smith   if (dm == cdm) cdm = NULL;
78339566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
78349566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7835a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
78363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7837a8fb8f29SToby Isaac }
7838a8fb8f29SToby Isaac 
783988bdff64SToby Isaac /*@
7840bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
784188bdff64SToby Isaac 
784288bdff64SToby Isaac   Input Parameter:
7843bb7acecfSBarry Smith . dm - The `DM` object
784488bdff64SToby Isaac 
784588bdff64SToby Isaac   Output Parameter:
7846bb7acecfSBarry Smith . fdm - The fine `DM`
784788bdff64SToby Isaac 
784888bdff64SToby Isaac   Level: intermediate
784988bdff64SToby Isaac 
78501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
785188bdff64SToby Isaac @*/
7852d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7853d71ae5a4SJacob Faibussowitsch {
785488bdff64SToby Isaac   PetscFunctionBegin;
785588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78564f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
785788bdff64SToby Isaac   *fdm = dm->fineMesh;
78583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
785988bdff64SToby Isaac }
786088bdff64SToby Isaac 
786188bdff64SToby Isaac /*@
7862bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
786388bdff64SToby Isaac 
786488bdff64SToby Isaac   Input Parameters:
7865bb7acecfSBarry Smith + dm  - The `DM` object
7866bb7acecfSBarry Smith - fdm - The fine `DM`
786788bdff64SToby Isaac 
7868bb7acecfSBarry Smith   Level: developer
786988bdff64SToby Isaac 
7870bb7acecfSBarry Smith   Note:
7871bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7872bb7acecfSBarry Smith 
78731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
787488bdff64SToby Isaac @*/
7875d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7876d71ae5a4SJacob Faibussowitsch {
787788bdff64SToby Isaac   PetscFunctionBegin;
787888bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
787988bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
788089d734beSBarry Smith   if (dm == fdm) fdm = NULL;
78819566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
78829566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
788388bdff64SToby Isaac   dm->fineMesh = fdm;
78843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
788588bdff64SToby Isaac }
788688bdff64SToby Isaac 
7887a6ba4734SToby Isaac /*@C
7888bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7889a6ba4734SToby Isaac 
789020f4b53cSBarry Smith   Collective
7891783e2ec8SMatthew G. Knepley 
7892a6ba4734SToby Isaac   Input Parameters:
7893bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7894bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7895a6ba4734SToby Isaac . name     - The BC name
789645480ffeSMatthew G. Knepley . label    - The label defining constrained points
7897bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
789845480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7899a6ba4734SToby Isaac . field    - The field to constrain
790045480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7901a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7902a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
790356cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7904a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7905a6ba4734SToby Isaac 
790645480ffeSMatthew G. Knepley   Output Parameter:
790745480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
790845480ffeSMatthew G. Knepley 
7909a6ba4734SToby Isaac   Options Database Keys:
7910a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
7911a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7912a6ba4734SToby Isaac 
791320f4b53cSBarry Smith   Level: intermediate
791420f4b53cSBarry Smith 
7915bb7acecfSBarry Smith   Notes:
791637fdd005SBarry 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\:
7917*73ff1848SBarry Smith .vb
7918*73ff1848SBarry Smith  void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
7919*73ff1848SBarry Smith .ve
792056cf3b9cSMatthew G. Knepley 
7921a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
792256cf3b9cSMatthew G. Knepley 
792320f4b53cSBarry Smith .vb
792420f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
792520f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
792620f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
792720f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
792820f4b53cSBarry Smith .ve
792956cf3b9cSMatthew G. Knepley + dim - the spatial dimension
793056cf3b9cSMatthew G. Knepley . Nf - the number of fields
793156cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
793256cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
793356cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
793456cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
793556cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
793656cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
793756cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
793856cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
793956cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
794056cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
794156cf3b9cSMatthew G. Knepley . t - current time
794256cf3b9cSMatthew G. Knepley . x - coordinates of the current point
794356cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
794456cf3b9cSMatthew G. Knepley . constants - constant parameters
794556cf3b9cSMatthew G. Knepley - bcval - output values at the current point
794656cf3b9cSMatthew G. Knepley 
79471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
7948a6ba4734SToby Isaac @*/
7949d71ae5a4SJacob 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)
7950d71ae5a4SJacob Faibussowitsch {
7951e5e52638SMatthew G. Knepley   PetscDS ds;
7952a6ba4734SToby Isaac 
7953a6ba4734SToby Isaac   PetscFunctionBegin;
7954a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7955783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
795645480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
795745480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
795845480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
795945480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
796001a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
79619566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7962799db056SMatthew G. Knepley   /* Complete label */
7963799db056SMatthew G. Knepley   if (label) {
7964799db056SMatthew G. Knepley     PetscObject  obj;
7965799db056SMatthew G. Knepley     PetscClassId id;
7966799db056SMatthew G. Knepley 
7967799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7968799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7969799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7970799db056SMatthew G. Knepley       DM plex;
7971799db056SMatthew G. Knepley 
7972799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7973799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7974799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7975799db056SMatthew G. Knepley     }
7976799db056SMatthew G. Knepley   }
79779566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
79783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7979a6ba4734SToby Isaac }
7980a6ba4734SToby Isaac 
798145480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7982d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
7983d71ae5a4SJacob Faibussowitsch {
7984e5e52638SMatthew G. Knepley   PetscDS     ds;
7985dff059c6SToby Isaac   DMBoundary *lastnext;
7986e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7987e6f8dbb6SToby Isaac 
7988e6f8dbb6SToby Isaac   PetscFunctionBegin;
79899566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7990e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
799147a1f5adSToby Isaac   if (dm->boundary) {
799247a1f5adSToby Isaac     DMBoundary next = dm->boundary;
799347a1f5adSToby Isaac 
799447a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
79953ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
799647a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
799747a1f5adSToby Isaac     while (next) {
799847a1f5adSToby Isaac       DMBoundary b = next;
799947a1f5adSToby Isaac 
800047a1f5adSToby Isaac       next = b->next;
80019566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
8002a6ba4734SToby Isaac     }
800347a1f5adSToby Isaac     dm->boundary = NULL;
8004a6ba4734SToby Isaac   }
800547a1f5adSToby Isaac 
8006f4f49eeaSPierre Jolivet   lastnext = &dm->boundary;
8007e6f8dbb6SToby Isaac   while (dsbound) {
8008e6f8dbb6SToby Isaac     DMBoundary dmbound;
8009e6f8dbb6SToby Isaac 
80109566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
8011e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
801245480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
801347a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8014dff059c6SToby Isaac     *lastnext = dmbound;
8015f4f49eeaSPierre Jolivet     lastnext  = &dmbound->next;
8016dff059c6SToby Isaac     dsbound   = dsbound->next;
8017a6ba4734SToby Isaac   }
80183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8019a6ba4734SToby Isaac }
8020a6ba4734SToby Isaac 
8021bb7acecfSBarry Smith /* TODO: missing manual page */
8022d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8023d71ae5a4SJacob Faibussowitsch {
8024b95f2879SToby Isaac   DMBoundary b;
8025a6ba4734SToby Isaac 
8026a6ba4734SToby Isaac   PetscFunctionBegin;
8027a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80284f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
8029a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
80309566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
8031b95f2879SToby Isaac   b = dm->boundary;
8032a6ba4734SToby Isaac   while (b && !(*isBd)) {
8033e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8034e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8035a6ba4734SToby Isaac     PetscInt   i;
8036a6ba4734SToby Isaac 
803745480ffeSMatthew G. Knepley     if (label) {
80389566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8039a6ba4734SToby Isaac     }
8040a6ba4734SToby Isaac     b = b->next;
8041a6ba4734SToby Isaac   }
80423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8043a6ba4734SToby Isaac }
80444d6f44ffSToby Isaac 
80454d6f44ffSToby Isaac /*@C
8046bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
8047a6e0b375SMatthew G. Knepley 
804820f4b53cSBarry Smith   Collective
80494d6f44ffSToby Isaac 
80504d6f44ffSToby Isaac   Input Parameters:
8051bb7acecfSBarry Smith + dm    - The `DM`
80520709b2feSToby Isaac . time  - The time
80534d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
80544d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
80554d6f44ffSToby Isaac - mode  - The insertion mode for values
80564d6f44ffSToby Isaac 
80574d6f44ffSToby Isaac   Output Parameter:
80584d6f44ffSToby Isaac . X - vector
80594d6f44ffSToby Isaac 
806020f4b53cSBarry Smith   Calling sequence of `funcs`:
80614d6f44ffSToby Isaac + dim  - The spatial dimension
80628ec8862eSJed Brown . time - The time at which to sample
80634d6f44ffSToby Isaac . x    - The coordinates
806477b739a6SMatthew Knepley . Nc   - The number of components
80654d6f44ffSToby Isaac . u    - The output field values
80664d6f44ffSToby Isaac - ctx  - optional user-defined function context
80674d6f44ffSToby Isaac 
80684d6f44ffSToby Isaac   Level: developer
80694d6f44ffSToby Isaac 
8070bb7acecfSBarry Smith   Developer Notes:
8071bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8072bb7acecfSBarry Smith 
8073bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8074bb7acecfSBarry Smith 
80751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
80764d6f44ffSToby Isaac @*/
8077a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec X)
8078d71ae5a4SJacob Faibussowitsch {
80794d6f44ffSToby Isaac   Vec localX;
80804d6f44ffSToby Isaac 
80814d6f44ffSToby Isaac   PetscFunctionBegin;
80824d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8083708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
80849566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8085f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
80869566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
80879566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
80889566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
80899566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
8090708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
80913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80924d6f44ffSToby Isaac }
80934d6f44ffSToby Isaac 
8094a6e0b375SMatthew G. Knepley /*@C
8095bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
8096a6e0b375SMatthew G. Knepley 
809720f4b53cSBarry Smith   Not Collective
8098a6e0b375SMatthew G. Knepley 
8099a6e0b375SMatthew G. Knepley   Input Parameters:
8100bb7acecfSBarry Smith + dm    - The `DM`
8101a6e0b375SMatthew G. Knepley . time  - The time
8102a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
8103a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8104a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
8105a6e0b375SMatthew G. Knepley 
8106a6e0b375SMatthew G. Knepley   Output Parameter:
8107a6e0b375SMatthew G. Knepley . localX - vector
8108a6e0b375SMatthew G. Knepley 
810920f4b53cSBarry Smith   Calling sequence of `funcs`:
8110a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8111a4e35b19SJacob Faibussowitsch . time - The current timestep
8112a6e0b375SMatthew G. Knepley . x    - The coordinates
811377b739a6SMatthew Knepley . Nc   - The number of components
8114a6e0b375SMatthew G. Knepley . u    - The output field values
8115a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8116a6e0b375SMatthew G. Knepley 
8117a6e0b375SMatthew G. Knepley   Level: developer
8118a6e0b375SMatthew G. Knepley 
8119bb7acecfSBarry Smith   Developer Notes:
8120bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8121bb7acecfSBarry Smith 
8122bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8123bb7acecfSBarry Smith 
81241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8125a6e0b375SMatthew G. Knepley @*/
8126a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec localX)
8127d71ae5a4SJacob Faibussowitsch {
81284d6f44ffSToby Isaac   PetscFunctionBegin;
81294d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8130064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8131fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlocal, time, funcs, ctxs, mode, localX);
81323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81334d6f44ffSToby Isaac }
81344d6f44ffSToby Isaac 
8135a6e0b375SMatthew G. Knepley /*@C
8136bb7acecfSBarry 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.
8137a6e0b375SMatthew G. Knepley 
813820f4b53cSBarry Smith   Collective
8139a6e0b375SMatthew G. Knepley 
8140a6e0b375SMatthew G. Knepley   Input Parameters:
8141bb7acecfSBarry Smith + dm     - The `DM`
8142a6e0b375SMatthew G. Knepley . time   - The time
8143a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8144a4e35b19SJacob Faibussowitsch . ids    - The ids
8145a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8146a4e35b19SJacob Faibussowitsch . comps  - The components
8147bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8148a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8149bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8150a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8151a6e0b375SMatthew G. Knepley 
8152a6e0b375SMatthew G. Knepley   Output Parameter:
8153a6e0b375SMatthew G. Knepley . X - vector
8154a6e0b375SMatthew G. Knepley 
815520f4b53cSBarry Smith   Calling sequence of `funcs`:
8156a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8157a4e35b19SJacob Faibussowitsch . time - The current timestep
8158a6e0b375SMatthew G. Knepley . x    - The coordinates
815977b739a6SMatthew Knepley . Nc   - The number of components
8160a6e0b375SMatthew G. Knepley . u    - The output field values
8161a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8162a6e0b375SMatthew G. Knepley 
8163a6e0b375SMatthew G. Knepley   Level: developer
8164a6e0b375SMatthew G. Knepley 
8165bb7acecfSBarry Smith   Developer Notes:
8166bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8167bb7acecfSBarry Smith 
8168bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8169bb7acecfSBarry Smith 
81701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8171a6e0b375SMatthew G. Knepley @*/
8172a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec X)
8173d71ae5a4SJacob Faibussowitsch {
81742c53366bSMatthew G. Knepley   Vec localX;
81752c53366bSMatthew G. Knepley 
81762c53366bSMatthew G. Knepley   PetscFunctionBegin;
81772c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81789566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8179f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
81809566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
81819566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
81829566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
81839566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
81843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81852c53366bSMatthew G. Knepley }
81862c53366bSMatthew G. Knepley 
8187a6e0b375SMatthew G. Knepley /*@C
8188bb7acecfSBarry 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.
8189a6e0b375SMatthew G. Knepley 
819020f4b53cSBarry Smith   Not Collective
8191a6e0b375SMatthew G. Knepley 
8192a6e0b375SMatthew G. Knepley   Input Parameters:
8193bb7acecfSBarry Smith + dm     - The `DM`
8194a6e0b375SMatthew G. Knepley . time   - The time
8195bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8196a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8197a4e35b19SJacob Faibussowitsch . ids    - The ids
8198a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8199a4e35b19SJacob Faibussowitsch . comps  - The components
8200a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8201a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8202a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8203a6e0b375SMatthew G. Knepley 
8204a6e0b375SMatthew G. Knepley   Output Parameter:
8205a6e0b375SMatthew G. Knepley . localX - vector
8206a6e0b375SMatthew G. Knepley 
820720f4b53cSBarry Smith   Calling sequence of `funcs`:
8208a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8209a4e35b19SJacob Faibussowitsch . time - The current time
8210a6e0b375SMatthew G. Knepley . x    - The coordinates
821177b739a6SMatthew Knepley . Nc   - The number of components
8212a6e0b375SMatthew G. Knepley . u    - The output field values
8213a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8214a6e0b375SMatthew G. Knepley 
8215a6e0b375SMatthew G. Knepley   Level: developer
8216a6e0b375SMatthew G. Knepley 
8217bb7acecfSBarry Smith   Developer Notes:
8218bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8219bb7acecfSBarry Smith 
8220bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8221bb7acecfSBarry Smith 
82221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8223a6e0b375SMatthew G. Knepley @*/
8224a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec localX)
8225d71ae5a4SJacob Faibussowitsch {
82264d6f44ffSToby Isaac   PetscFunctionBegin;
82274d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8228064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8229fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlabellocal, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);
82303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82314d6f44ffSToby Isaac }
82322716604bSToby Isaac 
8233a6e0b375SMatthew G. Knepley /*@C
8234bb7acecfSBarry 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.
8235a6e0b375SMatthew G. Knepley 
823620f4b53cSBarry Smith   Not Collective
8237a6e0b375SMatthew G. Knepley 
8238a6e0b375SMatthew G. Knepley   Input Parameters:
8239bb7acecfSBarry Smith + dm     - The `DM`
8240a6e0b375SMatthew G. Knepley . time   - The time
824120f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8242a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8243a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8244a6e0b375SMatthew G. Knepley 
8245a6e0b375SMatthew G. Knepley   Output Parameter:
8246a6e0b375SMatthew G. Knepley . localX - The output vector
8247a6e0b375SMatthew G. Knepley 
824820f4b53cSBarry Smith   Calling sequence of `funcs`:
8249a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8250a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8251a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8252a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8253a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8254a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8255a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8256a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8257a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8258a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8259a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8260a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8261a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8262a6e0b375SMatthew G. Knepley . t            - The current time
8263a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8264a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8265a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8266a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8267a6e0b375SMatthew G. Knepley 
8268*73ff1848SBarry Smith   Level: intermediate
8269*73ff1848SBarry Smith 
8270bb7acecfSBarry Smith   Note:
8271bb7acecfSBarry 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.
8272bb7acecfSBarry 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
8273bb7acecfSBarry 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
8274a6e0b375SMatthew 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.
8275a6e0b375SMatthew G. Knepley 
8276bb7acecfSBarry Smith   Developer Notes:
8277bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8278bb7acecfSBarry Smith 
8279bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8280bb7acecfSBarry Smith 
8281a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8282a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8283a6e0b375SMatthew G. Knepley @*/
8284a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX)
8285d71ae5a4SJacob Faibussowitsch {
82868c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82878c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8288eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
82898c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8290fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlocal, time, localU, funcs, mode, localX);
82913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82928c6c5593SMatthew G. Knepley }
82938c6c5593SMatthew G. Knepley 
8294a6e0b375SMatthew G. Knepley /*@C
8295a6e0b375SMatthew 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.
8296a6e0b375SMatthew G. Knepley 
829720f4b53cSBarry Smith   Not Collective
8298a6e0b375SMatthew G. Knepley 
8299a6e0b375SMatthew G. Knepley   Input Parameters:
8300bb7acecfSBarry Smith + dm     - The `DM`
8301a6e0b375SMatthew G. Knepley . time   - The time
8302bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8303a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8304a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8305bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
830620f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8307a6e0b375SMatthew G. Knepley . localU - The input field vector
8308a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8309a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8310a6e0b375SMatthew G. Knepley 
8311a6e0b375SMatthew G. Knepley   Output Parameter:
8312a6e0b375SMatthew G. Knepley . localX - The output vector
8313a6e0b375SMatthew G. Knepley 
831420f4b53cSBarry Smith   Calling sequence of `funcs`:
8315a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8316a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8317a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8318a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8319a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8320a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8321a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8322a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8323a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8324a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8325a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8326a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8327a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8328a6e0b375SMatthew G. Knepley . t            - The current time
8329a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8330a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8331a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8332a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8333a6e0b375SMatthew G. Knepley 
8334*73ff1848SBarry Smith   Level: intermediate
8335*73ff1848SBarry Smith 
8336bb7acecfSBarry Smith   Note:
8337bb7acecfSBarry 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.
8338bb7acecfSBarry 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
8339bb7acecfSBarry 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
8340a6e0b375SMatthew 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.
8341a6e0b375SMatthew G. Knepley 
8342bb7acecfSBarry Smith   Developer Notes:
8343bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8344bb7acecfSBarry Smith 
8345bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8346bb7acecfSBarry Smith 
83471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8348a6e0b375SMatthew G. Knepley @*/
8349a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX)
8350d71ae5a4SJacob Faibussowitsch {
83518c6c5593SMatthew G. Knepley   PetscFunctionBegin;
83528c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8353064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8354064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8355fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
83563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83578c6c5593SMatthew G. Knepley }
83588c6c5593SMatthew G. Knepley 
83592716604bSToby Isaac /*@C
8360d29d7c6eSMatthew 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.
8361d29d7c6eSMatthew G. Knepley 
836220f4b53cSBarry Smith   Not Collective
8363d29d7c6eSMatthew G. Knepley 
8364d29d7c6eSMatthew G. Knepley   Input Parameters:
8365bb7acecfSBarry Smith + dm     - The `DM`
8366d29d7c6eSMatthew G. Knepley . time   - The time
8367bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8368d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8369d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8370bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
837120f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8372d29d7c6eSMatthew G. Knepley . U      - The input field vector
8373d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8374d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8375d29d7c6eSMatthew G. Knepley 
8376d29d7c6eSMatthew G. Knepley   Output Parameter:
8377d29d7c6eSMatthew G. Knepley . X - The output vector
8378d29d7c6eSMatthew G. Knepley 
837920f4b53cSBarry Smith   Calling sequence of `funcs`:
8380d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8381d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8382d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8383d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8384d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8385d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
8386d29d7c6eSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8387d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8388d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8389d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8390d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8391d29d7c6eSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8392d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8393d29d7c6eSMatthew G. Knepley . t            - The current time
8394d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8395d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8396d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8397d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8398d29d7c6eSMatthew G. Knepley 
8399*73ff1848SBarry Smith   Level: intermediate
8400*73ff1848SBarry Smith 
8401bb7acecfSBarry Smith   Note:
8402bb7acecfSBarry 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.
8403bb7acecfSBarry 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
8404bb7acecfSBarry 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
8405d29d7c6eSMatthew 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.
8406d29d7c6eSMatthew G. Knepley 
8407bb7acecfSBarry Smith   Developer Notes:
8408bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8409bb7acecfSBarry Smith 
8410bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8411bb7acecfSBarry Smith 
84121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8413d29d7c6eSMatthew G. Knepley @*/
8414a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec U, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec X)
8415d71ae5a4SJacob Faibussowitsch {
8416d29d7c6eSMatthew G. Knepley   DM  dmIn;
8417d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8418d29d7c6eSMatthew G. Knepley 
8419d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8420d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8421d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8422d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8423d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8424f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
842572fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
842672fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8427d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8428d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8429d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8430d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8431d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
84323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8433d29d7c6eSMatthew G. Knepley }
8434d29d7c6eSMatthew G. Knepley 
8435d29d7c6eSMatthew G. Knepley /*@C
8436ece3a9fcSMatthew 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.
8437ece3a9fcSMatthew G. Knepley 
843820f4b53cSBarry Smith   Not Collective
8439ece3a9fcSMatthew G. Knepley 
8440ece3a9fcSMatthew G. Knepley   Input Parameters:
8441bb7acecfSBarry Smith + dm     - The `DM`
8442ece3a9fcSMatthew G. Knepley . time   - The time
8443bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8444ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8445ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8446bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
844720f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8448ece3a9fcSMatthew G. Knepley . localU - The input field vector
8449ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8450ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8451ece3a9fcSMatthew G. Knepley 
8452ece3a9fcSMatthew G. Knepley   Output Parameter:
8453ece3a9fcSMatthew G. Knepley . localX - The output vector
8454ece3a9fcSMatthew G. Knepley 
845520f4b53cSBarry Smith   Calling sequence of `funcs`:
8456ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8457ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8458ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8459ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8460ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8461ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
8462ece3a9fcSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8463ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8464ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8465ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8466ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8467ece3a9fcSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8468ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8469ece3a9fcSMatthew G. Knepley . t            - The current time
8470ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8471ece3a9fcSMatthew G. Knepley . n            - The face normal
8472ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8473ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8474ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8475ece3a9fcSMatthew G. Knepley 
8476*73ff1848SBarry Smith   Level: intermediate
8477*73ff1848SBarry Smith 
8478ece3a9fcSMatthew G. Knepley   Note:
8479bb7acecfSBarry 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.
8480bb7acecfSBarry 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
8481bb7acecfSBarry 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
8482ece3a9fcSMatthew 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.
8483ece3a9fcSMatthew G. Knepley 
8484bb7acecfSBarry Smith   Developer Notes:
8485bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8486bb7acecfSBarry Smith 
8487bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8488bb7acecfSBarry Smith 
84891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8490ece3a9fcSMatthew G. Knepley @*/
8491a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], const PetscReal n[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX)
8492d71ae5a4SJacob Faibussowitsch {
8493ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8494ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8495064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8496064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8497fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectbdfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
84983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8499ece3a9fcSMatthew G. Knepley }
8500ece3a9fcSMatthew G. Knepley 
8501ece3a9fcSMatthew G. Knepley /*@C
85022716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
85032716604bSToby Isaac 
850420f4b53cSBarry Smith   Collective
8505bb7acecfSBarry Smith 
85062716604bSToby Isaac   Input Parameters:
8507bb7acecfSBarry Smith + dm    - The `DM`
85080709b2feSToby Isaac . time  - The time
85092716604bSToby Isaac . funcs - The functions to evaluate for each field component
85102716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8511574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
85122716604bSToby Isaac 
85132716604bSToby Isaac   Output Parameter:
85142716604bSToby Isaac . diff - The diff ||u - u_h||_2
85152716604bSToby Isaac 
85162716604bSToby Isaac   Level: developer
85172716604bSToby Isaac 
8518bb7acecfSBarry Smith   Developer Notes:
8519bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8520bb7acecfSBarry Smith 
8521bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8522bb7acecfSBarry Smith 
85231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
85242716604bSToby Isaac @*/
8525d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8526d71ae5a4SJacob Faibussowitsch {
85272716604bSToby Isaac   PetscFunctionBegin;
85282716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8529b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8530fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2diff, time, funcs, ctxs, X, diff);
85313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
85322716604bSToby Isaac }
8533b698f381SToby Isaac 
8534b698f381SToby Isaac /*@C
8535b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8536b698f381SToby Isaac 
853720f4b53cSBarry Smith   Collective
8538d083f849SBarry Smith 
8539b698f381SToby Isaac   Input Parameters:
8540bb7acecfSBarry Smith + dm    - The `DM`
8541a4e35b19SJacob Faibussowitsch . time  - The time
8542b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8543b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8544574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8545b698f381SToby Isaac - n     - The vector to project along
8546b698f381SToby Isaac 
8547b698f381SToby Isaac   Output Parameter:
8548b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8549b698f381SToby Isaac 
8550b698f381SToby Isaac   Level: developer
8551b698f381SToby Isaac 
8552bb7acecfSBarry Smith   Developer Notes:
8553bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8554bb7acecfSBarry Smith 
8555bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8556bb7acecfSBarry Smith 
85571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8558b698f381SToby Isaac @*/
8559d71ae5a4SJacob 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)
8560d71ae5a4SJacob Faibussowitsch {
8561b698f381SToby Isaac   PetscFunctionBegin;
8562b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8563b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8564fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2gradientdiff, time, funcs, ctxs, X, n, diff);
85653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8566b698f381SToby Isaac }
8567b698f381SToby Isaac 
85682a16baeaSToby Isaac /*@C
85692a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
85702a16baeaSToby Isaac 
857120f4b53cSBarry Smith   Collective
8572d083f849SBarry Smith 
85732a16baeaSToby Isaac   Input Parameters:
8574bb7acecfSBarry Smith + dm    - The `DM`
85752a16baeaSToby Isaac . time  - The time
85762a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
85772a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8578574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
85792a16baeaSToby Isaac 
85802a16baeaSToby Isaac   Output Parameter:
85812a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
85822a16baeaSToby Isaac 
85832a16baeaSToby Isaac   Level: developer
85842a16baeaSToby Isaac 
8585bb7acecfSBarry Smith   Developer Notes:
8586bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8587bb7acecfSBarry Smith 
8588bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8589bb7acecfSBarry Smith 
859042747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
85912a16baeaSToby Isaac @*/
8592d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8593d71ae5a4SJacob Faibussowitsch {
85942a16baeaSToby Isaac   PetscFunctionBegin;
85952a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85962a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8597fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2fielddiff, time, funcs, ctxs, X, diff);
85983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
85992a16baeaSToby Isaac }
86002a16baeaSToby Isaac 
8601df0b854cSToby Isaac /*@C
8602bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8603502a2867SDave May 
8604502a2867SDave May   Not Collective
8605502a2867SDave May 
8606502a2867SDave May   Input Parameter:
8607bb7acecfSBarry Smith . dm - The `DM`
8608502a2867SDave May 
86090a19bb7dSprj-   Output Parameters:
86100a19bb7dSprj- + nranks - the number of neighbours
86110a19bb7dSprj- - ranks  - the neighbors ranks
8612502a2867SDave May 
86139bdbcad8SBarry Smith   Level: beginner
86149bdbcad8SBarry Smith 
8615bb7acecfSBarry Smith   Note:
8616bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8617502a2867SDave May 
86181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8619502a2867SDave May @*/
8620d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8621d71ae5a4SJacob Faibussowitsch {
8622502a2867SDave May   PetscFunctionBegin;
8623502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8624fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, getneighbors, nranks, ranks);
86253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8626502a2867SDave May }
8627502a2867SDave May 
8628531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8629531c7667SBarry Smith 
8630531c7667SBarry Smith /*
8631531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
86322b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8633531c7667SBarry Smith */
863466976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8635d71ae5a4SJacob Faibussowitsch {
8636531c7667SBarry Smith   PetscFunctionBegin;
8637531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8638531c7667SBarry Smith     Vec x1local;
8639531c7667SBarry Smith     DM  dm;
86409566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
86417a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
86429566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
86439566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
86449566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8645531c7667SBarry Smith     x1 = x1local;
8646531c7667SBarry Smith   }
86479566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8648531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8649531c7667SBarry Smith     DM dm;
86509566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
86519566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8652531c7667SBarry Smith   }
86533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8654531c7667SBarry Smith }
8655531c7667SBarry Smith 
8656531c7667SBarry Smith /*@
8657bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8658531c7667SBarry Smith 
8659a4e35b19SJacob Faibussowitsch   Input Parameters:
8660a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8661a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8662531c7667SBarry Smith 
86639bdbcad8SBarry Smith   Level: advanced
86649bdbcad8SBarry Smith 
8665*73ff1848SBarry Smith   Developer Note:
8666*73ff1848SBarry Smith   This routine exists because the PETSc `Mat` library does not know about the `DM` objects
8667531c7667SBarry Smith 
86681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8669531c7667SBarry Smith @*/
8670d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8671d71ae5a4SJacob Faibussowitsch {
8672531c7667SBarry Smith   PetscFunctionBegin;
8673531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
86743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8675531c7667SBarry Smith }
86768320bc6fSPatrick Sanan 
86778320bc6fSPatrick Sanan /*@
8678bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
86798320bc6fSPatrick Sanan 
86808320bc6fSPatrick Sanan   Collective
86818320bc6fSPatrick Sanan 
86828320bc6fSPatrick Sanan   Input Parameters:
8683bb7acecfSBarry Smith + dm1 - the first `DM`
8684bb7acecfSBarry Smith - dm2 - the second `DM`
86858320bc6fSPatrick Sanan 
86868320bc6fSPatrick Sanan   Output Parameters:
8687bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8688bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
86898320bc6fSPatrick Sanan 
869020f4b53cSBarry Smith   Level: advanced
869120f4b53cSBarry Smith 
86928320bc6fSPatrick Sanan   Notes:
8693bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
86943d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
86958320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8696bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
86973d862458SPatrick Sanan   decomposition, but hold different data.
86988320bc6fSPatrick Sanan 
86998320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8700bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
87018320bc6fSPatrick Sanan 
8702bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
87038320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
87048320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8705bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
87068320bc6fSPatrick Sanan 
8707bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8708bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
87098320bc6fSPatrick Sanan .vb
87108320bc6fSPatrick Sanan   ...
87119566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
87128320bc6fSPatrick Sanan   if (set && compatible)  {
87139566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
87149566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
87159566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
87168320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
87178320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
87188320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
87198320bc6fSPatrick Sanan       }
87208320bc6fSPatrick Sanan     }
87219566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
87229566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
87238320bc6fSPatrick Sanan   } else {
87248320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
87258320bc6fSPatrick Sanan   }
87268320bc6fSPatrick Sanan   ...
87278320bc6fSPatrick Sanan .ve
87288320bc6fSPatrick Sanan 
8729bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
87308320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
87318320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
87328320bc6fSPatrick Sanan   always check the "set" output parameter.
87338320bc6fSPatrick Sanan 
8734bb7acecfSBarry Smith   A `DM` is always compatible with itself.
87358320bc6fSPatrick Sanan 
8736bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
87378320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
87388320bc6fSPatrick Sanan   incompatible.
87398320bc6fSPatrick Sanan 
87408320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8741bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
87428320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
87438320bc6fSPatrick Sanan 
8744*73ff1848SBarry Smith   Developer Note:
8745bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
87463d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8747a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8748bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
87498320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
87503d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8751bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
87528320bc6fSPatrick Sanan 
87531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
87548320bc6fSPatrick Sanan @*/
8755d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8756d71ae5a4SJacob Faibussowitsch {
87578320bc6fSPatrick Sanan   PetscMPIInt compareResult;
87588320bc6fSPatrick Sanan   DMType      type, type2;
87598320bc6fSPatrick Sanan   PetscBool   sameType;
87608320bc6fSPatrick Sanan 
87618320bc6fSPatrick Sanan   PetscFunctionBegin;
8762a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
87638320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
87648320bc6fSPatrick Sanan 
87658320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8766a5bc1bf3SBarry Smith   if (dm1 == dm2) {
87678320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
87688320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
87693ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
87708320bc6fSPatrick Sanan   }
87718320bc6fSPatrick Sanan 
87728320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
87738320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
87748320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
87758320bc6fSPatrick Sanan      determined by the implementation-specific logic */
87769566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
87778320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
87788320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
87798320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
87803ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
87818320bc6fSPatrick Sanan   }
87828320bc6fSPatrick Sanan 
87838320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8784a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8785dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
87863ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
87878320bc6fSPatrick Sanan   }
87888320bc6fSPatrick Sanan 
8789a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
87908320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
87919566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
87929566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
87939566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
87948320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8795dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
87968320bc6fSPatrick Sanan   } else {
87978320bc6fSPatrick Sanan     *set = PETSC_FALSE;
87988320bc6fSPatrick Sanan   }
87993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
88008320bc6fSPatrick Sanan }
8801c0f0dcc3SMatthew G. Knepley 
8802c0f0dcc3SMatthew G. Knepley /*@C
8803bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8804c0f0dcc3SMatthew G. Knepley 
880520f4b53cSBarry Smith   Logically Collective
8806c0f0dcc3SMatthew G. Knepley 
8807c0f0dcc3SMatthew G. Knepley   Input Parameters:
880860225df5SJacob Faibussowitsch + dm             - the `DM`
8809c0f0dcc3SMatthew G. Knepley . f              - the monitor function
881020f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
881120f4b53cSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`)
8812c0f0dcc3SMatthew G. Knepley 
881320f4b53cSBarry Smith   Options Database Key:
881460225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8815c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8816c0f0dcc3SMatthew G. Knepley 
88179bdbcad8SBarry Smith   Level: intermediate
88189bdbcad8SBarry Smith 
8819bb7acecfSBarry Smith   Note:
8820c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8821bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8822c0f0dcc3SMatthew G. Knepley   order in which they were set.
8823c0f0dcc3SMatthew G. Knepley 
8824*73ff1848SBarry Smith   Fortran Note:
8825bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8826bb7acecfSBarry Smith 
8827*73ff1848SBarry Smith   Developer Note:
8828bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8829c0f0dcc3SMatthew G. Knepley 
88301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8831c0f0dcc3SMatthew G. Knepley @*/
8832d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8833d71ae5a4SJacob Faibussowitsch {
8834c0f0dcc3SMatthew G. Knepley   PetscInt m;
8835c0f0dcc3SMatthew G. Knepley 
8836c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8837c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8838c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8839c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8840c0f0dcc3SMatthew G. Knepley 
88419566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
88423ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8843c0f0dcc3SMatthew G. Knepley   }
88447a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8845c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8846c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8847c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
88483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8849c0f0dcc3SMatthew G. Knepley }
8850c0f0dcc3SMatthew G. Knepley 
8851c0f0dcc3SMatthew G. Knepley /*@
8852bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8853c0f0dcc3SMatthew G. Knepley 
885420f4b53cSBarry Smith   Logically Collective
8855c0f0dcc3SMatthew G. Knepley 
8856c0f0dcc3SMatthew G. Knepley   Input Parameter:
8857c0f0dcc3SMatthew G. Knepley . dm - the DM
8858c0f0dcc3SMatthew G. Knepley 
8859c0f0dcc3SMatthew G. Knepley   Options Database Key:
8860c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8861bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8862c0f0dcc3SMatthew G. Knepley   set via the options database
8863c0f0dcc3SMatthew G. Knepley 
88649bdbcad8SBarry Smith   Level: intermediate
88659bdbcad8SBarry Smith 
8866bb7acecfSBarry Smith   Note:
8867bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8868c0f0dcc3SMatthew G. Knepley 
88691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8870c0f0dcc3SMatthew G. Knepley @*/
8871d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8872d71ae5a4SJacob Faibussowitsch {
8873c0f0dcc3SMatthew G. Knepley   PetscInt m;
8874c0f0dcc3SMatthew G. Knepley 
8875c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8876c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8877c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
88789566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8879c0f0dcc3SMatthew G. Knepley   }
8880c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
88813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8882c0f0dcc3SMatthew G. Knepley }
8883c0f0dcc3SMatthew G. Knepley 
8884c0f0dcc3SMatthew G. Knepley /*@C
8885c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8886c0f0dcc3SMatthew G. Knepley 
888720f4b53cSBarry Smith   Collective
8888c0f0dcc3SMatthew G. Knepley 
8889c0f0dcc3SMatthew G. Knepley   Input Parameters:
8890bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
8891c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
8892c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
8893c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
8894c0f0dcc3SMatthew G. Knepley . monitor      - the monitor function
8895bb7acecfSBarry 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
8896c0f0dcc3SMatthew G. Knepley 
8897c0f0dcc3SMatthew G. Knepley   Output Parameter:
8898c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8899c0f0dcc3SMatthew G. Knepley 
8900c0f0dcc3SMatthew G. Knepley   Level: developer
8901c0f0dcc3SMatthew G. Knepley 
89021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8903db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
890460225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
8905db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8906c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8907db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8908bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8909c0f0dcc3SMatthew G. Knepley @*/
8910d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8911d71ae5a4SJacob Faibussowitsch {
8912c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8913c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8914c0f0dcc3SMatthew G. Knepley 
8915c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8916c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89179566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8918c0f0dcc3SMatthew G. Knepley   if (*flg) {
8919c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8920c0f0dcc3SMatthew G. Knepley 
89219566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
8922cd791dc2SBarry Smith     PetscCall(PetscOptionsRestoreViewer(&viewer));
89239566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
89249566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8925c0f0dcc3SMatthew G. Knepley   }
89263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8927c0f0dcc3SMatthew G. Knepley }
8928c0f0dcc3SMatthew G. Knepley 
8929c0f0dcc3SMatthew G. Knepley /*@
8930c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
8931c0f0dcc3SMatthew G. Knepley 
893220f4b53cSBarry Smith   Collective
8933c0f0dcc3SMatthew G. Knepley 
89342fe279fdSBarry Smith   Input Parameter:
8935bb7acecfSBarry Smith . dm - The `DM`
8936c0f0dcc3SMatthew G. Knepley 
8937c0f0dcc3SMatthew G. Knepley   Level: developer
8938c0f0dcc3SMatthew G. Knepley 
8939*73ff1848SBarry Smith   Developer Note:
8940a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
8941a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
8942a4e35b19SJacob Faibussowitsch   concept of discretization.
8943bb7acecfSBarry Smith 
89441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8945c0f0dcc3SMatthew G. Knepley @*/
8946d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8947d71ae5a4SJacob Faibussowitsch {
8948c0f0dcc3SMatthew G. Knepley   PetscInt m;
8949c0f0dcc3SMatthew G. Knepley 
8950c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
89513ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
8952c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
895348a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
89543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8955c0f0dcc3SMatthew G. Knepley }
89562e4af2aeSMatthew G. Knepley 
89572e4af2aeSMatthew G. Knepley /*@
8958bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
89592e4af2aeSMatthew G. Knepley 
896020f4b53cSBarry Smith   Collective
89612e4af2aeSMatthew G. Knepley 
89622e4af2aeSMatthew G. Knepley   Input Parameters:
8963bb7acecfSBarry Smith + dm  - The `DM`
89646b867d5aSJose E. Roman - sol - The solution vector
89652e4af2aeSMatthew G. Knepley 
89666b867d5aSJose E. Roman   Input/Output Parameter:
896720f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
89686b867d5aSJose E. Roman            contains the error in each field
89696b867d5aSJose E. Roman 
89706b867d5aSJose E. Roman   Output Parameter:
897120f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
897220f4b53cSBarry Smith 
897320f4b53cSBarry Smith   Level: developer
89742e4af2aeSMatthew G. Knepley 
8975bb7acecfSBarry Smith   Note:
8976bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
89772e4af2aeSMatthew G. Knepley 
89781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
89792e4af2aeSMatthew G. Knepley @*/
8980d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
8981d71ae5a4SJacob Faibussowitsch {
89822e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
89832e4af2aeSMatthew G. Knepley   void    **ctxs;
89842e4af2aeSMatthew G. Knepley   PetscReal time;
89852e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
89862e4af2aeSMatthew G. Knepley 
89872e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
89889566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
89899566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
89909566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
89912e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
89922e4af2aeSMatthew G. Knepley     PetscDS         ds;
89932e4af2aeSMatthew G. Knepley     DMLabel         label;
89942e4af2aeSMatthew G. Knepley     IS              fieldIS;
89952e4af2aeSMatthew G. Knepley     const PetscInt *fields;
89962e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
89972e4af2aeSMatthew G. Knepley 
899807218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
89999566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
90009566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
90012e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
90022e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
90039566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
90042e4af2aeSMatthew G. Knepley     }
90059566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
90062e4af2aeSMatthew G. Knepley   }
9007ad540459SPierre 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);
90089566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
90099566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
90102e4af2aeSMatthew G. Knepley   if (errorVec) {
90112e4af2aeSMatthew G. Knepley     DM             edm;
90122e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
90132e4af2aeSMatthew G. Knepley     PetscBool      simplex;
90142e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
90152e4af2aeSMatthew G. Knepley 
90169566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
90179566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
90189566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
90199566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
90202e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
90219566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
90222e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
90232e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
90242e4af2aeSMatthew G. Knepley       PetscQuadrature q;
90252e4af2aeSMatthew G. Knepley       const char     *name;
90262e4af2aeSMatthew G. Knepley 
90279566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
90289566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
90299566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
90309566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
90319566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
90329566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
90339566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
90349566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
90352e4af2aeSMatthew G. Knepley     }
90369566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
90372e4af2aeSMatthew G. Knepley 
90389566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
90399566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
90409566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
90419566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
90422e4af2aeSMatthew G. Knepley   }
90439566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
90443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90452e4af2aeSMatthew G. Knepley }
90469a2a23afSMatthew G. Knepley 
90479a2a23afSMatthew G. Knepley /*@
9048bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
90499a2a23afSMatthew G. Knepley 
905020f4b53cSBarry Smith   Not Collective
90519a2a23afSMatthew G. Knepley 
90529a2a23afSMatthew G. Knepley   Input Parameter:
9053bb7acecfSBarry Smith . dm - The `DM`
90549a2a23afSMatthew G. Knepley 
90559a2a23afSMatthew G. Knepley   Output Parameter:
9056a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
90579a2a23afSMatthew G. Knepley 
90589a2a23afSMatthew G. Knepley   Level: advanced
90599a2a23afSMatthew G. Knepley 
9060e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
90619a2a23afSMatthew G. Knepley @*/
9062d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
9063d71ae5a4SJacob Faibussowitsch {
90649a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90659a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90669566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
90673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90689a2a23afSMatthew G. Knepley }
90699a2a23afSMatthew G. Knepley 
90709a2a23afSMatthew G. Knepley /*@
9071ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
90729a2a23afSMatthew G. Knepley 
907320f4b53cSBarry Smith   Not Collective
90749a2a23afSMatthew G. Knepley 
90759a2a23afSMatthew G. Knepley   Input Parameters:
9076bb7acecfSBarry Smith + dm    - The `DM`
9077bb7acecfSBarry Smith . label - The `DMLabel`
9078ac17215fSMatthew G. Knepley . value - The label value indicating the region
9079ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
90809a2a23afSMatthew G. Knepley 
90819a2a23afSMatthew G. Knepley   Output Parameter:
9082bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
90839a2a23afSMatthew G. Knepley 
90849bdbcad8SBarry Smith   Level: advanced
90859bdbcad8SBarry Smith 
9086bb7acecfSBarry Smith   Note:
9087bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
908804c51a94SMatthew G. Knepley 
9089e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
90909a2a23afSMatthew G. Knepley @*/
9091d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
9092d71ae5a4SJacob Faibussowitsch {
9093ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
909404c51a94SMatthew G. Knepley   PetscBool       has;
90959a2a23afSMatthew G. Knepley 
90969a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90979a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90989a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
90999a2a23afSMatthew G. Knepley   key.label = label;
91009a2a23afSMatthew G. Knepley   key.value = value;
9101ac17215fSMatthew G. Knepley   key.part  = part;
91029566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
91039566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
91049566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
91053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91069a2a23afSMatthew G. Knepley }
91079a2a23afSMatthew G. Knepley 
91089a2a23afSMatthew G. Knepley /*@
9109bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
91109a2a23afSMatthew G. Knepley 
911120f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
91129a2a23afSMatthew G. Knepley 
91139a2a23afSMatthew G. Knepley   Input Parameters:
9114bb7acecfSBarry Smith + dm    - The `DM`
9115bb7acecfSBarry Smith . label - The `DMLabel`
91169a2a23afSMatthew G. Knepley . value - The label value indicating the region
9117ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
9118bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
91199a2a23afSMatthew G. Knepley 
91209a2a23afSMatthew G. Knepley   Level: advanced
91219a2a23afSMatthew G. Knepley 
9122e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
91239a2a23afSMatthew G. Knepley @*/
9124d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9125d71ae5a4SJacob Faibussowitsch {
91269a2a23afSMatthew G. Knepley   Vec             old;
91279a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
91289a2a23afSMatthew G. Knepley 
91299a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91309a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91319a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
91329a2a23afSMatthew G. Knepley   key.label = label;
91339a2a23afSMatthew G. Knepley   key.value = value;
9134ac17215fSMatthew G. Knepley   key.part  = part;
91359566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
91369566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
91379566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
91389566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
9139d705d0eaSStefano Zampini   PetscCall(VecDestroy(&old));
91403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91419a2a23afSMatthew G. Knepley }
91429a2a23afSMatthew G. Knepley 
91439a2a23afSMatthew G. Knepley /*@C
9144bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
91459a2a23afSMatthew G. Knepley 
914620f4b53cSBarry Smith   Not Collective
91479a2a23afSMatthew G. Knepley 
91489a2a23afSMatthew G. Knepley   Input Parameter:
9149bb7acecfSBarry Smith . dm - The `DM`
91509a2a23afSMatthew G. Knepley 
91519a2a23afSMatthew G. Knepley   Output Parameters:
9152bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9153bb7acecfSBarry Smith . values - The label values for each `Vec`
9154bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
91559a2a23afSMatthew G. Knepley 
91569bdbcad8SBarry Smith   Level: advanced
91579bdbcad8SBarry Smith 
9158bb7acecfSBarry Smith   Note:
9159bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
91609a2a23afSMatthew G. Knepley 
9161e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
91629a2a23afSMatthew G. Knepley @*/
9163d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9164d71ae5a4SJacob Faibussowitsch {
91659a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
91669a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
91679a2a23afSMatthew G. Knepley 
91689a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91699a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91704f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
91714f572ea9SToby Isaac   PetscAssertPointer(values, 3);
91724f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
91739566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
91749566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
91759566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
91769371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
91779371c9d4SSatish Balay     labels[i] = keys[i].label;
91789371c9d4SSatish Balay     values[i] = keys[i].value;
91799371c9d4SSatish Balay     parts[i]  = keys[i].part;
91809371c9d4SSatish Balay   }
91819566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
91823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91839a2a23afSMatthew G. Knepley }
91849a2a23afSMatthew G. Knepley 
91859a2a23afSMatthew G. Knepley /*@
9186bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
91879a2a23afSMatthew G. Knepley 
918820f4b53cSBarry Smith   Not Collective
91899a2a23afSMatthew G. Knepley 
91909a2a23afSMatthew G. Knepley   Input Parameter:
9191bb7acecfSBarry Smith . dm - The `DM`
91929a2a23afSMatthew G. Knepley 
91939a2a23afSMatthew G. Knepley   Output Parameter:
9194bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
91959a2a23afSMatthew G. Knepley 
91969a2a23afSMatthew G. Knepley   Level: advanced
91979a2a23afSMatthew G. Knepley 
9198bb7acecfSBarry Smith   Note:
9199bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9200bb7acecfSBarry Smith 
9201e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
92029a2a23afSMatthew G. Knepley @*/
9203d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9204d71ae5a4SJacob Faibussowitsch {
92059a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92069a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9207d705d0eaSStefano Zampini   PetscValidHeaderSpecific(dmNew, DM_CLASSID, 2);
9208d705d0eaSStefano Zampini   if (dm == dmNew) PetscFunctionReturn(PETSC_SUCCESS);
9209e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(dmNew));
9210e4d5475eSStefano Zampini 
9211e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
92129566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
9213d705d0eaSStefano Zampini   {
9214d705d0eaSStefano Zampini     Vec     *auxData;
9215d705d0eaSStefano Zampini     PetscInt n, i, off = 0;
9216d705d0eaSStefano Zampini 
9217d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetSize(dmNew->auxData, &n));
9218d705d0eaSStefano Zampini     PetscCall(PetscMalloc1(n, &auxData));
9219d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetVals(dmNew->auxData, &off, auxData));
9220d705d0eaSStefano Zampini     for (i = 0; i < n; ++i) PetscCall(PetscObjectReference((PetscObject)auxData[i]));
9221d705d0eaSStefano Zampini     PetscCall(PetscFree(auxData));
9222e4d5475eSStefano Zampini   }
9223e4d5475eSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
9224e4d5475eSStefano Zampini }
9225e4d5475eSStefano Zampini 
9226e4d5475eSStefano Zampini /*@
9227e4d5475eSStefano Zampini   DMClearAuxiliaryVec - Destroys the auxiliary vector information and creates a new empty one
9228e4d5475eSStefano Zampini 
9229e4d5475eSStefano Zampini   Not Collective
9230e4d5475eSStefano Zampini 
9231e4d5475eSStefano Zampini   Input Parameter:
9232e4d5475eSStefano Zampini . dm - The `DM`
9233e4d5475eSStefano Zampini 
9234e4d5475eSStefano Zampini   Level: advanced
9235e4d5475eSStefano Zampini 
9236e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMCopyAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
9237e4d5475eSStefano Zampini @*/
9238e4d5475eSStefano Zampini PetscErrorCode DMClearAuxiliaryVec(DM dm)
9239e4d5475eSStefano Zampini {
9240e4d5475eSStefano Zampini   Vec     *auxData;
9241e4d5475eSStefano Zampini   PetscInt n, i, off = 0;
9242e4d5475eSStefano Zampini 
9243e4d5475eSStefano Zampini   PetscFunctionBegin;
9244e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetSize(dm->auxData, &n));
9245d705d0eaSStefano Zampini   PetscCall(PetscMalloc1(n, &auxData));
9246e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetVals(dm->auxData, &off, auxData));
9247d705d0eaSStefano Zampini   for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
9248d705d0eaSStefano Zampini   PetscCall(PetscFree(auxData));
9249e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dm->auxData));
9250e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxCreate(&dm->auxData));
92513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92529a2a23afSMatthew G. Knepley }
9253b5a892a1SMatthew G. Knepley 
9254b5a892a1SMatthew G. Knepley /*@C
9255bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9256b5a892a1SMatthew G. Knepley 
925720f4b53cSBarry Smith   Not Collective
9258b5a892a1SMatthew G. Knepley 
9259b5a892a1SMatthew G. Knepley   Input Parameters:
9260bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9261b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9262b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9263b5a892a1SMatthew G. Knepley 
9264b5a892a1SMatthew G. Knepley   Output Parameters:
9265bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9266b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9267b5a892a1SMatthew G. Knepley 
9268b5a892a1SMatthew G. Knepley   Level: advanced
9269b5a892a1SMatthew G. Knepley 
9270bb7acecfSBarry Smith   Note:
9271bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9272bb7acecfSBarry Smith 
927385036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9274bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9275bb7acecfSBarry Smith 
9276bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9277bb7acecfSBarry Smith 
92781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9279b5a892a1SMatthew G. Knepley @*/
9280d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9281d71ae5a4SJacob Faibussowitsch {
9282b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
928385036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9284b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9285b5a892a1SMatthew G. Knepley 
9286b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92879371c9d4SSatish Balay   if (!nO) {
92889371c9d4SSatish Balay     *ornt  = 0;
92899371c9d4SSatish Balay     *found = PETSC_TRUE;
92903ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
92919371c9d4SSatish Balay   }
9292b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
929385036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
9294b5a892a1SMatthew G. Knepley 
92959371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
92969371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
92979371c9d4SSatish Balay     if (c == cS) {
92989371c9d4SSatish Balay       *ornt = o;
92999371c9d4SSatish Balay       break;
93009371c9d4SSatish Balay     }
9301b5a892a1SMatthew G. Knepley   }
9302b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
93033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9304b5a892a1SMatthew G. Knepley }
9305b5a892a1SMatthew G. Knepley 
9306b5a892a1SMatthew G. Knepley /*@C
9307bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9308b5a892a1SMatthew G. Knepley 
930920f4b53cSBarry Smith   Not Collective
9310b5a892a1SMatthew G. Knepley 
9311b5a892a1SMatthew G. Knepley   Input Parameters:
9312bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9313b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9314b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9315b5a892a1SMatthew G. Knepley 
93162fe279fdSBarry Smith   Output Parameter:
9317bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9318b5a892a1SMatthew G. Knepley 
9319b5a892a1SMatthew G. Knepley   Level: advanced
9320b5a892a1SMatthew G. Knepley 
9321bb7acecfSBarry Smith   Note:
9322bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9323bb7acecfSBarry Smith 
9324*73ff1848SBarry Smith   Developer Note:
9325bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9326bb7acecfSBarry Smith 
93271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9328b5a892a1SMatthew G. Knepley @*/
9329d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9330d71ae5a4SJacob Faibussowitsch {
9331b5a892a1SMatthew G. Knepley   PetscBool found;
9332b5a892a1SMatthew G. Knepley 
9333b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93349566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
93357a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
93363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9337b5a892a1SMatthew G. Knepley }
9338b5a892a1SMatthew G. Knepley 
9339b5a892a1SMatthew G. Knepley /*@C
9340bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9341b5a892a1SMatthew G. Knepley 
934220f4b53cSBarry Smith   Not Collective
9343b5a892a1SMatthew G. Knepley 
9344b5a892a1SMatthew G. Knepley   Input Parameters:
9345bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9346b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9347b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9348b5a892a1SMatthew G. Knepley 
9349b5a892a1SMatthew G. Knepley   Output Parameters:
9350bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9351b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9352b5a892a1SMatthew G. Knepley 
9353b5a892a1SMatthew G. Knepley   Level: advanced
9354b5a892a1SMatthew G. Knepley 
9355*73ff1848SBarry Smith   Notes:
9356bb7acecfSBarry Smith   An arrangement is a vertex order
9357bb7acecfSBarry Smith 
935885036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9359bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9360bb7acecfSBarry Smith 
9361bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9362bb7acecfSBarry Smith 
936385036b15SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangement()`
9364b5a892a1SMatthew G. Knepley @*/
9365d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9366d71ae5a4SJacob Faibussowitsch {
9367b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
936885036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9369b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9370b5a892a1SMatthew G. Knepley 
9371b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93729371c9d4SSatish Balay   if (!nO) {
93739371c9d4SSatish Balay     *ornt  = 0;
93749371c9d4SSatish Balay     *found = PETSC_TRUE;
93753ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
93769371c9d4SSatish Balay   }
9377b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
937885036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangement(ct, o);
9379b5a892a1SMatthew G. Knepley 
93809371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
93819371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
93829371c9d4SSatish Balay     if (c == cS) {
93839371c9d4SSatish Balay       *ornt = o;
93849371c9d4SSatish Balay       break;
93859371c9d4SSatish Balay     }
9386b5a892a1SMatthew G. Knepley   }
9387b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
93883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9389b5a892a1SMatthew G. Knepley }
9390b5a892a1SMatthew G. Knepley 
9391b5a892a1SMatthew G. Knepley /*@C
9392bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9393b5a892a1SMatthew G. Knepley 
939420f4b53cSBarry Smith   Not Collective
9395b5a892a1SMatthew G. Knepley 
9396b5a892a1SMatthew G. Knepley   Input Parameters:
9397bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9398b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9399b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9400b5a892a1SMatthew G. Knepley 
94012fe279fdSBarry Smith   Output Parameter:
9402bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9403b5a892a1SMatthew G. Knepley 
9404b5a892a1SMatthew G. Knepley   Level: advanced
9405b5a892a1SMatthew G. Knepley 
9406bb7acecfSBarry Smith   Note:
9407bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9408bb7acecfSBarry Smith 
9409*73ff1848SBarry Smith   Developer Note:
9410bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9411bb7acecfSBarry Smith 
94121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9413b5a892a1SMatthew G. Knepley @*/
9414d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9415d71ae5a4SJacob Faibussowitsch {
9416b5a892a1SMatthew G. Knepley   PetscBool found;
9417b5a892a1SMatthew G. Knepley 
9418b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94199566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
94207a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
94213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9422b5a892a1SMatthew G. Knepley }
9423012bc364SMatthew G. Knepley 
9424012bc364SMatthew G. Knepley /*@C
9425012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9426012bc364SMatthew G. Knepley 
942720f4b53cSBarry Smith   Not Collective
9428012bc364SMatthew G. Knepley 
9429012bc364SMatthew G. Knepley   Input Parameters:
9430bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9431012bc364SMatthew G. Knepley - point - Coordinates of the point
9432012bc364SMatthew G. Knepley 
94332fe279fdSBarry Smith   Output Parameter:
9434012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9435012bc364SMatthew G. Knepley 
9436012bc364SMatthew G. Knepley   Level: advanced
9437012bc364SMatthew G. Knepley 
94381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9439012bc364SMatthew G. Knepley @*/
9440d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9441d71ae5a4SJacob Faibussowitsch {
9442012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9443012bc364SMatthew G. Knepley   PetscInt  d;
9444012bc364SMatthew G. Knepley 
9445012bc364SMatthew G. Knepley   PetscFunctionBegin;
9446012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9447012bc364SMatthew G. Knepley   switch (ct) {
9448012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9449012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9450012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
94519371c9d4SSatish Balay       if (point[d] < -1.0) {
94529371c9d4SSatish Balay         *inside = PETSC_FALSE;
94539371c9d4SSatish Balay         break;
94549371c9d4SSatish Balay       }
9455012bc364SMatthew G. Knepley       sum += point[d];
9456012bc364SMatthew G. Knepley     }
94579371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
94589371c9d4SSatish Balay       *inside = PETSC_FALSE;
94599371c9d4SSatish Balay       break;
94609371c9d4SSatish Balay     }
9461012bc364SMatthew G. Knepley     break;
9462012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9463012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9464012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
94659371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
94669371c9d4SSatish Balay         *inside = PETSC_FALSE;
9467012bc364SMatthew G. Knepley         break;
94689371c9d4SSatish Balay       }
94699371c9d4SSatish Balay     break;
9470d71ae5a4SJacob Faibussowitsch   default:
9471d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9472012bc364SMatthew G. Knepley   }
94733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9474012bc364SMatthew G. Knepley }
9475adc21957SMatthew G. Knepley 
9476adc21957SMatthew G. Knepley /*@
9477adc21957SMatthew G. Knepley   DMReorderSectionSetDefault - Set flag indicating whether the local section should be reordered by default
9478adc21957SMatthew G. Knepley 
9479adc21957SMatthew G. Knepley   Logically collective
9480adc21957SMatthew G. Knepley 
9481adc21957SMatthew G. Knepley   Input Parameters:
9482adc21957SMatthew G. Knepley + dm      - The DM
9483adc21957SMatthew G. Knepley - reorder - Flag for reordering
9484adc21957SMatthew G. Knepley 
9485adc21957SMatthew G. Knepley   Level: intermediate
9486adc21957SMatthew G. Knepley 
9487adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetDefault()`
9488adc21957SMatthew G. Knepley @*/
9489adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetDefault(DM dm, DMReorderDefaultFlag reorder)
9490adc21957SMatthew G. Knepley {
9491adc21957SMatthew G. Knepley   PetscFunctionBegin;
9492adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9493adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetDefault_C", (DM, DMReorderDefaultFlag), (dm, reorder));
9494adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9495adc21957SMatthew G. Knepley }
9496adc21957SMatthew G. Knepley 
9497adc21957SMatthew G. Knepley /*@
9498adc21957SMatthew G. Knepley   DMReorderSectionGetDefault - Get flag indicating whether the local section should be reordered by default
9499adc21957SMatthew G. Knepley 
9500adc21957SMatthew G. Knepley   Not collective
9501adc21957SMatthew G. Knepley 
9502adc21957SMatthew G. Knepley   Input Parameter:
9503adc21957SMatthew G. Knepley . dm - The DM
9504adc21957SMatthew G. Knepley 
9505adc21957SMatthew G. Knepley   Output Parameter:
9506adc21957SMatthew G. Knepley . reorder - Flag for reordering
9507adc21957SMatthew G. Knepley 
9508adc21957SMatthew G. Knepley   Level: intermediate
9509adc21957SMatthew G. Knepley 
9510adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`
9511adc21957SMatthew G. Knepley @*/
9512adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetDefault(DM dm, DMReorderDefaultFlag *reorder)
9513adc21957SMatthew G. Knepley {
9514adc21957SMatthew G. Knepley   PetscFunctionBegin;
9515adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9516adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9517adc21957SMatthew G. Knepley   *reorder = DM_REORDER_DEFAULT_NOTSET;
9518adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetDefault_C", (DM, DMReorderDefaultFlag *), (dm, reorder));
9519adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9520adc21957SMatthew G. Knepley }
9521adc21957SMatthew G. Knepley 
9522adc21957SMatthew G. Knepley /*@C
9523adc21957SMatthew G. Knepley   DMReorderSectionSetType - Set the type of local section reordering
9524adc21957SMatthew G. Knepley 
9525adc21957SMatthew G. Knepley   Logically collective
9526adc21957SMatthew G. Knepley 
9527adc21957SMatthew G. Knepley   Input Parameters:
9528adc21957SMatthew G. Knepley + dm      - The DM
9529adc21957SMatthew G. Knepley - reorder - The reordering method
9530adc21957SMatthew G. Knepley 
9531adc21957SMatthew G. Knepley   Level: intermediate
9532adc21957SMatthew G. Knepley 
9533adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetType()`, `DMReorderSectionSetDefault()`
9534adc21957SMatthew G. Knepley @*/
9535adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetType(DM dm, MatOrderingType reorder)
9536adc21957SMatthew G. Knepley {
9537adc21957SMatthew G. Knepley   PetscFunctionBegin;
9538adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9539adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetType_C", (DM, MatOrderingType), (dm, reorder));
9540adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9541adc21957SMatthew G. Knepley }
9542adc21957SMatthew G. Knepley 
9543adc21957SMatthew G. Knepley /*@C
9544adc21957SMatthew G. Knepley   DMReorderSectionGetType - Get the reordering type for the local section
9545adc21957SMatthew G. Knepley 
9546adc21957SMatthew G. Knepley   Not collective
9547adc21957SMatthew G. Knepley 
9548adc21957SMatthew G. Knepley   Input Parameter:
9549adc21957SMatthew G. Knepley . dm - The DM
9550adc21957SMatthew G. Knepley 
9551adc21957SMatthew G. Knepley   Output Parameter:
9552adc21957SMatthew G. Knepley . reorder - The reordering method
9553adc21957SMatthew G. Knepley 
9554adc21957SMatthew G. Knepley   Level: intermediate
9555adc21957SMatthew G. Knepley 
9556adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`, `DMReorderSectionGetDefault()`
9557adc21957SMatthew G. Knepley @*/
9558adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetType(DM dm, MatOrderingType *reorder)
9559adc21957SMatthew G. Knepley {
9560adc21957SMatthew G. Knepley   PetscFunctionBegin;
9561adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9562adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9563adc21957SMatthew G. Knepley   *reorder = NULL;
9564adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetType_C", (DM, MatOrderingType *), (dm, reorder));
9565adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9566adc21957SMatthew G. Knepley }
9567