xref: /petsc/src/dm/interface/dm.c (revision 52947b74860436d0874ff45053db8ecceba51351)
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 
310bb7acecfSBarry Smith   Note:
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 
44660225df5SJacob Faibussowitsch   Developer Notes:
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 
47460225df5SJacob Faibussowitsch   Developer Notes:
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 
55760225df5SJacob Faibussowitsch   Fortran Notes:
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 
951cd7e8a5eSksagiyam   Notes:
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 
1164bb7acecfSBarry Smith   Note:
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 
1233a4e35b19SJacob Faibussowitsch   Notes:
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 
123760225df5SJacob Faibussowitsch   Developer Notes:
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 
1524a4e35b19SJacob Faibussowitsch   Notes:
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 
1648bb7acecfSBarry Smith   Note:
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 
171560225df5SJacob Faibussowitsch   Developer Notes:
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:
188321c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1884bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1885bb7acecfSBarry Smith   `PetscFree()`.
188621c9b008SJed Brown 
188760225df5SJacob Faibussowitsch   Developer Notes:
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 
2014e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2015bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm 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 
202160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2022e7c4fc90SDmitry Karpeev @*/
2023d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2024d71ae5a4SJacob Faibussowitsch {
2025e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2026e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20278865f1eaSKarl Rupp   if (len) {
20284f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20298865f1eaSKarl Rupp     *len = 0;
20308865f1eaSKarl Rupp   }
20318865f1eaSKarl Rupp   if (namelist) {
20324f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2033ea78f98cSLisandro Dalcin     *namelist = NULL;
20348865f1eaSKarl Rupp   }
20358865f1eaSKarl Rupp   if (islist) {
20364f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2037ea78f98cSLisandro Dalcin     *islist = NULL;
20388865f1eaSKarl Rupp   }
20398865f1eaSKarl Rupp   if (dmlist) {
20404f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2041ea78f98cSLisandro Dalcin     *dmlist = NULL;
20428865f1eaSKarl Rupp   }
2043f3f0edfdSDmitry Karpeev   /*
2044f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2045f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2046f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2047f3f0edfdSDmitry Karpeev    */
20487a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
204916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2050435a35e8SMatthew G Knepley     PetscSection section;
2051435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2052435a35e8SMatthew G Knepley 
20539566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20549566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2055435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2056f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20579566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20589566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20599566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2060435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2061435a35e8SMatthew G Knepley         const char *fieldName;
2062435a35e8SMatthew G Knepley 
20639566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
206403dc3394SMatthew G. Knepley         if (namelist) {
20659566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20669566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2067435a35e8SMatthew G Knepley         }
206803dc3394SMatthew G. Knepley       }
2069435a35e8SMatthew G Knepley     } else {
20709566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2071e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20720298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2073e7c4fc90SDmitry Karpeev     }
2074dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
207616621825SDmitry Karpeev }
207716621825SDmitry Karpeev 
2078412a4547SAlexis Marboeuf /*@C
207920f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
208020f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2081435a35e8SMatthew G Knepley 
2082435a35e8SMatthew G Knepley   Not collective
2083435a35e8SMatthew G Knepley 
2084435a35e8SMatthew G Knepley   Input Parameters:
2085bb7acecfSBarry Smith + dm        - The `DM` object
2086bb7acecfSBarry Smith . numFields - The number of fields to select
20872adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2088435a35e8SMatthew G Knepley 
2089435a35e8SMatthew G Knepley   Output Parameters:
2090bb7acecfSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`
2091bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2092435a35e8SMatthew G Knepley 
209320f4b53cSBarry Smith   Level: intermediate
209420f4b53cSBarry Smith 
2095bb7acecfSBarry Smith   Note:
2096bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20975d3b26e6SMatthew G. Knepley 
209860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2099435a35e8SMatthew G Knepley @*/
2100d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2101d71ae5a4SJacob Faibussowitsch {
2102435a35e8SMatthew G Knepley   PetscFunctionBegin;
2103435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21044f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
21054f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
21064f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2107dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2109435a35e8SMatthew G Knepley }
2110435a35e8SMatthew G Knepley 
21112adcc780SMatthew G. Knepley /*@C
2112bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21132adcc780SMatthew G. Knepley 
21142adcc780SMatthew G. Knepley   Not collective
21152adcc780SMatthew G. Knepley 
2116d8d19677SJose E. Roman   Input Parameters:
2117bb7acecfSBarry Smith + dms - The `DM` objects
2118bb7acecfSBarry Smith - n   - The number of `DM`s
21192adcc780SMatthew G. Knepley 
21202adcc780SMatthew G. Knepley   Output Parameters:
2121bb7acecfSBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or NULL
2122bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21232adcc780SMatthew G. Knepley 
212420f4b53cSBarry Smith   Level: intermediate
212520f4b53cSBarry Smith 
2126bb7acecfSBarry Smith   Note:
2127bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21285d3b26e6SMatthew G. Knepley 
21291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
21302adcc780SMatthew G. Knepley @*/
2131d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
2132d71ae5a4SJacob Faibussowitsch {
21332adcc780SMatthew G. Knepley   PetscInt i;
21342adcc780SMatthew G. Knepley 
21352adcc780SMatthew G. Knepley   PetscFunctionBegin;
21364f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2137ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21384f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
21394f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2140bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2141bb7acecfSBarry Smith   if (n) {
2142b9d85ea2SLisandro Dalcin     DM dm = dms[0];
214300045ab3SPierre Jolivet     PetscCheck(dm->ops->createsuperdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No method createsuperdm for DM of type %s", ((PetscObject)dm)->type_name);
2144dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21452adcc780SMatthew G. Knepley   }
21463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21472adcc780SMatthew G. Knepley }
21482adcc780SMatthew G. Knepley 
214916621825SDmitry Karpeev /*@C
2150a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2151a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
215216621825SDmitry Karpeev 
215320f4b53cSBarry Smith   Not Collective
215416621825SDmitry Karpeev 
215516621825SDmitry Karpeev   Input Parameter:
2156bb7acecfSBarry Smith . dm - the `DM` object
215716621825SDmitry Karpeev 
215816621825SDmitry Karpeev   Output Parameters:
215920f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
216020f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
21610298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
21620298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
216320f4b53cSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if `NULL` is returned, no `DM`s are defined)
216416621825SDmitry Karpeev 
216516621825SDmitry Karpeev   Level: intermediate
216616621825SDmitry Karpeev 
2167bb7acecfSBarry Smith   Note:
2168a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2169a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2170a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2171a4e35b19SJacob Faibussowitsch 
2172a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2173a4e35b19SJacob Faibussowitsch 
217416621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2175bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2176bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
217716621825SDmitry Karpeev 
2178a4e35b19SJacob Faibussowitsch   Developer Notes:
217920f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2180bb7acecfSBarry Smith 
218160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
218216621825SDmitry Karpeev @*/
2183d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2184d71ae5a4SJacob Faibussowitsch {
2185be081cd6SPeter Brune   DMSubDomainHookLink link;
2186be081cd6SPeter Brune   PetscInt            i, l;
218716621825SDmitry Karpeev 
218816621825SDmitry Karpeev   PetscFunctionBegin;
218916621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21909371c9d4SSatish Balay   if (n) {
21914f572ea9SToby Isaac     PetscAssertPointer(n, 2);
21929371c9d4SSatish Balay     *n = 0;
21939371c9d4SSatish Balay   }
21949371c9d4SSatish Balay   if (namelist) {
21954f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
21969371c9d4SSatish Balay     *namelist = NULL;
21979371c9d4SSatish Balay   }
21989371c9d4SSatish Balay   if (innerislist) {
21994f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
22009371c9d4SSatish Balay     *innerislist = NULL;
22019371c9d4SSatish Balay   }
22029371c9d4SSatish Balay   if (outerislist) {
22034f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
22049371c9d4SSatish Balay     *outerislist = NULL;
22059371c9d4SSatish Balay   }
22069371c9d4SSatish Balay   if (dmlist) {
22074f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
22089371c9d4SSatish Balay     *dmlist = NULL;
22099371c9d4SSatish Balay   }
2210f3f0edfdSDmitry Karpeev   /*
2211f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2212f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2213f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2214f3f0edfdSDmitry Karpeev    */
22157a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
221616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2217dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
221814a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2219f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2220be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2221be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22229566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2223be081cd6SPeter Brune         }
2224648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2225e7c4fc90SDmitry Karpeev       }
222614a18fd3SPeter Brune     }
2227bb7acecfSBarry Smith     if (n) *n = l;
222814a18fd3SPeter Brune   }
22293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2230e30e807fSPeter Brune }
2231e30e807fSPeter Brune 
2232e30e807fSPeter Brune /*@C
2233e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2234e30e807fSPeter Brune 
223520f4b53cSBarry Smith   Not Collective
2236e30e807fSPeter Brune 
2237e30e807fSPeter Brune   Input Parameters:
2238bb7acecfSBarry Smith + dm     - the `DM` object
2239e30e807fSPeter Brune . n      - the number of subdomain scatters
2240e30e807fSPeter Brune - subdms - the local subdomains
2241e30e807fSPeter Brune 
2242e30e807fSPeter Brune   Output Parameters:
22436b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2244e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2245e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2246e30e807fSPeter Brune 
224720f4b53cSBarry Smith   Level: developer
224820f4b53cSBarry Smith 
2249bb7acecfSBarry Smith   Note:
2250bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2251e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2252e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2253e30e807fSPeter Brune   solution and residual data.
2254e30e807fSPeter Brune 
2255a4e35b19SJacob Faibussowitsch   Developer Notes:
2256a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2257a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2258bb7acecfSBarry Smith 
22591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2260e30e807fSPeter Brune @*/
2261d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat)
2262d71ae5a4SJacob Faibussowitsch {
2263e30e807fSPeter Brune   PetscFunctionBegin;
2264e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22654f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2266dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2268e7c4fc90SDmitry Karpeev }
2269e7c4fc90SDmitry Karpeev 
227047c6ae99SBarry Smith /*@
2271bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
227247c6ae99SBarry Smith 
227320f4b53cSBarry Smith   Collective
227447c6ae99SBarry Smith 
2275d8d19677SJose E. Roman   Input Parameters:
2276bb7acecfSBarry Smith + dm   - the `DM` object
2277bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
227847c6ae99SBarry Smith 
227947c6ae99SBarry Smith   Output Parameter:
228020f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2281ae0a1c52SMatthew G Knepley 
228220f4b53cSBarry Smith   Options Database Key:
2283412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2284412e9a14SMatthew G. Knepley 
228547c6ae99SBarry Smith   Level: developer
228647c6ae99SBarry Smith 
228720f4b53cSBarry Smith   Note:
228820f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
228920f4b53cSBarry Smith 
22901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
229147c6ae99SBarry Smith @*/
2292d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2293d71ae5a4SJacob Faibussowitsch {
2294c833c3b5SJed Brown   DMRefineHookLink link;
229547c6ae99SBarry Smith 
229647c6ae99SBarry Smith   PetscFunctionBegin;
2297732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22989566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2299dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
23004057135bSMatthew G Knepley   if (*dmf) {
230143842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
23028865f1eaSKarl Rupp 
23039566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23048865f1eaSKarl Rupp 
2305644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23060598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2307656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23088865f1eaSKarl Rupp 
23099566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2310c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23111baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2312c833c3b5SJed Brown     }
2313c833c3b5SJed Brown   }
23149566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2316c833c3b5SJed Brown }
2317c833c3b5SJed Brown 
2318bb9467b5SJed Brown /*@C
2319c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2320c833c3b5SJed Brown 
232120f4b53cSBarry Smith   Logically Collective; No Fortran Support
2322c833c3b5SJed Brown 
23234165533cSJose E. Roman   Input Parameters:
2324bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2325bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2326f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
232720f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2328c833c3b5SJed Brown 
232920f4b53cSBarry Smith   Calling sequence of `refinehook`:
2330bb7acecfSBarry Smith + coarse - coarse level `DM`
2331bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2332c833c3b5SJed Brown - ctx    - optional user-defined function context
2333c833c3b5SJed Brown 
233420f4b53cSBarry Smith   Calling sequence of `interphook`:
2335bb7acecfSBarry Smith + coarse - coarse level `DM`
2336c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2337bb7acecfSBarry Smith . fine   - fine level `DM` to update
2338c833c3b5SJed Brown - ctx    - optional user-defined function context
2339c833c3b5SJed Brown 
2340c833c3b5SJed Brown   Level: advanced
2341c833c3b5SJed Brown 
2342c833c3b5SJed Brown   Notes:
2343bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2344bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2345bb7acecfSBarry Smith 
2346bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2347c833c3b5SJed Brown 
2348c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2349c833c3b5SJed Brown 
23501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2351c833c3b5SJed Brown @*/
2352a4e35b19SJacob 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)
2353d71ae5a4SJacob Faibussowitsch {
2354c833c3b5SJed Brown   DMRefineHookLink link, *p;
2355c833c3b5SJed Brown 
2356c833c3b5SJed Brown   PetscFunctionBegin;
2357c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23583d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23593ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23603d8e3701SJed Brown   }
23619566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2362c833c3b5SJed Brown   link->refinehook = refinehook;
2363c833c3b5SJed Brown   link->interphook = interphook;
2364c833c3b5SJed Brown   link->ctx        = ctx;
23650298fd71SBarry Smith   link->next       = NULL;
2366c833c3b5SJed Brown   *p               = link;
23673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2368c833c3b5SJed Brown }
2369c833c3b5SJed Brown 
23703d8e3701SJed Brown /*@C
2371bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2372bb7acecfSBarry Smith   a nonlinear problem to a finer grid
23733d8e3701SJed Brown 
237420f4b53cSBarry Smith   Logically Collective; No Fortran Support
23753d8e3701SJed Brown 
23764165533cSJose E. Roman   Input Parameters:
2377bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2378bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2379bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
238020f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
23813d8e3701SJed Brown 
23823d8e3701SJed Brown   Level: advanced
23833d8e3701SJed Brown 
2384bb7acecfSBarry Smith   Note:
23853d8e3701SJed Brown   This function does nothing if the hook is not in the list.
23863d8e3701SJed Brown 
23871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
23883d8e3701SJed Brown @*/
2389d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2390d71ae5a4SJacob Faibussowitsch {
23913d8e3701SJed Brown   DMRefineHookLink link, *p;
23923d8e3701SJed Brown 
23933d8e3701SJed Brown   PetscFunctionBegin;
23943d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23953d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
23963d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
23973d8e3701SJed Brown       link = *p;
23983d8e3701SJed Brown       *p   = link->next;
23999566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
24003d8e3701SJed Brown       break;
24013d8e3701SJed Brown     }
24023d8e3701SJed Brown   }
24033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24043d8e3701SJed Brown }
24053d8e3701SJed Brown 
2406c833c3b5SJed Brown /*@
2407bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2408c833c3b5SJed Brown 
2409c833c3b5SJed Brown   Collective if any hooks are
2410c833c3b5SJed Brown 
24114165533cSJose E. Roman   Input Parameters:
2412bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2413bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2414bb7acecfSBarry Smith - fine   - finer `DM` to update
2415c833c3b5SJed Brown 
2416c833c3b5SJed Brown   Level: developer
2417c833c3b5SJed Brown 
241860225df5SJacob Faibussowitsch   Developer Notes:
2419bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2420bb7acecfSBarry Smith   an API with consistent terminology.
2421bb7acecfSBarry Smith 
24221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2423c833c3b5SJed Brown @*/
2424d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2425d71ae5a4SJacob Faibussowitsch {
2426c833c3b5SJed Brown   DMRefineHookLink link;
2427c833c3b5SJed Brown 
2428c833c3b5SJed Brown   PetscFunctionBegin;
2429c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24301baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24314057135bSMatthew G Knepley   }
24323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
243347c6ae99SBarry Smith }
243447c6ae99SBarry Smith 
2435eb3f98d2SBarry Smith /*@
24361f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24371f3379b2SToby Isaac 
243820f4b53cSBarry Smith   Collective
24391f3379b2SToby Isaac 
24404165533cSJose E. Roman   Input Parameters:
2441bb7acecfSBarry Smith + coarse    - coarse `DM`
2442bb7acecfSBarry Smith . fine      - fine `DM`
2443bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2444bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2445bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24461f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
24471f3379b2SToby Isaac 
24484165533cSJose E. Roman   Output Parameter:
24491f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
24501f3379b2SToby Isaac 
24511f3379b2SToby Isaac   Level: developer
24521f3379b2SToby Isaac 
2453bb7acecfSBarry Smith   Note:
2454bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
24551f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24561f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24571f3379b2SToby Isaac   slope-limiting reconstruction.
24581f3379b2SToby Isaac 
245960225df5SJacob Faibussowitsch   Developer Notes:
2460bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2461bb7acecfSBarry Smith 
24621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24631f3379b2SToby Isaac @*/
2464d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2465d71ae5a4SJacob Faibussowitsch {
24661f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24671f3379b2SToby Isaac 
24681f3379b2SToby Isaac   PetscFunctionBegin;
24691f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24701f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24711f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24721f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24731f3379b2SToby Isaac 
24749566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24751f3379b2SToby Isaac   if (interpsol) {
24769566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24771f3379b2SToby Isaac   } else if (interp) {
24789566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
247998921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24811f3379b2SToby Isaac }
24821f3379b2SToby Isaac 
24831f3379b2SToby Isaac /*@
2484bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2485eb3f98d2SBarry Smith 
2486eb3f98d2SBarry Smith   Not Collective
2487eb3f98d2SBarry Smith 
2488eb3f98d2SBarry Smith   Input Parameter:
2489bb7acecfSBarry Smith . dm - the `DM` object
2490eb3f98d2SBarry Smith 
2491eb3f98d2SBarry Smith   Output Parameter:
2492eb3f98d2SBarry Smith . level - number of refinements
2493eb3f98d2SBarry Smith 
2494eb3f98d2SBarry Smith   Level: developer
2495eb3f98d2SBarry Smith 
2496bb7acecfSBarry Smith   Note:
2497bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2498bb7acecfSBarry Smith 
24991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2500eb3f98d2SBarry Smith @*/
2501d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2502d71ae5a4SJacob Faibussowitsch {
2503eb3f98d2SBarry Smith   PetscFunctionBegin;
2504eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2505eb3f98d2SBarry Smith   *level = dm->levelup;
25063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2507eb3f98d2SBarry Smith }
2508eb3f98d2SBarry Smith 
2509fef3a512SBarry Smith /*@
2510bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2511fef3a512SBarry Smith 
2512fef3a512SBarry Smith   Not Collective
2513fef3a512SBarry Smith 
2514d8d19677SJose E. Roman   Input Parameters:
2515bb7acecfSBarry Smith + dm    - the `DM` object
2516fef3a512SBarry Smith - level - number of refinements
2517fef3a512SBarry Smith 
2518fef3a512SBarry Smith   Level: advanced
2519fef3a512SBarry Smith 
252095452b02SPatrick Sanan   Notes:
2521bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2522fef3a512SBarry Smith 
2523bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2524bb7acecfSBarry Smith 
25251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2526fef3a512SBarry Smith @*/
2527d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2528d71ae5a4SJacob Faibussowitsch {
2529fef3a512SBarry Smith   PetscFunctionBegin;
2530fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2531fef3a512SBarry Smith   dm->levelup = level;
25323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2533fef3a512SBarry Smith }
2534fef3a512SBarry Smith 
2535d410b0cfSMatthew G. Knepley /*@
2536bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2537d410b0cfSMatthew G. Knepley 
253820f4b53cSBarry Smith   Collective
2539d410b0cfSMatthew G. Knepley 
2540f1a722f8SMatthew G. Knepley   Input Parameters:
2541bb7acecfSBarry Smith + dm     - the `DM` object
2542d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2543d410b0cfSMatthew G. Knepley 
2544d410b0cfSMatthew G. Knepley   Output Parameter:
254520f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2546d410b0cfSMatthew G. Knepley 
2547d410b0cfSMatthew G. Knepley   Level: developer
2548d410b0cfSMatthew G. Knepley 
254920f4b53cSBarry Smith   Note:
255020f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
255120f4b53cSBarry Smith 
25521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2553d410b0cfSMatthew G. Knepley @*/
2554d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2555d71ae5a4SJacob Faibussowitsch {
2556d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2557d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2558dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2559d410b0cfSMatthew G. Knepley   if (*dme) {
2560d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25619566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2562d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25639566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2564d410b0cfSMatthew G. Knepley   }
25653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2566d410b0cfSMatthew G. Knepley }
2567d410b0cfSMatthew G. Knepley 
2568d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2569d71ae5a4SJacob Faibussowitsch {
2570ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2571ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25724f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2573ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
25743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2575ca3d3a14SMatthew G. Knepley }
2576ca3d3a14SMatthew G. Knepley 
2577d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2578d71ae5a4SJacob Faibussowitsch {
2579ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2580ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25814f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2582ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
25833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2584ca3d3a14SMatthew G. Knepley }
2585ca3d3a14SMatthew G. Knepley 
2586ca3d3a14SMatthew G. Knepley /*@
2587bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2588ca3d3a14SMatthew G. Knepley 
2589ca3d3a14SMatthew G. Knepley   Input Parameter:
259020f4b53cSBarry Smith . dm - The `DM`
2591ca3d3a14SMatthew G. Knepley 
2592ca3d3a14SMatthew G. Knepley   Output Parameter:
259320f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2594ca3d3a14SMatthew G. Knepley 
2595ca3d3a14SMatthew G. Knepley   Level: developer
2596ca3d3a14SMatthew G. Knepley 
25971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2598ca3d3a14SMatthew G. Knepley @*/
2599d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2600d71ae5a4SJacob Faibussowitsch {
2601ca3d3a14SMatthew G. Knepley   Vec tv;
2602ca3d3a14SMatthew G. Knepley 
2603ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2604ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26054f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
26069566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2607ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2609ca3d3a14SMatthew G. Knepley }
2610ca3d3a14SMatthew G. Knepley 
2611d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2612d71ae5a4SJacob Faibussowitsch {
2613ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2614ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2615ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2616ca3d3a14SMatthew G. Knepley 
2617ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26189566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26199566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26209566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26219566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26229566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26239566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26249566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26259566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2626ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26279566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2628ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2629ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2630ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26319566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2632ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26339566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26349566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2635ca3d3a14SMatthew G. Knepley     }
2636ca3d3a14SMatthew G. Knepley   }
26379566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26389566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26399566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2640ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2641ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26429566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2643ca3d3a14SMatthew G. Knepley       if (dof) {
2644ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2645ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2646ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2647ca3d3a14SMatthew G. Knepley 
2648ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26499566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26509566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26519566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2652ca3d3a14SMatthew G. Knepley       }
2653ca3d3a14SMatthew G. Knepley     }
2654ca3d3a14SMatthew G. Knepley   }
26559566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2657ca3d3a14SMatthew G. Knepley }
2658ca3d3a14SMatthew G. Knepley 
2659d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2660d71ae5a4SJacob Faibussowitsch {
2661ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2662ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2663ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2664ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2665ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2666ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2667ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26689566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2670ca3d3a14SMatthew G. Knepley }
2671ca3d3a14SMatthew G. Knepley 
2672bb9467b5SJed Brown /*@C
2673bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2674baf369e7SPeter Brune 
267520f4b53cSBarry Smith   Logically Collective
2676baf369e7SPeter Brune 
26774165533cSJose E. Roman   Input Parameters:
2678bb7acecfSBarry Smith + dm        - the `DM`
2679bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2680bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
268120f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2682baf369e7SPeter Brune 
268320f4b53cSBarry Smith   Calling sequence of `beginhook`:
2684a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2685baf369e7SPeter Brune . g    - global vector
2686baf369e7SPeter Brune . mode - mode
2687baf369e7SPeter Brune . l    - local vector
2688baf369e7SPeter Brune - ctx  - optional user-defined function context
2689baf369e7SPeter Brune 
269020f4b53cSBarry Smith   Calling sequence of `endhook`:
2691a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2692a4e35b19SJacob Faibussowitsch . g    - global vector
2693a4e35b19SJacob Faibussowitsch . mode - mode
2694a4e35b19SJacob Faibussowitsch . l    - local vector
2695baf369e7SPeter Brune - ctx  - optional user-defined function context
2696baf369e7SPeter Brune 
2697baf369e7SPeter Brune   Level: advanced
2698baf369e7SPeter Brune 
2699bb7acecfSBarry Smith   Note:
2700bb7acecfSBarry 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.
2701bb7acecfSBarry Smith 
27021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2703baf369e7SPeter Brune @*/
2704a4e35b19SJacob 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)
2705d71ae5a4SJacob Faibussowitsch {
2706baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2707baf369e7SPeter Brune 
2708baf369e7SPeter Brune   PetscFunctionBegin;
2709baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2710baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27119566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2712baf369e7SPeter Brune   link->beginhook = beginhook;
2713baf369e7SPeter Brune   link->endhook   = endhook;
2714baf369e7SPeter Brune   link->ctx       = ctx;
27150298fd71SBarry Smith   link->next      = NULL;
2716baf369e7SPeter Brune   *p              = link;
27173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2718baf369e7SPeter Brune }
2719baf369e7SPeter Brune 
2720d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2721d71ae5a4SJacob Faibussowitsch {
27224c274da1SToby Isaac   Mat          cMat;
272379769bd5SJed Brown   Vec          cVec, cBias;
27244c274da1SToby Isaac   PetscSection section, cSec;
27254c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27264c274da1SToby Isaac 
27274c274da1SToby Isaac   PetscFunctionBegin;
2728a4e35b19SJacob Faibussowitsch   (void)g;
2729a4e35b19SJacob Faibussowitsch   (void)ctx;
27304c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27319566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27324c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27335db9a05bSToby Isaac     PetscInt nRows;
27345db9a05bSToby Isaac 
27359566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27363ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27379566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27389566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27399566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27409566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27419566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27424c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27439566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27444c274da1SToby Isaac       if (dof) {
27454c274da1SToby Isaac         PetscScalar *vals;
27469566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27479566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27484c274da1SToby Isaac       }
27494c274da1SToby Isaac     }
27509566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27514c274da1SToby Isaac   }
27523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27534c274da1SToby Isaac }
27544c274da1SToby Isaac 
275547c6ae99SBarry Smith /*@
275601729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
275701729b5cSPatrick Sanan 
275820f4b53cSBarry Smith   Neighbor-wise Collective
275901729b5cSPatrick Sanan 
276001729b5cSPatrick Sanan   Input Parameters:
2761bb7acecfSBarry Smith + dm   - the `DM` object
276201729b5cSPatrick Sanan . g    - the global vector
2763bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
276401729b5cSPatrick Sanan - l    - the local vector
276501729b5cSPatrick Sanan 
276620f4b53cSBarry Smith   Level: beginner
276720f4b53cSBarry Smith 
276801729b5cSPatrick Sanan   Notes:
2769bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2770bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2771bb7acecfSBarry Smith 
2772bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
277301729b5cSPatrick Sanan 
27741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
277560225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2776bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
277701729b5cSPatrick Sanan @*/
2778d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2779d71ae5a4SJacob Faibussowitsch {
278001729b5cSPatrick Sanan   PetscFunctionBegin;
27819566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27829566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
27833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
278401729b5cSPatrick Sanan }
278501729b5cSPatrick Sanan 
278601729b5cSPatrick Sanan /*@
278747c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
278847c6ae99SBarry Smith 
278920f4b53cSBarry Smith   Neighbor-wise Collective
279047c6ae99SBarry Smith 
279147c6ae99SBarry Smith   Input Parameters:
2792bb7acecfSBarry Smith + dm   - the `DM` object
279347c6ae99SBarry Smith . g    - the global vector
2794bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
279547c6ae99SBarry Smith - l    - the local vector
279647c6ae99SBarry Smith 
279701729b5cSPatrick Sanan   Level: intermediate
279847c6ae99SBarry Smith 
2799bb7acecfSBarry Smith   Notes:
2800bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2801bb7acecfSBarry Smith 
2802bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2803bb7acecfSBarry Smith 
2804bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2805bb7acecfSBarry Smith 
2806bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2807bb7acecfSBarry Smith 
280860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
280947c6ae99SBarry Smith @*/
2810d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2811d71ae5a4SJacob Faibussowitsch {
28127128ae9fSMatthew G Knepley   PetscSF                 sf;
2813baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
281447c6ae99SBarry Smith 
281547c6ae99SBarry Smith   PetscFunctionBegin;
2816171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2817baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28181baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2819baf369e7SPeter Brune   }
28209566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28217128ae9fSMatthew G Knepley   if (sf) {
2822ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2823ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2824d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28257128ae9fSMatthew G Knepley 
28267a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28279566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28289566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28299566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28309566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28319566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28327128ae9fSMatthew G Knepley   } else {
2833fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28347128ae9fSMatthew G Knepley   }
28353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
283647c6ae99SBarry Smith }
283747c6ae99SBarry Smith 
283847c6ae99SBarry Smith /*@
283947c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
284047c6ae99SBarry Smith 
284120f4b53cSBarry Smith   Neighbor-wise Collective
284247c6ae99SBarry Smith 
284347c6ae99SBarry Smith   Input Parameters:
2844bb7acecfSBarry Smith + dm   - the `DM` object
284547c6ae99SBarry Smith . g    - the global vector
2846bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
284747c6ae99SBarry Smith - l    - the local vector
284847c6ae99SBarry Smith 
284901729b5cSPatrick Sanan   Level: intermediate
285047c6ae99SBarry Smith 
2851bb7acecfSBarry Smith   Note:
2852bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2853bb7acecfSBarry Smith 
285460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
285547c6ae99SBarry Smith @*/
2856d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2857d71ae5a4SJacob Faibussowitsch {
28587128ae9fSMatthew G Knepley   PetscSF                 sf;
2859ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2860ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2861ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2862baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2863d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
286447c6ae99SBarry Smith 
286547c6ae99SBarry Smith   PetscFunctionBegin;
2866171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28679566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28689566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28697128ae9fSMatthew G Knepley   if (sf) {
28707a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28717128ae9fSMatthew G Knepley 
28729566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28739566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28749566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28759566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28769566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28779566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28787128ae9fSMatthew G Knepley   } else {
2879fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28807128ae9fSMatthew G Knepley   }
28819566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2882baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28839566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2884baf369e7SPeter Brune   }
28853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
288647c6ae99SBarry Smith }
288747c6ae99SBarry Smith 
2888d4d07f1eSToby Isaac /*@C
2889d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2890d4d07f1eSToby Isaac 
289120f4b53cSBarry Smith   Logically Collective
2892d4d07f1eSToby Isaac 
28934165533cSJose E. Roman   Input Parameters:
2894bb7acecfSBarry Smith + dm        - the `DM`
2895bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2896bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
289720f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2898d4d07f1eSToby Isaac 
289920f4b53cSBarry Smith   Calling sequence of `beginhook`:
2900a4e35b19SJacob Faibussowitsch + global - global `DM`
2901d4d07f1eSToby Isaac . l      - local vector
2902d4d07f1eSToby Isaac . mode   - mode
2903d4d07f1eSToby Isaac . g      - global vector
2904d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2905d4d07f1eSToby Isaac 
290620f4b53cSBarry Smith   Calling sequence of `endhook`:
2907bb7acecfSBarry Smith + global - global `DM`
2908d4d07f1eSToby Isaac . l      - local vector
2909d4d07f1eSToby Isaac . mode   - mode
2910d4d07f1eSToby Isaac . g      - global vector
2911d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2912d4d07f1eSToby Isaac 
2913d4d07f1eSToby Isaac   Level: advanced
2914d4d07f1eSToby Isaac 
29151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2916d4d07f1eSToby Isaac @*/
2917a4e35b19SJacob 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)
2918d71ae5a4SJacob Faibussowitsch {
2919d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2920d4d07f1eSToby Isaac 
2921d4d07f1eSToby Isaac   PetscFunctionBegin;
2922d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2923d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29249566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2925d4d07f1eSToby Isaac   link->beginhook = beginhook;
2926d4d07f1eSToby Isaac   link->endhook   = endhook;
2927d4d07f1eSToby Isaac   link->ctx       = ctx;
2928d4d07f1eSToby Isaac   link->next      = NULL;
2929d4d07f1eSToby Isaac   *p              = link;
29303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2931d4d07f1eSToby Isaac }
2932d4d07f1eSToby Isaac 
2933d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2934d71ae5a4SJacob Faibussowitsch {
29354c274da1SToby Isaac   Mat          cMat;
29364c274da1SToby Isaac   Vec          cVec;
29374c274da1SToby Isaac   PetscSection section, cSec;
29384c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
29394c274da1SToby Isaac 
29404c274da1SToby Isaac   PetscFunctionBegin;
2941a4e35b19SJacob Faibussowitsch   (void)g;
2942a4e35b19SJacob Faibussowitsch   (void)ctx;
29434c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29449566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29454c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
29465db9a05bSToby Isaac     PetscInt nRows;
29475db9a05bSToby Isaac 
29489566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29493ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29509566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29519566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29534c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29549566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29554c274da1SToby Isaac       if (dof) {
29564c274da1SToby Isaac         PetscInt     d;
29574c274da1SToby Isaac         PetscScalar *vals;
29589566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29599566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29604c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29614c274da1SToby Isaac          * we just extracted */
2962ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29634c274da1SToby Isaac       }
29644c274da1SToby Isaac     }
29659566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29669566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29674c274da1SToby Isaac   }
29683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29694c274da1SToby Isaac }
297001729b5cSPatrick Sanan /*@
297101729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
297201729b5cSPatrick Sanan 
297320f4b53cSBarry Smith   Neighbor-wise Collective
297401729b5cSPatrick Sanan 
297501729b5cSPatrick Sanan   Input Parameters:
2976bb7acecfSBarry Smith + dm   - the `DM` object
297701729b5cSPatrick Sanan . l    - the local vector
2978bb7acecfSBarry Smith . mode - if `INSERT_VALUES` then no parallel communication is used, if `ADD_VALUES` then all ghost points from the same base point accumulate into that base point.
297901729b5cSPatrick Sanan - g    - the global vector
298001729b5cSPatrick Sanan 
298120f4b53cSBarry Smith   Level: beginner
298220f4b53cSBarry Smith 
298301729b5cSPatrick Sanan   Notes:
298401729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
2985bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
298601729b5cSPatrick Sanan 
2987bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2988bb7acecfSBarry Smith 
2989bb7acecfSBarry 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.
2990bb7acecfSBarry Smith 
2991bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
299201729b5cSPatrick Sanan 
29931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
299401729b5cSPatrick Sanan @*/
2995d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
2996d71ae5a4SJacob Faibussowitsch {
299701729b5cSPatrick Sanan   PetscFunctionBegin;
29989566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
29999566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
30003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
300101729b5cSPatrick Sanan }
30024c274da1SToby Isaac 
300347c6ae99SBarry Smith /*@
300401729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
30059a42bb27SBarry Smith 
300620f4b53cSBarry Smith   Neighbor-wise Collective
30079a42bb27SBarry Smith 
30089a42bb27SBarry Smith   Input Parameters:
3009bb7acecfSBarry Smith + dm   - the `DM` object
3010f6813fd5SJed Brown . l    - the local vector
3011aa624791SPierre 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.
30121eb28f2eSBarry Smith - g    - the global vector
30139a42bb27SBarry Smith 
301420f4b53cSBarry Smith   Level: intermediate
301520f4b53cSBarry Smith 
301695452b02SPatrick Sanan   Notes:
3017bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3018bb7acecfSBarry Smith 
3019bb7acecfSBarry 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.
3020bb7acecfSBarry Smith 
3021bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3022bb7acecfSBarry Smith 
3023bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3024bb7acecfSBarry Smith 
3025bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30269a42bb27SBarry Smith 
30271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30289a42bb27SBarry Smith @*/
3029d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3030d71ae5a4SJacob Faibussowitsch {
30317128ae9fSMatthew G Knepley   PetscSF                 sf;
303284330215SMatthew G. Knepley   PetscSection            s, gs;
3033d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3034ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3035ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3036ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3037fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3038d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30399a42bb27SBarry Smith 
30409a42bb27SBarry Smith   PetscFunctionBegin;
3041171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3042d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30431baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3044d4d07f1eSToby Isaac   }
30459566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30469566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30479566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30487128ae9fSMatthew G Knepley   switch (mode) {
30497128ae9fSMatthew G Knepley   case INSERT_VALUES:
30507128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3051d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3052d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3053d71ae5a4SJacob Faibussowitsch     break;
30547128ae9fSMatthew G Knepley   case ADD_VALUES:
30557128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3056d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3057d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3058d71ae5a4SJacob Faibussowitsch     break;
3059d71ae5a4SJacob Faibussowitsch   default:
3060d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30617128ae9fSMatthew G Knepley   }
3062ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30639566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3064ca3d3a14SMatthew G. Knepley     if (transform) {
30659566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30669566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30679566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30689566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3069fa88e482SJed Brown     } else if (isInsert) {
30709566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3071fa88e482SJed Brown     } else {
30729566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3073fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3074ca3d3a14SMatthew G. Knepley     }
3075fa88e482SJed Brown     if (s && isInsert) {
30769566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3077fa88e482SJed Brown     } else {
30789566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3079fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3080fa88e482SJed Brown     }
3081ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30829566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
308384330215SMatthew G. Knepley     } else if (s && isInsert) {
308484330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
308584330215SMatthew G. Knepley 
30869566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
30879566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
30889566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
308984330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3090b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
309184330215SMatthew G. Knepley 
30929566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
30939566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
30949566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
30959566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
30969566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
30979566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3098b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
309903442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
31007a8be351SBarry 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);
3101b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3102b3b16f48SMatthew G. Knepley         if (!gcdof) {
310384330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3104b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3105b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
310684330215SMatthew G. Knepley           const PetscInt *cdofs;
310784330215SMatthew G. Knepley           PetscInt        cind = 0;
310884330215SMatthew G. Knepley 
31099566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3110b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31119371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31129371c9d4SSatish Balay               ++cind;
31139371c9d4SSatish Balay               continue;
31149371c9d4SSatish Balay             }
3115b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
311684330215SMatthew G. Knepley           }
31177a8be351SBarry 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);
311884330215SMatthew G. Knepley       }
3119ca3d3a14SMatthew G. Knepley     }
3120fa88e482SJed Brown     if (g_inplace) {
31219566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3122fa88e482SJed Brown     } else {
31239566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3124fa88e482SJed Brown     }
3125ca3d3a14SMatthew G. Knepley     if (transform) {
31269566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31279566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3128fa88e482SJed Brown     } else if (l_inplace) {
31299566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3130ca3d3a14SMatthew G. Knepley     } else {
31319566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3132ca3d3a14SMatthew G. Knepley     }
31337128ae9fSMatthew G Knepley   } else {
3134fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalbegin, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
31357128ae9fSMatthew G Knepley   }
31363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31379a42bb27SBarry Smith }
31389a42bb27SBarry Smith 
31399a42bb27SBarry Smith /*@
31409a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
314147c6ae99SBarry Smith 
314220f4b53cSBarry Smith   Neighbor-wise Collective
314347c6ae99SBarry Smith 
314447c6ae99SBarry Smith   Input Parameters:
3145bb7acecfSBarry Smith + dm   - the `DM` object
3146f6813fd5SJed Brown . l    - the local vector
3147bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3148f6813fd5SJed Brown - g    - the global vector
314947c6ae99SBarry Smith 
315001729b5cSPatrick Sanan   Level: intermediate
315147c6ae99SBarry Smith 
3152bb7acecfSBarry Smith   Note:
3153bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3154bb7acecfSBarry Smith 
315560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
315647c6ae99SBarry Smith @*/
3157d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3158d71ae5a4SJacob Faibussowitsch {
31597128ae9fSMatthew G Knepley   PetscSF                 sf;
316084330215SMatthew G. Knepley   PetscSection            s;
3161d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3162ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
316347c6ae99SBarry Smith 
316447c6ae99SBarry Smith   PetscFunctionBegin;
3165171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31669566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31679566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31687128ae9fSMatthew G Knepley   switch (mode) {
31697128ae9fSMatthew G Knepley   case INSERT_VALUES:
3170d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3171d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3172d71ae5a4SJacob Faibussowitsch     break;
31737128ae9fSMatthew G Knepley   case ADD_VALUES:
3174d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3175d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3176d71ae5a4SJacob Faibussowitsch     break;
3177d71ae5a4SJacob Faibussowitsch   default:
3178d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31797128ae9fSMatthew G Knepley   }
318084330215SMatthew G. Knepley   if (sf && !isInsert) {
3181ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3182ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3183ca3d3a14SMatthew G. Knepley     Vec                tmpl;
318484330215SMatthew G. Knepley 
31859566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3186ca3d3a14SMatthew G. Knepley     if (transform) {
31879566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31889566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3189ca3d3a14SMatthew G. Knepley     } else {
31909566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3191ca3d3a14SMatthew G. Knepley     }
31929566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
31939566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3194ca3d3a14SMatthew G. Knepley     if (transform) {
31959566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31969566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3197ca3d3a14SMatthew G. Knepley     } else {
31989566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3199ca3d3a14SMatthew G. Knepley     }
32009566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
320184330215SMatthew G. Knepley   } else if (s && isInsert) {
32027128ae9fSMatthew G Knepley   } else {
3203fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalend, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
32047128ae9fSMatthew G Knepley   }
3205d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32069566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3207d4d07f1eSToby Isaac   }
32083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
320947c6ae99SBarry Smith }
321047c6ae99SBarry Smith 
3211f089877aSRichard Tran Mills /*@
3212a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3213a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3214a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3215f089877aSRichard Tran Mills 
321620f4b53cSBarry Smith   Neighbor-wise Collective
3217f089877aSRichard Tran Mills 
3218f089877aSRichard Tran Mills   Input Parameters:
3219bb7acecfSBarry Smith + dm   - the `DM` object
3220bc0a1609SRichard Tran Mills . g    - the original local vector
3221bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3222f089877aSRichard Tran Mills 
3223bc0a1609SRichard Tran Mills   Output Parameter:
3224bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3225f089877aSRichard Tran Mills 
3226f089877aSRichard Tran Mills   Level: intermediate
3227f089877aSRichard Tran Mills 
3228a4e35b19SJacob Faibussowitsch   Notes:
3229a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3230a4e35b19SJacob Faibussowitsch 
323160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3232f089877aSRichard Tran Mills @*/
3233d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3234d71ae5a4SJacob Faibussowitsch {
3235f089877aSRichard Tran Mills   PetscFunctionBegin;
3236f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32379f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32389f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32399f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3241f089877aSRichard Tran Mills }
3242f089877aSRichard Tran Mills 
3243f089877aSRichard Tran Mills /*@
3244bb7acecfSBarry Smith   DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3245bb7acecfSBarry Smith   points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3246f089877aSRichard Tran Mills 
324720f4b53cSBarry Smith   Neighbor-wise Collective
3248f089877aSRichard Tran Mills 
3249f089877aSRichard Tran Mills   Input Parameters:
325060225df5SJacob Faibussowitsch + dm   - the `DM` object
3251bc0a1609SRichard Tran Mills . g    - the original local vector
3252bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3253f089877aSRichard Tran Mills 
3254bc0a1609SRichard Tran Mills   Output Parameter:
3255bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3256f089877aSRichard Tran Mills 
3257f089877aSRichard Tran Mills   Level: intermediate
3258f089877aSRichard Tran Mills 
325960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3260f089877aSRichard Tran Mills @*/
3261d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3262d71ae5a4SJacob Faibussowitsch {
3263f089877aSRichard Tran Mills   PetscFunctionBegin;
3264f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32659f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32669f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32679f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3269f089877aSRichard Tran Mills }
3270f089877aSRichard Tran Mills 
327147c6ae99SBarry Smith /*@
3272bb7acecfSBarry Smith   DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
327347c6ae99SBarry Smith 
327420f4b53cSBarry Smith   Collective
327547c6ae99SBarry Smith 
3276d8d19677SJose E. Roman   Input Parameters:
3277bb7acecfSBarry Smith + dm   - the `DM` object
327820f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
327947c6ae99SBarry Smith 
328047c6ae99SBarry Smith   Output Parameter:
3281bb7acecfSBarry Smith . dmc - the coarsened `DM`
328247c6ae99SBarry Smith 
328347c6ae99SBarry Smith   Level: developer
328447c6ae99SBarry Smith 
32851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
328647c6ae99SBarry Smith @*/
3287d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3288d71ae5a4SJacob Faibussowitsch {
3289b17ce1afSJed Brown   DMCoarsenHookLink link;
329047c6ae99SBarry Smith 
329147c6ae99SBarry Smith   PetscFunctionBegin;
3292171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32939566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3294dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3295b9d85ea2SLisandro Dalcin   if (*dmc) {
3296a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
32979566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
329843842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
32999566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3300644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
33010598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3302656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
33039566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3304b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33059566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3306b17ce1afSJed Brown     }
3307b9d85ea2SLisandro Dalcin   }
33089566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33097a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3311b17ce1afSJed Brown }
3312b17ce1afSJed Brown 
3313bb9467b5SJed Brown /*@C
3314b17ce1afSJed Brown   DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3315b17ce1afSJed Brown 
331620f4b53cSBarry Smith   Logically Collective; No Fortran Support
3317b17ce1afSJed Brown 
33184165533cSJose E. Roman   Input Parameters:
3319bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3320b17ce1afSJed Brown . coarsenhook  - function to run when setting up a coarser level
3321bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
332220f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3323b17ce1afSJed Brown 
332420f4b53cSBarry Smith   Calling sequence of `coarsenhook`:
3325bb7acecfSBarry Smith + fine   - fine level `DM`
3326bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3327b17ce1afSJed Brown - ctx    - optional user-defined function context
3328b17ce1afSJed Brown 
332920f4b53cSBarry Smith   Calling sequence of `restricthook`:
3330bb7acecfSBarry Smith + fine      - fine level `DM`
3331bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3332c833c3b5SJed Brown . rscale    - scaling vector for restriction
3333c833c3b5SJed Brown . inject    - matrix restricting by injection
3334b17ce1afSJed Brown . coarse    - coarse level DM to update
3335b17ce1afSJed Brown - ctx       - optional user-defined function context
3336b17ce1afSJed Brown 
3337b17ce1afSJed Brown   Level: advanced
3338b17ce1afSJed Brown 
3339b17ce1afSJed Brown   Notes:
3340bb7acecfSBarry 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`.
3341b17ce1afSJed Brown 
3342b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3343b17ce1afSJed Brown 
3344b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3345bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3346b17ce1afSJed Brown 
3347bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3348bb7acecfSBarry Smith 
33491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3350b17ce1afSJed Brown @*/
3351a4e35b19SJacob 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)
3352d71ae5a4SJacob Faibussowitsch {
3353b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3354b17ce1afSJed Brown 
3355b17ce1afSJed Brown   PetscFunctionBegin;
3356b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33571e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33583ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33591e3d8eccSJed Brown   }
33609566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3361b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3362b17ce1afSJed Brown   link->restricthook = restricthook;
3363b17ce1afSJed Brown   link->ctx          = ctx;
33640298fd71SBarry Smith   link->next         = NULL;
3365b17ce1afSJed Brown   *p                 = link;
33663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3367b17ce1afSJed Brown }
3368b17ce1afSJed Brown 
3369dc822a44SJed Brown /*@C
3370bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3371dc822a44SJed Brown 
337220f4b53cSBarry Smith   Logically Collective; No Fortran Support
3373dc822a44SJed Brown 
33744165533cSJose E. Roman   Input Parameters:
3375bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3376dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3377bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
337820f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3379dc822a44SJed Brown 
3380dc822a44SJed Brown   Level: advanced
3381dc822a44SJed Brown 
3382bb7acecfSBarry Smith   Note:
3383dc822a44SJed Brown   This function does nothing if the hook is not in the list.
3384dc822a44SJed Brown 
33851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3386dc822a44SJed Brown @*/
3387d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3388d71ae5a4SJacob Faibussowitsch {
3389dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3390dc822a44SJed Brown 
3391dc822a44SJed Brown   PetscFunctionBegin;
3392dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3393dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3394dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3395dc822a44SJed Brown       link = *p;
3396dc822a44SJed Brown       *p   = link->next;
33979566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3398dc822a44SJed Brown       break;
3399dc822a44SJed Brown     }
3400dc822a44SJed Brown   }
34013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3402dc822a44SJed Brown }
3403dc822a44SJed Brown 
3404b17ce1afSJed Brown /*@
3405bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3406b17ce1afSJed Brown 
3407b17ce1afSJed Brown   Collective if any hooks are
3408b17ce1afSJed Brown 
34094165533cSJose E. Roman   Input Parameters:
3410bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3411bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3412e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3413bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
341420f4b53cSBarry Smith - coarse  - coarser `DM` to update
3415b17ce1afSJed Brown 
3416b17ce1afSJed Brown   Level: developer
3417b17ce1afSJed Brown 
341860225df5SJacob Faibussowitsch   Developer Notes:
3419bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3420bb7acecfSBarry Smith 
34211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3422b17ce1afSJed Brown @*/
3423d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3424d71ae5a4SJacob Faibussowitsch {
3425b17ce1afSJed Brown   DMCoarsenHookLink link;
3426b17ce1afSJed Brown 
3427b17ce1afSJed Brown   PetscFunctionBegin;
3428b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34291baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3430b17ce1afSJed Brown   }
34313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
343247c6ae99SBarry Smith }
343347c6ae99SBarry Smith 
3434bb9467b5SJed Brown /*@C
3435be081cd6SPeter Brune   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
34365dbd56e3SPeter Brune 
343720f4b53cSBarry Smith   Logically Collective; No Fortran Support
34385dbd56e3SPeter Brune 
34394165533cSJose E. Roman   Input Parameters:
3440bb7acecfSBarry Smith + global       - global `DM`
3441bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
34425dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
344320f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34445dbd56e3SPeter Brune 
344520f4b53cSBarry Smith   Calling sequence of `ddhook`:
3446bb7acecfSBarry Smith + global - global `DM`
3447bb7acecfSBarry Smith . block  - block `DM`
3448ec4806b8SPeter Brune - ctx    - optional user-defined function context
3449ec4806b8SPeter Brune 
345020f4b53cSBarry Smith   Calling sequence of `restricthook`:
3451bb7acecfSBarry Smith + global - global `DM`
34525dbd56e3SPeter Brune . out    - scatter to the outer (with ghost and overlap points) block vector
34535dbd56e3SPeter Brune . in     - scatter to block vector values only owned locally
3454bb7acecfSBarry Smith . block  - block `DM`
34555dbd56e3SPeter Brune - ctx    - optional user-defined function context
34565dbd56e3SPeter Brune 
34575dbd56e3SPeter Brune   Level: advanced
34585dbd56e3SPeter Brune 
34595dbd56e3SPeter Brune   Notes:
3460bb7acecfSBarry Smith   This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
34615dbd56e3SPeter Brune 
34625dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
34635dbd56e3SPeter Brune 
34645dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3465bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
34665dbd56e3SPeter Brune 
34671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
34685dbd56e3SPeter Brune @*/
3469a4e35b19SJacob 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)
3470d71ae5a4SJacob Faibussowitsch {
3471be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34725dbd56e3SPeter Brune 
34735dbd56e3SPeter Brune   PetscFunctionBegin;
34745dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3475b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
34763ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3477b3a6b972SJed Brown   }
34789566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
34795dbd56e3SPeter Brune   link->restricthook = restricthook;
3480be081cd6SPeter Brune   link->ddhook       = ddhook;
34815dbd56e3SPeter Brune   link->ctx          = ctx;
34820298fd71SBarry Smith   link->next         = NULL;
34835dbd56e3SPeter Brune   *p                 = link;
34843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
34855dbd56e3SPeter Brune }
34865dbd56e3SPeter Brune 
3487b3a6b972SJed Brown /*@C
3488b3a6b972SJed Brown   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3489b3a6b972SJed Brown 
349020f4b53cSBarry Smith   Logically Collective; No Fortran Support
3491b3a6b972SJed Brown 
34924165533cSJose E. Roman   Input Parameters:
3493bb7acecfSBarry Smith + global       - global `DM`
3494bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3495b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
349620f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3497b3a6b972SJed Brown 
3498b3a6b972SJed Brown   Level: advanced
3499b3a6b972SJed Brown 
35001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3501b3a6b972SJed Brown @*/
3502d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3503d71ae5a4SJacob Faibussowitsch {
3504b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3505b3a6b972SJed Brown 
3506b3a6b972SJed Brown   PetscFunctionBegin;
3507b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3508b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3509b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3510b3a6b972SJed Brown       link = *p;
3511b3a6b972SJed Brown       *p   = link->next;
35129566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3513b3a6b972SJed Brown       break;
3514b3a6b972SJed Brown     }
3515b3a6b972SJed Brown   }
35163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3517b3a6b972SJed Brown }
3518b3a6b972SJed Brown 
35195dbd56e3SPeter Brune /*@
3520bb7acecfSBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
35215dbd56e3SPeter Brune 
35225dbd56e3SPeter Brune   Collective if any hooks are
35235dbd56e3SPeter Brune 
35244165533cSJose E. Roman   Input Parameters:
3525a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3526a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3527a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3528a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
35295dbd56e3SPeter Brune 
35305dbd56e3SPeter Brune   Level: developer
35315dbd56e3SPeter Brune 
35321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`
35335dbd56e3SPeter Brune @*/
3534d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3535d71ae5a4SJacob Faibussowitsch {
3536be081cd6SPeter Brune   DMSubDomainHookLink link;
35375dbd56e3SPeter Brune 
35385dbd56e3SPeter Brune   PetscFunctionBegin;
3539be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35401baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35415dbd56e3SPeter Brune   }
35423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35435dbd56e3SPeter Brune }
35445dbd56e3SPeter Brune 
35455fe1f584SPeter Brune /*@
3546bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35475fe1f584SPeter Brune 
35485fe1f584SPeter Brune   Not Collective
35495fe1f584SPeter Brune 
35505fe1f584SPeter Brune   Input Parameter:
3551bb7acecfSBarry Smith . dm - the `DM` object
35525fe1f584SPeter Brune 
35535fe1f584SPeter Brune   Output Parameter:
35546a7d9d85SPeter Brune . level - number of coarsenings
35555fe1f584SPeter Brune 
35565fe1f584SPeter Brune   Level: developer
35575fe1f584SPeter Brune 
35581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35595fe1f584SPeter Brune @*/
3560d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3561d71ae5a4SJacob Faibussowitsch {
35625fe1f584SPeter Brune   PetscFunctionBegin;
35635fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35644f572ea9SToby Isaac   PetscAssertPointer(level, 2);
35655fe1f584SPeter Brune   *level = dm->leveldown;
35663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35675fe1f584SPeter Brune }
35685fe1f584SPeter Brune 
35699a64c4a8SMatthew G. Knepley /*@
3570bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
35719a64c4a8SMatthew G. Knepley 
357220f4b53cSBarry Smith   Collective
35739a64c4a8SMatthew G. Knepley 
35749a64c4a8SMatthew G. Knepley   Input Parameters:
3575bb7acecfSBarry Smith + dm    - the `DM` object
35769a64c4a8SMatthew G. Knepley - level - number of coarsenings
35779a64c4a8SMatthew G. Knepley 
35789a64c4a8SMatthew G. Knepley   Level: developer
35799a64c4a8SMatthew G. Knepley 
3580bb7acecfSBarry Smith   Note:
3581bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3582bb7acecfSBarry Smith 
358342747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35849a64c4a8SMatthew G. Knepley @*/
3585d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3586d71ae5a4SJacob Faibussowitsch {
35879a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
35889a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35899a64c4a8SMatthew G. Knepley   dm->leveldown = level;
35903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35919a64c4a8SMatthew G. Knepley }
35929a64c4a8SMatthew G. Knepley 
359347c6ae99SBarry Smith /*@C
3594bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
359547c6ae99SBarry Smith 
359620f4b53cSBarry Smith   Collective
359747c6ae99SBarry Smith 
3598d8d19677SJose E. Roman   Input Parameters:
3599bb7acecfSBarry Smith + dm      - the `DM` object
360047c6ae99SBarry Smith - nlevels - the number of levels of refinement
360147c6ae99SBarry Smith 
360247c6ae99SBarry Smith   Output Parameter:
3603bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
360447c6ae99SBarry Smith 
360547c6ae99SBarry Smith   Level: developer
360647c6ae99SBarry Smith 
36071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
360847c6ae99SBarry Smith @*/
3609d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3610d71ae5a4SJacob Faibussowitsch {
361147c6ae99SBarry Smith   PetscFunctionBegin;
3612171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36137a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36143ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36154f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
3616213acdd3SPierre Jolivet   if (dm->ops->refine && !dm->ops->refinehierarchy) {
361747c6ae99SBarry Smith     PetscInt i;
361847c6ae99SBarry Smith 
36199566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
362048a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3621213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
36223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
362347c6ae99SBarry Smith }
362447c6ae99SBarry Smith 
362547c6ae99SBarry Smith /*@C
3626bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
362747c6ae99SBarry Smith 
362820f4b53cSBarry Smith   Collective
362947c6ae99SBarry Smith 
3630d8d19677SJose E. Roman   Input Parameters:
3631bb7acecfSBarry Smith + dm      - the `DM` object
363247c6ae99SBarry Smith - nlevels - the number of levels of coarsening
363347c6ae99SBarry Smith 
363447c6ae99SBarry Smith   Output Parameter:
3635bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
363647c6ae99SBarry Smith 
363747c6ae99SBarry Smith   Level: developer
363847c6ae99SBarry Smith 
36391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
364047c6ae99SBarry Smith @*/
3641d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3642d71ae5a4SJacob Faibussowitsch {
364347c6ae99SBarry Smith   PetscFunctionBegin;
3644171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36457a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36463ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36474f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
3648213acdd3SPierre Jolivet   if (dm->ops->coarsen && !dm->ops->coarsenhierarchy) {
364947c6ae99SBarry Smith     PetscInt i;
365047c6ae99SBarry Smith 
36519566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
365248a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3653213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
36543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
365547c6ae99SBarry Smith }
365647c6ae99SBarry Smith 
36571a266240SBarry Smith /*@C
3658bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36591a266240SBarry Smith 
3660bb7acecfSBarry Smith   Logically Collective if the function is collective
36611a266240SBarry Smith 
36621a266240SBarry Smith   Input Parameters:
3663bb7acecfSBarry Smith + dm      - the `DM` object
36641a266240SBarry Smith - destroy - the destroy function
36651a266240SBarry Smith 
36661a266240SBarry Smith   Level: intermediate
36671a266240SBarry Smith 
36681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3669f07f9ceaSJed Brown @*/
3670d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3671d71ae5a4SJacob Faibussowitsch {
36721a266240SBarry Smith   PetscFunctionBegin;
3673171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36741a266240SBarry Smith   dm->ctxdestroy = destroy;
36753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36761a266240SBarry Smith }
36771a266240SBarry Smith 
3678b07ff414SBarry Smith /*@
3679bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
368047c6ae99SBarry Smith 
368147c6ae99SBarry Smith   Not Collective
368247c6ae99SBarry Smith 
368347c6ae99SBarry Smith   Input Parameters:
3684bb7acecfSBarry Smith + dm  - the `DM` object
368547c6ae99SBarry Smith - ctx - the user context
368647c6ae99SBarry Smith 
368747c6ae99SBarry Smith   Level: intermediate
368847c6ae99SBarry Smith 
3689bb7acecfSBarry Smith   Note:
369035cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3691bb7acecfSBarry Smith 
369260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
369347c6ae99SBarry Smith @*/
3694d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3695d71ae5a4SJacob Faibussowitsch {
369647c6ae99SBarry Smith   PetscFunctionBegin;
3697171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
369847c6ae99SBarry Smith   dm->ctx = ctx;
36993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
370047c6ae99SBarry Smith }
370147c6ae99SBarry Smith 
370247c6ae99SBarry Smith /*@
3703bb7acecfSBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object
370447c6ae99SBarry Smith 
370547c6ae99SBarry Smith   Not Collective
370647c6ae99SBarry Smith 
370747c6ae99SBarry Smith   Input Parameter:
3708bb7acecfSBarry Smith . dm - the `DM` object
370947c6ae99SBarry Smith 
371047c6ae99SBarry Smith   Output Parameter:
371147c6ae99SBarry Smith . ctx - the user context
371247c6ae99SBarry Smith 
371347c6ae99SBarry Smith   Level: intermediate
371447c6ae99SBarry Smith 
3715bb7acecfSBarry Smith   Note:
371635cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3717bb7acecfSBarry Smith 
371842747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
371947c6ae99SBarry Smith @*/
3720d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3721d71ae5a4SJacob Faibussowitsch {
372247c6ae99SBarry Smith   PetscFunctionBegin;
3723171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37241b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
372647c6ae99SBarry Smith }
372747c6ae99SBarry Smith 
372808da532bSDmitry Karpeev /*@C
3729bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
373008da532bSDmitry Karpeev 
373120f4b53cSBarry Smith   Logically Collective
373208da532bSDmitry Karpeev 
3733d8d19677SJose E. Roman   Input Parameters:
373408da532bSDmitry Karpeev + dm - the DM object
373520f4b53cSBarry Smith - f  - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
373608da532bSDmitry Karpeev 
373708da532bSDmitry Karpeev   Level: intermediate
373808da532bSDmitry Karpeev 
37391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3740db781477SPatrick Sanan          `DMSetJacobian()`
374108da532bSDmitry Karpeev @*/
3742d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3743d71ae5a4SJacob Faibussowitsch {
374408da532bSDmitry Karpeev   PetscFunctionBegin;
37455a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
374608da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
374808da532bSDmitry Karpeev }
374908da532bSDmitry Karpeev 
375008da532bSDmitry Karpeev /*@
3751bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
375208da532bSDmitry Karpeev 
375308da532bSDmitry Karpeev   Not Collective
375408da532bSDmitry Karpeev 
375508da532bSDmitry Karpeev   Input Parameter:
3756bb7acecfSBarry Smith . dm - the `DM` object to destroy
375708da532bSDmitry Karpeev 
375808da532bSDmitry Karpeev   Output Parameter:
3759bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
376008da532bSDmitry Karpeev 
376108da532bSDmitry Karpeev   Level: developer
376208da532bSDmitry Karpeev 
37631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
376408da532bSDmitry Karpeev @*/
3765d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3766d71ae5a4SJacob Faibussowitsch {
376708da532bSDmitry Karpeev   PetscFunctionBegin;
37685a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37694f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
377008da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
37713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
377208da532bSDmitry Karpeev }
377308da532bSDmitry Karpeev 
377408da532bSDmitry Karpeev /*@C
3775bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
377608da532bSDmitry Karpeev 
377720f4b53cSBarry Smith   Logically Collective
377808da532bSDmitry Karpeev 
3779f899ff85SJose E. Roman   Input Parameter:
3780bb7acecfSBarry Smith . dm - the `DM` object
378108da532bSDmitry Karpeev 
378260225df5SJacob Faibussowitsch   Output Parameters:
378308da532bSDmitry Karpeev + xl - lower bound
378408da532bSDmitry Karpeev - xu - upper bound
378508da532bSDmitry Karpeev 
3786907376e6SBarry Smith   Level: advanced
3787907376e6SBarry Smith 
378820f4b53cSBarry Smith   Note:
378995452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
379008da532bSDmitry Karpeev 
37911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
379208da532bSDmitry Karpeev @*/
3793d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3794d71ae5a4SJacob Faibussowitsch {
379508da532bSDmitry Karpeev   PetscFunctionBegin;
37965a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
379708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
37985a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3799dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
380108da532bSDmitry Karpeev }
380208da532bSDmitry Karpeev 
3803b0ae01b7SPeter Brune /*@
3804bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3805b0ae01b7SPeter Brune 
3806b0ae01b7SPeter Brune   Not Collective
3807b0ae01b7SPeter Brune 
3808b0ae01b7SPeter Brune   Input Parameter:
3809b0ae01b7SPeter Brune . dm - the DM object
3810b0ae01b7SPeter Brune 
3811b0ae01b7SPeter Brune   Output Parameter:
3812bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3813b0ae01b7SPeter Brune 
3814b0ae01b7SPeter Brune   Level: developer
3815b0ae01b7SPeter Brune 
38161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3817b0ae01b7SPeter Brune @*/
3818d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3819d71ae5a4SJacob Faibussowitsch {
3820b0ae01b7SPeter Brune   PetscFunctionBegin;
38215a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38224f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3823b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3825b0ae01b7SPeter Brune }
3826b0ae01b7SPeter Brune 
38273ad4599aSBarry Smith /*@
3828bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38293ad4599aSBarry Smith 
38303ad4599aSBarry Smith   Not Collective
38313ad4599aSBarry Smith 
38323ad4599aSBarry Smith   Input Parameter:
3833bb7acecfSBarry Smith . dm - the `DM` object
38343ad4599aSBarry Smith 
38353ad4599aSBarry Smith   Output Parameter:
3836bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38373ad4599aSBarry Smith 
38383ad4599aSBarry Smith   Level: developer
38393ad4599aSBarry Smith 
38401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38413ad4599aSBarry Smith @*/
3842d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3843d71ae5a4SJacob Faibussowitsch {
38443ad4599aSBarry Smith   PetscFunctionBegin;
38455a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38464f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
38473ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38493ad4599aSBarry Smith }
38503ad4599aSBarry Smith 
3851a7058e45SLawrence Mitchell /*@
3852bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3853a7058e45SLawrence Mitchell 
3854a7058e45SLawrence Mitchell   Not Collective
3855a7058e45SLawrence Mitchell 
3856a7058e45SLawrence Mitchell   Input Parameter:
3857bb7acecfSBarry Smith . dm - the `DM` object
3858a7058e45SLawrence Mitchell 
3859a7058e45SLawrence Mitchell   Output Parameter:
3860bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3861a7058e45SLawrence Mitchell 
3862a7058e45SLawrence Mitchell   Level: developer
3863a7058e45SLawrence Mitchell 
38641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3865a7058e45SLawrence Mitchell @*/
3866d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3867d71ae5a4SJacob Faibussowitsch {
3868a7058e45SLawrence Mitchell   PetscFunctionBegin;
38695a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38704f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3871dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3872ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
38733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3874a7058e45SLawrence Mitchell }
3875a7058e45SLawrence Mitchell 
38760298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3877264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3878264ace61SBarry Smith 
3879264ace61SBarry Smith /*@C
3880bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3881264ace61SBarry Smith 
388220f4b53cSBarry Smith   Collective
3883264ace61SBarry Smith 
3884264ace61SBarry Smith   Input Parameters:
3885bb7acecfSBarry Smith + dm     - The `DM` object
3886bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3887264ace61SBarry Smith 
3888264ace61SBarry Smith   Options Database Key:
3889bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3890264ace61SBarry Smith 
3891264ace61SBarry Smith   Level: intermediate
3892264ace61SBarry Smith 
3893bb7acecfSBarry Smith   Note:
38940462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3895bb7acecfSBarry Smith 
38961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3897264ace61SBarry Smith @*/
3898d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3899d71ae5a4SJacob Faibussowitsch {
3900264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3901264ace61SBarry Smith   PetscBool match;
3902264ace61SBarry Smith 
3903264ace61SBarry Smith   PetscFunctionBegin;
3904264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39059566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39063ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3907264ace61SBarry Smith 
39089566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39099566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39107a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3911264ace61SBarry Smith 
3912dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39139566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39149566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39159566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3917264ace61SBarry Smith }
3918264ace61SBarry Smith 
3919264ace61SBarry Smith /*@C
3920bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3921264ace61SBarry Smith 
3922264ace61SBarry Smith   Not Collective
3923264ace61SBarry Smith 
3924264ace61SBarry Smith   Input Parameter:
3925bb7acecfSBarry Smith . dm - The `DM`
3926264ace61SBarry Smith 
3927264ace61SBarry Smith   Output Parameter:
3928bb7acecfSBarry Smith . type - The `DMType` name
3929264ace61SBarry Smith 
3930264ace61SBarry Smith   Level: intermediate
3931264ace61SBarry Smith 
39321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3933264ace61SBarry Smith @*/
3934d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3935d71ae5a4SJacob Faibussowitsch {
3936264ace61SBarry Smith   PetscFunctionBegin;
3937264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39384f572ea9SToby Isaac   PetscAssertPointer(type, 2);
39399566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3940264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3942264ace61SBarry Smith }
3943264ace61SBarry Smith 
394467a56275SMatthew G Knepley /*@C
3945bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
394667a56275SMatthew G Knepley 
394720f4b53cSBarry Smith   Collective
394867a56275SMatthew G Knepley 
394967a56275SMatthew G Knepley   Input Parameters:
3950bb7acecfSBarry Smith + dm      - the `DM`
3951bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
395267a56275SMatthew G Knepley 
395367a56275SMatthew G Knepley   Output Parameter:
3954bb7acecfSBarry Smith . M - pointer to new `DM`
395567a56275SMatthew G Knepley 
395620f4b53cSBarry Smith   Level: intermediate
395720f4b53cSBarry Smith 
395867a56275SMatthew G Knepley   Notes:
3959bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3960bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3961bb7acecfSBarry Smith   of the input `DM`.
396267a56275SMatthew G Knepley 
39631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
396467a56275SMatthew G Knepley @*/
3965d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3966d71ae5a4SJacob Faibussowitsch {
396767a56275SMatthew G Knepley   DM        B;
396867a56275SMatthew G Knepley   char      convname[256];
3969c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
397067a56275SMatthew G Knepley 
397167a56275SMatthew G Knepley   PetscFunctionBegin;
397267a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
397367a56275SMatthew G Knepley   PetscValidType(dm, 1);
39744f572ea9SToby Isaac   PetscAssertPointer(M, 3);
39759566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
39769566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3977c067b6caSMatthew G. Knepley   if (sametype) {
3978c067b6caSMatthew G. Knepley     *M = dm;
39799566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
39803ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3981c067b6caSMatthew G. Knepley   } else {
39820298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
398367a56275SMatthew G Knepley 
398467a56275SMatthew G Knepley     /*
398567a56275SMatthew G Knepley        Order of precedence:
398667a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
398767a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
398867a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
398967a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
399067a56275SMatthew G Knepley        5) Use a really basic converter.
399167a56275SMatthew G Knepley     */
399267a56275SMatthew G Knepley 
399367a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
39949566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39959566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
39969566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
39979566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
39989566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
39999566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
400067a56275SMatthew G Knepley     if (conv) goto foundconv;
400167a56275SMatthew G Knepley 
400267a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40039566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40049566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40059566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40069566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40079566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40089566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40099566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40109566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
401167a56275SMatthew G Knepley     if (conv) {
40129566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
401367a56275SMatthew G Knepley       goto foundconv;
401467a56275SMatthew G Knepley     }
401567a56275SMatthew G Knepley 
401667a56275SMatthew G Knepley #if 0
401767a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
401867a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40199566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
402067a56275SMatthew G Knepley     if (conv) goto foundconv;
402167a56275SMatthew G Knepley 
402267a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
402367a56275SMatthew G Knepley     if (dm->ops->convert) {
402467a56275SMatthew G Knepley       conv = dm->ops->convert;
402567a56275SMatthew G Knepley     }
402667a56275SMatthew G Knepley     if (conv) goto foundconv;
402767a56275SMatthew G Knepley #endif
402867a56275SMatthew G Knepley 
402967a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
403098921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
403167a56275SMatthew G Knepley 
403267a56275SMatthew G Knepley   foundconv:
40339566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40349566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
403512fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
403690b157c4SStefano Zampini     {
40374fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40386858538eSMatthew G. Knepley 
40394fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40404fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4041c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40429566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40439566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40449566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40459566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
404612fa691eSMatthew G. Knepley     }
40479566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
404867a56275SMatthew G Knepley   }
40499566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
405167a56275SMatthew G Knepley }
4052264ace61SBarry Smith 
4053264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4054264ace61SBarry Smith 
4055264ace61SBarry Smith /*@C
4056bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40571c84c290SBarry Smith 
40581c84c290SBarry Smith   Not Collective
40591c84c290SBarry Smith 
40601c84c290SBarry Smith   Input Parameters:
406120f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
406220f4b53cSBarry Smith - function - The creation routine itself
406320f4b53cSBarry Smith 
406420f4b53cSBarry Smith   Level: advanced
40651c84c290SBarry Smith 
40661c84c290SBarry Smith   Notes:
406720f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
40681c84c290SBarry Smith 
406960225df5SJacob Faibussowitsch   Example Usage:
40701c84c290SBarry Smith .vb
4071bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40721c84c290SBarry Smith .ve
40731c84c290SBarry Smith 
407420f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
40751c84c290SBarry Smith .vb
40761c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
40771c84c290SBarry Smith     DMSetType(DM,"my_da");
40781c84c290SBarry Smith .ve
40791c84c290SBarry Smith   or at runtime via the option
40801c84c290SBarry Smith .vb
40811c84c290SBarry Smith     -da_type my_da
40821c84c290SBarry Smith .ve
4083264ace61SBarry Smith 
40841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4085264ace61SBarry Smith @*/
4086d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4087d71ae5a4SJacob Faibussowitsch {
4088264ace61SBarry Smith   PetscFunctionBegin;
40899566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
40909566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
40913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4092264ace61SBarry Smith }
4093264ace61SBarry Smith 
4094b859378eSBarry Smith /*@C
4095bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4096b859378eSBarry Smith 
409720f4b53cSBarry Smith   Collective
4098b859378eSBarry Smith 
4099b859378eSBarry Smith   Input Parameters:
4100bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4101bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4102bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4103bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4104b859378eSBarry Smith 
4105b859378eSBarry Smith   Level: intermediate
4106b859378eSBarry Smith 
4107b859378eSBarry Smith   Notes:
410855849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4109b859378eSBarry Smith 
4110bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4111bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4112bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4113cd7e8a5eSksagiyam 
41141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4115b859378eSBarry Smith @*/
4116d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4117d71ae5a4SJacob Faibussowitsch {
41189331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4119b859378eSBarry Smith 
4120b859378eSBarry Smith   PetscFunctionBegin;
4121b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4122b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41239566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41249566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41259566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41269566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41279331c7a4SMatthew G. Knepley   if (isbinary) {
41289331c7a4SMatthew G. Knepley     PetscInt classid;
41299331c7a4SMatthew G. Knepley     char     type[256];
4130b859378eSBarry Smith 
41319566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41327a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41339566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41349566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4135dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41369331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4137dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41389331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41399566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4141b859378eSBarry Smith }
4142b859378eSBarry Smith 
41437da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41447da65231SMatthew G Knepley 
41452ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[])
41462ecf6ec3SMatthew G. Knepley {
41472ecf6ec3SMatthew G. Knepley   PetscInt f;
41482ecf6ec3SMatthew G. Knepley 
41492ecf6ec3SMatthew G. Knepley   PetscFunctionBegin;
41502ecf6ec3SMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41512ecf6ec3SMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %" PetscInt_FMT " |\n", x[f]));
41522ecf6ec3SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41532ecf6ec3SMatthew G. Knepley }
41542ecf6ec3SMatthew G. Knepley 
4155d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4156d71ae5a4SJacob Faibussowitsch {
41571d47ebbbSSatish Balay   PetscInt f;
41581b30c384SMatthew G Knepley 
41597da65231SMatthew G Knepley   PetscFunctionBegin;
416063a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
416148a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41637da65231SMatthew G Knepley }
41647da65231SMatthew G Knepley 
41655962854dSMatthew G. Knepley PetscErrorCode DMPrintCellVectorReal(PetscInt c, const char name[], PetscInt len, const PetscReal x[])
41665962854dSMatthew G. Knepley {
41675962854dSMatthew G. Knepley   PetscInt f;
41685962854dSMatthew G. Knepley 
41695962854dSMatthew G. Knepley   PetscFunctionBegin;
41705962854dSMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41715962854dSMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)x[f]));
41725962854dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41735962854dSMatthew G. Knepley }
41745962854dSMatthew G. Knepley 
4175d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4176d71ae5a4SJacob Faibussowitsch {
41771b30c384SMatthew G Knepley   PetscInt f, g;
41787da65231SMatthew G Knepley 
41797da65231SMatthew G Knepley   PetscFunctionBegin;
418063a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41811d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
41829566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
418348a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
41849566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
41857da65231SMatthew G Knepley   }
41863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41877da65231SMatthew G Knepley }
4188e7c4fc90SDmitry Karpeev 
4189d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4190d71ae5a4SJacob Faibussowitsch {
41910c5b8624SToby Isaac   PetscInt           localSize, bs;
41920c5b8624SToby Isaac   PetscMPIInt        size;
41930c5b8624SToby Isaac   Vec                x, xglob;
41940c5b8624SToby Isaac   const PetscScalar *xarray;
4195e759306cSMatthew G. Knepley 
4196e759306cSMatthew G. Knepley   PetscFunctionBegin;
41979566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
41989566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
41999566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
420093ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
42019566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
42020c5b8624SToby Isaac   if (size > 1) {
42039566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
42049566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
42059566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
42069566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42070c5b8624SToby Isaac   } else {
42080c5b8624SToby Isaac     xglob = x;
42090c5b8624SToby Isaac   }
42109566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42110c5b8624SToby Isaac   if (size > 1) {
42129566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42139566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42140c5b8624SToby Isaac   }
42159566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
42163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4217e759306cSMatthew G. Knepley }
4218e759306cSMatthew G. Knepley 
421988ed4aceSMatthew G Knepley /*@
4220bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4221061576a5SJed Brown 
4222061576a5SJed Brown   Input Parameter:
4223bb7acecfSBarry Smith . dm - The `DM`
4224061576a5SJed Brown 
4225061576a5SJed Brown   Output Parameter:
4226bb7acecfSBarry Smith . section - The `PetscSection`
4227061576a5SJed Brown 
422820f4b53cSBarry Smith   Options Database Key:
4229bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4230061576a5SJed Brown 
4231061576a5SJed Brown   Level: advanced
4232061576a5SJed Brown 
4233061576a5SJed Brown   Notes:
4234bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4235061576a5SJed Brown 
4236bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4237061576a5SJed Brown 
42381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4239061576a5SJed Brown @*/
4240d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4241d71ae5a4SJacob Faibussowitsch {
4242061576a5SJed Brown   PetscFunctionBegin;
42439566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4245061576a5SJed Brown }
4246061576a5SJed Brown 
4247061576a5SJed Brown /*@
4248bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
424988ed4aceSMatthew G Knepley 
425088ed4aceSMatthew G Knepley   Input Parameter:
4251bb7acecfSBarry Smith . dm - The `DM`
425288ed4aceSMatthew G Knepley 
425388ed4aceSMatthew G Knepley   Output Parameter:
4254bb7acecfSBarry Smith . section - The `PetscSection`
425588ed4aceSMatthew G Knepley 
425620f4b53cSBarry Smith   Options Database Key:
4257bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4258e5893cccSMatthew G. Knepley 
425988ed4aceSMatthew G Knepley   Level: intermediate
426088ed4aceSMatthew G Knepley 
4261bb7acecfSBarry Smith   Note:
4262bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
426388ed4aceSMatthew G Knepley 
42641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
426588ed4aceSMatthew G Knepley @*/
4266d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4267d71ae5a4SJacob Faibussowitsch {
426888ed4aceSMatthew G Knepley   PetscFunctionBegin;
426988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42704f572ea9SToby Isaac   PetscAssertPointer(section, 2);
42711bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4272e5e52638SMatthew G. Knepley     PetscInt d;
4273e5e52638SMatthew G. Knepley 
427445480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
427545480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
427645480ffeSMatthew G. Knepley       PetscViewer       viewer;
427745480ffeSMatthew G. Knepley       PetscViewerFormat format;
427845480ffeSMatthew G. Knepley       PetscBool         flg;
427945480ffeSMatthew G. Knepley 
42809566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
42819566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
428245480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
42839566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
42849566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
428545480ffeSMatthew G. Knepley       }
428645480ffeSMatthew G. Knepley       if (flg) {
42879566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
42889566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
4289cd791dc2SBarry Smith         PetscCall(PetscOptionsRestoreViewer(&viewer));
429045480ffeSMatthew G. Knepley       }
429145480ffeSMatthew G. Knepley     }
4292dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
42939566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
42942f0f8703SMatthew G. Knepley   }
42951bb6d2a8SBarry Smith   *section = dm->localSection;
42963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
429788ed4aceSMatthew G Knepley }
429888ed4aceSMatthew G Knepley 
429988ed4aceSMatthew G Knepley /*@
4300bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4301061576a5SJed Brown 
4302061576a5SJed Brown   Input Parameters:
4303bb7acecfSBarry Smith + dm      - The `DM`
4304bb7acecfSBarry Smith - section - The `PetscSection`
4305061576a5SJed Brown 
4306061576a5SJed Brown   Level: advanced
4307061576a5SJed Brown 
4308061576a5SJed Brown   Notes:
4309bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4310061576a5SJed Brown 
4311bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4312061576a5SJed Brown 
43131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4314061576a5SJed Brown @*/
4315d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4316d71ae5a4SJacob Faibussowitsch {
4317061576a5SJed Brown   PetscFunctionBegin;
43189566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
43193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4320061576a5SJed Brown }
4321061576a5SJed Brown 
4322061576a5SJed Brown /*@
4323bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
432488ed4aceSMatthew G Knepley 
432588ed4aceSMatthew G Knepley   Input Parameters:
4326bb7acecfSBarry Smith + dm      - The `DM`
4327bb7acecfSBarry Smith - section - The `PetscSection`
432888ed4aceSMatthew G Knepley 
432988ed4aceSMatthew G Knepley   Level: intermediate
433088ed4aceSMatthew G Knepley 
4331bb7acecfSBarry Smith   Note:
4332bb7acecfSBarry Smith   Any existing Section will be destroyed
433388ed4aceSMatthew G Knepley 
43341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
433588ed4aceSMatthew G Knepley @*/
4336d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4337d71ae5a4SJacob Faibussowitsch {
4338c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4339af122d2aSMatthew G Knepley   PetscInt f;
434088ed4aceSMatthew G Knepley 
434188ed4aceSMatthew G Knepley   PetscFunctionBegin;
434288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4343b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43449566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43459566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43461bb6d2a8SBarry Smith   dm->localSection = section;
43479566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4348af122d2aSMatthew G Knepley   if (numFields) {
43499566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4350af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43510f21e855SMatthew G. Knepley       PetscObject disc;
4352af122d2aSMatthew G Knepley       const char *name;
4353af122d2aSMatthew G Knepley 
43549566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43559566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43569566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4357af122d2aSMatthew G Knepley     }
4358af122d2aSMatthew G Knepley   }
435950350c8eSStefano Zampini   /* The global section and the SectionSF will be rebuilt
436050350c8eSStefano Zampini      in the next call to DMGetGlobalSection() and DMGetSectionSF(). */
43619566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
436250350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
4363*52947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
436450350c8eSStefano Zampini 
436550350c8eSStefano Zampini   /* Clear scratch vectors */
436650350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
436750350c8eSStefano Zampini   PetscCall(DMClearLocalVectors(dm));
436850350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
436950350c8eSStefano Zampini   PetscCall(DMClearNamedLocalVectors(dm));
43703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
437188ed4aceSMatthew G Knepley }
437288ed4aceSMatthew G Knepley 
4373adc21957SMatthew G. Knepley /*@C
4374adc21957SMatthew G. Knepley   DMCreateSectionPermutation - Create a permutation of the `PetscSection` chart and optionally a blokc structure.
4375adc21957SMatthew G. Knepley 
4376adc21957SMatthew G. Knepley   Input Parameter:
4377adc21957SMatthew G. Knepley . dm - The `DM`
4378adc21957SMatthew G. Knepley 
4379adc21957SMatthew G. Knepley   Output Parameter:
4380adc21957SMatthew G. Knepley + perm        - A permutation of the mesh points in the chart
4381adc21957SMatthew G. Knepley - blockStarts - A high bit is set for the point that begins every block, or NULL for default blocking
4382adc21957SMatthew G. Knepley 
4383adc21957SMatthew G. Knepley   Level: developer
4384adc21957SMatthew G. Knepley 
4385adc21957SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMGetGlobalSection()`
4386adc21957SMatthew G. Knepley @*/
4387adc21957SMatthew G. Knepley PetscErrorCode DMCreateSectionPermutation(DM dm, IS *perm, PetscBT *blockStarts)
4388adc21957SMatthew G. Knepley {
4389adc21957SMatthew G. Knepley   PetscFunctionBegin;
4390adc21957SMatthew G. Knepley   *perm        = NULL;
4391adc21957SMatthew G. Knepley   *blockStarts = NULL;
4392adc21957SMatthew G. Knepley   PetscTryTypeMethod(dm, createsectionpermutation, perm, blockStarts);
4393adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
4394adc21957SMatthew G. Knepley }
4395adc21957SMatthew G. Knepley 
43969435951eSToby Isaac /*@
4397bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
43989435951eSToby Isaac 
439920f4b53cSBarry Smith   not Collective
4400e228b242SToby Isaac 
44019435951eSToby Isaac   Input Parameter:
4402bb7acecfSBarry Smith . dm - The `DM`
44039435951eSToby Isaac 
4404d8d19677SJose E. Roman   Output Parameters:
440520f4b53cSBarry 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.
440620f4b53cSBarry 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.
440779769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
44089435951eSToby Isaac 
44099435951eSToby Isaac   Level: advanced
44109435951eSToby Isaac 
4411bb7acecfSBarry Smith   Note:
4412bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
44139435951eSToby Isaac 
44141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
44159435951eSToby Isaac @*/
4416d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4417d71ae5a4SJacob Faibussowitsch {
44189435951eSToby Isaac   PetscFunctionBegin;
44199435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4420dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44213b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44223b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
442379769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44259435951eSToby Isaac }
44269435951eSToby Isaac 
44279435951eSToby Isaac /*@
4428bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44299435951eSToby Isaac 
443020f4b53cSBarry Smith   Collective
4431e228b242SToby Isaac 
44329435951eSToby Isaac   Input Parameters:
4433bb7acecfSBarry Smith + dm      - The `DM`
4434bb7acecfSBarry 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).
443520f4b53cSBarry 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).
443620f4b53cSBarry 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).
44379435951eSToby Isaac 
44389435951eSToby Isaac   Level: advanced
44399435951eSToby Isaac 
444020f4b53cSBarry Smith   Notes:
444120f4b53cSBarry 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()`.
444220f4b53cSBarry Smith 
444320f4b53cSBarry 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.
444420f4b53cSBarry Smith 
4445bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44469435951eSToby Isaac 
44471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
44489435951eSToby Isaac @*/
4449d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4450d71ae5a4SJacob Faibussowitsch {
4451e228b242SToby Isaac   PetscMPIInt result;
44529435951eSToby Isaac 
44539435951eSToby Isaac   PetscFunctionBegin;
44549435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4455e228b242SToby Isaac   if (section) {
4456e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44579566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44587a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4459e228b242SToby Isaac   }
4460e228b242SToby Isaac   if (mat) {
4461e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44629566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44637a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4464e228b242SToby Isaac   }
446579769bd5SJed Brown   if (bias) {
446679769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44679566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
446879769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
446979769bd5SJed Brown   }
44709566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44719566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44723b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44739566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44749566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44753b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44769566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44779566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
447879769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44809435951eSToby Isaac }
44819435951eSToby Isaac 
4482497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4483507e4973SMatthew G. Knepley /*
4484bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4485507e4973SMatthew G. Knepley 
4486507e4973SMatthew G. Knepley   Input Parameters:
4487bb7acecfSBarry Smith + dm - The `DM`
4488bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4489bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4490507e4973SMatthew G. Knepley 
4491507e4973SMatthew G. Knepley   Level: intermediate
4492507e4973SMatthew G. Knepley 
44931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4494507e4973SMatthew G. Knepley */
4495d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4496d71ae5a4SJacob Faibussowitsch {
4497507e4973SMatthew G. Knepley   MPI_Comm        comm;
4498507e4973SMatthew G. Knepley   PetscLayout     layout;
4499507e4973SMatthew G. Knepley   const PetscInt *ranges;
4500507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4501507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4502507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4503507e4973SMatthew G. Knepley 
4504507e4973SMatthew G. Knepley   PetscFunctionBegin;
45059566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4506507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45079566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
45089566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
45099566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
45109566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
45119566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
45129566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
45139566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
45149566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45159566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4516507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4517f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4518507e4973SMatthew G. Knepley 
45199566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45209566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45219566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45229566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45239566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4525507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45269371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45279371c9d4SSatish 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));
45289371c9d4SSatish Balay       valid = PETSC_FALSE;
45299371c9d4SSatish Balay     }
45309371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45319371c9d4SSatish 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));
45329371c9d4SSatish Balay       valid = PETSC_FALSE;
45339371c9d4SSatish Balay     }
4534507e4973SMatthew G. Knepley     if (gdof < 0) {
4535507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4536507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4537507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4538507e4973SMatthew G. Knepley 
45399566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4540507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45419371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45429371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45439371c9d4SSatish Balay           valid = PETSC_FALSE;
45449371c9d4SSatish Balay           break;
45459371c9d4SSatish Balay         }
4546507e4973SMatthew G. Knepley       }
4547507e4973SMatthew G. Knepley     }
4548507e4973SMatthew G. Knepley   }
45499566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45509566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
45511c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4552507e4973SMatthew G. Knepley   if (!gvalid) {
45539566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4554507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4555507e4973SMatthew G. Knepley   }
45563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4557507e4973SMatthew G. Knepley }
4558f741bcd2SMatthew G. Knepley #endif
4559507e4973SMatthew G. Knepley 
45605f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45616725e60dSJed Brown {
45626725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45634d86920dSPierre Jolivet 
45646725e60dSJed Brown   PetscFunctionBegin;
45656725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45664f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
45675f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45686725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
45696725e60dSJed Brown   else *sf = dm->sf;
45703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45716725e60dSJed Brown }
45726725e60dSJed Brown 
457388ed4aceSMatthew G Knepley /*@
4574bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
457588ed4aceSMatthew G Knepley 
457620f4b53cSBarry Smith   Collective
45778b1ab98fSJed Brown 
457888ed4aceSMatthew G Knepley   Input Parameter:
4579bb7acecfSBarry Smith . dm - The `DM`
458088ed4aceSMatthew G Knepley 
458188ed4aceSMatthew G Knepley   Output Parameter:
4582bb7acecfSBarry Smith . section - The `PetscSection`
458388ed4aceSMatthew G Knepley 
458488ed4aceSMatthew G Knepley   Level: intermediate
458588ed4aceSMatthew G Knepley 
4586bb7acecfSBarry Smith   Note:
4587bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
458888ed4aceSMatthew G Knepley 
45891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
459088ed4aceSMatthew G Knepley @*/
4591d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4592d71ae5a4SJacob Faibussowitsch {
459388ed4aceSMatthew G Knepley   PetscFunctionBegin;
459488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45954f572ea9SToby Isaac   PetscAssertPointer(section, 2);
45961bb6d2a8SBarry Smith   if (!dm->globalSection) {
4597fd59a867SMatthew G. Knepley     PetscSection s;
45986725e60dSJed Brown     PetscSF      sf;
4599fd59a867SMatthew G. Knepley 
46009566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
46017a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
46027a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
46035f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
4604eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
46059566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
46069566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
46079566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
460888ed4aceSMatthew G Knepley   }
46091bb6d2a8SBarry Smith   *section = dm->globalSection;
46103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
461188ed4aceSMatthew G Knepley }
461288ed4aceSMatthew G Knepley 
4613b21d0597SMatthew G Knepley /*@
4614bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4615b21d0597SMatthew G Knepley 
4616b21d0597SMatthew G Knepley   Input Parameters:
4617bb7acecfSBarry Smith + dm      - The `DM`
46182fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4619b21d0597SMatthew G Knepley 
4620b21d0597SMatthew G Knepley   Level: intermediate
4621b21d0597SMatthew G Knepley 
4622bb7acecfSBarry Smith   Note:
4623bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4624b21d0597SMatthew G Knepley 
46251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4626b21d0597SMatthew G Knepley @*/
4627d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4628d71ae5a4SJacob Faibussowitsch {
4629b21d0597SMatthew G Knepley   PetscFunctionBegin;
4630b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46315080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46329566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46339566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46341bb6d2a8SBarry Smith   dm->globalSection = section;
4635497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46369566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4637507e4973SMatthew G. Knepley #endif
463850350c8eSStefano Zampini   /* Clear global scratch vectors and sectionSF */
463950350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
4640*52947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
464150350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
464250350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
46433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4644b21d0597SMatthew G Knepley }
4645b21d0597SMatthew G Knepley 
464688ed4aceSMatthew G Knepley /*@
4647bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4648bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
464988ed4aceSMatthew G Knepley 
465088ed4aceSMatthew G Knepley   Input Parameter:
4651bb7acecfSBarry Smith . dm - The `DM`
465288ed4aceSMatthew G Knepley 
465388ed4aceSMatthew G Knepley   Output Parameter:
4654bb7acecfSBarry Smith . sf - The `PetscSF`
465588ed4aceSMatthew G Knepley 
465688ed4aceSMatthew G Knepley   Level: intermediate
465788ed4aceSMatthew G Knepley 
4658bb7acecfSBarry Smith   Note:
4659bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
466088ed4aceSMatthew G Knepley 
46611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
466288ed4aceSMatthew G Knepley @*/
4663d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4664d71ae5a4SJacob Faibussowitsch {
466588ed4aceSMatthew G Knepley   PetscInt nroots;
466688ed4aceSMatthew G Knepley 
466788ed4aceSMatthew G Knepley   PetscFunctionBegin;
466888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46694f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
467048a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
46719566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
467288ed4aceSMatthew G Knepley   if (nroots < 0) {
467388ed4aceSMatthew G Knepley     PetscSection section, gSection;
467488ed4aceSMatthew G Knepley 
46759566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
467631ea6d37SMatthew G Knepley     if (section) {
46779566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
46789566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
467931ea6d37SMatthew G Knepley     } else {
46800298fd71SBarry Smith       *sf = NULL;
46813ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
468231ea6d37SMatthew G Knepley     }
468388ed4aceSMatthew G Knepley   }
46841bb6d2a8SBarry Smith   *sf = dm->sectionSF;
46853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
468688ed4aceSMatthew G Knepley }
468788ed4aceSMatthew G Knepley 
468888ed4aceSMatthew G Knepley /*@
4689bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
469088ed4aceSMatthew G Knepley 
469188ed4aceSMatthew G Knepley   Input Parameters:
4692bb7acecfSBarry Smith + dm - The `DM`
4693bb7acecfSBarry Smith - sf - The `PetscSF`
469488ed4aceSMatthew G Knepley 
469588ed4aceSMatthew G Knepley   Level: intermediate
469688ed4aceSMatthew G Knepley 
4697bb7acecfSBarry Smith   Note:
4698bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
469988ed4aceSMatthew G Knepley 
47001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
470188ed4aceSMatthew G Knepley @*/
4702d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4703d71ae5a4SJacob Faibussowitsch {
470488ed4aceSMatthew G Knepley   PetscFunctionBegin;
470588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4706b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47079566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47089566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
47091bb6d2a8SBarry Smith   dm->sectionSF = sf;
47103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
471188ed4aceSMatthew G Knepley }
471288ed4aceSMatthew G Knepley 
471388ed4aceSMatthew G Knepley /*@C
4714bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
471588ed4aceSMatthew G Knepley   describing the data layout.
471688ed4aceSMatthew G Knepley 
471788ed4aceSMatthew G Knepley   Input Parameters:
4718bb7acecfSBarry Smith + dm            - The `DM`
4719bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4720bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
472188ed4aceSMatthew G Knepley 
47221bb6d2a8SBarry Smith   Level: developer
47231bb6d2a8SBarry Smith 
4724bb7acecfSBarry Smith   Note:
4725bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4726bb7acecfSBarry Smith 
472760225df5SJacob Faibussowitsch   Developer Notes:
4728bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4729bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4730bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
47311bb6d2a8SBarry Smith 
47321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
473388ed4aceSMatthew G Knepley @*/
4734d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4735d71ae5a4SJacob Faibussowitsch {
473688ed4aceSMatthew G Knepley   PetscFunctionBegin;
473788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47389566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
47393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
474088ed4aceSMatthew G Knepley }
4741af122d2aSMatthew G Knepley 
4742b21d0597SMatthew G Knepley /*@
4743bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4744bb7acecfSBarry Smith 
4745bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4746b21d0597SMatthew G Knepley 
4747b21d0597SMatthew G Knepley   Input Parameter:
4748bb7acecfSBarry Smith . dm - The `DM`
4749b21d0597SMatthew G Knepley 
4750b21d0597SMatthew G Knepley   Output Parameter:
4751bb7acecfSBarry Smith . sf - The `PetscSF`
4752b21d0597SMatthew G Knepley 
4753b21d0597SMatthew G Knepley   Level: intermediate
4754b21d0597SMatthew G Knepley 
4755bb7acecfSBarry Smith   Note:
4756bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4757b21d0597SMatthew G Knepley 
47581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4759b21d0597SMatthew G Knepley @*/
4760d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4761d71ae5a4SJacob Faibussowitsch {
4762b21d0597SMatthew G Knepley   PetscFunctionBegin;
4763b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47644f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4765b21d0597SMatthew G Knepley   *sf = dm->sf;
47663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4767b21d0597SMatthew G Knepley }
4768b21d0597SMatthew G Knepley 
4769057b4bcdSMatthew G Knepley /*@
4770bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4771bb7acecfSBarry Smith 
477220f4b53cSBarry Smith   Collective
4773057b4bcdSMatthew G Knepley 
4774057b4bcdSMatthew G Knepley   Input Parameters:
4775bb7acecfSBarry Smith + dm - The `DM`
4776bb7acecfSBarry Smith - sf - The `PetscSF`
4777057b4bcdSMatthew G Knepley 
4778057b4bcdSMatthew G Knepley   Level: intermediate
4779057b4bcdSMatthew G Knepley 
47801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4781057b4bcdSMatthew G Knepley @*/
4782d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4783d71ae5a4SJacob Faibussowitsch {
4784057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4785057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4786b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47879566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47889566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4789057b4bcdSMatthew G Knepley   dm->sf = sf;
47903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4791057b4bcdSMatthew G Knepley }
4792057b4bcdSMatthew G Knepley 
47934f37162bSMatthew G. Knepley /*@
4794bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
47954f37162bSMatthew G. Knepley 
47964f37162bSMatthew G. Knepley   Input Parameter:
4797bb7acecfSBarry Smith . dm - The `DM`
47984f37162bSMatthew G. Knepley 
47994f37162bSMatthew G. Knepley   Output Parameter:
4800bb7acecfSBarry Smith . sf - The `PetscSF`
48014f37162bSMatthew G. Knepley 
48024f37162bSMatthew G. Knepley   Level: intermediate
48034f37162bSMatthew G. Knepley 
4804bb7acecfSBarry Smith   Note:
4805bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
48064f37162bSMatthew G. Knepley 
48071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48084f37162bSMatthew G. Knepley @*/
4809d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4810d71ae5a4SJacob Faibussowitsch {
48114f37162bSMatthew G. Knepley   PetscFunctionBegin;
48124f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48134f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
48144f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
48153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48164f37162bSMatthew G. Knepley }
48174f37162bSMatthew G. Knepley 
48184f37162bSMatthew G. Knepley /*@
48194f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48204f37162bSMatthew G. Knepley 
48214f37162bSMatthew G. Knepley   Input Parameters:
48224f37162bSMatthew G. Knepley + dm - The DM
48234f37162bSMatthew G. Knepley - sf - The PetscSF
48244f37162bSMatthew G. Knepley 
48254f37162bSMatthew G. Knepley   Level: intermediate
48264f37162bSMatthew G. Knepley 
48271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48284f37162bSMatthew G. Knepley @*/
4829d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4830d71ae5a4SJacob Faibussowitsch {
48314f37162bSMatthew G. Knepley   PetscFunctionBegin;
48324f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48334f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48349566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48359566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48364f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48384f37162bSMatthew G. Knepley }
48394f37162bSMatthew G. Knepley 
4840d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4841d71ae5a4SJacob Faibussowitsch {
484234aa8a36SMatthew G. Knepley   PetscClassId id;
484334aa8a36SMatthew G. Knepley 
484434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48459566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
484634aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48479566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
484834aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48499566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
485017c1d62eSMatthew G. Knepley   } else {
48519566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
485234aa8a36SMatthew G. Knepley   }
48533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
485434aa8a36SMatthew G. Knepley }
485534aa8a36SMatthew G. Knepley 
4856d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4857d71ae5a4SJacob Faibussowitsch {
485844a7f3ddSMatthew G. Knepley   RegionField *tmpr;
485944a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
486044a7f3ddSMatthew G. Knepley 
486144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
48623ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
48639566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
486444a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
48659371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
48669371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48679371c9d4SSatish Balay     tmpr[f].label       = NULL;
48689371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
48699371c9d4SSatish Balay   }
48709566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
487144a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
487244a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
48733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
487444a7f3ddSMatthew G. Knepley }
487544a7f3ddSMatthew G. Knepley 
487644a7f3ddSMatthew G. Knepley /*@
487720f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
487844a7f3ddSMatthew G. Knepley 
487920f4b53cSBarry Smith   Logically Collective
488044a7f3ddSMatthew G. Knepley 
488144a7f3ddSMatthew G. Knepley   Input Parameter:
488220f4b53cSBarry Smith . dm - The `DM`
488344a7f3ddSMatthew G. Knepley 
488444a7f3ddSMatthew G. Knepley   Level: intermediate
488544a7f3ddSMatthew G. Knepley 
48861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
488744a7f3ddSMatthew G. Knepley @*/
4888d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4889d71ae5a4SJacob Faibussowitsch {
489044a7f3ddSMatthew G. Knepley   PetscInt f;
489144a7f3ddSMatthew G. Knepley 
489244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
489344a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
489444a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
48959566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
48969566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
489744a7f3ddSMatthew G. Knepley   }
48989566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
489944a7f3ddSMatthew G. Knepley   dm->fields = NULL;
490044a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
49013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
490244a7f3ddSMatthew G. Knepley }
490344a7f3ddSMatthew G. Knepley 
4904689b5837SMatthew G. Knepley /*@
490520f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4906689b5837SMatthew G. Knepley 
490720f4b53cSBarry Smith   Not Collective
4908689b5837SMatthew G. Knepley 
4909689b5837SMatthew G. Knepley   Input Parameter:
491020f4b53cSBarry Smith . dm - The `DM`
4911689b5837SMatthew G. Knepley 
4912689b5837SMatthew G. Knepley   Output Parameter:
491360225df5SJacob Faibussowitsch . numFields - The number of fields
4914689b5837SMatthew G. Knepley 
4915689b5837SMatthew G. Knepley   Level: intermediate
4916689b5837SMatthew G. Knepley 
49171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4918689b5837SMatthew G. Knepley @*/
4919d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4920d71ae5a4SJacob Faibussowitsch {
49210f21e855SMatthew G. Knepley   PetscFunctionBegin;
49220f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49234f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
492444a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
49253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4926af122d2aSMatthew G Knepley }
4927af122d2aSMatthew G Knepley 
4928689b5837SMatthew G. Knepley /*@
492920f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4930689b5837SMatthew G. Knepley 
493120f4b53cSBarry Smith   Logically Collective
4932689b5837SMatthew G. Knepley 
4933689b5837SMatthew G. Knepley   Input Parameters:
493420f4b53cSBarry Smith + dm        - The `DM`
493560225df5SJacob Faibussowitsch - numFields - The number of fields
4936689b5837SMatthew G. Knepley 
4937689b5837SMatthew G. Knepley   Level: intermediate
4938689b5837SMatthew G. Knepley 
49391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4940689b5837SMatthew G. Knepley @*/
4941d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4942d71ae5a4SJacob Faibussowitsch {
49430f21e855SMatthew G. Knepley   PetscInt Nf, f;
4944af122d2aSMatthew G Knepley 
4945af122d2aSMatthew G Knepley   PetscFunctionBegin;
4946af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49479566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49480f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49490f21e855SMatthew G. Knepley     PetscContainer obj;
49500f21e855SMatthew G. Knepley 
49519566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49529566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
49539566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4954af122d2aSMatthew G Knepley   }
49553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4956af122d2aSMatthew G Knepley }
4957af122d2aSMatthew G Knepley 
4958c1929be8SMatthew G. Knepley /*@
4959bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4960c1929be8SMatthew G. Knepley 
496120f4b53cSBarry Smith   Not Collective
4962c1929be8SMatthew G. Knepley 
4963c1929be8SMatthew G. Knepley   Input Parameters:
4964bb7acecfSBarry Smith + dm - The `DM`
4965c1929be8SMatthew G. Knepley - f  - The field number
4966c1929be8SMatthew G. Knepley 
496744a7f3ddSMatthew G. Knepley   Output Parameters:
496820f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
496920f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
4970c1929be8SMatthew G. Knepley 
497144a7f3ddSMatthew G. Knepley   Level: intermediate
4972c1929be8SMatthew G. Knepley 
49731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
4974c1929be8SMatthew G. Knepley @*/
4975d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4976d71ae5a4SJacob Faibussowitsch {
4977af122d2aSMatthew G Knepley   PetscFunctionBegin;
4978af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49794f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
49807a8be351SBarry 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);
498144a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4982bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
49833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4984decb47aaSMatthew G. Knepley }
4985decb47aaSMatthew G. Knepley 
4986083401c6SMatthew G. Knepley /* Does not clear the DS */
4987d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4988d71ae5a4SJacob Faibussowitsch {
4989083401c6SMatthew G. Knepley   PetscFunctionBegin;
49909566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
49919566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
49929566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4993083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4994bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
49959566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4996bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
49973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4998083401c6SMatthew G. Knepley }
4999083401c6SMatthew G. Knepley 
500046560f82SMatthew G. Knepley /*@C
5001bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
5002bb7acecfSBarry Smith   the field numbering.
5003c1929be8SMatthew G. Knepley 
500420f4b53cSBarry Smith   Logically Collective
5005c1929be8SMatthew G. Knepley 
5006c1929be8SMatthew G. Knepley   Input Parameters:
5007bb7acecfSBarry Smith + dm    - The `DM`
5008c1929be8SMatthew G. Knepley . f     - The field number
500920f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5010bb7acecfSBarry Smith - disc  - The discretization object
5011c1929be8SMatthew G. Knepley 
501244a7f3ddSMatthew G. Knepley   Level: intermediate
5013c1929be8SMatthew G. Knepley 
50141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
5015c1929be8SMatthew G. Knepley @*/
5016d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5017d71ae5a4SJacob Faibussowitsch {
5018decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5019decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5020e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
5021bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50227a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5023bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5024bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50259566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
502744a7f3ddSMatthew G. Knepley }
502844a7f3ddSMatthew G. Knepley 
502946560f82SMatthew G. Knepley /*@C
5030bb7acecfSBarry 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)
5031bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
503244a7f3ddSMatthew G. Knepley 
503320f4b53cSBarry Smith   Logically Collective
503444a7f3ddSMatthew G. Knepley 
503544a7f3ddSMatthew G. Knepley   Input Parameters:
5036bb7acecfSBarry Smith + dm    - The `DM`
503720f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5038bb7acecfSBarry Smith - disc  - The discretization object
503944a7f3ddSMatthew G. Knepley 
504044a7f3ddSMatthew G. Knepley   Level: intermediate
504144a7f3ddSMatthew G. Knepley 
5042bb7acecfSBarry Smith   Notes:
5043bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5044bb7acecfSBarry Smith 
5045da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5046bb7acecfSBarry 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
5047bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5048bb7acecfSBarry Smith 
50491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
505044a7f3ddSMatthew G. Knepley @*/
5051d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5052d71ae5a4SJacob Faibussowitsch {
505344a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
505444a7f3ddSMatthew G. Knepley 
505544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
505644a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5057064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5058bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50599566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
506044a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5061bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50629566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5063bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5064bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50659566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5067af122d2aSMatthew G Knepley }
50686636e97aSMatthew G Knepley 
5069e5e52638SMatthew G. Knepley /*@
5070e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5071e0b68406SMatthew Knepley 
507220f4b53cSBarry Smith   Logically Collective
5073e0b68406SMatthew Knepley 
5074e0b68406SMatthew Knepley   Input Parameters:
5075bb7acecfSBarry Smith + dm          - The `DM`
5076e0b68406SMatthew Knepley . f           - The field index
5077bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5078e0b68406SMatthew Knepley 
5079e0b68406SMatthew Knepley   Level: intermediate
5080e0b68406SMatthew Knepley 
50811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5082e0b68406SMatthew Knepley @*/
5083d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5084d71ae5a4SJacob Faibussowitsch {
5085e0b68406SMatthew Knepley   PetscFunctionBegin;
508663a3b9bcSJacob 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);
5087e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
50883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5089e0b68406SMatthew Knepley }
5090e0b68406SMatthew Knepley 
5091e0b68406SMatthew Knepley /*@
5092e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5093e0b68406SMatthew Knepley 
509420f4b53cSBarry Smith   Not Collective
5095e0b68406SMatthew Knepley 
5096e0b68406SMatthew Knepley   Input Parameters:
5097bb7acecfSBarry Smith + dm - The `DM`
5098e0b68406SMatthew Knepley - f  - The field index
5099e0b68406SMatthew Knepley 
5100e0b68406SMatthew Knepley   Output Parameter:
5101e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5102e0b68406SMatthew Knepley 
5103e0b68406SMatthew Knepley   Level: intermediate
5104e0b68406SMatthew Knepley 
510560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5106e0b68406SMatthew Knepley @*/
5107d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5108d71ae5a4SJacob Faibussowitsch {
5109e0b68406SMatthew Knepley   PetscFunctionBegin;
511063a3b9bcSJacob 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);
5111e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
51123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5113e0b68406SMatthew Knepley }
5114e0b68406SMatthew Knepley 
5115e0b68406SMatthew Knepley /*@
5116bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5117e5e52638SMatthew G. Knepley 
511820f4b53cSBarry Smith   Collective
5119e5e52638SMatthew G. Knepley 
5120e5e52638SMatthew G. Knepley   Input Parameter:
5121bb7acecfSBarry Smith . dm - The `DM`
5122e5e52638SMatthew G. Knepley 
5123e5e52638SMatthew G. Knepley   Output Parameter:
5124bb7acecfSBarry Smith . newdm - The `DM`
5125e5e52638SMatthew G. Knepley 
5126e5e52638SMatthew G. Knepley   Level: advanced
5127e5e52638SMatthew G. Knepley 
51281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5129e5e52638SMatthew G. Knepley @*/
5130d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5131d71ae5a4SJacob Faibussowitsch {
5132e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5133e5e52638SMatthew G. Knepley 
5134e5e52638SMatthew G. Knepley   PetscFunctionBegin;
51353ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
51369566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
51379566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5138e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5139e5e52638SMatthew G. Knepley     DMLabel     label;
5140e5e52638SMatthew G. Knepley     PetscObject field;
514134aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5142e5e52638SMatthew G. Knepley 
51439566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
51449566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
51459566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
51469566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
514734aa8a36SMatthew G. Knepley   }
51483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
514934aa8a36SMatthew G. Knepley }
515034aa8a36SMatthew G. Knepley 
515134aa8a36SMatthew G. Knepley /*@
515234aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
515334aa8a36SMatthew G. Knepley 
515420f4b53cSBarry Smith   Not Collective
515534aa8a36SMatthew G. Knepley 
515634aa8a36SMatthew G. Knepley   Input Parameters:
515720f4b53cSBarry Smith + dm - The `DM` object
515820f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
515934aa8a36SMatthew G. Knepley 
5160d8d19677SJose E. Roman   Output Parameters:
516134aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
516234aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
516334aa8a36SMatthew G. Knepley 
516434aa8a36SMatthew G. Knepley   Level: developer
516534aa8a36SMatthew G. Knepley 
516620f4b53cSBarry Smith   Notes:
516720f4b53cSBarry Smith .vb
516820f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
516920f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
517020f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
517120f4b53cSBarry Smith .ve
517220f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
517320f4b53cSBarry Smith 
51741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
517534aa8a36SMatthew G. Knepley @*/
5176d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5177d71ae5a4SJacob Faibussowitsch {
517834aa8a36SMatthew G. Knepley   PetscFunctionBegin;
517934aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51804f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
51814f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
518234aa8a36SMatthew G. Knepley   if (f < 0) {
518334aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
518434aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
518534aa8a36SMatthew G. Knepley   } else {
518634aa8a36SMatthew G. Knepley     PetscInt Nf;
518734aa8a36SMatthew G. Knepley 
51889566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51897a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
519034aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
519134aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
519234aa8a36SMatthew G. Knepley   }
51933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
519434aa8a36SMatthew G. Knepley }
519534aa8a36SMatthew G. Knepley 
519634aa8a36SMatthew G. Knepley /*@
519734aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
519834aa8a36SMatthew G. Knepley 
519920f4b53cSBarry Smith   Not Collective
520034aa8a36SMatthew G. Knepley 
520134aa8a36SMatthew G. Knepley   Input Parameters:
520220f4b53cSBarry Smith + dm         - The `DM` object
520334aa8a36SMatthew G. Knepley . f          - The field number
520434aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
520534aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
520634aa8a36SMatthew G. Knepley 
520734aa8a36SMatthew G. Knepley   Level: developer
520834aa8a36SMatthew G. Knepley 
520920f4b53cSBarry Smith   Notes:
521020f4b53cSBarry Smith .vb
521120f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
521220f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
521320f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
521420f4b53cSBarry Smith .ve
521520f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
521620f4b53cSBarry Smith 
52171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
521834aa8a36SMatthew G. Knepley @*/
5219d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5220d71ae5a4SJacob Faibussowitsch {
522134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
522234aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
522334aa8a36SMatthew G. Knepley   if (f < 0) {
522434aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
522534aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
522634aa8a36SMatthew G. Knepley   } else {
522734aa8a36SMatthew G. Knepley     PetscInt Nf;
522834aa8a36SMatthew G. Knepley 
52299566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52307a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
523134aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
523234aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5233e5e52638SMatthew G. Knepley   }
52343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5235e5e52638SMatthew G. Knepley }
5236e5e52638SMatthew G. Knepley 
5237b0441da4SMatthew G. Knepley /*@
5238b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5239b0441da4SMatthew G. Knepley 
5240b0441da4SMatthew G. Knepley   Not collective
5241b0441da4SMatthew G. Knepley 
5242f899ff85SJose E. Roman   Input Parameter:
524320f4b53cSBarry Smith . dm - The `DM` object
5244b0441da4SMatthew G. Knepley 
5245d8d19677SJose E. Roman   Output Parameters:
5246b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5247b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5248b0441da4SMatthew G. Knepley 
5249b0441da4SMatthew G. Knepley   Level: developer
5250b0441da4SMatthew G. Knepley 
525120f4b53cSBarry Smith   Notes:
525220f4b53cSBarry Smith .vb
525320f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
525420f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
525520f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
525620f4b53cSBarry Smith .ve
525720f4b53cSBarry Smith 
52581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5259b0441da4SMatthew G. Knepley @*/
5260d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5261d71ae5a4SJacob Faibussowitsch {
5262b0441da4SMatthew G. Knepley   PetscInt Nf;
5263b0441da4SMatthew G. Knepley 
5264b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5265b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52664f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
52674f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
52689566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5269b0441da4SMatthew G. Knepley   if (!Nf) {
52709566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5271b0441da4SMatthew G. Knepley   } else {
52729566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5273b0441da4SMatthew G. Knepley   }
52743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5275b0441da4SMatthew G. Knepley }
5276b0441da4SMatthew G. Knepley 
5277b0441da4SMatthew G. Knepley /*@
5278b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5279b0441da4SMatthew G. Knepley 
528020f4b53cSBarry Smith   Not Collective
5281b0441da4SMatthew G. Knepley 
5282b0441da4SMatthew G. Knepley   Input Parameters:
528320f4b53cSBarry Smith + dm         - The `DM` object
5284b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5285b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5286b0441da4SMatthew G. Knepley 
5287b0441da4SMatthew G. Knepley   Level: developer
5288b0441da4SMatthew G. Knepley 
528920f4b53cSBarry Smith   Notes:
529020f4b53cSBarry Smith .vb
529120f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
529220f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
529320f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
529420f4b53cSBarry Smith .ve
529520f4b53cSBarry Smith 
52961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5297b0441da4SMatthew G. Knepley @*/
5298d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5299d71ae5a4SJacob Faibussowitsch {
5300b0441da4SMatthew G. Knepley   PetscInt Nf;
5301b0441da4SMatthew G. Knepley 
5302b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5303b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53049566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5305b0441da4SMatthew G. Knepley   if (!Nf) {
53069566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5307b0441da4SMatthew G. Knepley   } else {
53089566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5309e5e52638SMatthew G. Knepley   }
53103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5311e5e52638SMatthew G. Knepley }
5312e5e52638SMatthew G. Knepley 
5313d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5314d71ae5a4SJacob Faibussowitsch {
5315799db056SMatthew G. Knepley   DM           plex;
5316799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5317799db056SMatthew G. Knepley   const char **names;
5318799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5319799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5320799db056SMatthew G. Knepley   size_t       len;
5321799db056SMatthew G. Knepley   MPI_Comm     comm;
5322799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5323783e2ec8SMatthew G. Knepley 
5324783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5325799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5326799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5327799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5328799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5329799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5330799db056SMatthew G. Knepley     PetscDS  dsBC;
5331799db056SMatthew G. Knepley     PetscInt numBd;
5332799db056SMatthew G. Knepley 
533307218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5334799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5335799db056SMatthew G. Knepley     maxLabels += numBd;
5336799db056SMatthew G. Knepley   }
5337799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5338799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5339799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5340799db056SMatthew G. Knepley     PetscDS  dsBC;
5341799db056SMatthew G. Knepley     PetscInt numBd, bd;
5342799db056SMatthew G. Knepley 
534307218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5344799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5345799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5346799db056SMatthew G. Knepley       DMLabel      label;
5347799db056SMatthew G. Knepley       PetscInt     field;
5348799db056SMatthew G. Knepley       PetscObject  obj;
5349799db056SMatthew G. Knepley       PetscClassId id;
5350799db056SMatthew G. Knepley 
5351799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
53529566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
53539566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5354799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
53559371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
53569371c9d4SSatish Balay         if (labels[l] == label) break;
5357799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5358783e2ec8SMatthew G. Knepley     }
5359799db056SMatthew G. Knepley   }
5360799db056SMatthew G. Knepley   /* Get label names */
5361799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5362799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
53639371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
53649371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
53659371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
53669371c9d4SSatish Balay   }
5367799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5368712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5369799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5370c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5371799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5372799db056SMatthew G. Knepley   /* Put all names on all processes */
5373799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5374799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5375799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5376799db056SMatthew G. Knepley   gNl = displs[size];
53779371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
53789371c9d4SSatish Balay     counts[p] *= gmaxLen;
53799371c9d4SSatish Balay     displs[p] *= gmaxLen;
53809371c9d4SSatish Balay   }
5381799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5382799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5383799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5384799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5385799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5386799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5387799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
53889371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
53899371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
53909566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5391799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
53929566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5393799db056SMatthew G. Knepley     ++gl;
5394783e2ec8SMatthew G. Knepley   }
5395799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
53963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5397783e2ec8SMatthew G. Knepley }
5398783e2ec8SMatthew G. Knepley 
5399d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5400d71ae5a4SJacob Faibussowitsch {
5401e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5402e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5403e5e52638SMatthew G. Knepley 
5404e5e52638SMatthew G. Knepley   PetscFunctionBegin;
54053ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
54069566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5407e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
54089371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
54099371c9d4SSatish Balay     tmpd[s].ds     = NULL;
54109371c9d4SSatish Balay     tmpd[s].label  = NULL;
54119371c9d4SSatish Balay     tmpd[s].fields = NULL;
54129371c9d4SSatish Balay   }
54139566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5414e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5415e5e52638SMatthew G. Knepley   dm->probs = tmpd;
54163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5417e5e52638SMatthew G. Knepley }
5418e5e52638SMatthew G. Knepley 
5419e5e52638SMatthew G. Knepley /*@
542020f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5421e5e52638SMatthew G. Knepley 
542220f4b53cSBarry Smith   Not Collective
5423e5e52638SMatthew G. Knepley 
5424e5e52638SMatthew G. Knepley   Input Parameter:
542520f4b53cSBarry Smith . dm - The `DM`
5426e5e52638SMatthew G. Knepley 
5427e5e52638SMatthew G. Knepley   Output Parameter:
542820f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5429e5e52638SMatthew G. Knepley 
5430e5e52638SMatthew G. Knepley   Level: intermediate
5431e5e52638SMatthew G. Knepley 
54321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5433e5e52638SMatthew G. Knepley @*/
5434d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5435d71ae5a4SJacob Faibussowitsch {
5436e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5437e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54384f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5439e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
54403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5441e5e52638SMatthew G. Knepley }
5442e5e52638SMatthew G. Knepley 
5443e5e52638SMatthew G. Knepley /*@
544420f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5445e5e52638SMatthew G. Knepley 
544620f4b53cSBarry Smith   Logically Collective
5447e5e52638SMatthew G. Knepley 
5448e5e52638SMatthew G. Knepley   Input Parameter:
544920f4b53cSBarry Smith . dm - The `DM`
5450e5e52638SMatthew G. Knepley 
5451e5e52638SMatthew G. Knepley   Level: intermediate
5452e5e52638SMatthew G. Knepley 
54531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5454e5e52638SMatthew G. Knepley @*/
5455d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5456d71ae5a4SJacob Faibussowitsch {
5457e5e52638SMatthew G. Knepley   PetscInt s;
5458e5e52638SMatthew G. Knepley 
5459e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5460e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5461e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
54629566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
546307218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
54649566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
54659566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5466e5e52638SMatthew G. Knepley   }
54679566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5468e5e52638SMatthew G. Knepley   dm->probs = NULL;
5469e5e52638SMatthew G. Knepley   dm->Nds   = 0;
54703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5471e5e52638SMatthew G. Knepley }
5472e5e52638SMatthew G. Knepley 
5473e5e52638SMatthew G. Knepley /*@
547420f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5475e5e52638SMatthew G. Knepley 
547620f4b53cSBarry Smith   Not Collective
5477e5e52638SMatthew G. Knepley 
5478e5e52638SMatthew G. Knepley   Input Parameter:
547920f4b53cSBarry Smith . dm - The `DM`
5480e5e52638SMatthew G. Knepley 
5481e5e52638SMatthew G. Knepley   Output Parameter:
548207218a29SMatthew G. Knepley . ds - The default `PetscDS`
5483e5e52638SMatthew G. Knepley 
5484e5e52638SMatthew G. Knepley   Level: intermediate
5485e5e52638SMatthew G. Knepley 
54861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5487e5e52638SMatthew G. Knepley @*/
548807218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5489d71ae5a4SJacob Faibussowitsch {
5490e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5491e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54924f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
549307218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
549407218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
54953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5496e5e52638SMatthew G. Knepley }
5497e5e52638SMatthew G. Knepley 
5498e5e52638SMatthew G. Knepley /*@
549920f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5500e5e52638SMatthew G. Knepley 
550120f4b53cSBarry Smith   Not Collective
5502e5e52638SMatthew G. Knepley 
5503e5e52638SMatthew G. Knepley   Input Parameters:
550420f4b53cSBarry Smith + dm    - The `DM`
550520f4b53cSBarry Smith - point - Cell for the `PetscDS`
5506e5e52638SMatthew G. Knepley 
550707218a29SMatthew G. Knepley   Output Parameters:
550807218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
550907218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5510e5e52638SMatthew G. Knepley 
5511e5e52638SMatthew G. Knepley   Level: developer
5512e5e52638SMatthew G. Knepley 
55131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5514e5e52638SMatthew G. Knepley @*/
551507218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5516d71ae5a4SJacob Faibussowitsch {
551707218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5518e5e52638SMatthew G. Knepley   PetscInt s;
5519e5e52638SMatthew G. Knepley 
5520e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5521e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55224f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
55234f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
552463a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
552507218a29SMatthew G. Knepley   if (ds) *ds = NULL;
552607218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5527e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5528e5e52638SMatthew G. Knepley     PetscInt val;
5529e5e52638SMatthew G. Knepley 
55309371c9d4SSatish Balay     if (!dm->probs[s].label) {
553107218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
55329371c9d4SSatish Balay     } else {
55339566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
55349371c9d4SSatish Balay       if (val >= 0) {
553507218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
553607218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
55379371c9d4SSatish Balay         break;
55389371c9d4SSatish Balay       }
5539e5e52638SMatthew G. Knepley     }
5540e5e52638SMatthew G. Knepley   }
554107218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
55423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5543e5e52638SMatthew G. Knepley }
5544e5e52638SMatthew G. Knepley 
5545e5e52638SMatthew G. Knepley /*@
554620f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5547e5e52638SMatthew G. Knepley 
554820f4b53cSBarry Smith   Not Collective
5549e5e52638SMatthew G. Knepley 
5550e5e52638SMatthew G. Knepley   Input Parameters:
555120f4b53cSBarry Smith + dm    - The `DM`
555220f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5553e5e52638SMatthew G. Knepley 
5554b3cf3223SMatthew G. Knepley   Output Parameters:
555520f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
555607218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
555707218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5558e5e52638SMatthew G. Knepley 
5559e5e52638SMatthew G. Knepley   Level: advanced
5560e5e52638SMatthew G. Knepley 
556120f4b53cSBarry Smith   Note:
556220f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
556320f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
556407218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
556520f4b53cSBarry Smith 
55661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5567e5e52638SMatthew G. Knepley @*/
556807218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5569d71ae5a4SJacob Faibussowitsch {
5570e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5571e5e52638SMatthew G. Knepley 
5572e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5573e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5574e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
55759371c9d4SSatish Balay   if (fields) {
55764f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
55779371c9d4SSatish Balay     *fields = NULL;
55789371c9d4SSatish Balay   }
55799371c9d4SSatish Balay   if (ds) {
55804f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
55819371c9d4SSatish Balay     *ds = NULL;
55829371c9d4SSatish Balay   }
558307218a29SMatthew G. Knepley   if (dsIn) {
55844f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
558507218a29SMatthew G. Knepley     *dsIn = NULL;
558607218a29SMatthew G. Knepley   }
5587e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5588154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5589b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5590b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
559107218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
55923ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5593b3cf3223SMatthew G. Knepley     }
5594e5e52638SMatthew G. Knepley   }
55953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5596e5e52638SMatthew G. Knepley }
5597e5e52638SMatthew G. Knepley 
5598e5e52638SMatthew G. Knepley /*@
5599bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5600083401c6SMatthew G. Knepley 
560120f4b53cSBarry Smith   Collective
5602083401c6SMatthew G. Knepley 
5603083401c6SMatthew G. Knepley   Input Parameters:
5604bb7acecfSBarry Smith + dm     - The `DM`
560520f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
560607218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
560707218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
560807218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5609083401c6SMatthew G. Knepley 
561020f4b53cSBarry Smith   Level: advanced
561120f4b53cSBarry Smith 
5612bb7acecfSBarry Smith   Note:
5613bb7acecfSBarry 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,
5614083401c6SMatthew G. Knepley   the fields argument is ignored.
5615083401c6SMatthew G. Knepley 
56161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5617083401c6SMatthew G. Knepley @*/
561807218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5619d71ae5a4SJacob Faibussowitsch {
5620083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5621083401c6SMatthew G. Knepley 
5622083401c6SMatthew G. Knepley   PetscFunctionBegin;
5623083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5624083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
562507218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5626064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
562707218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5628083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5629083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
56309566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
563107218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5632083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
563307218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
56343ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5635083401c6SMatthew G. Knepley     }
5636083401c6SMatthew G. Knepley   }
56379566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
56389566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56399566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
56409566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
564107218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5642083401c6SMatthew G. Knepley   if (!label) {
5643083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5644083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5645083401c6SMatthew G. Knepley     Nds = 0;
5646083401c6SMatthew G. Knepley   }
5647083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5648083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5649083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
565007218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
56513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5652083401c6SMatthew G. Knepley }
5653083401c6SMatthew G. Knepley 
5654083401c6SMatthew G. Knepley /*@
565520f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5656e5e52638SMatthew G. Knepley 
565720f4b53cSBarry Smith   Not Collective
5658e5e52638SMatthew G. Knepley 
5659e5e52638SMatthew G. Knepley   Input Parameters:
566020f4b53cSBarry Smith + dm  - The `DM`
5661e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5662e5e52638SMatthew G. Knepley 
5663e5e52638SMatthew G. Knepley   Output Parameters:
566420f4b53cSBarry Smith + label  - The region label, or `NULL`
566520f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
566607218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
566707218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5668e5e52638SMatthew G. Knepley 
5669e5e52638SMatthew G. Knepley   Level: advanced
5670e5e52638SMatthew G. Knepley 
56711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5672e5e52638SMatthew G. Knepley @*/
567307218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5674d71ae5a4SJacob Faibussowitsch {
5675e5e52638SMatthew G. Knepley   PetscInt Nds;
5676e5e52638SMatthew G. Knepley 
5677e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5678e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56799566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
568063a3b9bcSJacob 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);
5681e5e52638SMatthew G. Knepley   if (label) {
56824f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5683e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5684e5e52638SMatthew G. Knepley   }
5685b3cf3223SMatthew G. Knepley   if (fields) {
56864f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5687b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5688b3cf3223SMatthew G. Knepley   }
5689e5e52638SMatthew G. Knepley   if (ds) {
56904f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5691e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5692e5e52638SMatthew G. Knepley   }
569307218a29SMatthew G. Knepley   if (dsIn) {
56944f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
569507218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
569607218a29SMatthew G. Knepley   }
56973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5698e5e52638SMatthew G. Knepley }
5699e5e52638SMatthew G. Knepley 
5700e5e52638SMatthew G. Knepley /*@
570120f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5702e5e52638SMatthew G. Knepley 
570320f4b53cSBarry Smith   Not Collective
5704e5e52638SMatthew G. Knepley 
5705e5e52638SMatthew G. Knepley   Input Parameters:
570620f4b53cSBarry Smith + dm     - The `DM`
5707083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
570820f4b53cSBarry Smith . label  - The region label, or `NULL`
570907218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
571007218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
571107218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5712e5e52638SMatthew G. Knepley 
5713e5e52638SMatthew G. Knepley   Level: advanced
5714e5e52638SMatthew G. Knepley 
57151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5716e5e52638SMatthew G. Knepley @*/
571707218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5718d71ae5a4SJacob Faibussowitsch {
5719083401c6SMatthew G. Knepley   PetscInt Nds;
5720e5e52638SMatthew G. Knepley 
5721e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5722e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5723ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
57249566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
572563a3b9bcSJacob 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);
57269566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57279566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5728083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5729083401c6SMatthew G. Knepley   if (fields) {
5730083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
57319566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
57329566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5733083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5734e5e52638SMatthew G. Knepley   }
5735083401c6SMatthew G. Knepley   if (ds) {
5736083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
57379566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
57389566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5739083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5740083401c6SMatthew G. Knepley   }
574107218a29SMatthew G. Knepley   if (dsIn) {
574207218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
574307218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
574407218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
574507218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
574607218a29SMatthew G. Knepley   }
57473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5748e5e52638SMatthew G. Knepley }
5749e5e52638SMatthew G. Knepley 
5750e5e52638SMatthew G. Knepley /*@
575120f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
57521d3af9e0SMatthew G. Knepley 
575320f4b53cSBarry Smith   Not Collective
57541d3af9e0SMatthew G. Knepley 
57551d3af9e0SMatthew G. Knepley   Input Parameters:
575620f4b53cSBarry Smith + dm - The `DM`
575720f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
57581d3af9e0SMatthew G. Knepley 
57591d3af9e0SMatthew G. Knepley   Output Parameter:
57601d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
57611d3af9e0SMatthew G. Knepley 
57621d3af9e0SMatthew G. Knepley   Level: advanced
57631d3af9e0SMatthew G. Knepley 
57641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
57651d3af9e0SMatthew G. Knepley @*/
5766d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5767d71ae5a4SJacob Faibussowitsch {
57681d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
57691d3af9e0SMatthew G. Knepley 
57701d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
57711d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57721d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
57734f572ea9SToby Isaac   PetscAssertPointer(num, 3);
57749566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
57759371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
57769371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
57771d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
57781d3af9e0SMatthew G. Knepley   else *num = n;
57793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57801d3af9e0SMatthew G. Knepley }
57811d3af9e0SMatthew G. Knepley 
57822df84da0SMatthew G. Knepley /*@C
5783bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
57842df84da0SMatthew G. Knepley 
578520f4b53cSBarry Smith   Not Collective
57862df84da0SMatthew G. Knepley 
5787f1a722f8SMatthew G. Knepley   Input Parameters:
5788bb7acecfSBarry Smith + dm     - The `DM`
57892df84da0SMatthew G. Knepley . Nc     - The number of components for the field
579020f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5791bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
57922df84da0SMatthew G. Knepley 
57932df84da0SMatthew G. Knepley   Output Parameter:
5794bb7acecfSBarry Smith . fem - The `PetscFE`
57952df84da0SMatthew G. Knepley 
579620f4b53cSBarry Smith   Level: intermediate
579720f4b53cSBarry Smith 
5798bb7acecfSBarry Smith   Note:
5799bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
58002df84da0SMatthew G. Knepley 
58011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
58022df84da0SMatthew G. Knepley @*/
5803d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5804d71ae5a4SJacob Faibussowitsch {
58052df84da0SMatthew G. Knepley   DMPolytopeType ct;
58062df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
58072df84da0SMatthew G. Knepley 
58082df84da0SMatthew G. Knepley   PetscFunctionBegin;
58092df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58102df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
58114f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
58122df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
58134f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
58149566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
58159566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
58169566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
58179566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
58183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58192df84da0SMatthew G. Knepley }
58202df84da0SMatthew G. Knepley 
58211d3af9e0SMatthew G. Knepley /*@
5822bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5823e5e52638SMatthew G. Knepley 
582420f4b53cSBarry Smith   Collective
5825e5e52638SMatthew G. Knepley 
5826e5e52638SMatthew G. Knepley   Input Parameter:
5827bb7acecfSBarry Smith . dm - The `DM`
5828e5e52638SMatthew G. Knepley 
582920f4b53cSBarry Smith   Options Database Key:
5830bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
583145480ffeSMatthew G. Knepley 
583220f4b53cSBarry Smith   Level: intermediate
583320f4b53cSBarry Smith 
58341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5835e5e52638SMatthew G. Knepley @*/
5836d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5837d71ae5a4SJacob Faibussowitsch {
5838e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5839083401c6SMatthew G. Knepley   PetscDS   dsDef;
5840083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5841f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5842f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5843e5e52638SMatthew G. Knepley 
5844e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5845e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58463ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
58479566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
58489566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5849083401c6SMatthew G. Knepley   /* Determine how many regions we have */
58509566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5851083401c6SMatthew G. Knepley   Nl   = 0;
5852083401c6SMatthew G. Knepley   Ndef = 0;
5853083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5854083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5855083401c6SMatthew G. Knepley     PetscInt l;
5856083401c6SMatthew G. Knepley 
5857f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5858f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5859f918ec44SMatthew G. Knepley     {
5860f918ec44SMatthew G. Knepley       PetscClassId id;
5861f918ec44SMatthew G. Knepley 
58629566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5863f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5864f918ec44SMatthew G. Knepley         Ceed ceed;
5865f918ec44SMatthew G. Knepley 
58669566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
58679566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5868f918ec44SMatthew G. Knepley       }
5869f918ec44SMatthew G. Knepley     }
5870f918ec44SMatthew G. Knepley #endif
58719371c9d4SSatish Balay     if (!label) {
58729371c9d4SSatish Balay       ++Ndef;
58739371c9d4SSatish Balay       continue;
58749371c9d4SSatish Balay     }
58759371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
58769371c9d4SSatish Balay       if (label == labelSet[l]) break;
5877083401c6SMatthew G. Knepley     if (l < Nl) continue;
5878083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5879083401c6SMatthew G. Knepley   }
5880083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
588107218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5882083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5883b3cf3223SMatthew G. Knepley     IS        fields;
5884b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5885b3cf3223SMatthew G. Knepley 
58869371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58879371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
58887a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
58899566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
58909371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58919371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
58929566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58939566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58949566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58959566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
589688f0c812SMatthew G. Knepley 
58979566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
589807218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
58999566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59009566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
59012df9ee95SMatthew G. Knepley   }
590207218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
59039566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5904083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5905083401c6SMatthew G. Knepley   if (Ndef && Nl) {
59060122748bSMatthew G. Knepley     DM              plex;
5907083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5908083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5909083401c6SMatthew G. Knepley     PetscInt       *fields;
5910083401c6SMatthew G. Knepley     const PetscInt *cells;
5911083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
59120122748bSMatthew G. Knepley 
59139566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
59149566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
59159566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
59169566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
59175fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5918083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5919083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5920083401c6SMatthew G. Knepley       IS      pointIS;
5921083401c6SMatthew G. Knepley 
59229566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
59239566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
59249566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
59259566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5926083401c6SMatthew G. Knepley     }
59279566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5928083401c6SMatthew G. Knepley 
59299566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
59309566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
59319566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
59329566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
59339566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
59349566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
59359566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5936083401c6SMatthew G. Knepley 
59379566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
59389371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
59399371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
59409566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
59419566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
59429566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
59439566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5944083401c6SMatthew G. Knepley 
59459566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
594607218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
59479566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
59489566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
59499566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59509566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
59519566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5952083401c6SMatthew G. Knepley   }
5953083401c6SMatthew G. Knepley   /* Create label DSes
5954083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5955083401c6SMatthew G. Knepley   */
5956083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5957083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5958083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
595907218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
5960083401c6SMatthew G. Knepley     IS        fields;
5961083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5962083401c6SMatthew G. Knepley 
59639566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
59649371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59659371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
59669566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59679371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59689371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
59699566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59709566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59719566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59729566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
59739566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5974083401c6SMatthew G. Knepley     {
5975083401c6SMatthew G. Knepley       DMPolytopeType ct;
5976083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
59775fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
59780122748bSMatthew G. Knepley 
59799566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5980665f567fSMatthew G. Knepley       if (lStart >= 0) {
59819566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5982412e9a14SMatthew G. Knepley         switch (ct) {
5983412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5984412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5985412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
5986d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5987d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
5988d71ae5a4SJacob Faibussowitsch           break;
5989d71ae5a4SJacob Faibussowitsch         default:
5990d71ae5a4SJacob Faibussowitsch           break;
5991412e9a14SMatthew G. Knepley         }
5992665f567fSMatthew G. Knepley       }
5993712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
599407218a29SMatthew G. Knepley       if (isCohesive) {
599507218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
599607218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
599707218a29SMatthew G. Knepley       }
59985fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
59995fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
60005fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
60019566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
60029566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
600307218a29SMatthew G. Knepley             if (dsIn) {
600407218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
600507218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
600607218a29SMatthew G. Knepley             }
60075fedec97SMatthew G. Knepley           }
60085fedec97SMatthew G. Knepley           ++nf;
60095fedec97SMatthew G. Knepley         }
60105fedec97SMatthew G. Knepley       }
6011e5e52638SMatthew G. Knepley     }
601207218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
601307218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
60149566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
601507218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
6016e5e52638SMatthew G. Knepley   }
60179566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
6018e5e52638SMatthew G. Knepley   /* Set fields in DSes */
6019083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
6020083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
602107218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
6022083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
6023083401c6SMatthew G. Knepley     const PetscInt *fld;
60245fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
60255fedec97SMatthew G. Knepley     PetscBool       isCohesive;
6026e5e52638SMatthew G. Knepley 
60279566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
60289566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
60299566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
60309566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
6031083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
6032083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
60335fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
6034e5e52638SMatthew G. Knepley       PetscClassId id;
6035e5e52638SMatthew G. Knepley 
60365fedec97SMatthew G. Knepley       /* Handle DS with no fields */
60379566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
60385fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
603907218a29SMatthew G. Knepley       if (isCohesive) {
604007218a29SMatthew G. Knepley         if (!isCohesiveField) {
604107218a29SMatthew G. Knepley           PetscObject bdDisc;
604207218a29SMatthew G. Knepley 
604307218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
604407218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
604507218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
604607218a29SMatthew G. Knepley         } else {
60479566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
604807218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
604907218a29SMatthew G. Knepley         }
605007218a29SMatthew G. Knepley       } else {
605107218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
605207218a29SMatthew G. Knepley       }
6053083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
60549566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
6055e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
6056e5e52638SMatthew G. Knepley     }
60579566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6058e5e52638SMatthew G. Knepley   }
6059f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
60609566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6061f9244615SMatthew G. Knepley   if (flg) {
60623b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
60633b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
606407218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
60653b4aee56SMatthew G. Knepley       PetscInt Nf, f;
60663b4aee56SMatthew G. Knepley 
60679566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
606807218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
606907218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
607007218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
607107218a29SMatthew G. Knepley       }
60723b4aee56SMatthew G. Knepley     }
6073f9244615SMatthew G. Knepley   }
6074e5e52638SMatthew G. Knepley   /* Setup DSes */
6075e5e52638SMatthew G. Knepley   if (doSetup) {
607607218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
6077cf1363e4SMatthew G. Knepley       if (dm->setfromoptionscalled) {
6078cf1363e4SMatthew G. Knepley         PetscCall(PetscDSSetFromOptions(dm->probs[s].ds));
6079cf1363e4SMatthew G. Knepley         if (dm->probs[s].dsIn) PetscCall(PetscDSSetFromOptions(dm->probs[s].dsIn));
6080cf1363e4SMatthew G. Knepley       }
608107218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
608207218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
608307218a29SMatthew G. Knepley     }
6084e5e52638SMatthew G. Knepley   }
60853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6086e5e52638SMatthew G. Knepley }
6087e5e52638SMatthew G. Knepley 
6088e5e52638SMatthew G. Knepley /*@
6089d2b2dc1eSMatthew G. Knepley   DMUseTensorOrder - Use a tensor product closure ordering for the default section
6090d2b2dc1eSMatthew G. Knepley 
6091d2b2dc1eSMatthew G. Knepley   Input Parameters:
6092d2b2dc1eSMatthew G. Knepley + dm     - The DM
6093d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order
6094d2b2dc1eSMatthew G. Knepley 
6095d2b2dc1eSMatthew G. Knepley   Level: developer
6096d2b2dc1eSMatthew G. Knepley 
6097d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()`
6098d2b2dc1eSMatthew G. Knepley @*/
6099d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor)
6100d2b2dc1eSMatthew G. Knepley {
6101d2b2dc1eSMatthew G. Knepley   PetscInt  Nf;
6102d2b2dc1eSMatthew G. Knepley   PetscBool reorder = PETSC_TRUE, isPlex;
6103d2b2dc1eSMatthew G. Knepley 
6104d2b2dc1eSMatthew G. Knepley   PetscFunctionBegin;
6105d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex));
6106d2b2dc1eSMatthew G. Knepley   PetscCall(DMGetNumFields(dm, &Nf));
6107d2b2dc1eSMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
6108d2b2dc1eSMatthew G. Knepley     PetscObject  obj;
6109d2b2dc1eSMatthew G. Knepley     PetscClassId id;
6110d2b2dc1eSMatthew G. Knepley 
6111d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetField(dm, f, NULL, &obj));
6112d2b2dc1eSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
6113d2b2dc1eSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
6114d2b2dc1eSMatthew G. Knepley       PetscSpace sp;
6115d2b2dc1eSMatthew G. Knepley       PetscBool  tensor;
6116d2b2dc1eSMatthew G. Knepley 
6117d2b2dc1eSMatthew G. Knepley       PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp));
6118d2b2dc1eSMatthew G. Knepley       PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor));
6119d2b2dc1eSMatthew G. Knepley       reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE;
6120d2b2dc1eSMatthew G. Knepley     } else reorder = PETSC_FALSE;
6121d2b2dc1eSMatthew G. Knepley   }
6122d2b2dc1eSMatthew G. Knepley   if (tensor) {
6123d2b2dc1eSMatthew G. Knepley     if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
6124d2b2dc1eSMatthew G. Knepley   } else {
6125d2b2dc1eSMatthew G. Knepley     PetscSection s;
6126d2b2dc1eSMatthew G. Knepley 
6127d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetLocalSection(dm, &s));
6128d2b2dc1eSMatthew G. Knepley     if (s) PetscCall(PetscSectionResetClosurePermutation(s));
6129d2b2dc1eSMatthew G. Knepley   }
6130d2b2dc1eSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6131d2b2dc1eSMatthew G. Knepley }
6132d2b2dc1eSMatthew G. Knepley 
6133d2b2dc1eSMatthew G. Knepley /*@
6134bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
61357f96f943SMatthew G. Knepley 
613620f4b53cSBarry Smith   Collective
6137f2cacb80SMatthew G. Knepley 
61387f96f943SMatthew G. Knepley   Input Parameters:
6139bb7acecfSBarry Smith + dm   - The `DM`
61407f96f943SMatthew G. Knepley - time - The time
61417f96f943SMatthew G. Knepley 
61427f96f943SMatthew G. Knepley   Output Parameters:
614320f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
614420f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
614520f4b53cSBarry Smith 
614620f4b53cSBarry Smith   Level: developer
61477f96f943SMatthew G. Knepley 
6148bb7acecfSBarry Smith   Note:
6149bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
61507f96f943SMatthew G. Knepley 
61511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
61527f96f943SMatthew G. Knepley @*/
6153d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6154d71ae5a4SJacob Faibussowitsch {
61557f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
61567f96f943SMatthew G. Knepley   void   **ectxs;
6157f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
61587f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
61597f96f943SMatthew G. Knepley 
61607f96f943SMatthew G. Knepley   PetscFunctionBegin;
6161f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6162f60fa741SMatthew G. Knepley   if (u) {
6163f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6164f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6165f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6166f60fa741SMatthew G. Knepley   }
6167f60fa741SMatthew G. Knepley   if (u_t) {
6168f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6169f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6170f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6171f60fa741SMatthew G. Knepley   }
61729566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
61739566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
61749566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
61757f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
61767f96f943SMatthew G. Knepley     PetscDS         ds;
61777f96f943SMatthew G. Knepley     DMLabel         label;
61787f96f943SMatthew G. Knepley     IS              fieldIS;
61797f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
61807f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
61817f96f943SMatthew G. Knepley 
618207218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
61839566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
61849566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
61859566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
61869566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6187f2cacb80SMatthew G. Knepley     if (u) {
6188f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6189f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6190f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
61917f96f943SMatthew G. Knepley     }
6192f2cacb80SMatthew G. Knepley     if (u_t) {
61939566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
61949566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6195f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6196f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6197f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6198f2cacb80SMatthew G. Knepley     }
61999566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6200f2cacb80SMatthew G. Knepley   }
6201f2cacb80SMatthew G. Knepley   if (u) {
62029566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
62039566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6204f2cacb80SMatthew G. Knepley   }
6205f2cacb80SMatthew G. Knepley   if (u_t) {
62069566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
62079566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6208f2cacb80SMatthew G. Knepley   }
62099566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6210f60fa741SMatthew G. Knepley   if (u) {
6211f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6212f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6213f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6214f60fa741SMatthew G. Knepley   }
6215f60fa741SMatthew G. Knepley   if (u_t) {
6216f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6217f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6218f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6219f60fa741SMatthew G. Knepley   }
62203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
62217f96f943SMatthew G. Knepley }
62227f96f943SMatthew G. Knepley 
622307218a29SMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
6224d71ae5a4SJacob Faibussowitsch {
622507218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
622645480ffeSMatthew G. Knepley 
622745480ffeSMatthew G. Knepley   PetscFunctionBegin;
62289566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
622907218a29SMatthew G. Knepley   PetscCall(PetscDSCopy(ds, dm, dsNew));
623007218a29SMatthew G. Knepley   if (dsIn) {
623107218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
623207218a29SMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, dm, dsInNew));
623345480ffeSMatthew G. Knepley   }
623407218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
62359566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
623607218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
62373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
623845480ffeSMatthew G. Knepley }
623945480ffeSMatthew G. Knepley 
62407f96f943SMatthew G. Knepley /*@
6241bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6242e5e52638SMatthew G. Knepley 
624320f4b53cSBarry Smith   Collective
6244e5e52638SMatthew G. Knepley 
6245e5e52638SMatthew G. Knepley   Input Parameter:
6246bb7acecfSBarry Smith . dm - The `DM`
6247e5e52638SMatthew G. Knepley 
6248e5e52638SMatthew G. Knepley   Output Parameter:
6249bb7acecfSBarry Smith . newdm - The `DM`
6250e5e52638SMatthew G. Knepley 
6251e5e52638SMatthew G. Knepley   Level: advanced
6252e5e52638SMatthew G. Knepley 
62531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6254e5e52638SMatthew G. Knepley @*/
6255d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6256d71ae5a4SJacob Faibussowitsch {
6257e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6258e5e52638SMatthew G. Knepley 
6259e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62603ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
62619566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62629566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6263e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6264e5e52638SMatthew G. Knepley     DMLabel  label;
6265b3cf3223SMatthew G. Knepley     IS       fields;
626607218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6267783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6268e5e52638SMatthew G. Knepley 
626907218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6270b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
627107218a29SMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds, dsIn));
6272d5b43468SJose E. Roman     /* Complete new labels in the new DS */
627307218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
62749566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6275783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6276b8025e53SMatthew G. Knepley       PetscWeakForm wf;
627745480ffeSMatthew G. Knepley       DMLabel       label;
6278783e2ec8SMatthew G. Knepley       PetscInt      field;
6279783e2ec8SMatthew G. Knepley 
62809566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
62819566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6282783e2ec8SMatthew G. Knepley     }
6283e5e52638SMatthew G. Knepley   }
6284799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
62853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6286e5e52638SMatthew G. Knepley }
6287e5e52638SMatthew G. Knepley 
6288e5e52638SMatthew G. Knepley /*@
6289bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6290e5e52638SMatthew G. Knepley 
629120f4b53cSBarry Smith   Collective
6292e5e52638SMatthew G. Knepley 
6293e5e52638SMatthew G. Knepley   Input Parameter:
6294bb7acecfSBarry Smith . dm - The `DM`
6295e5e52638SMatthew G. Knepley 
6296e5e52638SMatthew G. Knepley   Output Parameter:
6297bb7acecfSBarry Smith . newdm - The `DM`
6298e5e52638SMatthew G. Knepley 
6299e5e52638SMatthew G. Knepley   Level: advanced
6300e5e52638SMatthew G. Knepley 
630160225df5SJacob Faibussowitsch   Developer Notes:
6302bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6303bb7acecfSBarry Smith 
63041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6305e5e52638SMatthew G. Knepley @*/
6306d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6307d71ae5a4SJacob Faibussowitsch {
6308e5e52638SMatthew G. Knepley   PetscFunctionBegin;
63099566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
63109566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
63113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6312e5e52638SMatthew G. Knepley }
6313e5e52638SMatthew G. Knepley 
6314c73cfb54SMatthew G. Knepley /*@
6315bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6316c73cfb54SMatthew G. Knepley 
631720f4b53cSBarry Smith   Not Collective
6318c73cfb54SMatthew G. Knepley 
6319c73cfb54SMatthew G. Knepley   Input Parameter:
6320bb7acecfSBarry Smith . dm - The `DM`
6321c73cfb54SMatthew G. Knepley 
6322c73cfb54SMatthew G. Knepley   Output Parameter:
6323c73cfb54SMatthew G. Knepley . dim - The topological dimension
6324c73cfb54SMatthew G. Knepley 
6325c73cfb54SMatthew G. Knepley   Level: beginner
6326c73cfb54SMatthew G. Knepley 
63271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6328c73cfb54SMatthew G. Knepley @*/
6329d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6330d71ae5a4SJacob Faibussowitsch {
6331c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6332c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63334f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6334c73cfb54SMatthew G. Knepley   *dim = dm->dim;
63353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6336c73cfb54SMatthew G. Knepley }
6337c73cfb54SMatthew G. Knepley 
6338c73cfb54SMatthew G. Knepley /*@
6339bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6340c73cfb54SMatthew G. Knepley 
634120f4b53cSBarry Smith   Collective
6342c73cfb54SMatthew G. Knepley 
6343c73cfb54SMatthew G. Knepley   Input Parameters:
6344bb7acecfSBarry Smith + dm  - The `DM`
6345c73cfb54SMatthew G. Knepley - dim - The topological dimension
6346c73cfb54SMatthew G. Knepley 
6347c73cfb54SMatthew G. Knepley   Level: beginner
6348c73cfb54SMatthew G. Knepley 
63491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6350c73cfb54SMatthew G. Knepley @*/
6351d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6352d71ae5a4SJacob Faibussowitsch {
6353e5e52638SMatthew G. Knepley   PetscDS  ds;
635445480ffeSMatthew G. Knepley   PetscInt Nds, n;
6355f17e8794SMatthew G. Knepley 
6356c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6357c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6358c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6359c73cfb54SMatthew G. Knepley   dm->dim = dim;
6360d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
63619566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
636245480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
636307218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
63649566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
636545480ffeSMatthew G. Knepley     }
6366d17bd122SMatthew G. Knepley   }
63673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6368c73cfb54SMatthew G. Knepley }
6369c73cfb54SMatthew G. Knepley 
6370793f3fe5SMatthew G. Knepley /*@
6371793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6372793f3fe5SMatthew G. Knepley 
637320f4b53cSBarry Smith   Collective
6374793f3fe5SMatthew G. Knepley 
6375793f3fe5SMatthew G. Knepley   Input Parameters:
6376bb7acecfSBarry Smith + dm  - the `DM`
6377793f3fe5SMatthew G. Knepley - dim - the dimension
6378793f3fe5SMatthew G. Knepley 
6379793f3fe5SMatthew G. Knepley   Output Parameters:
6380793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6381aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6382793f3fe5SMatthew G. Knepley 
638320f4b53cSBarry Smith   Level: intermediate
638420f4b53cSBarry Smith 
6385793f3fe5SMatthew G. Knepley   Note:
6386793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6387a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6388793f3fe5SMatthew G. Knepley   then the interval is empty.
6389793f3fe5SMatthew G. Knepley 
63901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6391793f3fe5SMatthew G. Knepley @*/
6392d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6393d71ae5a4SJacob Faibussowitsch {
6394793f3fe5SMatthew G. Knepley   PetscInt d;
6395793f3fe5SMatthew G. Knepley 
6396793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6397793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63989566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
63997a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6400dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
64013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6402793f3fe5SMatthew G. Knepley }
6403793f3fe5SMatthew G. Knepley 
64046636e97aSMatthew G Knepley /*@
6405bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6406f4d763aaSMatthew G. Knepley 
640720f4b53cSBarry Smith   Collective
64088f700142SStefano Zampini 
6409f4d763aaSMatthew G. Knepley   Input Parameter:
6410bb7acecfSBarry Smith . dm - The original `DM`
6411f4d763aaSMatthew G. Knepley 
6412f4d763aaSMatthew G. Knepley   Output Parameter:
6413bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6414f4d763aaSMatthew G. Knepley 
6415f4d763aaSMatthew G. Knepley   Level: intermediate
6416f4d763aaSMatthew G. Knepley 
6417bb7acecfSBarry Smith   Note:
6418bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6419bb7acecfSBarry 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
6420bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6421bb7acecfSBarry Smith 
64221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6423f4d763aaSMatthew G. Knepley @*/
6424d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6425d71ae5a4SJacob Faibussowitsch {
6426c26acbdeSMatthew G. Knepley   PetscSection section;
6427eb9d3e4dSMatthew G. Knepley   IS           perm;
6428eb9d3e4dSMatthew G. Knepley   PetscBool    hasConstraints, newDM, gnewDM;
642914f150ffSMatthew G. Knepley 
643014f150ffSMatthew G. Knepley   PetscFunctionBegin;
643114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64324f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
64339566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
64349566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6435eb9d3e4dSMatthew G. Knepley   PetscCall(PetscSectionGetPermutation(section, &perm));
6436eb9d3e4dSMatthew G. Knepley   newDM = hasConstraints || perm ? PETSC_TRUE : PETSC_FALSE;
6437eb9d3e4dSMatthew G. Knepley   PetscCall(MPIU_Allreduce(&newDM, &gnewDM, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
6438eb9d3e4dSMatthew G. Knepley   if (!gnewDM) {
6439c26acbdeSMatthew G. Knepley     *odm = dm;
64403ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6441c26acbdeSMatthew G. Knepley   }
644214f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6443c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
644414f150ffSMatthew G. Knepley     PetscSF      sf;
6445eb9d3e4dSMatthew G. Knepley     PetscBool    usePerm = dm->ignorePermOutput ? PETSC_FALSE : PETSC_TRUE;
644614f150ffSMatthew G. Knepley 
64479566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
64489566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
64499566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
64509566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
64519566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
64529566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
6453eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(section, sf, usePerm, PETSC_TRUE, PETSC_FALSE, &gsection));
64549566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
64559566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
645614f150ffSMatthew G. Knepley   }
645714f150ffSMatthew G. Knepley   *odm = dm->dmBC;
64583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
645914f150ffSMatthew G. Knepley }
6460f4d763aaSMatthew G. Knepley 
6461f4d763aaSMatthew G. Knepley /*@
6462cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6463f4d763aaSMatthew G. Knepley 
6464f4d763aaSMatthew G. Knepley   Input Parameter:
6465bb7acecfSBarry Smith . dm - The original `DM`
6466f4d763aaSMatthew G. Knepley 
6467cdb7a50dSMatthew G. Knepley   Output Parameters:
6468cdb7a50dSMatthew G. Knepley + num - The output sequence number
6469cdb7a50dSMatthew G. Knepley - val - The output sequence value
6470f4d763aaSMatthew G. Knepley 
6471f4d763aaSMatthew G. Knepley   Level: intermediate
6472f4d763aaSMatthew G. Knepley 
6473bb7acecfSBarry Smith   Note:
6474bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6475bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6476bb7acecfSBarry Smith 
647760225df5SJacob Faibussowitsch   Developer Notes:
6478bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6479bb7acecfSBarry Smith   not directly related to the `DM`.
6480f4d763aaSMatthew G. Knepley 
64811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6482f4d763aaSMatthew G. Knepley @*/
6483d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6484d71ae5a4SJacob Faibussowitsch {
6485f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6486f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64879371c9d4SSatish Balay   if (num) {
64884f572ea9SToby Isaac     PetscAssertPointer(num, 2);
64899371c9d4SSatish Balay     *num = dm->outputSequenceNum;
64909371c9d4SSatish Balay   }
64919371c9d4SSatish Balay   if (val) {
64924f572ea9SToby Isaac     PetscAssertPointer(val, 3);
64939371c9d4SSatish Balay     *val = dm->outputSequenceVal;
64949371c9d4SSatish Balay   }
64953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6496f4d763aaSMatthew G. Knepley }
6497f4d763aaSMatthew G. Knepley 
6498f4d763aaSMatthew G. Knepley /*@
6499cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6500f4d763aaSMatthew G. Knepley 
6501f4d763aaSMatthew G. Knepley   Input Parameters:
6502bb7acecfSBarry Smith + dm  - The original `DM`
6503cdb7a50dSMatthew G. Knepley . num - The output sequence number
6504cdb7a50dSMatthew G. Knepley - val - The output sequence value
6505f4d763aaSMatthew G. Knepley 
6506f4d763aaSMatthew G. Knepley   Level: intermediate
6507f4d763aaSMatthew G. Knepley 
6508bb7acecfSBarry Smith   Note:
6509bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6510bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6511f4d763aaSMatthew G. Knepley 
65121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6513f4d763aaSMatthew G. Knepley @*/
6514d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6515d71ae5a4SJacob Faibussowitsch {
6516f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6517f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6518f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6519cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
65203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6521cdb7a50dSMatthew G. Knepley }
6522cdb7a50dSMatthew G. Knepley 
6523cdb7a50dSMatthew G. Knepley /*@C
6524bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6525cdb7a50dSMatthew G. Knepley 
6526cdb7a50dSMatthew G. Knepley   Input Parameters:
6527bb7acecfSBarry Smith + dm     - The original `DM`
6528a4e35b19SJacob Faibussowitsch . viewer - The viewer to get it from
6529cdb7a50dSMatthew G. Knepley . name   - The sequence name
6530cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6531cdb7a50dSMatthew G. Knepley 
6532cdb7a50dSMatthew G. Knepley   Output Parameter:
6533cdb7a50dSMatthew G. Knepley . val - The output sequence value
6534cdb7a50dSMatthew G. Knepley 
6535cdb7a50dSMatthew G. Knepley   Level: intermediate
6536cdb7a50dSMatthew G. Knepley 
6537bb7acecfSBarry Smith   Note:
6538bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6539bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6540bb7acecfSBarry Smith 
654160225df5SJacob Faibussowitsch   Developer Notes:
6542bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6543cdb7a50dSMatthew G. Knepley 
65441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6545cdb7a50dSMatthew G. Knepley @*/
6546d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6547d71ae5a4SJacob Faibussowitsch {
6548cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6549cdb7a50dSMatthew G. Knepley 
6550cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6551cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6552cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
65534f572ea9SToby Isaac   PetscAssertPointer(val, 5);
65549566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6555cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6556cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6557cdb7a50dSMatthew G. Knepley     PetscScalar value;
6558cdb7a50dSMatthew G. Knepley 
65599566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
65604aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6561cdb7a50dSMatthew G. Knepley #endif
6562cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
65633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6564f4d763aaSMatthew G. Knepley }
65658e4ac7eaSMatthew G. Knepley 
65668e4ac7eaSMatthew G. Knepley /*@
6567bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
65688e4ac7eaSMatthew G. Knepley 
656920f4b53cSBarry Smith   Not Collective
65708e4ac7eaSMatthew G. Knepley 
65718e4ac7eaSMatthew G. Knepley   Input Parameter:
6572bb7acecfSBarry Smith . dm - The `DM`
65738e4ac7eaSMatthew G. Knepley 
65748e4ac7eaSMatthew G. Knepley   Output Parameter:
6575bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
65768e4ac7eaSMatthew G. Knepley 
65778e4ac7eaSMatthew G. Knepley   Level: beginner
65788e4ac7eaSMatthew G. Knepley 
65791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
65808e4ac7eaSMatthew G. Knepley @*/
6581d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6582d71ae5a4SJacob Faibussowitsch {
65838e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
65848e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65854f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
65868e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
65873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65888e4ac7eaSMatthew G. Knepley }
65898e4ac7eaSMatthew G. Knepley 
65908e4ac7eaSMatthew G. Knepley /*@
6591bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
65928e4ac7eaSMatthew G. Knepley 
659320f4b53cSBarry Smith   Collective
65948e4ac7eaSMatthew G. Knepley 
65958e4ac7eaSMatthew G. Knepley   Input Parameters:
6596bb7acecfSBarry Smith + dm         - The `DM`
6597bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
65988e4ac7eaSMatthew G. Knepley 
6599bb7acecfSBarry Smith   Note:
6600bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
66015d3b26e6SMatthew G. Knepley 
66028e4ac7eaSMatthew G. Knepley   Level: beginner
66038e4ac7eaSMatthew G. Knepley 
66041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
66058e4ac7eaSMatthew G. Knepley @*/
6606d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6607d71ae5a4SJacob Faibussowitsch {
66088e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66098e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66108833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
66118e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
66123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66138e4ac7eaSMatthew G. Knepley }
6614c58f1c22SToby Isaac 
6615c58f1c22SToby Isaac /*@C
6616bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6617c58f1c22SToby Isaac 
6618c58f1c22SToby Isaac   Not Collective
6619c58f1c22SToby Isaac 
6620c58f1c22SToby Isaac   Input Parameters:
6621bb7acecfSBarry Smith + dm   - The `DM` object
6622c58f1c22SToby Isaac - name - The label name
6623c58f1c22SToby Isaac 
6624c58f1c22SToby Isaac   Level: intermediate
6625c58f1c22SToby Isaac 
66261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6627c58f1c22SToby Isaac @*/
6628d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6629d71ae5a4SJacob Faibussowitsch {
66305d80c0bfSVaclav Hapla   PetscBool flg;
66315d80c0bfSVaclav Hapla   DMLabel   label;
6632c58f1c22SToby Isaac 
6633c58f1c22SToby Isaac   PetscFunctionBegin;
6634c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66354f572ea9SToby Isaac   PetscAssertPointer(name, 2);
66369566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6637c58f1c22SToby Isaac   if (!flg) {
66389566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
66399566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
66409566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6641c58f1c22SToby Isaac   }
66423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6643c58f1c22SToby Isaac }
6644c58f1c22SToby Isaac 
6645c58f1c22SToby Isaac /*@C
6646bb7acecfSBarry 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.
66470fdc7489SMatthew Knepley 
66480fdc7489SMatthew Knepley   Not Collective
66490fdc7489SMatthew Knepley 
66500fdc7489SMatthew Knepley   Input Parameters:
6651bb7acecfSBarry Smith + dm   - The `DM` object
66520fdc7489SMatthew Knepley . l    - The index for the label
66530fdc7489SMatthew Knepley - name - The label name
66540fdc7489SMatthew Knepley 
66550fdc7489SMatthew Knepley   Level: intermediate
66560fdc7489SMatthew Knepley 
66571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
66580fdc7489SMatthew Knepley @*/
6659d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6660d71ae5a4SJacob Faibussowitsch {
66610fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
66620fdc7489SMatthew Knepley   DMLabel     label;
66630fdc7489SMatthew Knepley   PetscInt    Nl, m;
66640fdc7489SMatthew Knepley   PetscBool   flg, match;
66650fdc7489SMatthew Knepley   const char *lname;
66660fdc7489SMatthew Knepley 
66670fdc7489SMatthew Knepley   PetscFunctionBegin;
66680fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66694f572ea9SToby Isaac   PetscAssertPointer(name, 3);
66709566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
66710fdc7489SMatthew Knepley   if (!flg) {
66729566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
66739566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
66749566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
66750fdc7489SMatthew Knepley   }
66769566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
667763a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
66780fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
66799566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
66809566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
66810fdc7489SMatthew Knepley     if (match) break;
66820fdc7489SMatthew Knepley   }
66833ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
66840fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
66850fdc7489SMatthew Knepley   else prev->next = orig->next;
66860fdc7489SMatthew Knepley   if (!l) {
66870fdc7489SMatthew Knepley     orig->next = dm->labels;
66880fdc7489SMatthew Knepley     dm->labels = orig;
66890fdc7489SMatthew Knepley   } else {
6690fbccb6d4SPierre Jolivet     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next);
66910fdc7489SMatthew Knepley     orig->next = prev->next;
66920fdc7489SMatthew Knepley     prev->next = orig;
66930fdc7489SMatthew Knepley   }
66943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66950fdc7489SMatthew Knepley }
66960fdc7489SMatthew Knepley 
66970fdc7489SMatthew Knepley /*@C
6698bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6699c58f1c22SToby Isaac 
6700c58f1c22SToby Isaac   Not Collective
6701c58f1c22SToby Isaac 
6702c58f1c22SToby Isaac   Input Parameters:
6703bb7acecfSBarry Smith + dm    - The `DM` object
6704c58f1c22SToby Isaac . name  - The label name
6705c58f1c22SToby Isaac - point - The mesh point
6706c58f1c22SToby Isaac 
6707c58f1c22SToby Isaac   Output Parameter:
6708c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6709c58f1c22SToby Isaac 
6710c58f1c22SToby Isaac   Level: beginner
6711c58f1c22SToby Isaac 
67121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6713c58f1c22SToby Isaac @*/
6714d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6715d71ae5a4SJacob Faibussowitsch {
6716c58f1c22SToby Isaac   DMLabel label;
6717c58f1c22SToby Isaac 
6718c58f1c22SToby Isaac   PetscFunctionBegin;
6719c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67204f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67219566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67227a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
67239566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
67243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6725c58f1c22SToby Isaac }
6726c58f1c22SToby Isaac 
6727c58f1c22SToby Isaac /*@C
6728bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6729c58f1c22SToby Isaac 
6730c58f1c22SToby Isaac   Not Collective
6731c58f1c22SToby Isaac 
6732c58f1c22SToby Isaac   Input Parameters:
6733bb7acecfSBarry Smith + dm    - The `DM` object
6734c58f1c22SToby Isaac . name  - The label name
6735c58f1c22SToby Isaac . point - The mesh point
6736c58f1c22SToby Isaac - value - The label value for this point
6737c58f1c22SToby Isaac 
6738c58f1c22SToby Isaac   Output Parameter:
6739c58f1c22SToby Isaac 
6740c58f1c22SToby Isaac   Level: beginner
6741c58f1c22SToby Isaac 
67421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6743c58f1c22SToby Isaac @*/
6744d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6745d71ae5a4SJacob Faibussowitsch {
6746c58f1c22SToby Isaac   DMLabel label;
6747c58f1c22SToby Isaac 
6748c58f1c22SToby Isaac   PetscFunctionBegin;
6749c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67504f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67519566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6752c58f1c22SToby Isaac   if (!label) {
67539566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
67549566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6755c58f1c22SToby Isaac   }
67569566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
67573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6758c58f1c22SToby Isaac }
6759c58f1c22SToby Isaac 
6760c58f1c22SToby Isaac /*@C
6761bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6762c58f1c22SToby Isaac 
6763c58f1c22SToby Isaac   Not Collective
6764c58f1c22SToby Isaac 
6765c58f1c22SToby Isaac   Input Parameters:
6766bb7acecfSBarry Smith + dm    - The `DM` object
6767c58f1c22SToby Isaac . name  - The label name
6768c58f1c22SToby Isaac . point - The mesh point
6769c58f1c22SToby Isaac - value - The label value for this point
6770c58f1c22SToby Isaac 
6771c58f1c22SToby Isaac   Level: beginner
6772c58f1c22SToby Isaac 
67731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6774c58f1c22SToby Isaac @*/
6775d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6776d71ae5a4SJacob Faibussowitsch {
6777c58f1c22SToby Isaac   DMLabel label;
6778c58f1c22SToby Isaac 
6779c58f1c22SToby Isaac   PetscFunctionBegin;
6780c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67814f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67829566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67833ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67849566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
67853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6786c58f1c22SToby Isaac }
6787c58f1c22SToby Isaac 
6788c58f1c22SToby Isaac /*@C
6789bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6790c58f1c22SToby Isaac 
6791c58f1c22SToby Isaac   Not Collective
6792c58f1c22SToby Isaac 
6793c58f1c22SToby Isaac   Input Parameters:
6794bb7acecfSBarry Smith + dm   - The `DM` object
6795c58f1c22SToby Isaac - name - The label name
6796c58f1c22SToby Isaac 
6797c58f1c22SToby Isaac   Output Parameter:
6798c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6799c58f1c22SToby Isaac 
6800c58f1c22SToby Isaac   Level: beginner
6801c58f1c22SToby Isaac 
680260225df5SJacob Faibussowitsch   Developer Notes:
6803bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6804bb7acecfSBarry Smith 
68051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6806c58f1c22SToby Isaac @*/
6807d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6808d71ae5a4SJacob Faibussowitsch {
6809c58f1c22SToby Isaac   DMLabel label;
6810c58f1c22SToby Isaac 
6811c58f1c22SToby Isaac   PetscFunctionBegin;
6812c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68134f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68144f572ea9SToby Isaac   PetscAssertPointer(size, 3);
68159566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6816c58f1c22SToby Isaac   *size = 0;
68173ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68189566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
68193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6820c58f1c22SToby Isaac }
6821c58f1c22SToby Isaac 
6822c58f1c22SToby Isaac /*@C
6823bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6824c58f1c22SToby Isaac 
6825c58f1c22SToby Isaac   Not Collective
6826c58f1c22SToby Isaac 
6827c58f1c22SToby Isaac   Input Parameters:
682860225df5SJacob Faibussowitsch + dm   - The `DM` object
6829c58f1c22SToby Isaac - name - The label name
6830c58f1c22SToby Isaac 
6831c58f1c22SToby Isaac   Output Parameter:
683220f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6833c58f1c22SToby Isaac 
6834c58f1c22SToby Isaac   Level: beginner
6835c58f1c22SToby Isaac 
68361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6837c58f1c22SToby Isaac @*/
6838d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6839d71ae5a4SJacob Faibussowitsch {
6840c58f1c22SToby Isaac   DMLabel label;
6841c58f1c22SToby Isaac 
6842c58f1c22SToby Isaac   PetscFunctionBegin;
6843c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68444f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68454f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
68469566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6847c58f1c22SToby Isaac   *ids = NULL;
6848dab2e251SBlaise Bourdin   if (label) {
68499566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6850dab2e251SBlaise Bourdin   } else {
6851dab2e251SBlaise Bourdin     /* returning an empty IS */
68529566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6853dab2e251SBlaise Bourdin   }
68543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6855c58f1c22SToby Isaac }
6856c58f1c22SToby Isaac 
6857c58f1c22SToby Isaac /*@C
6858c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6859c58f1c22SToby Isaac 
6860c58f1c22SToby Isaac   Not Collective
6861c58f1c22SToby Isaac 
6862c58f1c22SToby Isaac   Input Parameters:
6863bb7acecfSBarry Smith + dm    - The `DM` object
6864c58f1c22SToby Isaac . name  - The label name
6865c58f1c22SToby Isaac - value - The stratum value
6866c58f1c22SToby Isaac 
6867c58f1c22SToby Isaac   Output Parameter:
6868bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6869c58f1c22SToby Isaac 
6870c58f1c22SToby Isaac   Level: beginner
6871c58f1c22SToby Isaac 
68721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6873c58f1c22SToby Isaac @*/
6874d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6875d71ae5a4SJacob Faibussowitsch {
6876c58f1c22SToby Isaac   DMLabel label;
6877c58f1c22SToby Isaac 
6878c58f1c22SToby Isaac   PetscFunctionBegin;
6879c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68804f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68814f572ea9SToby Isaac   PetscAssertPointer(size, 4);
68829566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6883c58f1c22SToby Isaac   *size = 0;
68843ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68859566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
68863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6887c58f1c22SToby Isaac }
6888c58f1c22SToby Isaac 
6889c58f1c22SToby Isaac /*@C
6890c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6891c58f1c22SToby Isaac 
6892c58f1c22SToby Isaac   Not Collective
6893c58f1c22SToby Isaac 
6894c58f1c22SToby Isaac   Input Parameters:
6895bb7acecfSBarry Smith + dm    - The `DM` object
6896c58f1c22SToby Isaac . name  - The label name
6897c58f1c22SToby Isaac - value - The stratum value
6898c58f1c22SToby Isaac 
6899c58f1c22SToby Isaac   Output Parameter:
690020f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
6901c58f1c22SToby Isaac 
6902c58f1c22SToby Isaac   Level: beginner
6903c58f1c22SToby Isaac 
69041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6905c58f1c22SToby Isaac @*/
6906d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6907d71ae5a4SJacob Faibussowitsch {
6908c58f1c22SToby Isaac   DMLabel label;
6909c58f1c22SToby Isaac 
6910c58f1c22SToby Isaac   PetscFunctionBegin;
6911c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69124f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69134f572ea9SToby Isaac   PetscAssertPointer(points, 4);
69149566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6915c58f1c22SToby Isaac   *points = NULL;
69163ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69179566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
69183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6919c58f1c22SToby Isaac }
6920c58f1c22SToby Isaac 
69214de306b1SToby Isaac /*@C
69229044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
69234de306b1SToby Isaac 
69244de306b1SToby Isaac   Not Collective
69254de306b1SToby Isaac 
69264de306b1SToby Isaac   Input Parameters:
6927bb7acecfSBarry Smith + dm     - The `DM` object
69284de306b1SToby Isaac . name   - The label name
69294de306b1SToby Isaac . value  - The stratum value
69304de306b1SToby Isaac - points - The stratum points
69314de306b1SToby Isaac 
69324de306b1SToby Isaac   Level: beginner
69334de306b1SToby Isaac 
69341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
69354de306b1SToby Isaac @*/
6936d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6937d71ae5a4SJacob Faibussowitsch {
69384de306b1SToby Isaac   DMLabel label;
69394de306b1SToby Isaac 
69404de306b1SToby Isaac   PetscFunctionBegin;
69414de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69424f572ea9SToby Isaac   PetscAssertPointer(name, 2);
6943292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
69449566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
69453ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69469566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
69473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
69484de306b1SToby Isaac }
69494de306b1SToby Isaac 
6950c58f1c22SToby Isaac /*@C
6951bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6952c58f1c22SToby Isaac 
6953c58f1c22SToby Isaac   Not Collective
6954c58f1c22SToby Isaac 
6955c58f1c22SToby Isaac   Input Parameters:
6956bb7acecfSBarry Smith + dm    - The `DM` object
6957c58f1c22SToby Isaac . name  - The label name
6958c58f1c22SToby Isaac - value - The label value for this point
6959c58f1c22SToby Isaac 
6960c58f1c22SToby Isaac   Output Parameter:
6961c58f1c22SToby Isaac 
6962c58f1c22SToby Isaac   Level: beginner
6963c58f1c22SToby Isaac 
69641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6965c58f1c22SToby Isaac @*/
6966d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6967d71ae5a4SJacob Faibussowitsch {
6968c58f1c22SToby Isaac   DMLabel label;
6969c58f1c22SToby Isaac 
6970c58f1c22SToby Isaac   PetscFunctionBegin;
6971c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69724f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69739566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
69743ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69759566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
69763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6977c58f1c22SToby Isaac }
6978c58f1c22SToby Isaac 
6979c58f1c22SToby Isaac /*@
6980bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6981c58f1c22SToby Isaac 
6982c58f1c22SToby Isaac   Not Collective
6983c58f1c22SToby Isaac 
6984c58f1c22SToby Isaac   Input Parameter:
6985bb7acecfSBarry Smith . dm - The `DM` object
6986c58f1c22SToby Isaac 
6987c58f1c22SToby Isaac   Output Parameter:
6988c58f1c22SToby Isaac . numLabels - the number of Labels
6989c58f1c22SToby Isaac 
6990c58f1c22SToby Isaac   Level: intermediate
6991c58f1c22SToby Isaac 
69921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6993c58f1c22SToby Isaac @*/
6994d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6995d71ae5a4SJacob Faibussowitsch {
69965d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6997c58f1c22SToby Isaac   PetscInt    n    = 0;
6998c58f1c22SToby Isaac 
6999c58f1c22SToby Isaac   PetscFunctionBegin;
7000c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70014f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
70029371c9d4SSatish Balay   while (next) {
70039371c9d4SSatish Balay     ++n;
70049371c9d4SSatish Balay     next = next->next;
70059371c9d4SSatish Balay   }
7006c58f1c22SToby Isaac   *numLabels = n;
70073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7008c58f1c22SToby Isaac }
7009c58f1c22SToby Isaac 
7010c58f1c22SToby Isaac /*@C
7011c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7012c58f1c22SToby Isaac 
7013c58f1c22SToby Isaac   Not Collective
7014c58f1c22SToby Isaac 
7015c58f1c22SToby Isaac   Input Parameters:
7016bb7acecfSBarry Smith + dm - The `DM` object
7017c58f1c22SToby Isaac - n  - the label number
7018c58f1c22SToby Isaac 
7019c58f1c22SToby Isaac   Output Parameter:
7020c58f1c22SToby Isaac . name - the label name
7021c58f1c22SToby Isaac 
7022c58f1c22SToby Isaac   Level: intermediate
7023c58f1c22SToby Isaac 
702460225df5SJacob Faibussowitsch   Developer Notes:
7025bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
7026bb7acecfSBarry Smith 
70271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7028c58f1c22SToby Isaac @*/
7029d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7030d71ae5a4SJacob Faibussowitsch {
70315d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7032c58f1c22SToby Isaac   PetscInt    l    = 0;
7033c58f1c22SToby Isaac 
7034c58f1c22SToby Isaac   PetscFunctionBegin;
7035c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70364f572ea9SToby Isaac   PetscAssertPointer(name, 3);
7037c58f1c22SToby Isaac   while (next) {
7038c58f1c22SToby Isaac     if (l == n) {
70399566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
70403ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7041c58f1c22SToby Isaac     }
7042c58f1c22SToby Isaac     ++l;
7043c58f1c22SToby Isaac     next = next->next;
7044c58f1c22SToby Isaac   }
704563a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7046c58f1c22SToby Isaac }
7047c58f1c22SToby Isaac 
7048c58f1c22SToby Isaac /*@C
7049bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
7050c58f1c22SToby Isaac 
7051c58f1c22SToby Isaac   Not Collective
7052c58f1c22SToby Isaac 
7053c58f1c22SToby Isaac   Input Parameters:
7054bb7acecfSBarry Smith + dm   - The `DM` object
7055c58f1c22SToby Isaac - name - The label name
7056c58f1c22SToby Isaac 
7057c58f1c22SToby Isaac   Output Parameter:
7058bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
7059c58f1c22SToby Isaac 
7060c58f1c22SToby Isaac   Level: intermediate
7061c58f1c22SToby Isaac 
70621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7063c58f1c22SToby Isaac @*/
7064d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7065d71ae5a4SJacob Faibussowitsch {
70665d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7067d67d17b1SMatthew G. Knepley   const char *lname;
7068c58f1c22SToby Isaac 
7069c58f1c22SToby Isaac   PetscFunctionBegin;
7070c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70714f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70724f572ea9SToby Isaac   PetscAssertPointer(hasLabel, 3);
7073c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7074c58f1c22SToby Isaac   while (next) {
70759566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70769566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7077c58f1c22SToby Isaac     if (*hasLabel) break;
7078c58f1c22SToby Isaac     next = next->next;
7079c58f1c22SToby Isaac   }
70803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7081c58f1c22SToby Isaac }
7082c58f1c22SToby Isaac 
7083a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7084c58f1c22SToby Isaac /*@C
708520f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
7086c58f1c22SToby Isaac 
7087c58f1c22SToby Isaac   Not Collective
7088c58f1c22SToby Isaac 
7089c58f1c22SToby Isaac   Input Parameters:
7090bb7acecfSBarry Smith + dm   - The `DM` object
7091c58f1c22SToby Isaac - name - The label name
7092c58f1c22SToby Isaac 
7093c58f1c22SToby Isaac   Output Parameter:
709420f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
7095c58f1c22SToby Isaac 
7096bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7097bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7098bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7099bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7100bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7101bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7102bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
71036d7c9049SMatthew G. Knepley 
7104c58f1c22SToby Isaac   Level: intermediate
7105c58f1c22SToby Isaac 
710660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7107c58f1c22SToby Isaac @*/
7108d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7109d71ae5a4SJacob Faibussowitsch {
71105d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7111c58f1c22SToby Isaac   PetscBool   hasLabel;
7112d67d17b1SMatthew G. Knepley   const char *lname;
7113c58f1c22SToby Isaac 
7114c58f1c22SToby Isaac   PetscFunctionBegin;
7115c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71164f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71174f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7118c58f1c22SToby Isaac   *label = NULL;
7119c58f1c22SToby Isaac   while (next) {
71209566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71219566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7122c58f1c22SToby Isaac     if (hasLabel) {
7123c58f1c22SToby Isaac       *label = next->label;
7124c58f1c22SToby Isaac       break;
7125c58f1c22SToby Isaac     }
7126c58f1c22SToby Isaac     next = next->next;
7127c58f1c22SToby Isaac   }
71283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7129c58f1c22SToby Isaac }
7130c58f1c22SToby Isaac 
7131c58f1c22SToby Isaac /*@C
7132bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7133c58f1c22SToby Isaac 
7134c58f1c22SToby Isaac   Not Collective
7135c58f1c22SToby Isaac 
7136c58f1c22SToby Isaac   Input Parameters:
7137bb7acecfSBarry Smith + dm - The `DM` object
7138c58f1c22SToby Isaac - n  - the label number
7139c58f1c22SToby Isaac 
7140c58f1c22SToby Isaac   Output Parameter:
7141c58f1c22SToby Isaac . label - the label
7142c58f1c22SToby Isaac 
7143c58f1c22SToby Isaac   Level: intermediate
7144c58f1c22SToby Isaac 
71451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7146c58f1c22SToby Isaac @*/
7147d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7148d71ae5a4SJacob Faibussowitsch {
71495d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7150c58f1c22SToby Isaac   PetscInt    l    = 0;
7151c58f1c22SToby Isaac 
7152c58f1c22SToby Isaac   PetscFunctionBegin;
7153c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71544f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7155c58f1c22SToby Isaac   while (next) {
7156c58f1c22SToby Isaac     if (l == n) {
7157c58f1c22SToby Isaac       *label = next->label;
71583ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7159c58f1c22SToby Isaac     }
7160c58f1c22SToby Isaac     ++l;
7161c58f1c22SToby Isaac     next = next->next;
7162c58f1c22SToby Isaac   }
716363a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7164c58f1c22SToby Isaac }
7165c58f1c22SToby Isaac 
7166c58f1c22SToby Isaac /*@C
7167bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7168c58f1c22SToby Isaac 
7169c58f1c22SToby Isaac   Not Collective
7170c58f1c22SToby Isaac 
7171c58f1c22SToby Isaac   Input Parameters:
7172bb7acecfSBarry Smith + dm    - The `DM` object
7173bb7acecfSBarry Smith - label - The `DMLabel`
7174c58f1c22SToby Isaac 
7175c58f1c22SToby Isaac   Level: developer
7176c58f1c22SToby Isaac 
717760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7178c58f1c22SToby Isaac @*/
7179d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7180d71ae5a4SJacob Faibussowitsch {
71815d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7182c58f1c22SToby Isaac   PetscBool   hasLabel;
7183d67d17b1SMatthew G. Knepley   const char *lname;
71845d80c0bfSVaclav Hapla   PetscBool   flg;
7185c58f1c22SToby Isaac 
7186c58f1c22SToby Isaac   PetscFunctionBegin;
7187c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71889566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
71899566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
71907a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
71919566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7192c58f1c22SToby Isaac   tmpLabel->label  = label;
7193c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
71945d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
71955d80c0bfSVaclav Hapla   *p = tmpLabel;
71969566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
71979566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
71985d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
71999566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7200ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
72013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7202c58f1c22SToby Isaac }
7203c58f1c22SToby Isaac 
7204a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7205c58f1c22SToby Isaac /*@C
72064a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
72074a7ee7d0SMatthew G. Knepley 
72084a7ee7d0SMatthew G. Knepley   Not Collective
72094a7ee7d0SMatthew G. Knepley 
72104a7ee7d0SMatthew G. Knepley   Input Parameters:
7211bb7acecfSBarry Smith + dm    - The `DM` object
7212bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
72134a7ee7d0SMatthew G. Knepley 
7214bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7215bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7216bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7217bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7218bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7219bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7220bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
72214a7ee7d0SMatthew G. Knepley 
72224a7ee7d0SMatthew G. Knepley   Level: intermediate
72234a7ee7d0SMatthew G. Knepley 
72241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
72254a7ee7d0SMatthew G. Knepley @*/
7226d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7227d71ae5a4SJacob Faibussowitsch {
72284a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
72294a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
72304a7ee7d0SMatthew G. Knepley   const char *name, *lname;
72314a7ee7d0SMatthew G. Knepley 
72324a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
72334a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72344a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
72359566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
72364a7ee7d0SMatthew G. Knepley   while (next) {
72379566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72389566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
72394a7ee7d0SMatthew G. Knepley     if (hasLabel) {
72409566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
72419566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
72424a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
72439566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
72444a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
72459566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
72464a7ee7d0SMatthew G. Knepley       next->label = label;
72474a7ee7d0SMatthew G. Knepley       break;
72484a7ee7d0SMatthew G. Knepley     }
72494a7ee7d0SMatthew G. Knepley     next = next->next;
72504a7ee7d0SMatthew G. Knepley   }
72513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
72524a7ee7d0SMatthew G. Knepley }
72534a7ee7d0SMatthew G. Knepley 
72544a7ee7d0SMatthew G. Knepley /*@C
7255bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7256c58f1c22SToby Isaac 
7257c58f1c22SToby Isaac   Not Collective
7258c58f1c22SToby Isaac 
7259c58f1c22SToby Isaac   Input Parameters:
7260bb7acecfSBarry Smith + dm   - The `DM` object
7261c58f1c22SToby Isaac - name - The label name
7262c58f1c22SToby Isaac 
7263c58f1c22SToby Isaac   Output Parameter:
726420f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7265bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7266c58f1c22SToby Isaac 
7267c58f1c22SToby Isaac   Level: developer
7268c58f1c22SToby Isaac 
72691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7270c58f1c22SToby Isaac @*/
7271d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7272d71ae5a4SJacob Faibussowitsch {
727395d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7274c58f1c22SToby Isaac   PetscBool   hasLabel;
7275d67d17b1SMatthew G. Knepley   const char *lname;
7276c58f1c22SToby Isaac 
7277c58f1c22SToby Isaac   PetscFunctionBegin;
7278c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72794f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7280e5472504SVaclav Hapla   if (label) {
72814f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7282c58f1c22SToby Isaac     *label = NULL;
7283e5472504SVaclav Hapla   }
72845d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
72859566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
72869566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7287c58f1c22SToby Isaac     if (hasLabel) {
728895d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
72899566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
729095d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
72919566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7292ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
729395d578d6SVaclav Hapla       if (label) *label = link->label;
72949566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
72959566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7296c58f1c22SToby Isaac       break;
7297c58f1c22SToby Isaac     }
7298c58f1c22SToby Isaac   }
72993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7300c58f1c22SToby Isaac }
7301c58f1c22SToby Isaac 
7302306894acSVaclav Hapla /*@
7303bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7304306894acSVaclav Hapla 
7305306894acSVaclav Hapla   Not Collective
7306306894acSVaclav Hapla 
7307306894acSVaclav Hapla   Input Parameters:
7308bb7acecfSBarry Smith + dm           - The `DM` object
7309bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
731020f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7311306894acSVaclav Hapla 
7312306894acSVaclav Hapla   Level: developer
7313306894acSVaclav Hapla 
7314bb7acecfSBarry Smith   Note:
7315306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7316bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7317306894acSVaclav Hapla   *label nullified.
7318306894acSVaclav Hapla 
73191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7320306894acSVaclav Hapla @*/
7321d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7322d71ae5a4SJacob Faibussowitsch {
732343e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7324306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7325306894acSVaclav Hapla 
7326306894acSVaclav Hapla   PetscFunctionBegin;
7327306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73284f572ea9SToby Isaac   PetscAssertPointer(label, 2);
73293ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7330306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7331306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
73325d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
733343e45a93SVaclav Hapla     if (*label == link->label) {
7334306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
733543e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7336306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7337ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
733843e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
73399566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
73409566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7341306894acSVaclav Hapla       break;
7342306894acSVaclav Hapla     }
7343306894acSVaclav Hapla   }
73447a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
73453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7346306894acSVaclav Hapla }
7347306894acSVaclav Hapla 
7348c58f1c22SToby Isaac /*@C
7349c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7350c58f1c22SToby Isaac 
7351c58f1c22SToby Isaac   Not Collective
7352c58f1c22SToby Isaac 
7353c58f1c22SToby Isaac   Input Parameters:
7354bb7acecfSBarry Smith + dm   - The `DM` object
7355c58f1c22SToby Isaac - name - The label name
7356c58f1c22SToby Isaac 
7357c58f1c22SToby Isaac   Output Parameter:
7358c58f1c22SToby Isaac . output - The flag for output
7359c58f1c22SToby Isaac 
7360c58f1c22SToby Isaac   Level: developer
7361c58f1c22SToby Isaac 
73621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7363c58f1c22SToby Isaac @*/
7364d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7365d71ae5a4SJacob Faibussowitsch {
73665d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7367d67d17b1SMatthew G. Knepley   const char *lname;
7368c58f1c22SToby Isaac 
7369c58f1c22SToby Isaac   PetscFunctionBegin;
7370c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73714f572ea9SToby Isaac   PetscAssertPointer(name, 2);
73724f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7373c58f1c22SToby Isaac   while (next) {
7374c58f1c22SToby Isaac     PetscBool flg;
7375c58f1c22SToby Isaac 
73769566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73779566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
73789371c9d4SSatish Balay     if (flg) {
73799371c9d4SSatish Balay       *output = next->output;
73803ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
73819371c9d4SSatish Balay     }
7382c58f1c22SToby Isaac     next = next->next;
7383c58f1c22SToby Isaac   }
738498921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7385c58f1c22SToby Isaac }
7386c58f1c22SToby Isaac 
7387c58f1c22SToby Isaac /*@C
7388bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7389c58f1c22SToby Isaac 
7390c58f1c22SToby Isaac   Not Collective
7391c58f1c22SToby Isaac 
7392c58f1c22SToby Isaac   Input Parameters:
7393bb7acecfSBarry Smith + dm     - The `DM` object
7394c58f1c22SToby Isaac . name   - The label name
7395bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7396c58f1c22SToby Isaac 
7397c58f1c22SToby Isaac   Level: developer
7398c58f1c22SToby Isaac 
73991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7400c58f1c22SToby Isaac @*/
7401d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7402d71ae5a4SJacob Faibussowitsch {
74035d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7404d67d17b1SMatthew G. Knepley   const char *lname;
7405c58f1c22SToby Isaac 
7406c58f1c22SToby Isaac   PetscFunctionBegin;
7407c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74084f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7409c58f1c22SToby Isaac   while (next) {
7410c58f1c22SToby Isaac     PetscBool flg;
7411c58f1c22SToby Isaac 
74129566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74139566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74149371c9d4SSatish Balay     if (flg) {
74159371c9d4SSatish Balay       next->output = output;
74163ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74179371c9d4SSatish Balay     }
7418c58f1c22SToby Isaac     next = next->next;
7419c58f1c22SToby Isaac   }
742098921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7421c58f1c22SToby Isaac }
7422c58f1c22SToby Isaac 
7423c58f1c22SToby Isaac /*@
7424bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7425c58f1c22SToby Isaac 
742620f4b53cSBarry Smith   Collective
7427c58f1c22SToby Isaac 
7428d8d19677SJose E. Roman   Input Parameters:
7429bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7430bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7431bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7432bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7433bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7434c58f1c22SToby Isaac 
7435c58f1c22SToby Isaac   Level: intermediate
7436c58f1c22SToby Isaac 
7437bb7acecfSBarry Smith   Note:
74382cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7439c58f1c22SToby Isaac 
74401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7441c58f1c22SToby Isaac @*/
7442d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7443d71ae5a4SJacob Faibussowitsch {
74442cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7445c58f1c22SToby Isaac   const char *name;
7446c58f1c22SToby Isaac   PetscBool   flg;
74475d80c0bfSVaclav Hapla   DMLabelLink link;
7448c58f1c22SToby Isaac 
74495d80c0bfSVaclav Hapla   PetscFunctionBegin;
74505d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
74515d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
74525d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
74535d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
74547a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
74553ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
74565d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
74575d80c0bfSVaclav Hapla     label = link->label;
74589566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
74595d80c0bfSVaclav Hapla     if (!all) {
74609566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7461c58f1c22SToby Isaac       if (flg) continue;
74629566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
74637d5acc75SStefano Zampini       if (flg) continue;
74649566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7465ba2698f1SMatthew G. Knepley       if (flg) continue;
74665d80c0bfSVaclav Hapla     }
74679566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
74682cbb9b06SVaclav Hapla     if (labelOld) {
74692cbb9b06SVaclav Hapla       switch (emode) {
7470d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7471d71ae5a4SJacob Faibussowitsch         continue;
7472d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7473d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7474d71ae5a4SJacob Faibussowitsch         break;
7475d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7476d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7477d71ae5a4SJacob Faibussowitsch       default:
7478d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
74792cbb9b06SVaclav Hapla       }
74802cbb9b06SVaclav Hapla     }
74815d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
74829566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
74835d80c0bfSVaclav Hapla     } else {
74845d80c0bfSVaclav Hapla       labelNew = label;
74855d80c0bfSVaclav Hapla     }
74869566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
74879566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7488c58f1c22SToby Isaac   }
74893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7490c58f1c22SToby Isaac }
7491461a15a0SLisandro Dalcin 
7492609dae6eSVaclav Hapla /*@C
7493b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7494609dae6eSVaclav Hapla 
749520f4b53cSBarry Smith   Collective; No Fortran Support
7496609dae6eSVaclav Hapla 
7497609dae6eSVaclav Hapla   Input Parameters:
7498bb7acecfSBarry Smith + dm0 - First `DM` object
7499bb7acecfSBarry Smith - dm1 - Second `DM` object
7500609dae6eSVaclav Hapla 
7501a4e35b19SJacob Faibussowitsch   Output Parameters:
75025efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
750320f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7504609dae6eSVaclav Hapla 
7505609dae6eSVaclav Hapla   Level: intermediate
7506609dae6eSVaclav Hapla 
7507609dae6eSVaclav Hapla   Notes:
7508bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7509bb7acecfSBarry Smith 
751020f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7511bb7acecfSBarry Smith 
751220f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7513609dae6eSVaclav Hapla 
75145efe38ccSVaclav Hapla   The output message is set independently on each rank.
7515bb7acecfSBarry Smith 
7516bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7517bb7acecfSBarry Smith 
751820f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7519bb7acecfSBarry Smith 
752020f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7521609dae6eSVaclav Hapla 
7522609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7523bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7524609dae6eSVaclav Hapla 
75251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7526609dae6eSVaclav Hapla @*/
7527d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7528d71ae5a4SJacob Faibussowitsch {
75295efe38ccSVaclav Hapla   PetscInt    n, i;
7530609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
75315efe38ccSVaclav Hapla   PetscBool   eq;
7532609dae6eSVaclav Hapla   MPI_Comm    comm;
75335efe38ccSVaclav Hapla   PetscMPIInt rank;
7534609dae6eSVaclav Hapla 
7535609dae6eSVaclav Hapla   PetscFunctionBegin;
7536609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7537609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7538609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
75394f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
75404f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
75419566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
75429566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
75435efe38ccSVaclav Hapla   {
75445efe38ccSVaclav Hapla     PetscInt n1;
75455efe38ccSVaclav Hapla 
75469566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
75479566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
75485efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
754948a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7550712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
75515efe38ccSVaclav Hapla     if (!eq) goto finish;
75525efe38ccSVaclav Hapla   }
75535efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7554609dae6eSVaclav Hapla     DMLabel     l0, l1;
7555609dae6eSVaclav Hapla     const char *name;
7556609dae6eSVaclav Hapla     char       *msgInner;
7557609dae6eSVaclav Hapla 
7558609dae6eSVaclav Hapla     /* Ignore label order */
75599566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
75609566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
75619566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7562609dae6eSVaclav Hapla     if (!l1) {
756363a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
75645efe38ccSVaclav Hapla       eq = PETSC_FALSE;
75655efe38ccSVaclav Hapla       break;
7566609dae6eSVaclav Hapla     }
75679566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
75689566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
75699566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
75705efe38ccSVaclav Hapla     if (!eq) break;
7571609dae6eSVaclav Hapla   }
7572712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7573609dae6eSVaclav Hapla finish:
75745efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7575609dae6eSVaclav Hapla   if (message) {
7576609dae6eSVaclav Hapla     *message = NULL;
757748a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
75785efe38ccSVaclav Hapla   } else {
757948a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
75809566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
75815efe38ccSVaclav Hapla   }
75825efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
75835efe38ccSVaclav Hapla   if (equal) *equal = eq;
75847a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
75853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7586609dae6eSVaclav Hapla }
7587609dae6eSVaclav Hapla 
7588d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7589d71ae5a4SJacob Faibussowitsch {
7590461a15a0SLisandro Dalcin   PetscFunctionBegin;
75914f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7592461a15a0SLisandro Dalcin   if (!*label) {
75939566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
75949566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7595461a15a0SLisandro Dalcin   }
75969566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
75973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7598461a15a0SLisandro Dalcin }
7599461a15a0SLisandro Dalcin 
76000fdc7489SMatthew Knepley /*
76010fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
76020fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
76030fdc7489SMatthew Knepley   (label, id) pair in the DM.
76040fdc7489SMatthew Knepley 
76050fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
76060fdc7489SMatthew Knepley   each label.
76070fdc7489SMatthew Knepley */
7608d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7609d71ae5a4SJacob Faibussowitsch {
76100fdc7489SMatthew Knepley   DMUniversalLabel ul;
76110fdc7489SMatthew Knepley   PetscBool       *active;
76120fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
76130fdc7489SMatthew Knepley 
76140fdc7489SMatthew Knepley   PetscFunctionBegin;
76159566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
76169566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
76179566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
76189566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
76190fdc7489SMatthew Knepley   ul->Nl = 0;
76200fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
76210fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
76220fdc7489SMatthew Knepley     const char *name;
76230fdc7489SMatthew Knepley 
76249566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
76259566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
76269566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
76270fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
76280fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
76290fdc7489SMatthew Knepley   }
76309566063dSJacob 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));
76310fdc7489SMatthew Knepley   ul->Nv = 0;
76320fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
76330fdc7489SMatthew Knepley     DMLabel     label;
76340fdc7489SMatthew Knepley     PetscInt    nv;
76350fdc7489SMatthew Knepley     const char *name;
76360fdc7489SMatthew Knepley 
76370fdc7489SMatthew Knepley     if (!active[l]) continue;
76389566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
76399566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
76409566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
76419566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
76420fdc7489SMatthew Knepley     ul->indices[m] = l;
76430fdc7489SMatthew Knepley     ul->Nv += nv;
76440fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
76450fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
76460fdc7489SMatthew Knepley     ++m;
76470fdc7489SMatthew Knepley   }
76480fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
76490fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
76500fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
76510fdc7489SMatthew Knepley   }
76520fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
76530fdc7489SMatthew Knepley     PetscInt b;
76540fdc7489SMatthew Knepley 
76550fdc7489SMatthew Knepley     ul->masks[l] = 0;
76560fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
76570fdc7489SMatthew Knepley   }
76589566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
76590fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
76600fdc7489SMatthew Knepley     DMLabel         label;
76610fdc7489SMatthew Knepley     IS              valueIS;
76620fdc7489SMatthew Knepley     const PetscInt *varr;
76630fdc7489SMatthew Knepley     PetscInt        nv, v;
76640fdc7489SMatthew Knepley 
76650fdc7489SMatthew Knepley     if (!active[l]) continue;
76669566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
76679566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
76689566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
76699566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7670ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
76719566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
76729566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
76739566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
76740fdc7489SMatthew Knepley     ++m;
76750fdc7489SMatthew Knepley   }
76769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
76770fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
76780fdc7489SMatthew Knepley     PetscInt  uval   = 0;
76790fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
76800fdc7489SMatthew Knepley 
76810fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
76820fdc7489SMatthew Knepley       DMLabel  label;
76830649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
76840fdc7489SMatthew Knepley 
76850fdc7489SMatthew Knepley       if (!active[l]) continue;
76869566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
76879566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
76889566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
76899371c9d4SSatish Balay       if (val == defval) {
76909371c9d4SSatish Balay         ++m;
76919371c9d4SSatish Balay         continue;
76929371c9d4SSatish Balay       }
76930649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
76940fdc7489SMatthew Knepley       marked = PETSC_TRUE;
76959566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
769663a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
76970fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
76980fdc7489SMatthew Knepley       ++m;
76990fdc7489SMatthew Knepley     }
77009566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
77010fdc7489SMatthew Knepley   }
77029566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
77030fdc7489SMatthew Knepley   *universal = ul;
77043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77050fdc7489SMatthew Knepley }
77060fdc7489SMatthew Knepley 
7707d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7708d71ae5a4SJacob Faibussowitsch {
77090fdc7489SMatthew Knepley   PetscInt l;
77100fdc7489SMatthew Knepley 
77110fdc7489SMatthew Knepley   PetscFunctionBegin;
77129566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
77139566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
77149566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
77159566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
77169566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
77170fdc7489SMatthew Knepley   *universal = NULL;
77183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77190fdc7489SMatthew Knepley }
77200fdc7489SMatthew Knepley 
7721d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7722d71ae5a4SJacob Faibussowitsch {
77230fdc7489SMatthew Knepley   PetscFunctionBegin;
77244f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
77250fdc7489SMatthew Knepley   *ulabel = ul->label;
77263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77270fdc7489SMatthew Knepley }
77280fdc7489SMatthew Knepley 
7729d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7730d71ae5a4SJacob Faibussowitsch {
77310fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
77320fdc7489SMatthew Knepley 
77330fdc7489SMatthew Knepley   PetscFunctionBegin;
7734064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
77350fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
77369566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
77379566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
77380fdc7489SMatthew Knepley   }
77390fdc7489SMatthew Knepley   if (preserveOrder) {
77400fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
77410fdc7489SMatthew Knepley       const char *name;
77420fdc7489SMatthew Knepley       PetscBool   match;
77430fdc7489SMatthew Knepley 
77449566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
77459566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
774663a3b9bcSJacob 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]);
77470fdc7489SMatthew Knepley     }
77480fdc7489SMatthew Knepley   }
77493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77500fdc7489SMatthew Knepley }
77510fdc7489SMatthew Knepley 
7752d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7753d71ae5a4SJacob Faibussowitsch {
77540fdc7489SMatthew Knepley   PetscInt l;
77550fdc7489SMatthew Knepley 
77560fdc7489SMatthew Knepley   PetscFunctionBegin;
77570fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
77580fdc7489SMatthew Knepley     DMLabel  label;
77590fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
77600fdc7489SMatthew Knepley 
77610fdc7489SMatthew Knepley     if (lval) {
77629566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
77639566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
77649566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
77650fdc7489SMatthew Knepley     }
77660fdc7489SMatthew Knepley   }
77673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77680fdc7489SMatthew Knepley }
7769a8fb8f29SToby Isaac 
7770a8fb8f29SToby Isaac /*@
7771bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7772bb7acecfSBarry Smith 
777320f4b53cSBarry Smith   Not Collective
7774a8fb8f29SToby Isaac 
7775a8fb8f29SToby Isaac   Input Parameter:
7776bb7acecfSBarry Smith . dm - The `DM` object
7777a8fb8f29SToby Isaac 
7778a8fb8f29SToby Isaac   Output Parameter:
7779bb7acecfSBarry Smith . cdm - The coarse `DM`
7780a8fb8f29SToby Isaac 
7781a8fb8f29SToby Isaac   Level: intermediate
7782a8fb8f29SToby Isaac 
77831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7784a8fb8f29SToby Isaac @*/
7785d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7786d71ae5a4SJacob Faibussowitsch {
7787a8fb8f29SToby Isaac   PetscFunctionBegin;
7788a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77894f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7790a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
77913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7792a8fb8f29SToby Isaac }
7793a8fb8f29SToby Isaac 
7794a8fb8f29SToby Isaac /*@
7795bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7796a8fb8f29SToby Isaac 
7797a8fb8f29SToby Isaac   Input Parameters:
7798bb7acecfSBarry Smith + dm  - The `DM` object
7799bb7acecfSBarry Smith - cdm - The coarse `DM`
7800a8fb8f29SToby Isaac 
7801a8fb8f29SToby Isaac   Level: intermediate
7802a8fb8f29SToby Isaac 
7803bb7acecfSBarry Smith   Note:
7804bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7805bb7acecfSBarry Smith 
78061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7807a8fb8f29SToby Isaac @*/
7808d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7809d71ae5a4SJacob Faibussowitsch {
7810a8fb8f29SToby Isaac   PetscFunctionBegin;
7811a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7812a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
781389d734beSBarry Smith   if (dm == cdm) cdm = NULL;
78149566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
78159566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7816a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
78173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7818a8fb8f29SToby Isaac }
7819a8fb8f29SToby Isaac 
782088bdff64SToby Isaac /*@
7821bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
782288bdff64SToby Isaac 
782388bdff64SToby Isaac   Input Parameter:
7824bb7acecfSBarry Smith . dm - The `DM` object
782588bdff64SToby Isaac 
782688bdff64SToby Isaac   Output Parameter:
7827bb7acecfSBarry Smith . fdm - The fine `DM`
782888bdff64SToby Isaac 
782988bdff64SToby Isaac   Level: intermediate
783088bdff64SToby Isaac 
78311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
783288bdff64SToby Isaac @*/
7833d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7834d71ae5a4SJacob Faibussowitsch {
783588bdff64SToby Isaac   PetscFunctionBegin;
783688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78374f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
783888bdff64SToby Isaac   *fdm = dm->fineMesh;
78393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
784088bdff64SToby Isaac }
784188bdff64SToby Isaac 
784288bdff64SToby Isaac /*@
7843bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
784488bdff64SToby Isaac 
784588bdff64SToby Isaac   Input Parameters:
7846bb7acecfSBarry Smith + dm  - The `DM` object
7847bb7acecfSBarry Smith - fdm - The fine `DM`
784888bdff64SToby Isaac 
7849bb7acecfSBarry Smith   Level: developer
785088bdff64SToby Isaac 
7851bb7acecfSBarry Smith   Note:
7852bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7853bb7acecfSBarry Smith 
78541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
785588bdff64SToby Isaac @*/
7856d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7857d71ae5a4SJacob Faibussowitsch {
785888bdff64SToby Isaac   PetscFunctionBegin;
785988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
786088bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
786189d734beSBarry Smith   if (dm == fdm) fdm = NULL;
78629566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
78639566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
786488bdff64SToby Isaac   dm->fineMesh = fdm;
78653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
786688bdff64SToby Isaac }
786788bdff64SToby Isaac 
7868a6ba4734SToby Isaac /*@C
7869bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7870a6ba4734SToby Isaac 
787120f4b53cSBarry Smith   Collective
7872783e2ec8SMatthew G. Knepley 
7873a6ba4734SToby Isaac   Input Parameters:
7874bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7875bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7876a6ba4734SToby Isaac . name     - The BC name
787745480ffeSMatthew G. Knepley . label    - The label defining constrained points
7878bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
787945480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7880a6ba4734SToby Isaac . field    - The field to constrain
788145480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7882a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7883a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
788456cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7885a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7886a6ba4734SToby Isaac 
788745480ffeSMatthew G. Knepley   Output Parameter:
788845480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
788945480ffeSMatthew G. Knepley 
7890a6ba4734SToby Isaac   Options Database Keys:
7891a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
7892a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7893a6ba4734SToby Isaac 
789420f4b53cSBarry Smith   Level: intermediate
789520f4b53cSBarry Smith 
7896bb7acecfSBarry Smith   Notes:
789737fdd005SBarry 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\:
789856cf3b9cSMatthew G. Knepley 
789920f4b53cSBarry Smith $ void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
790056cf3b9cSMatthew G. Knepley 
7901a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
790256cf3b9cSMatthew G. Knepley 
790320f4b53cSBarry Smith .vb
790420f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
790520f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
790620f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
790720f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
790820f4b53cSBarry Smith .ve
790956cf3b9cSMatthew G. Knepley + dim - the spatial dimension
791056cf3b9cSMatthew G. Knepley . Nf - the number of fields
791156cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
791256cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
791356cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
791456cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
791556cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
791656cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
791756cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
791856cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
791956cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
792056cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
792156cf3b9cSMatthew G. Knepley . t - current time
792256cf3b9cSMatthew G. Knepley . x - coordinates of the current point
792356cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
792456cf3b9cSMatthew G. Knepley . constants - constant parameters
792556cf3b9cSMatthew G. Knepley - bcval - output values at the current point
792656cf3b9cSMatthew G. Knepley 
79271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
7928a6ba4734SToby Isaac @*/
7929d71ae5a4SJacob 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)
7930d71ae5a4SJacob Faibussowitsch {
7931e5e52638SMatthew G. Knepley   PetscDS ds;
7932a6ba4734SToby Isaac 
7933a6ba4734SToby Isaac   PetscFunctionBegin;
7934a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7935783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
793645480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
793745480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
793845480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
793945480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
794001a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
79419566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7942799db056SMatthew G. Knepley   /* Complete label */
7943799db056SMatthew G. Knepley   if (label) {
7944799db056SMatthew G. Knepley     PetscObject  obj;
7945799db056SMatthew G. Knepley     PetscClassId id;
7946799db056SMatthew G. Knepley 
7947799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7948799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7949799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7950799db056SMatthew G. Knepley       DM plex;
7951799db056SMatthew G. Knepley 
7952799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7953799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7954799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7955799db056SMatthew G. Knepley     }
7956799db056SMatthew G. Knepley   }
79579566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
79583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7959a6ba4734SToby Isaac }
7960a6ba4734SToby Isaac 
796145480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7962d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
7963d71ae5a4SJacob Faibussowitsch {
7964e5e52638SMatthew G. Knepley   PetscDS     ds;
7965dff059c6SToby Isaac   DMBoundary *lastnext;
7966e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7967e6f8dbb6SToby Isaac 
7968e6f8dbb6SToby Isaac   PetscFunctionBegin;
79699566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7970e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
797147a1f5adSToby Isaac   if (dm->boundary) {
797247a1f5adSToby Isaac     DMBoundary next = dm->boundary;
797347a1f5adSToby Isaac 
797447a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
79753ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
797647a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
797747a1f5adSToby Isaac     while (next) {
797847a1f5adSToby Isaac       DMBoundary b = next;
797947a1f5adSToby Isaac 
798047a1f5adSToby Isaac       next = b->next;
79819566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7982a6ba4734SToby Isaac     }
798347a1f5adSToby Isaac     dm->boundary = NULL;
7984a6ba4734SToby Isaac   }
798547a1f5adSToby Isaac 
7986f4f49eeaSPierre Jolivet   lastnext = &dm->boundary;
7987e6f8dbb6SToby Isaac   while (dsbound) {
7988e6f8dbb6SToby Isaac     DMBoundary dmbound;
7989e6f8dbb6SToby Isaac 
79909566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7991e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
799245480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
799347a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7994dff059c6SToby Isaac     *lastnext = dmbound;
7995f4f49eeaSPierre Jolivet     lastnext  = &dmbound->next;
7996dff059c6SToby Isaac     dsbound   = dsbound->next;
7997a6ba4734SToby Isaac   }
79983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7999a6ba4734SToby Isaac }
8000a6ba4734SToby Isaac 
8001bb7acecfSBarry Smith /* TODO: missing manual page */
8002d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8003d71ae5a4SJacob Faibussowitsch {
8004b95f2879SToby Isaac   DMBoundary b;
8005a6ba4734SToby Isaac 
8006a6ba4734SToby Isaac   PetscFunctionBegin;
8007a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80084f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
8009a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
80109566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
8011b95f2879SToby Isaac   b = dm->boundary;
8012a6ba4734SToby Isaac   while (b && !(*isBd)) {
8013e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8014e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8015a6ba4734SToby Isaac     PetscInt   i;
8016a6ba4734SToby Isaac 
801745480ffeSMatthew G. Knepley     if (label) {
80189566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8019a6ba4734SToby Isaac     }
8020a6ba4734SToby Isaac     b = b->next;
8021a6ba4734SToby Isaac   }
80223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8023a6ba4734SToby Isaac }
80244d6f44ffSToby Isaac 
80254d6f44ffSToby Isaac /*@C
8026bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
8027a6e0b375SMatthew G. Knepley 
802820f4b53cSBarry Smith   Collective
80294d6f44ffSToby Isaac 
80304d6f44ffSToby Isaac   Input Parameters:
8031bb7acecfSBarry Smith + dm    - The `DM`
80320709b2feSToby Isaac . time  - The time
80334d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
80344d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
80354d6f44ffSToby Isaac - mode  - The insertion mode for values
80364d6f44ffSToby Isaac 
80374d6f44ffSToby Isaac   Output Parameter:
80384d6f44ffSToby Isaac . X - vector
80394d6f44ffSToby Isaac 
804020f4b53cSBarry Smith   Calling sequence of `funcs`:
80414d6f44ffSToby Isaac + dim  - The spatial dimension
80428ec8862eSJed Brown . time - The time at which to sample
80434d6f44ffSToby Isaac . x    - The coordinates
804477b739a6SMatthew Knepley . Nc   - The number of components
80454d6f44ffSToby Isaac . u    - The output field values
80464d6f44ffSToby Isaac - ctx  - optional user-defined function context
80474d6f44ffSToby Isaac 
80484d6f44ffSToby Isaac   Level: developer
80494d6f44ffSToby Isaac 
8050bb7acecfSBarry Smith   Developer Notes:
8051bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8052bb7acecfSBarry Smith 
8053bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8054bb7acecfSBarry Smith 
80551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
80564d6f44ffSToby Isaac @*/
8057a4e35b19SJacob 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)
8058d71ae5a4SJacob Faibussowitsch {
80594d6f44ffSToby Isaac   Vec localX;
80604d6f44ffSToby Isaac 
80614d6f44ffSToby Isaac   PetscFunctionBegin;
80624d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8063708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
80649566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8065f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
80669566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
80679566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
80689566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
80699566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
8070708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
80713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80724d6f44ffSToby Isaac }
80734d6f44ffSToby Isaac 
8074a6e0b375SMatthew G. Knepley /*@C
8075bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
8076a6e0b375SMatthew G. Knepley 
807720f4b53cSBarry Smith   Not Collective
8078a6e0b375SMatthew G. Knepley 
8079a6e0b375SMatthew G. Knepley   Input Parameters:
8080bb7acecfSBarry Smith + dm    - The `DM`
8081a6e0b375SMatthew G. Knepley . time  - The time
8082a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
8083a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8084a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
8085a6e0b375SMatthew G. Knepley 
8086a6e0b375SMatthew G. Knepley   Output Parameter:
8087a6e0b375SMatthew G. Knepley . localX - vector
8088a6e0b375SMatthew G. Knepley 
808920f4b53cSBarry Smith   Calling sequence of `funcs`:
8090a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8091a4e35b19SJacob Faibussowitsch . time - The current timestep
8092a6e0b375SMatthew G. Knepley . x    - The coordinates
809377b739a6SMatthew Knepley . Nc   - The number of components
8094a6e0b375SMatthew G. Knepley . u    - The output field values
8095a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8096a6e0b375SMatthew G. Knepley 
8097a6e0b375SMatthew G. Knepley   Level: developer
8098a6e0b375SMatthew G. Knepley 
8099bb7acecfSBarry Smith   Developer Notes:
8100bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8101bb7acecfSBarry Smith 
8102bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8103bb7acecfSBarry Smith 
81041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8105a6e0b375SMatthew G. Knepley @*/
8106a4e35b19SJacob 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)
8107d71ae5a4SJacob Faibussowitsch {
81084d6f44ffSToby Isaac   PetscFunctionBegin;
81094d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8110064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8111fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlocal, time, funcs, ctxs, mode, localX);
81123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81134d6f44ffSToby Isaac }
81144d6f44ffSToby Isaac 
8115a6e0b375SMatthew G. Knepley /*@C
8116bb7acecfSBarry 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.
8117a6e0b375SMatthew G. Knepley 
811820f4b53cSBarry Smith   Collective
8119a6e0b375SMatthew G. Knepley 
8120a6e0b375SMatthew G. Knepley   Input Parameters:
8121bb7acecfSBarry Smith + dm     - The `DM`
8122a6e0b375SMatthew G. Knepley . time   - The time
8123a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8124a4e35b19SJacob Faibussowitsch . ids    - The ids
8125a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8126a4e35b19SJacob Faibussowitsch . comps  - The components
8127bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8128a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8129bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8130a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8131a6e0b375SMatthew G. Knepley 
8132a6e0b375SMatthew G. Knepley   Output Parameter:
8133a6e0b375SMatthew G. Knepley . X - vector
8134a6e0b375SMatthew G. Knepley 
813520f4b53cSBarry Smith   Calling sequence of `funcs`:
8136a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8137a4e35b19SJacob Faibussowitsch . time - The current timestep
8138a6e0b375SMatthew G. Knepley . x    - The coordinates
813977b739a6SMatthew Knepley . Nc   - The number of components
8140a6e0b375SMatthew G. Knepley . u    - The output field values
8141a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8142a6e0b375SMatthew G. Knepley 
8143a6e0b375SMatthew G. Knepley   Level: developer
8144a6e0b375SMatthew G. Knepley 
8145bb7acecfSBarry Smith   Developer Notes:
8146bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8147bb7acecfSBarry Smith 
8148bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8149bb7acecfSBarry Smith 
81501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8151a6e0b375SMatthew G. Knepley @*/
8152a4e35b19SJacob 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)
8153d71ae5a4SJacob Faibussowitsch {
81542c53366bSMatthew G. Knepley   Vec localX;
81552c53366bSMatthew G. Knepley 
81562c53366bSMatthew G. Knepley   PetscFunctionBegin;
81572c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81589566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8159f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
81609566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
81619566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
81629566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
81639566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
81643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81652c53366bSMatthew G. Knepley }
81662c53366bSMatthew G. Knepley 
8167a6e0b375SMatthew G. Knepley /*@C
8168bb7acecfSBarry 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.
8169a6e0b375SMatthew G. Knepley 
817020f4b53cSBarry Smith   Not Collective
8171a6e0b375SMatthew G. Knepley 
8172a6e0b375SMatthew G. Knepley   Input Parameters:
8173bb7acecfSBarry Smith + dm     - The `DM`
8174a6e0b375SMatthew G. Knepley . time   - The time
8175bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8176a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8177a4e35b19SJacob Faibussowitsch . ids    - The ids
8178a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8179a4e35b19SJacob Faibussowitsch . comps  - The components
8180a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8181a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8182a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8183a6e0b375SMatthew G. Knepley 
8184a6e0b375SMatthew G. Knepley   Output Parameter:
8185a6e0b375SMatthew G. Knepley . localX - vector
8186a6e0b375SMatthew G. Knepley 
818720f4b53cSBarry Smith   Calling sequence of `funcs`:
8188a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8189a4e35b19SJacob Faibussowitsch . time - The current time
8190a6e0b375SMatthew G. Knepley . x    - The coordinates
819177b739a6SMatthew Knepley . Nc   - The number of components
8192a6e0b375SMatthew G. Knepley . u    - The output field values
8193a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8194a6e0b375SMatthew G. Knepley 
8195a6e0b375SMatthew G. Knepley   Level: developer
8196a6e0b375SMatthew G. Knepley 
8197bb7acecfSBarry Smith   Developer Notes:
8198bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8199bb7acecfSBarry Smith 
8200bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8201bb7acecfSBarry Smith 
82021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8203a6e0b375SMatthew G. Knepley @*/
8204a4e35b19SJacob 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)
8205d71ae5a4SJacob Faibussowitsch {
82064d6f44ffSToby Isaac   PetscFunctionBegin;
82074d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8208064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8209fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlabellocal, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);
82103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82114d6f44ffSToby Isaac }
82122716604bSToby Isaac 
8213a6e0b375SMatthew G. Knepley /*@C
8214bb7acecfSBarry 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.
8215a6e0b375SMatthew G. Knepley 
821620f4b53cSBarry Smith   Not Collective
8217a6e0b375SMatthew G. Knepley 
8218a6e0b375SMatthew G. Knepley   Input Parameters:
8219bb7acecfSBarry Smith + dm     - The `DM`
8220a6e0b375SMatthew G. Knepley . time   - The time
822120f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8222a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8223a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8224a6e0b375SMatthew G. Knepley 
8225a6e0b375SMatthew G. Knepley   Output Parameter:
8226a6e0b375SMatthew G. Knepley . localX - The output vector
8227a6e0b375SMatthew G. Knepley 
822820f4b53cSBarry Smith   Calling sequence of `funcs`:
8229a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8230a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8231a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8232a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8233a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8234a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8235a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8236a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8237a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8238a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8239a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8240a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8241a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8242a6e0b375SMatthew G. Knepley . t            - The current time
8243a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8244a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8245a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8246a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8247a6e0b375SMatthew G. Knepley 
8248bb7acecfSBarry Smith   Note:
8249bb7acecfSBarry 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.
8250bb7acecfSBarry 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
8251bb7acecfSBarry 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
8252a6e0b375SMatthew 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.
8253a6e0b375SMatthew G. Knepley 
8254a6e0b375SMatthew G. Knepley   Level: intermediate
8255a6e0b375SMatthew G. Knepley 
8256bb7acecfSBarry Smith   Developer Notes:
8257bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8258bb7acecfSBarry Smith 
8259bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8260bb7acecfSBarry Smith 
8261a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8262a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8263a6e0b375SMatthew G. Knepley @*/
8264a4e35b19SJacob 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)
8265d71ae5a4SJacob Faibussowitsch {
82668c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82678c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8268eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
82698c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8270fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlocal, time, localU, funcs, mode, localX);
82713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82728c6c5593SMatthew G. Knepley }
82738c6c5593SMatthew G. Knepley 
8274a6e0b375SMatthew G. Knepley /*@C
8275a6e0b375SMatthew 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.
8276a6e0b375SMatthew G. Knepley 
827720f4b53cSBarry Smith   Not Collective
8278a6e0b375SMatthew G. Knepley 
8279a6e0b375SMatthew G. Knepley   Input Parameters:
8280bb7acecfSBarry Smith + dm     - The `DM`
8281a6e0b375SMatthew G. Knepley . time   - The time
8282bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8283a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8284a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8285bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
828620f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8287a6e0b375SMatthew G. Knepley . localU - The input field vector
8288a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8289a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8290a6e0b375SMatthew G. Knepley 
8291a6e0b375SMatthew G. Knepley   Output Parameter:
8292a6e0b375SMatthew G. Knepley . localX - The output vector
8293a6e0b375SMatthew G. Knepley 
829420f4b53cSBarry Smith   Calling sequence of `funcs`:
8295a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8296a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8297a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8298a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8299a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8300a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8301a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8302a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8303a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8304a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8305a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8306a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8307a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8308a6e0b375SMatthew G. Knepley . t            - The current time
8309a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8310a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8311a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8312a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8313a6e0b375SMatthew G. Knepley 
8314bb7acecfSBarry Smith   Note:
8315bb7acecfSBarry 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.
8316bb7acecfSBarry 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
8317bb7acecfSBarry 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
8318a6e0b375SMatthew 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.
8319a6e0b375SMatthew G. Knepley 
8320a6e0b375SMatthew G. Knepley   Level: intermediate
8321a6e0b375SMatthew G. Knepley 
8322bb7acecfSBarry Smith   Developer Notes:
8323bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8324bb7acecfSBarry Smith 
8325bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8326bb7acecfSBarry Smith 
83271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8328a6e0b375SMatthew G. Knepley @*/
8329a4e35b19SJacob 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)
8330d71ae5a4SJacob Faibussowitsch {
83318c6c5593SMatthew G. Knepley   PetscFunctionBegin;
83328c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8333064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8334064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8335fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
83363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83378c6c5593SMatthew G. Knepley }
83388c6c5593SMatthew G. Knepley 
83392716604bSToby Isaac /*@C
8340d29d7c6eSMatthew 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.
8341d29d7c6eSMatthew G. Knepley 
834220f4b53cSBarry Smith   Not Collective
8343d29d7c6eSMatthew G. Knepley 
8344d29d7c6eSMatthew G. Knepley   Input Parameters:
8345bb7acecfSBarry Smith + dm     - The `DM`
8346d29d7c6eSMatthew G. Knepley . time   - The time
8347bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8348d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8349d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8350bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
835120f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8352d29d7c6eSMatthew G. Knepley . U      - The input field vector
8353d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8354d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8355d29d7c6eSMatthew G. Knepley 
8356d29d7c6eSMatthew G. Knepley   Output Parameter:
8357d29d7c6eSMatthew G. Knepley . X - The output vector
8358d29d7c6eSMatthew G. Knepley 
835920f4b53cSBarry Smith   Calling sequence of `funcs`:
8360d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8361d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8362d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8363d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8364d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8365d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
8366d29d7c6eSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8367d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8368d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8369d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8370d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8371d29d7c6eSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8372d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8373d29d7c6eSMatthew G. Knepley . t            - The current time
8374d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8375d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8376d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8377d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8378d29d7c6eSMatthew G. Knepley 
8379bb7acecfSBarry Smith   Note:
8380bb7acecfSBarry 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.
8381bb7acecfSBarry 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
8382bb7acecfSBarry 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
8383d29d7c6eSMatthew 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.
8384d29d7c6eSMatthew G. Knepley 
8385d29d7c6eSMatthew G. Knepley   Level: intermediate
8386d29d7c6eSMatthew G. Knepley 
8387bb7acecfSBarry Smith   Developer Notes:
8388bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8389bb7acecfSBarry Smith 
8390bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8391bb7acecfSBarry Smith 
83921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8393d29d7c6eSMatthew G. Knepley @*/
8394a4e35b19SJacob 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)
8395d71ae5a4SJacob Faibussowitsch {
8396d29d7c6eSMatthew G. Knepley   DM  dmIn;
8397d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8398d29d7c6eSMatthew G. Knepley 
8399d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8400d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8401d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8402d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8403d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8404f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
840572fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
840672fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8407d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8408d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8409d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8410d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8411d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
84123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8413d29d7c6eSMatthew G. Knepley }
8414d29d7c6eSMatthew G. Knepley 
8415d29d7c6eSMatthew G. Knepley /*@C
8416ece3a9fcSMatthew 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.
8417ece3a9fcSMatthew G. Knepley 
841820f4b53cSBarry Smith   Not Collective
8419ece3a9fcSMatthew G. Knepley 
8420ece3a9fcSMatthew G. Knepley   Input Parameters:
8421bb7acecfSBarry Smith + dm     - The `DM`
8422ece3a9fcSMatthew G. Knepley . time   - The time
8423bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8424ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8425ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8426bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
842720f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8428ece3a9fcSMatthew G. Knepley . localU - The input field vector
8429ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8430ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8431ece3a9fcSMatthew G. Knepley 
8432ece3a9fcSMatthew G. Knepley   Output Parameter:
8433ece3a9fcSMatthew G. Knepley . localX - The output vector
8434ece3a9fcSMatthew G. Knepley 
843520f4b53cSBarry Smith   Calling sequence of `funcs`:
8436ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8437ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8438ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8439ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8440ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8441ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
8442ece3a9fcSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8443ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8444ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8445ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8446ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8447ece3a9fcSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8448ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8449ece3a9fcSMatthew G. Knepley . t            - The current time
8450ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8451ece3a9fcSMatthew G. Knepley . n            - The face normal
8452ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8453ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8454ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8455ece3a9fcSMatthew G. Knepley 
8456ece3a9fcSMatthew G. Knepley   Note:
8457bb7acecfSBarry 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.
8458bb7acecfSBarry 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
8459bb7acecfSBarry 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
8460ece3a9fcSMatthew 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.
8461ece3a9fcSMatthew G. Knepley 
8462ece3a9fcSMatthew G. Knepley   Level: intermediate
8463ece3a9fcSMatthew G. Knepley 
8464bb7acecfSBarry Smith   Developer Notes:
8465bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8466bb7acecfSBarry Smith 
8467bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8468bb7acecfSBarry Smith 
84691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8470ece3a9fcSMatthew G. Knepley @*/
8471a4e35b19SJacob 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)
8472d71ae5a4SJacob Faibussowitsch {
8473ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8474ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8475064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8476064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8477fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectbdfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
84783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8479ece3a9fcSMatthew G. Knepley }
8480ece3a9fcSMatthew G. Knepley 
8481ece3a9fcSMatthew G. Knepley /*@C
84822716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
84832716604bSToby Isaac 
848420f4b53cSBarry Smith   Collective
8485bb7acecfSBarry Smith 
84862716604bSToby Isaac   Input Parameters:
8487bb7acecfSBarry Smith + dm    - The `DM`
84880709b2feSToby Isaac . time  - The time
84892716604bSToby Isaac . funcs - The functions to evaluate for each field component
84902716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8491574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84922716604bSToby Isaac 
84932716604bSToby Isaac   Output Parameter:
84942716604bSToby Isaac . diff - The diff ||u - u_h||_2
84952716604bSToby Isaac 
84962716604bSToby Isaac   Level: developer
84972716604bSToby Isaac 
8498bb7acecfSBarry Smith   Developer Notes:
8499bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8500bb7acecfSBarry Smith 
8501bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8502bb7acecfSBarry Smith 
85031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
85042716604bSToby Isaac @*/
8505d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8506d71ae5a4SJacob Faibussowitsch {
85072716604bSToby Isaac   PetscFunctionBegin;
85082716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8509b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8510fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2diff, time, funcs, ctxs, X, diff);
85113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
85122716604bSToby Isaac }
8513b698f381SToby Isaac 
8514b698f381SToby Isaac /*@C
8515b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8516b698f381SToby Isaac 
851720f4b53cSBarry Smith   Collective
8518d083f849SBarry Smith 
8519b698f381SToby Isaac   Input Parameters:
8520bb7acecfSBarry Smith + dm    - The `DM`
8521a4e35b19SJacob Faibussowitsch . time  - The time
8522b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8523b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8524574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8525b698f381SToby Isaac - n     - The vector to project along
8526b698f381SToby Isaac 
8527b698f381SToby Isaac   Output Parameter:
8528b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8529b698f381SToby Isaac 
8530b698f381SToby Isaac   Level: developer
8531b698f381SToby Isaac 
8532bb7acecfSBarry Smith   Developer Notes:
8533bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8534bb7acecfSBarry Smith 
8535bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8536bb7acecfSBarry Smith 
85371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8538b698f381SToby Isaac @*/
8539d71ae5a4SJacob 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)
8540d71ae5a4SJacob Faibussowitsch {
8541b698f381SToby Isaac   PetscFunctionBegin;
8542b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8543b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8544fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2gradientdiff, time, funcs, ctxs, X, n, diff);
85453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8546b698f381SToby Isaac }
8547b698f381SToby Isaac 
85482a16baeaSToby Isaac /*@C
85492a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
85502a16baeaSToby Isaac 
855120f4b53cSBarry Smith   Collective
8552d083f849SBarry Smith 
85532a16baeaSToby Isaac   Input Parameters:
8554bb7acecfSBarry Smith + dm    - The `DM`
85552a16baeaSToby Isaac . time  - The time
85562a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
85572a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8558574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
85592a16baeaSToby Isaac 
85602a16baeaSToby Isaac   Output Parameter:
85612a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
85622a16baeaSToby Isaac 
85632a16baeaSToby Isaac   Level: developer
85642a16baeaSToby Isaac 
8565bb7acecfSBarry Smith   Developer Notes:
8566bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8567bb7acecfSBarry Smith 
8568bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8569bb7acecfSBarry Smith 
857042747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
85712a16baeaSToby Isaac @*/
8572d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8573d71ae5a4SJacob Faibussowitsch {
85742a16baeaSToby Isaac   PetscFunctionBegin;
85752a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85762a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8577fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2fielddiff, time, funcs, ctxs, X, diff);
85783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
85792a16baeaSToby Isaac }
85802a16baeaSToby Isaac 
8581df0b854cSToby Isaac /*@C
8582bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8583502a2867SDave May 
8584502a2867SDave May   Not Collective
8585502a2867SDave May 
8586502a2867SDave May   Input Parameter:
8587bb7acecfSBarry Smith . dm - The `DM`
8588502a2867SDave May 
85890a19bb7dSprj-   Output Parameters:
85900a19bb7dSprj- + nranks - the number of neighbours
85910a19bb7dSprj- - ranks  - the neighbors ranks
8592502a2867SDave May 
85939bdbcad8SBarry Smith   Level: beginner
85949bdbcad8SBarry Smith 
8595bb7acecfSBarry Smith   Note:
8596bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8597502a2867SDave May 
85981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8599502a2867SDave May @*/
8600d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8601d71ae5a4SJacob Faibussowitsch {
8602502a2867SDave May   PetscFunctionBegin;
8603502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8604fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, getneighbors, nranks, ranks);
86053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8606502a2867SDave May }
8607502a2867SDave May 
8608531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8609531c7667SBarry Smith 
8610531c7667SBarry Smith /*
8611531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
86122b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8613531c7667SBarry Smith */
861466976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8615d71ae5a4SJacob Faibussowitsch {
8616531c7667SBarry Smith   PetscFunctionBegin;
8617531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8618531c7667SBarry Smith     Vec x1local;
8619531c7667SBarry Smith     DM  dm;
86209566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
86217a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
86229566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
86239566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
86249566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8625531c7667SBarry Smith     x1 = x1local;
8626531c7667SBarry Smith   }
86279566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8628531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8629531c7667SBarry Smith     DM dm;
86309566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
86319566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8632531c7667SBarry Smith   }
86333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8634531c7667SBarry Smith }
8635531c7667SBarry Smith 
8636531c7667SBarry Smith /*@
8637bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8638531c7667SBarry Smith 
8639a4e35b19SJacob Faibussowitsch   Input Parameters:
8640a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8641a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8642531c7667SBarry Smith 
86439bdbcad8SBarry Smith   Level: advanced
86449bdbcad8SBarry Smith 
864560225df5SJacob Faibussowitsch   Developer Notes:
8646bb7acecfSBarry Smith   this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8647531c7667SBarry Smith 
86481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8649531c7667SBarry Smith @*/
8650d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8651d71ae5a4SJacob Faibussowitsch {
8652531c7667SBarry Smith   PetscFunctionBegin;
8653531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
86543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8655531c7667SBarry Smith }
86568320bc6fSPatrick Sanan 
86578320bc6fSPatrick Sanan /*@
8658bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
86598320bc6fSPatrick Sanan 
86608320bc6fSPatrick Sanan   Collective
86618320bc6fSPatrick Sanan 
86628320bc6fSPatrick Sanan   Input Parameters:
8663bb7acecfSBarry Smith + dm1 - the first `DM`
8664bb7acecfSBarry Smith - dm2 - the second `DM`
86658320bc6fSPatrick Sanan 
86668320bc6fSPatrick Sanan   Output Parameters:
8667bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8668bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
86698320bc6fSPatrick Sanan 
867020f4b53cSBarry Smith   Level: advanced
867120f4b53cSBarry Smith 
86728320bc6fSPatrick Sanan   Notes:
8673bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
86743d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
86758320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8676bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
86773d862458SPatrick Sanan   decomposition, but hold different data.
86788320bc6fSPatrick Sanan 
86798320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8680bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
86818320bc6fSPatrick Sanan 
8682bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
86838320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
86848320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8685bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
86868320bc6fSPatrick Sanan 
8687bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8688bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
86898320bc6fSPatrick Sanan .vb
86908320bc6fSPatrick Sanan   ...
86919566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
86928320bc6fSPatrick Sanan   if (set && compatible)  {
86939566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
86949566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
86959566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
86968320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
86978320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
86988320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
86998320bc6fSPatrick Sanan       }
87008320bc6fSPatrick Sanan     }
87019566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
87029566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
87038320bc6fSPatrick Sanan   } else {
87048320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
87058320bc6fSPatrick Sanan   }
87068320bc6fSPatrick Sanan   ...
87078320bc6fSPatrick Sanan .ve
87088320bc6fSPatrick Sanan 
8709bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
87108320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
87118320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
87128320bc6fSPatrick Sanan   always check the "set" output parameter.
87138320bc6fSPatrick Sanan 
8714bb7acecfSBarry Smith   A `DM` is always compatible with itself.
87158320bc6fSPatrick Sanan 
8716bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
87178320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
87188320bc6fSPatrick Sanan   incompatible.
87198320bc6fSPatrick Sanan 
87208320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8721bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
87228320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
87238320bc6fSPatrick Sanan 
872460225df5SJacob Faibussowitsch   Developer Notes:
8725bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
87263d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8727a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8728bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
87298320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
87303d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8731bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
87328320bc6fSPatrick Sanan 
87331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
87348320bc6fSPatrick Sanan @*/
8735d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8736d71ae5a4SJacob Faibussowitsch {
87378320bc6fSPatrick Sanan   PetscMPIInt compareResult;
87388320bc6fSPatrick Sanan   DMType      type, type2;
87398320bc6fSPatrick Sanan   PetscBool   sameType;
87408320bc6fSPatrick Sanan 
87418320bc6fSPatrick Sanan   PetscFunctionBegin;
8742a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
87438320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
87448320bc6fSPatrick Sanan 
87458320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8746a5bc1bf3SBarry Smith   if (dm1 == dm2) {
87478320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
87488320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
87493ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
87508320bc6fSPatrick Sanan   }
87518320bc6fSPatrick Sanan 
87528320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
87538320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
87548320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
87558320bc6fSPatrick Sanan      determined by the implementation-specific logic */
87569566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
87578320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
87588320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
87598320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
87603ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
87618320bc6fSPatrick Sanan   }
87628320bc6fSPatrick Sanan 
87638320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8764a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8765dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
87663ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
87678320bc6fSPatrick Sanan   }
87688320bc6fSPatrick Sanan 
8769a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
87708320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
87719566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
87729566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
87739566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
87748320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8775dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
87768320bc6fSPatrick Sanan   } else {
87778320bc6fSPatrick Sanan     *set = PETSC_FALSE;
87788320bc6fSPatrick Sanan   }
87793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
87808320bc6fSPatrick Sanan }
8781c0f0dcc3SMatthew G. Knepley 
8782c0f0dcc3SMatthew G. Knepley /*@C
8783bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8784c0f0dcc3SMatthew G. Knepley 
878520f4b53cSBarry Smith   Logically Collective
8786c0f0dcc3SMatthew G. Knepley 
8787c0f0dcc3SMatthew G. Knepley   Input Parameters:
878860225df5SJacob Faibussowitsch + dm             - the `DM`
8789c0f0dcc3SMatthew G. Knepley . f              - the monitor function
879020f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
879120f4b53cSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`)
8792c0f0dcc3SMatthew G. Knepley 
879320f4b53cSBarry Smith   Options Database Key:
879460225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8795c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8796c0f0dcc3SMatthew G. Knepley 
87979bdbcad8SBarry Smith   Level: intermediate
87989bdbcad8SBarry Smith 
8799bb7acecfSBarry Smith   Note:
8800c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8801bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8802c0f0dcc3SMatthew G. Knepley   order in which they were set.
8803c0f0dcc3SMatthew G. Knepley 
880460225df5SJacob Faibussowitsch   Fortran Notes:
8805bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8806bb7acecfSBarry Smith 
880760225df5SJacob Faibussowitsch   Developer Notes:
8808bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8809c0f0dcc3SMatthew G. Knepley 
88101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8811c0f0dcc3SMatthew G. Knepley @*/
8812d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8813d71ae5a4SJacob Faibussowitsch {
8814c0f0dcc3SMatthew G. Knepley   PetscInt m;
8815c0f0dcc3SMatthew G. Knepley 
8816c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8817c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8818c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8819c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8820c0f0dcc3SMatthew G. Knepley 
88219566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
88223ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8823c0f0dcc3SMatthew G. Knepley   }
88247a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8825c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8826c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8827c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
88283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8829c0f0dcc3SMatthew G. Knepley }
8830c0f0dcc3SMatthew G. Knepley 
8831c0f0dcc3SMatthew G. Knepley /*@
8832bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8833c0f0dcc3SMatthew G. Knepley 
883420f4b53cSBarry Smith   Logically Collective
8835c0f0dcc3SMatthew G. Knepley 
8836c0f0dcc3SMatthew G. Knepley   Input Parameter:
8837c0f0dcc3SMatthew G. Knepley . dm - the DM
8838c0f0dcc3SMatthew G. Knepley 
8839c0f0dcc3SMatthew G. Knepley   Options Database Key:
8840c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8841bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8842c0f0dcc3SMatthew G. Knepley   set via the options database
8843c0f0dcc3SMatthew G. Knepley 
88449bdbcad8SBarry Smith   Level: intermediate
88459bdbcad8SBarry Smith 
8846bb7acecfSBarry Smith   Note:
8847bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8848c0f0dcc3SMatthew G. Knepley 
88491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8850c0f0dcc3SMatthew G. Knepley @*/
8851d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8852d71ae5a4SJacob Faibussowitsch {
8853c0f0dcc3SMatthew G. Knepley   PetscInt m;
8854c0f0dcc3SMatthew G. Knepley 
8855c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8856c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8857c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
88589566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8859c0f0dcc3SMatthew G. Knepley   }
8860c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
88613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8862c0f0dcc3SMatthew G. Knepley }
8863c0f0dcc3SMatthew G. Knepley 
8864c0f0dcc3SMatthew G. Knepley /*@C
8865c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8866c0f0dcc3SMatthew G. Knepley 
886720f4b53cSBarry Smith   Collective
8868c0f0dcc3SMatthew G. Knepley 
8869c0f0dcc3SMatthew G. Knepley   Input Parameters:
8870bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
8871c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
8872c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
8873c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
8874c0f0dcc3SMatthew G. Knepley . monitor      - the monitor function
8875bb7acecfSBarry 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
8876c0f0dcc3SMatthew G. Knepley 
8877c0f0dcc3SMatthew G. Knepley   Output Parameter:
8878c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8879c0f0dcc3SMatthew G. Knepley 
8880c0f0dcc3SMatthew G. Knepley   Level: developer
8881c0f0dcc3SMatthew G. Knepley 
88821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8883db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
888460225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
8885db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8886c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8887db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8888bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8889c0f0dcc3SMatthew G. Knepley @*/
8890d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8891d71ae5a4SJacob Faibussowitsch {
8892c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8893c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8894c0f0dcc3SMatthew G. Knepley 
8895c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8896c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88979566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8898c0f0dcc3SMatthew G. Knepley   if (*flg) {
8899c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8900c0f0dcc3SMatthew G. Knepley 
89019566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
8902cd791dc2SBarry Smith     PetscCall(PetscOptionsRestoreViewer(&viewer));
89039566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
89049566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8905c0f0dcc3SMatthew G. Knepley   }
89063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8907c0f0dcc3SMatthew G. Knepley }
8908c0f0dcc3SMatthew G. Knepley 
8909c0f0dcc3SMatthew G. Knepley /*@
8910c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
8911c0f0dcc3SMatthew G. Knepley 
891220f4b53cSBarry Smith   Collective
8913c0f0dcc3SMatthew G. Knepley 
89142fe279fdSBarry Smith   Input Parameter:
8915bb7acecfSBarry Smith . dm - The `DM`
8916c0f0dcc3SMatthew G. Knepley 
8917c0f0dcc3SMatthew G. Knepley   Level: developer
8918c0f0dcc3SMatthew G. Knepley 
8919a4e35b19SJacob Faibussowitsch   Developer Notes:
8920a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
8921a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
8922a4e35b19SJacob Faibussowitsch   concept of discretization.
8923bb7acecfSBarry Smith 
89241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8925c0f0dcc3SMatthew G. Knepley @*/
8926d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8927d71ae5a4SJacob Faibussowitsch {
8928c0f0dcc3SMatthew G. Knepley   PetscInt m;
8929c0f0dcc3SMatthew G. Knepley 
8930c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
89313ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
8932c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
893348a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
89343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8935c0f0dcc3SMatthew G. Knepley }
89362e4af2aeSMatthew G. Knepley 
89372e4af2aeSMatthew G. Knepley /*@
8938bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
89392e4af2aeSMatthew G. Knepley 
894020f4b53cSBarry Smith   Collective
89412e4af2aeSMatthew G. Knepley 
89422e4af2aeSMatthew G. Knepley   Input Parameters:
8943bb7acecfSBarry Smith + dm  - The `DM`
89446b867d5aSJose E. Roman - sol - The solution vector
89452e4af2aeSMatthew G. Knepley 
89466b867d5aSJose E. Roman   Input/Output Parameter:
894720f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
89486b867d5aSJose E. Roman            contains the error in each field
89496b867d5aSJose E. Roman 
89506b867d5aSJose E. Roman   Output Parameter:
895120f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
895220f4b53cSBarry Smith 
895320f4b53cSBarry Smith   Level: developer
89542e4af2aeSMatthew G. Knepley 
8955bb7acecfSBarry Smith   Note:
8956bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
89572e4af2aeSMatthew G. Knepley 
89581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
89592e4af2aeSMatthew G. Knepley @*/
8960d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
8961d71ae5a4SJacob Faibussowitsch {
89622e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
89632e4af2aeSMatthew G. Knepley   void    **ctxs;
89642e4af2aeSMatthew G. Knepley   PetscReal time;
89652e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
89662e4af2aeSMatthew G. Knepley 
89672e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
89689566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
89699566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
89709566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
89712e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
89722e4af2aeSMatthew G. Knepley     PetscDS         ds;
89732e4af2aeSMatthew G. Knepley     DMLabel         label;
89742e4af2aeSMatthew G. Knepley     IS              fieldIS;
89752e4af2aeSMatthew G. Knepley     const PetscInt *fields;
89762e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
89772e4af2aeSMatthew G. Knepley 
897807218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
89799566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
89809566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
89812e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
89822e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
89839566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
89842e4af2aeSMatthew G. Knepley     }
89859566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
89862e4af2aeSMatthew G. Knepley   }
8987ad540459SPierre 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);
89889566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
89899566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
89902e4af2aeSMatthew G. Knepley   if (errorVec) {
89912e4af2aeSMatthew G. Knepley     DM             edm;
89922e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
89932e4af2aeSMatthew G. Knepley     PetscBool      simplex;
89942e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
89952e4af2aeSMatthew G. Knepley 
89969566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
89979566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
89989566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
89999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
90002e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
90019566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
90022e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
90032e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
90042e4af2aeSMatthew G. Knepley       PetscQuadrature q;
90052e4af2aeSMatthew G. Knepley       const char     *name;
90062e4af2aeSMatthew G. Knepley 
90079566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
90089566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
90099566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
90109566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
90119566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
90129566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
90139566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
90149566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
90152e4af2aeSMatthew G. Knepley     }
90169566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
90172e4af2aeSMatthew G. Knepley 
90189566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
90199566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
90209566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
90219566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
90222e4af2aeSMatthew G. Knepley   }
90239566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
90243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90252e4af2aeSMatthew G. Knepley }
90269a2a23afSMatthew G. Knepley 
90279a2a23afSMatthew G. Knepley /*@
9028bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
90299a2a23afSMatthew G. Knepley 
903020f4b53cSBarry Smith   Not Collective
90319a2a23afSMatthew G. Knepley 
90329a2a23afSMatthew G. Knepley   Input Parameter:
9033bb7acecfSBarry Smith . dm - The `DM`
90349a2a23afSMatthew G. Knepley 
90359a2a23afSMatthew G. Knepley   Output Parameter:
9036a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
90379a2a23afSMatthew G. Knepley 
90389a2a23afSMatthew G. Knepley   Level: advanced
90399a2a23afSMatthew G. Knepley 
9040e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
90419a2a23afSMatthew G. Knepley @*/
9042d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
9043d71ae5a4SJacob Faibussowitsch {
90449a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90459a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90469566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
90473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90489a2a23afSMatthew G. Knepley }
90499a2a23afSMatthew G. Knepley 
90509a2a23afSMatthew G. Knepley /*@
9051ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
90529a2a23afSMatthew G. Knepley 
905320f4b53cSBarry Smith   Not Collective
90549a2a23afSMatthew G. Knepley 
90559a2a23afSMatthew G. Knepley   Input Parameters:
9056bb7acecfSBarry Smith + dm    - The `DM`
9057bb7acecfSBarry Smith . label - The `DMLabel`
9058ac17215fSMatthew G. Knepley . value - The label value indicating the region
9059ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
90609a2a23afSMatthew G. Knepley 
90619a2a23afSMatthew G. Knepley   Output Parameter:
9062bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
90639a2a23afSMatthew G. Knepley 
90649bdbcad8SBarry Smith   Level: advanced
90659bdbcad8SBarry Smith 
9066bb7acecfSBarry Smith   Note:
9067bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
906804c51a94SMatthew G. Knepley 
9069e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
90709a2a23afSMatthew G. Knepley @*/
9071d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
9072d71ae5a4SJacob Faibussowitsch {
9073ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
907404c51a94SMatthew G. Knepley   PetscBool       has;
90759a2a23afSMatthew G. Knepley 
90769a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90779a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90789a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
90799a2a23afSMatthew G. Knepley   key.label = label;
90809a2a23afSMatthew G. Knepley   key.value = value;
9081ac17215fSMatthew G. Knepley   key.part  = part;
90829566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
90839566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
90849566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
90853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90869a2a23afSMatthew G. Knepley }
90879a2a23afSMatthew G. Knepley 
90889a2a23afSMatthew G. Knepley /*@
9089bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
90909a2a23afSMatthew G. Knepley 
909120f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
90929a2a23afSMatthew G. Knepley 
90939a2a23afSMatthew G. Knepley   Input Parameters:
9094bb7acecfSBarry Smith + dm    - The `DM`
9095bb7acecfSBarry Smith . label - The `DMLabel`
90969a2a23afSMatthew G. Knepley . value - The label value indicating the region
9097ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
9098bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
90999a2a23afSMatthew G. Knepley 
91009a2a23afSMatthew G. Knepley   Level: advanced
91019a2a23afSMatthew G. Knepley 
9102e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
91039a2a23afSMatthew G. Knepley @*/
9104d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9105d71ae5a4SJacob Faibussowitsch {
91069a2a23afSMatthew G. Knepley   Vec             old;
91079a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
91089a2a23afSMatthew G. Knepley 
91099a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91109a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91119a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
91129a2a23afSMatthew G. Knepley   key.label = label;
91139a2a23afSMatthew G. Knepley   key.value = value;
9114ac17215fSMatthew G. Knepley   key.part  = part;
91159566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
91169566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
91179566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
91189566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
9119d705d0eaSStefano Zampini   PetscCall(VecDestroy(&old));
91203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91219a2a23afSMatthew G. Knepley }
91229a2a23afSMatthew G. Knepley 
91239a2a23afSMatthew G. Knepley /*@C
9124bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
91259a2a23afSMatthew G. Knepley 
912620f4b53cSBarry Smith   Not Collective
91279a2a23afSMatthew G. Knepley 
91289a2a23afSMatthew G. Knepley   Input Parameter:
9129bb7acecfSBarry Smith . dm - The `DM`
91309a2a23afSMatthew G. Knepley 
91319a2a23afSMatthew G. Knepley   Output Parameters:
9132bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9133bb7acecfSBarry Smith . values - The label values for each `Vec`
9134bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
91359a2a23afSMatthew G. Knepley 
91369bdbcad8SBarry Smith   Level: advanced
91379bdbcad8SBarry Smith 
9138bb7acecfSBarry Smith   Note:
9139bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
91409a2a23afSMatthew G. Knepley 
9141e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
91429a2a23afSMatthew G. Knepley @*/
9143d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9144d71ae5a4SJacob Faibussowitsch {
91459a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
91469a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
91479a2a23afSMatthew G. Knepley 
91489a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91499a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91504f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
91514f572ea9SToby Isaac   PetscAssertPointer(values, 3);
91524f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
91539566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
91549566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
91559566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
91569371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
91579371c9d4SSatish Balay     labels[i] = keys[i].label;
91589371c9d4SSatish Balay     values[i] = keys[i].value;
91599371c9d4SSatish Balay     parts[i]  = keys[i].part;
91609371c9d4SSatish Balay   }
91619566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
91623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91639a2a23afSMatthew G. Knepley }
91649a2a23afSMatthew G. Knepley 
91659a2a23afSMatthew G. Knepley /*@
9166bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
91679a2a23afSMatthew G. Knepley 
916820f4b53cSBarry Smith   Not Collective
91699a2a23afSMatthew G. Knepley 
91709a2a23afSMatthew G. Knepley   Input Parameter:
9171bb7acecfSBarry Smith . dm - The `DM`
91729a2a23afSMatthew G. Knepley 
91739a2a23afSMatthew G. Knepley   Output Parameter:
9174bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
91759a2a23afSMatthew G. Knepley 
91769a2a23afSMatthew G. Knepley   Level: advanced
91779a2a23afSMatthew G. Knepley 
9178bb7acecfSBarry Smith   Note:
9179bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9180bb7acecfSBarry Smith 
9181e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
91829a2a23afSMatthew G. Knepley @*/
9183d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9184d71ae5a4SJacob Faibussowitsch {
91859a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91869a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9187d705d0eaSStefano Zampini   PetscValidHeaderSpecific(dmNew, DM_CLASSID, 2);
9188d705d0eaSStefano Zampini   if (dm == dmNew) PetscFunctionReturn(PETSC_SUCCESS);
9189e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(dmNew));
9190e4d5475eSStefano Zampini 
9191e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
91929566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
9193d705d0eaSStefano Zampini   {
9194d705d0eaSStefano Zampini     Vec     *auxData;
9195d705d0eaSStefano Zampini     PetscInt n, i, off = 0;
9196d705d0eaSStefano Zampini 
9197d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetSize(dmNew->auxData, &n));
9198d705d0eaSStefano Zampini     PetscCall(PetscMalloc1(n, &auxData));
9199d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetVals(dmNew->auxData, &off, auxData));
9200d705d0eaSStefano Zampini     for (i = 0; i < n; ++i) PetscCall(PetscObjectReference((PetscObject)auxData[i]));
9201d705d0eaSStefano Zampini     PetscCall(PetscFree(auxData));
9202e4d5475eSStefano Zampini   }
9203e4d5475eSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
9204e4d5475eSStefano Zampini }
9205e4d5475eSStefano Zampini 
9206e4d5475eSStefano Zampini /*@
9207e4d5475eSStefano Zampini   DMClearAuxiliaryVec - Destroys the auxiliary vector information and creates a new empty one
9208e4d5475eSStefano Zampini 
9209e4d5475eSStefano Zampini   Not Collective
9210e4d5475eSStefano Zampini 
9211e4d5475eSStefano Zampini   Input Parameter:
9212e4d5475eSStefano Zampini . dm - The `DM`
9213e4d5475eSStefano Zampini 
9214e4d5475eSStefano Zampini   Level: advanced
9215e4d5475eSStefano Zampini 
9216e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMCopyAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
9217e4d5475eSStefano Zampini @*/
9218e4d5475eSStefano Zampini PetscErrorCode DMClearAuxiliaryVec(DM dm)
9219e4d5475eSStefano Zampini {
9220e4d5475eSStefano Zampini   Vec     *auxData;
9221e4d5475eSStefano Zampini   PetscInt n, i, off = 0;
9222e4d5475eSStefano Zampini 
9223e4d5475eSStefano Zampini   PetscFunctionBegin;
9224e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetSize(dm->auxData, &n));
9225d705d0eaSStefano Zampini   PetscCall(PetscMalloc1(n, &auxData));
9226e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetVals(dm->auxData, &off, auxData));
9227d705d0eaSStefano Zampini   for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
9228d705d0eaSStefano Zampini   PetscCall(PetscFree(auxData));
9229e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dm->auxData));
9230e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxCreate(&dm->auxData));
92313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92329a2a23afSMatthew G. Knepley }
9233b5a892a1SMatthew G. Knepley 
9234b5a892a1SMatthew G. Knepley /*@C
9235bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9236b5a892a1SMatthew G. Knepley 
923720f4b53cSBarry Smith   Not Collective
9238b5a892a1SMatthew G. Knepley 
9239b5a892a1SMatthew G. Knepley   Input Parameters:
9240bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9241b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9242b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9243b5a892a1SMatthew G. Knepley 
9244b5a892a1SMatthew G. Knepley   Output Parameters:
9245bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9246b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9247b5a892a1SMatthew G. Knepley 
9248b5a892a1SMatthew G. Knepley   Level: advanced
9249b5a892a1SMatthew G. Knepley 
9250bb7acecfSBarry Smith   Note:
9251bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9252bb7acecfSBarry Smith 
925385036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9254bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9255bb7acecfSBarry Smith 
9256bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9257bb7acecfSBarry Smith 
92581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9259b5a892a1SMatthew G. Knepley @*/
9260d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9261d71ae5a4SJacob Faibussowitsch {
9262b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
926385036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9264b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9265b5a892a1SMatthew G. Knepley 
9266b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92679371c9d4SSatish Balay   if (!nO) {
92689371c9d4SSatish Balay     *ornt  = 0;
92699371c9d4SSatish Balay     *found = PETSC_TRUE;
92703ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
92719371c9d4SSatish Balay   }
9272b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
927385036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
9274b5a892a1SMatthew G. Knepley 
92759371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
92769371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
92779371c9d4SSatish Balay     if (c == cS) {
92789371c9d4SSatish Balay       *ornt = o;
92799371c9d4SSatish Balay       break;
92809371c9d4SSatish Balay     }
9281b5a892a1SMatthew G. Knepley   }
9282b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
92833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9284b5a892a1SMatthew G. Knepley }
9285b5a892a1SMatthew G. Knepley 
9286b5a892a1SMatthew G. Knepley /*@C
9287bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9288b5a892a1SMatthew G. Knepley 
928920f4b53cSBarry Smith   Not Collective
9290b5a892a1SMatthew G. Knepley 
9291b5a892a1SMatthew G. Knepley   Input Parameters:
9292bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9293b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9294b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9295b5a892a1SMatthew G. Knepley 
92962fe279fdSBarry Smith   Output Parameter:
9297bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9298b5a892a1SMatthew G. Knepley 
9299b5a892a1SMatthew G. Knepley   Level: advanced
9300b5a892a1SMatthew G. Knepley 
9301bb7acecfSBarry Smith   Note:
9302bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9303bb7acecfSBarry Smith 
930460225df5SJacob Faibussowitsch   Developer Notes:
9305bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9306bb7acecfSBarry Smith 
93071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9308b5a892a1SMatthew G. Knepley @*/
9309d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9310d71ae5a4SJacob Faibussowitsch {
9311b5a892a1SMatthew G. Knepley   PetscBool found;
9312b5a892a1SMatthew G. Knepley 
9313b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93149566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
93157a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
93163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9317b5a892a1SMatthew G. Knepley }
9318b5a892a1SMatthew G. Knepley 
9319b5a892a1SMatthew G. Knepley /*@C
9320bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9321b5a892a1SMatthew G. Knepley 
932220f4b53cSBarry Smith   Not Collective
9323b5a892a1SMatthew G. Knepley 
9324b5a892a1SMatthew G. Knepley   Input Parameters:
9325bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9326b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9327b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9328b5a892a1SMatthew G. Knepley 
9329b5a892a1SMatthew G. Knepley   Output Parameters:
9330bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9331b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9332b5a892a1SMatthew G. Knepley 
9333b5a892a1SMatthew G. Knepley   Level: advanced
9334b5a892a1SMatthew G. Knepley 
9335bb7acecfSBarry Smith   Note:
9336bb7acecfSBarry Smith   An arrangement is a vertex order
9337bb7acecfSBarry Smith 
933885036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9339bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9340bb7acecfSBarry Smith 
9341bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9342bb7acecfSBarry Smith 
934385036b15SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangement()`
9344b5a892a1SMatthew G. Knepley @*/
9345d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9346d71ae5a4SJacob Faibussowitsch {
9347b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
934885036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9349b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9350b5a892a1SMatthew G. Knepley 
9351b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93529371c9d4SSatish Balay   if (!nO) {
93539371c9d4SSatish Balay     *ornt  = 0;
93549371c9d4SSatish Balay     *found = PETSC_TRUE;
93553ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
93569371c9d4SSatish Balay   }
9357b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
935885036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangement(ct, o);
9359b5a892a1SMatthew G. Knepley 
93609371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
93619371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
93629371c9d4SSatish Balay     if (c == cS) {
93639371c9d4SSatish Balay       *ornt = o;
93649371c9d4SSatish Balay       break;
93659371c9d4SSatish Balay     }
9366b5a892a1SMatthew G. Knepley   }
9367b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
93683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9369b5a892a1SMatthew G. Knepley }
9370b5a892a1SMatthew G. Knepley 
9371b5a892a1SMatthew G. Knepley /*@C
9372bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9373b5a892a1SMatthew G. Knepley 
937420f4b53cSBarry Smith   Not Collective
9375b5a892a1SMatthew G. Knepley 
9376b5a892a1SMatthew G. Knepley   Input Parameters:
9377bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9378b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9379b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9380b5a892a1SMatthew G. Knepley 
93812fe279fdSBarry Smith   Output Parameter:
9382bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9383b5a892a1SMatthew G. Knepley 
9384b5a892a1SMatthew G. Knepley   Level: advanced
9385b5a892a1SMatthew G. Knepley 
9386bb7acecfSBarry Smith   Note:
9387bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9388bb7acecfSBarry Smith 
938960225df5SJacob Faibussowitsch   Developer Notes:
9390bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9391bb7acecfSBarry Smith 
93921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9393b5a892a1SMatthew G. Knepley @*/
9394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9395d71ae5a4SJacob Faibussowitsch {
9396b5a892a1SMatthew G. Knepley   PetscBool found;
9397b5a892a1SMatthew G. Knepley 
9398b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93999566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
94007a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
94013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9402b5a892a1SMatthew G. Knepley }
9403012bc364SMatthew G. Knepley 
9404012bc364SMatthew G. Knepley /*@C
9405012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9406012bc364SMatthew G. Knepley 
940720f4b53cSBarry Smith   Not Collective
9408012bc364SMatthew G. Knepley 
9409012bc364SMatthew G. Knepley   Input Parameters:
9410bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9411012bc364SMatthew G. Knepley - point - Coordinates of the point
9412012bc364SMatthew G. Knepley 
94132fe279fdSBarry Smith   Output Parameter:
9414012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9415012bc364SMatthew G. Knepley 
9416012bc364SMatthew G. Knepley   Level: advanced
9417012bc364SMatthew G. Knepley 
94181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9419012bc364SMatthew G. Knepley @*/
9420d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9421d71ae5a4SJacob Faibussowitsch {
9422012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9423012bc364SMatthew G. Knepley   PetscInt  d;
9424012bc364SMatthew G. Knepley 
9425012bc364SMatthew G. Knepley   PetscFunctionBegin;
9426012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9427012bc364SMatthew G. Knepley   switch (ct) {
9428012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9429012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9430012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
94319371c9d4SSatish Balay       if (point[d] < -1.0) {
94329371c9d4SSatish Balay         *inside = PETSC_FALSE;
94339371c9d4SSatish Balay         break;
94349371c9d4SSatish Balay       }
9435012bc364SMatthew G. Knepley       sum += point[d];
9436012bc364SMatthew G. Knepley     }
94379371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
94389371c9d4SSatish Balay       *inside = PETSC_FALSE;
94399371c9d4SSatish Balay       break;
94409371c9d4SSatish Balay     }
9441012bc364SMatthew G. Knepley     break;
9442012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9443012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9444012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
94459371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
94469371c9d4SSatish Balay         *inside = PETSC_FALSE;
9447012bc364SMatthew G. Knepley         break;
94489371c9d4SSatish Balay       }
94499371c9d4SSatish Balay     break;
9450d71ae5a4SJacob Faibussowitsch   default:
9451d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9452012bc364SMatthew G. Knepley   }
94533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9454012bc364SMatthew G. Knepley }
9455adc21957SMatthew G. Knepley 
9456adc21957SMatthew G. Knepley /*@
9457adc21957SMatthew G. Knepley   DMReorderSectionSetDefault - Set flag indicating whether the local section should be reordered by default
9458adc21957SMatthew G. Knepley 
9459adc21957SMatthew G. Knepley   Logically collective
9460adc21957SMatthew G. Knepley 
9461adc21957SMatthew G. Knepley   Input Parameters:
9462adc21957SMatthew G. Knepley + dm      - The DM
9463adc21957SMatthew G. Knepley - reorder - Flag for reordering
9464adc21957SMatthew G. Knepley 
9465adc21957SMatthew G. Knepley   Level: intermediate
9466adc21957SMatthew G. Knepley 
9467adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetDefault()`
9468adc21957SMatthew G. Knepley @*/
9469adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetDefault(DM dm, DMReorderDefaultFlag reorder)
9470adc21957SMatthew G. Knepley {
9471adc21957SMatthew G. Knepley   PetscFunctionBegin;
9472adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9473adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetDefault_C", (DM, DMReorderDefaultFlag), (dm, reorder));
9474adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9475adc21957SMatthew G. Knepley }
9476adc21957SMatthew G. Knepley 
9477adc21957SMatthew G. Knepley /*@
9478adc21957SMatthew G. Knepley   DMReorderSectionGetDefault - Get flag indicating whether the local section should be reordered by default
9479adc21957SMatthew G. Knepley 
9480adc21957SMatthew G. Knepley   Not collective
9481adc21957SMatthew G. Knepley 
9482adc21957SMatthew G. Knepley   Input Parameter:
9483adc21957SMatthew G. Knepley . dm - The DM
9484adc21957SMatthew G. Knepley 
9485adc21957SMatthew G. Knepley   Output Parameter:
9486adc21957SMatthew G. Knepley . reorder - Flag for reordering
9487adc21957SMatthew G. Knepley 
9488adc21957SMatthew G. Knepley   Level: intermediate
9489adc21957SMatthew G. Knepley 
9490adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`
9491adc21957SMatthew G. Knepley @*/
9492adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetDefault(DM dm, DMReorderDefaultFlag *reorder)
9493adc21957SMatthew G. Knepley {
9494adc21957SMatthew G. Knepley   PetscFunctionBegin;
9495adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9496adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9497adc21957SMatthew G. Knepley   *reorder = DM_REORDER_DEFAULT_NOTSET;
9498adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetDefault_C", (DM, DMReorderDefaultFlag *), (dm, reorder));
9499adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9500adc21957SMatthew G. Knepley }
9501adc21957SMatthew G. Knepley 
9502adc21957SMatthew G. Knepley /*@C
9503adc21957SMatthew G. Knepley   DMReorderSectionSetType - Set the type of local section reordering
9504adc21957SMatthew G. Knepley 
9505adc21957SMatthew G. Knepley   Logically collective
9506adc21957SMatthew G. Knepley 
9507adc21957SMatthew G. Knepley   Input Parameters:
9508adc21957SMatthew G. Knepley + dm      - The DM
9509adc21957SMatthew G. Knepley - reorder - The reordering method
9510adc21957SMatthew G. Knepley 
9511adc21957SMatthew G. Knepley   Level: intermediate
9512adc21957SMatthew G. Knepley 
9513adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetType()`, `DMReorderSectionSetDefault()`
9514adc21957SMatthew G. Knepley @*/
9515adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetType(DM dm, MatOrderingType reorder)
9516adc21957SMatthew G. Knepley {
9517adc21957SMatthew G. Knepley   PetscFunctionBegin;
9518adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9519adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetType_C", (DM, MatOrderingType), (dm, reorder));
9520adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9521adc21957SMatthew G. Knepley }
9522adc21957SMatthew G. Knepley 
9523adc21957SMatthew G. Knepley /*@C
9524adc21957SMatthew G. Knepley   DMReorderSectionGetType - Get the reordering type for the local section
9525adc21957SMatthew G. Knepley 
9526adc21957SMatthew G. Knepley   Not collective
9527adc21957SMatthew G. Knepley 
9528adc21957SMatthew G. Knepley   Input Parameter:
9529adc21957SMatthew G. Knepley . dm - The DM
9530adc21957SMatthew G. Knepley 
9531adc21957SMatthew G. Knepley   Output Parameter:
9532adc21957SMatthew G. Knepley . reorder - The reordering method
9533adc21957SMatthew G. Knepley 
9534adc21957SMatthew G. Knepley   Level: intermediate
9535adc21957SMatthew G. Knepley 
9536adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`, `DMReorderSectionGetDefault()`
9537adc21957SMatthew G. Knepley @*/
9538adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetType(DM dm, MatOrderingType *reorder)
9539adc21957SMatthew G. Knepley {
9540adc21957SMatthew G. Knepley   PetscFunctionBegin;
9541adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9542adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9543adc21957SMatthew G. Knepley   *reorder = NULL;
9544adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetType_C", (DM, MatOrderingType *), (dm, reorder));
9545adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9546adc21957SMatthew G. Knepley }
9547