xref: /petsc/src/dm/interface/dm.c (revision ddedc8f6e483b62fe2621876df989d191b659d2a)
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));
774*ddedc8f6SJames Wright   for (PetscInt i = 0; i < (*dm)->periodic.num_affines; i++) {
775*ddedc8f6SJames Wright     PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local[i]));
776*ddedc8f6SJames Wright     PetscCall(VecDestroy(&(*dm)->periodic.affine[i]));
777*ddedc8f6SJames Wright   }
778*ddedc8f6SJames Wright   if ((*dm)->periodic.num_affines > 0) PetscCall(PetscFree2((*dm)->periodic.affine_to_local, (*dm)->periodic.affine));
7796636e97aSMatthew G Knepley 
7809566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7819566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
782e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7839566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
784732e2eb9SMatthew G Knepley 
785213acdd3SPierre Jolivet   PetscTryTypeMethod(*dm, destroy);
7869566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
787d2b2dc1eSMatthew G. Knepley   PetscCall(DMCeedDestroy(&(*dm)->dmceed));
788f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7899566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7909566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
791f918ec44SMatthew G. Knepley #endif
792435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7939566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
7943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79547c6ae99SBarry Smith }
79647c6ae99SBarry Smith 
797d7bf68aeSBarry Smith /*@
798bb7acecfSBarry Smith   DMSetUp - sets up the data structures inside a `DM` object
799d7bf68aeSBarry Smith 
80020f4b53cSBarry Smith   Collective
801d7bf68aeSBarry Smith 
802d7bf68aeSBarry Smith   Input Parameter:
803bb7acecfSBarry Smith . dm - the `DM` object to setup
804d7bf68aeSBarry Smith 
805bb7acecfSBarry Smith   Level: intermediate
806d7bf68aeSBarry Smith 
807bb7acecfSBarry Smith   Note:
808bb7acecfSBarry Smith   This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
809bb7acecfSBarry Smith 
8101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
811d7bf68aeSBarry Smith @*/
812d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
813d71ae5a4SJacob Faibussowitsch {
814d7bf68aeSBarry Smith   PetscFunctionBegin;
815171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8163ba16761SJacob Faibussowitsch   if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
817dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8188387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
8193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
820d7bf68aeSBarry Smith }
821d7bf68aeSBarry Smith 
822d7bf68aeSBarry Smith /*@
823bb7acecfSBarry Smith   DMSetFromOptions - sets parameters in a `DM` from the options database
824d7bf68aeSBarry Smith 
82520f4b53cSBarry Smith   Collective
826d7bf68aeSBarry Smith 
827d7bf68aeSBarry Smith   Input Parameter:
828bb7acecfSBarry Smith . dm - the `DM` object to set options for
829d7bf68aeSBarry Smith 
83020f4b53cSBarry Smith   Options Database Keys:
831bb7acecfSBarry Smith + -dm_preallocate_only                               - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
832bb7acecfSBarry Smith . -dm_vec_type <type>                                - type of vector to create inside `DM`
833bb7acecfSBarry Smith . -dm_mat_type <type>                                - type of matrix to create inside `DM`
834a4ea9b21SRichard Tran Mills . -dm_is_coloring_type                               - <global or local>
83520f4b53cSBarry 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`
83620f4b53cSBarry Smith . -dm_plex_filename <str>                            - File containing a mesh
8379318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>                   - File containing a mesh boundary
838cd7e8a5eSksagiyam . -dm_plex_name <str>                                - Name of the mesh in the file
8395dca41c3SJed Brown . -dm_plex_shape <shape>                             - The domain shape, such as `BOX`, `SPHERE`, etc.
8409318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                                 - Cell shape
8419318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool>              - Use a reference cell domain
8429318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                                 - Set the topological dimension
843bb7acecfSBarry Smith . -dm_plex_simplex <bool>                            - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
844bb7acecfSBarry Smith . -dm_plex_interpolate <bool>                        - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8459318fe57SMatthew G. Knepley . -dm_plex_scale <sc>                                - Scale factor for mesh coordinates
846be664eb1SMatthew G. Knepley . -dm_coord_remap <bool>                             - Map coordinates using a function
847be664eb1SMatthew G. Knepley . -dm_coord_map <mapname>                            - Select a builtin coordinate map
848be664eb1SMatthew G. Knepley . -dm_coord_map_params <p0,p1,p2,...>                - Set coordinate mapping parameters
8499318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>                         - Number of faces along each dimension
8509318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>                         - Specify lower-left-bottom coordinates for the box
8519318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>                         - Specify upper-right-top coordinates for the box
852bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>                         - Specify the `DMBoundaryType` for each direction
8539318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>                         - The sphere radius
8549318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>                           - Radius of the ball
8559318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>                          - Boundary type in the z direction
8569318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>                   - Number of wedges around the cylinder
857bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>                           - Reorder the mesh using the specified algorithm
8589318fe57SMatthew G. Knepley . -dm_refine_pre <n>                                 - The number of refinements before distribution
8599318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>                      - Flag for uniform refinement before distribution
8609318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>                    - The maximum cell volume after refinement before distribution
8619318fe57SMatthew G. Knepley . -dm_refine <n>                                     - The number of refinements after distribution
862bdf63967SMatthew G. Knepley . -dm_extrude <l>                                    - Activate extrusion and specify the number of layers to extrude
863d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
864d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
865d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
866d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
867d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
868909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells                     - Flag to create finite volume ghost cells on the boundary
869909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name>               - Label name for ghost cells boundary
8709318fe57SMatthew G. Knepley . -dm_distribute <bool>                              - Flag to redistribute a mesh among processes
8719318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>                         - The size of the overlap halo
8729318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>                           - Set adjacency direction
87320f4b53cSBarry Smith . -dm_plex_adj_closure <bool>                        - Set adjacency size
874d2b2dc1eSMatthew G. Knepley . -dm_plex_use_ceed <bool>                           - Use LibCEED as the FEM backend
87520f4b53cSBarry Smith . -dm_plex_check_symmetry                            - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
876bb7acecfSBarry Smith . -dm_plex_check_skeleton                            - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
877bb7acecfSBarry 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()`
878bb7acecfSBarry Smith . -dm_plex_check_geometry                            - Check that cells have positive volume - `DMPlexCheckGeometry()`
879bb7acecfSBarry Smith . -dm_plex_check_pointsf                             - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
880bb7acecfSBarry Smith . -dm_plex_check_interface_cones                     - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
881384a6580SVaclav Hapla - -dm_plex_check_all                                 - Perform all the checks above
882d7bf68aeSBarry Smith 
88395eb5ee5SVaclav Hapla   Level: intermediate
88495eb5ee5SVaclav Hapla 
8851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
886bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
88760225df5SJacob Faibussowitsch          `DMSetOptionsPrefix()`, `DMType`, `DMPLEX`, `DMDA`
888d7bf68aeSBarry Smith @*/
889d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
890d71ae5a4SJacob Faibussowitsch {
8917781c08eSBarry Smith   char      typeName[256];
892ca266f36SBarry Smith   PetscBool flg;
893d7bf68aeSBarry Smith 
894d7bf68aeSBarry Smith   PetscFunctionBegin;
895171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89649be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
8979566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
8989566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
899dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
900d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
9019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
9029566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9031baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9049566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9051baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
906863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9089566063dSJacob 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));
909eb9d3e4dSMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_ignore_perm_output", "Ignore the local section permutation on output", "DMGetOutputDM", dm->ignorePermOutput, &dm->ignorePermOutput, NULL));
910dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
911f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
912dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
913d0609cedSBarry Smith   PetscOptionsEnd();
9143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
915d7bf68aeSBarry Smith }
916d7bf68aeSBarry Smith 
917fc9bc008SSatish Balay /*@C
918bb7acecfSBarry Smith   DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
919fe2efc57SMark 
92020f4b53cSBarry Smith   Collective
921fe2efc57SMark 
922fe2efc57SMark   Input Parameters:
923bb7acecfSBarry Smith + dm   - the `DM` object
92420f4b53cSBarry Smith . obj  - optional object that provides the prefix for the options database (if `NULL` then the prefix in obj is used)
92560225df5SJacob Faibussowitsch - name - option string that is used to activate viewing
926fe2efc57SMark 
927fe2efc57SMark   Level: intermediate
928bb7acecfSBarry Smith 
929bb7acecfSBarry Smith   Note:
930bb7acecfSBarry Smith   See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
931bb7acecfSBarry Smith 
93260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`
933fe2efc57SMark @*/
934d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
935d71ae5a4SJacob Faibussowitsch {
936fe2efc57SMark   PetscFunctionBegin;
937fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9389566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
940fe2efc57SMark }
941fe2efc57SMark 
942fe2efc57SMark /*@C
943bb7acecfSBarry 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
944bb7acecfSBarry Smith   save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
94547c6ae99SBarry Smith 
94620f4b53cSBarry Smith   Collective
94747c6ae99SBarry Smith 
948d8d19677SJose E. Roman   Input Parameters:
949bb7acecfSBarry Smith + dm - the `DM` object to view
95047c6ae99SBarry Smith - v  - the viewer
95147c6ae99SBarry Smith 
95220f4b53cSBarry Smith   Level: beginner
95320f4b53cSBarry Smith 
954cd7e8a5eSksagiyam   Notes:
955bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
956bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
957bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
958cd7e8a5eSksagiyam 
9591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
96047c6ae99SBarry Smith @*/
961d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
962d71ae5a4SJacob Faibussowitsch {
96332c0f0efSBarry Smith   PetscBool         isbinary;
96476a8abe0SBarry Smith   PetscMPIInt       size;
96576a8abe0SBarry Smith   PetscViewerFormat format;
96647c6ae99SBarry Smith 
96747c6ae99SBarry Smith   PetscFunctionBegin;
968171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
96948a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
970b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
97174903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
97274903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
97374903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
97474903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
97574903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
97674903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
97774903a4fSStefano Zampini      in an error here */
97874903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9799566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
980b1b135c8SBarry Smith 
9819566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9829566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
9833ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
9849566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9859566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
98632c0f0efSBarry Smith   if (isbinary) {
98755849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
98832c0f0efSBarry Smith     char     type[256];
98932c0f0efSBarry Smith 
9909566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
991c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
9929566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
99332c0f0efSBarry Smith   }
994dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
9953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
99647c6ae99SBarry Smith }
99747c6ae99SBarry Smith 
99847c6ae99SBarry Smith /*@
999bb7acecfSBarry 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,
1000bb7acecfSBarry Smith   that is it has no ghost locations.
100147c6ae99SBarry Smith 
100220f4b53cSBarry Smith   Collective
100347c6ae99SBarry Smith 
100447c6ae99SBarry Smith   Input Parameter:
1005bb7acecfSBarry Smith . dm - the `DM` object
100647c6ae99SBarry Smith 
100747c6ae99SBarry Smith   Output Parameter:
100847c6ae99SBarry Smith . vec - the global vector
100947c6ae99SBarry Smith 
1010073dac72SJed Brown   Level: beginner
101147c6ae99SBarry Smith 
10121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1013bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
101447c6ae99SBarry Smith @*/
1015d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1016d71ae5a4SJacob Faibussowitsch {
101747c6ae99SBarry Smith   PetscFunctionBegin;
1018171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10194f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1020dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
102176bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1022c6b011d8SStefano Zampini     DM vdm;
1023c6b011d8SStefano Zampini 
10249566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10257a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1026c6b011d8SStefano Zampini   }
10273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
102847c6ae99SBarry Smith }
102947c6ae99SBarry Smith 
103047c6ae99SBarry Smith /*@
1031bb7acecfSBarry Smith   DMCreateLocalVector - Creates a local vector from a `DM` object.
103247c6ae99SBarry Smith 
103347c6ae99SBarry Smith   Not Collective
103447c6ae99SBarry Smith 
103547c6ae99SBarry Smith   Input Parameter:
1036bb7acecfSBarry Smith . dm - the `DM` object
103747c6ae99SBarry Smith 
103847c6ae99SBarry Smith   Output Parameter:
103947c6ae99SBarry Smith . vec - the local vector
104047c6ae99SBarry Smith 
1041073dac72SJed Brown   Level: beginner
104247c6ae99SBarry Smith 
104320f4b53cSBarry Smith   Note:
1044bb7acecfSBarry 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.
1045bb7acecfSBarry Smith 
10461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1047bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
104847c6ae99SBarry Smith @*/
1049d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1050d71ae5a4SJacob Faibussowitsch {
105147c6ae99SBarry Smith   PetscFunctionBegin;
1052171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10534f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1054dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
105576bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1056c6b011d8SStefano Zampini     DM vdm;
1057c6b011d8SStefano Zampini 
10589566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10597a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1060c6b011d8SStefano Zampini   }
10613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
106247c6ae99SBarry Smith }
106347c6ae99SBarry Smith 
10641411c6eeSJed Brown /*@
1065bb7acecfSBarry Smith   DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10661411c6eeSJed Brown 
106720f4b53cSBarry Smith   Collective
10681411c6eeSJed Brown 
10691411c6eeSJed Brown   Input Parameter:
1070bb7acecfSBarry Smith . dm - the `DM` that provides the mapping
10711411c6eeSJed Brown 
10721411c6eeSJed Brown   Output Parameter:
10731411c6eeSJed Brown . ltog - the mapping
10741411c6eeSJed Brown 
1075bb7acecfSBarry Smith   Level: advanced
10761411c6eeSJed Brown 
10771411c6eeSJed Brown   Notes:
1078bb7acecfSBarry Smith   The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10791411c6eeSJed Brown 
1080bb7acecfSBarry Smith   Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1081bb7acecfSBarry Smith   need to use this function with those objects.
1082bb7acecfSBarry Smith 
1083bb7acecfSBarry Smith   This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1084bb7acecfSBarry Smith 
108560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1086bb7acecfSBarry Smith           `DMCreateMatrix()`
10871411c6eeSJed Brown @*/
1088d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1089d71ae5a4SJacob Faibussowitsch {
10900be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10911411c6eeSJed Brown 
10921411c6eeSJed Brown   PetscFunctionBegin;
10931411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10944f572ea9SToby Isaac   PetscAssertPointer(ltog, 2);
10951411c6eeSJed Brown   if (!dm->ltogmap) {
109637d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
109737d0c07bSMatthew G Knepley 
10989566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
109937d0c07bSMatthew G Knepley     if (section) {
1100a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
110137d0c07bSMatthew G Knepley       PetscInt       *ltog;
1102ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
110337d0c07bSMatthew G Knepley 
11049566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11059566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11069566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11079566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
110837d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1109e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
111037d0c07bSMatthew G Knepley 
111137d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11129566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11139566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11149566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11159566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11161a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11171a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1118ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11195227eafbSStefano Zampini 
1120e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11215227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1122e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1123e6befd46SJed Brown             cind++;
1124e6befd46SJed Brown           } else {
11255227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1126e6befd46SJed Brown           }
112737d0c07bSMatthew G Knepley         }
112837d0c07bSMatthew G Knepley       }
1129bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11309371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11319371c9d4SSatish Balay       bsLocal[1] = bs;
11329566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11339371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11349371c9d4SSatish Balay         bs = 1;
11359371c9d4SSatish Balay       } else {
11369371c9d4SSatish Balay         bs = bsMinMax[0];
11379371c9d4SSatish Balay       }
11387591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11397591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1140ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1141ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1142ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1143ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1144ca469d19SJed Brown         }
1145ccf3bd66SMatthew G. Knepley         n /= bs;
1146ccf3bd66SMatthew G. Knepley       }
11479566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1148dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
114937d0c07bSMatthew G Knepley   }
11501411c6eeSJed Brown   *ltog = dm->ltogmap;
11513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11521411c6eeSJed Brown }
11531411c6eeSJed Brown 
11541411c6eeSJed Brown /*@
1155bb7acecfSBarry Smith   DMGetBlockSize - Gets the inherent block size associated with a `DM`
11561411c6eeSJed Brown 
11571411c6eeSJed Brown   Not Collective
11581411c6eeSJed Brown 
11591411c6eeSJed Brown   Input Parameter:
1160bb7acecfSBarry Smith . dm - the `DM` with block structure
11611411c6eeSJed Brown 
11621411c6eeSJed Brown   Output Parameter:
11631411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure
11641411c6eeSJed Brown 
11651411c6eeSJed Brown   Level: intermediate
11661411c6eeSJed Brown 
1167bb7acecfSBarry Smith   Note:
1168bb7acecfSBarry Smith   This might be the number of degrees of freedom at each grid point for a structured grid.
1169bb7acecfSBarry Smith 
1170bb7acecfSBarry Smith   Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1171bb7acecfSBarry Smith   rather different locations in the vectors may have a different block size.
1172bb7acecfSBarry Smith 
11731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11741411c6eeSJed Brown @*/
1175d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1176d71ae5a4SJacob Faibussowitsch {
11771411c6eeSJed Brown   PetscFunctionBegin;
11781411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11794f572ea9SToby Isaac   PetscAssertPointer(bs, 2);
11807a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11811411c6eeSJed Brown   *bs = dm->bs;
11823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11831411c6eeSJed Brown }
11841411c6eeSJed Brown 
118548eeb7c8SBarry Smith /*@C
1186bb7acecfSBarry Smith   DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1187bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
118847c6ae99SBarry Smith 
118920f4b53cSBarry Smith   Collective
119047c6ae99SBarry Smith 
1191d8d19677SJose E. Roman   Input Parameters:
1192bb7acecfSBarry Smith + dmc - the `DM` object
1193bb7acecfSBarry Smith - dmf - the second, finer `DM` object
119447c6ae99SBarry Smith 
1195d8d19677SJose E. Roman   Output Parameters:
119647c6ae99SBarry Smith + mat - the interpolation
1197bb7acecfSBarry Smith - vec - the scaling (optional), see `DMCreateInterpolationScale()`
119847c6ae99SBarry Smith 
119947c6ae99SBarry Smith   Level: developer
120047c6ae99SBarry Smith 
120195452b02SPatrick Sanan   Notes:
1202bb7acecfSBarry 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
1203bb7acecfSBarry Smith   DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1204d52bd9f3SBarry Smith 
1205bb7acecfSBarry Smith   For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1206bb7acecfSBarry Smith   vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
120785afcc9aSBarry Smith 
12081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
120947c6ae99SBarry Smith @*/
1210d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1211d71ae5a4SJacob Faibussowitsch {
121247c6ae99SBarry Smith   PetscFunctionBegin;
1213a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1214a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12154f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12169566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1217dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12189566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
122047c6ae99SBarry Smith }
122147c6ae99SBarry Smith 
12223ad4599aSBarry Smith /*@
1223a4e35b19SJacob Faibussowitsch   DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is
1224a4e35b19SJacob Faibussowitsch   the transpose of the interpolation between the `DM`.
12252ed6491fSPatrick Sanan 
12262ed6491fSPatrick Sanan   Input Parameters:
1227bb7acecfSBarry Smith + dac - `DM` that defines a coarse mesh
1228bb7acecfSBarry Smith . daf - `DM` that defines a fine mesh
12292ed6491fSPatrick Sanan - mat - the restriction (or interpolation operator) from fine to coarse
12302ed6491fSPatrick Sanan 
12312ed6491fSPatrick Sanan   Output Parameter:
12322ed6491fSPatrick Sanan . scale - the scaled vector
12332ed6491fSPatrick Sanan 
1234bb7acecfSBarry Smith   Level: advanced
12352ed6491fSPatrick Sanan 
1236a4e35b19SJacob Faibussowitsch   Notes:
1237a4e35b19SJacob Faibussowitsch   xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual)
1238a4e35b19SJacob Faibussowitsch   restriction. In other words xcoarse is the coarse representation of xfine.
1239a4e35b19SJacob Faibussowitsch 
124060225df5SJacob Faibussowitsch   Developer Notes:
1241bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1242e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1243e9c74fd6SRichard Tran Mills 
124460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, `DMCreateRestriction()`, `DMCreateGlobalVector()`
12452ed6491fSPatrick Sanan @*/
1246d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1247d71ae5a4SJacob Faibussowitsch {
12482ed6491fSPatrick Sanan   Vec         fine;
12492ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12509704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1251e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12529704db99SRichard Tran Mills #endif
12532ed6491fSPatrick Sanan 
12542ed6491fSPatrick Sanan   PetscFunctionBegin;
12559566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12569566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12579566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12589704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12599704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12609704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12619704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12629566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1263e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12649566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12659566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12669566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
126783aa49f4SRichard Tran Mills   }
12689704db99SRichard Tran Mills #endif
12699566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12709566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12719566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12732ed6491fSPatrick Sanan }
12742ed6491fSPatrick Sanan 
12752ed6491fSPatrick Sanan /*@
1276bb7acecfSBarry Smith   DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1277bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12783ad4599aSBarry Smith 
127920f4b53cSBarry Smith   Collective
12803ad4599aSBarry Smith 
1281d8d19677SJose E. Roman   Input Parameters:
1282bb7acecfSBarry Smith + dmc - the `DM` object
1283bb7acecfSBarry Smith - dmf - the second, finer `DM` object
12843ad4599aSBarry Smith 
12853ad4599aSBarry Smith   Output Parameter:
12863ad4599aSBarry Smith . mat - the restriction
12873ad4599aSBarry Smith 
12883ad4599aSBarry Smith   Level: developer
12893ad4599aSBarry Smith 
1290bb7acecfSBarry Smith   Note:
1291bb7acecfSBarry Smith   This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1292bb7acecfSBarry Smith   matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12933ad4599aSBarry Smith 
12941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12953ad4599aSBarry Smith @*/
1296d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1297d71ae5a4SJacob Faibussowitsch {
12983ad4599aSBarry Smith   PetscFunctionBegin;
1299a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1300a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13014f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13029566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1303dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13049566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13063ad4599aSBarry Smith }
13073ad4599aSBarry Smith 
130847c6ae99SBarry Smith /*@
1309a4e35b19SJacob Faibussowitsch   DMCreateInjection - Gets injection matrix between two `DM` objects.
131047c6ae99SBarry Smith 
131120f4b53cSBarry Smith   Collective
131247c6ae99SBarry Smith 
1313d8d19677SJose E. Roman   Input Parameters:
1314bb7acecfSBarry Smith + dac - the `DM` object
1315bb7acecfSBarry Smith - daf - the second, finer `DM` object
131647c6ae99SBarry Smith 
131747c6ae99SBarry Smith   Output Parameter:
13186dbf9973SLawrence Mitchell . mat - the injection
131947c6ae99SBarry Smith 
132047c6ae99SBarry Smith   Level: developer
132147c6ae99SBarry Smith 
1322a4e35b19SJacob Faibussowitsch   Notes:
1323a4e35b19SJacob Faibussowitsch   This is an operator that applied to a vector obtained with `DMCreateGlobalVector()` on the
1324a4e35b19SJacob Faibussowitsch   fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting
1325a4e35b19SJacob Faibussowitsch   the values on the coarse grid points. This compares to the operator obtained by
1326a4e35b19SJacob Faibussowitsch   `DMCreateRestriction()` or the transpose of the operator obtained by
1327a4e35b19SJacob Faibussowitsch   `DMCreateInterpolation()` that uses a "local weighted average" of the values around the
1328a4e35b19SJacob Faibussowitsch   coarse grid point as the coarse grid value.
1329a4e35b19SJacob Faibussowitsch 
1330bb7acecfSBarry 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
1331bb7acecfSBarry Smith   `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
133285afcc9aSBarry Smith 
13331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1334bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
133547c6ae99SBarry Smith @*/
1336d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1337d71ae5a4SJacob Faibussowitsch {
133847c6ae99SBarry Smith   PetscFunctionBegin;
1339a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1340a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13414f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13429566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1343dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13449566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
134647c6ae99SBarry Smith }
134747c6ae99SBarry Smith 
1348b412c318SBarry Smith /*@
1349bb7acecfSBarry 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
1350bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1351bd041c0cSMatthew G. Knepley 
135220f4b53cSBarry Smith   Collective
1353bd041c0cSMatthew G. Knepley 
1354d8d19677SJose E. Roman   Input Parameters:
1355bb7acecfSBarry Smith + dmc - the target `DM` object
1356bb7acecfSBarry Smith - dmf - the source `DM` object
1357bd041c0cSMatthew G. Knepley 
1358bd041c0cSMatthew G. Knepley   Output Parameter:
1359b4937a87SMatthew G. Knepley . mat - the mass matrix
1360bd041c0cSMatthew G. Knepley 
1361bd041c0cSMatthew G. Knepley   Level: developer
1362bd041c0cSMatthew G. Knepley 
1363bb7acecfSBarry Smith   Notes:
1364bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1365bb7acecfSBarry Smith 
136620f4b53cSBarry 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()`
1367bb7acecfSBarry Smith 
136842747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1369bd041c0cSMatthew G. Knepley @*/
1370d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1371d71ae5a4SJacob Faibussowitsch {
1372bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1373b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1374b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13754f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13765b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1377dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13785b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
13793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1380b4937a87SMatthew G. Knepley }
1381b4937a87SMatthew G. Knepley 
1382b4937a87SMatthew G. Knepley /*@
1383bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1384b4937a87SMatthew G. Knepley 
138520f4b53cSBarry Smith   Collective
1386b4937a87SMatthew G. Knepley 
1387b4937a87SMatthew G. Knepley   Input Parameter:
1388bb7acecfSBarry Smith . dm - the `DM` object
1389b4937a87SMatthew G. Knepley 
1390b4937a87SMatthew G. Knepley   Output Parameter:
1391bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1392b4937a87SMatthew G. Knepley 
1393b4937a87SMatthew G. Knepley   Level: developer
1394b4937a87SMatthew G. Knepley 
1395bb7acecfSBarry Smith   Note:
1396bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1397bb7acecfSBarry Smith 
139860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1399b4937a87SMatthew G. Knepley @*/
1400d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1401d71ae5a4SJacob Faibussowitsch {
1402b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1403b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14044f572ea9SToby Isaac   PetscAssertPointer(lm, 2);
1405dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
14063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1407bd041c0cSMatthew G. Knepley }
1408bd041c0cSMatthew G. Knepley 
1409bd041c0cSMatthew G. Knepley /*@
1410bb7acecfSBarry Smith   DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1411bb7acecfSBarry Smith   of a PDE on the `DM`.
141247c6ae99SBarry Smith 
141320f4b53cSBarry Smith   Collective
141447c6ae99SBarry Smith 
1415d8d19677SJose E. Roman   Input Parameters:
1416bb7acecfSBarry Smith + dm    - the `DM` object
1417bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
141847c6ae99SBarry Smith 
141947c6ae99SBarry Smith   Output Parameter:
142047c6ae99SBarry Smith . coloring - the coloring
142147c6ae99SBarry Smith 
14221bf8429eSBarry Smith   Level: developer
14231bf8429eSBarry Smith 
1424ec5066bdSBarry Smith   Notes:
1425bb7acecfSBarry 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
1426bb7acecfSBarry Smith   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1427ec5066bdSBarry Smith 
1428bb7acecfSBarry Smith   This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14291bf8429eSBarry 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,
14301bf8429eSBarry Smith   otherwise an error will be generated.
1431ec5066bdSBarry Smith 
14321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1433aab9d709SJed Brown @*/
1434d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1435d71ae5a4SJacob Faibussowitsch {
143647c6ae99SBarry Smith   PetscFunctionBegin;
1437171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14384f572ea9SToby Isaac   PetscAssertPointer(coloring, 3);
1439dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
144147c6ae99SBarry Smith }
144247c6ae99SBarry Smith 
1443b412c318SBarry Smith /*@
1444bb7acecfSBarry Smith   DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
144547c6ae99SBarry Smith 
144620f4b53cSBarry Smith   Collective
144747c6ae99SBarry Smith 
144847c6ae99SBarry Smith   Input Parameter:
1449bb7acecfSBarry Smith . dm - the `DM` object
145047c6ae99SBarry Smith 
145147c6ae99SBarry Smith   Output Parameter:
145247c6ae99SBarry Smith . mat - the empty Jacobian
145347c6ae99SBarry Smith 
145420f4b53cSBarry Smith   Options Database Key:
1455bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1456f27dd7c6SMatthew G. Knepley 
145720f4b53cSBarry Smith   Level: beginner
145820f4b53cSBarry Smith 
145995452b02SPatrick Sanan   Notes:
146095452b02SPatrick Sanan   This properly preallocates the number of nonzeros in the sparse matrix so you
146194013140SBarry Smith   do not need to do it yourself.
146294013140SBarry Smith 
146394013140SBarry Smith   By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1464bb7acecfSBarry Smith   the nonzero pattern call `DMSetMatrixPreallocateOnly()`
146594013140SBarry Smith 
1466bb7acecfSBarry Smith   For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
146794013140SBarry Smith   internally by PETSc.
146894013140SBarry Smith 
1469bb7acecfSBarry Smith   For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1470bb7acecfSBarry Smith   `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
147194013140SBarry Smith 
14721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1473aab9d709SJed Brown @*/
1474d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1475d71ae5a4SJacob Faibussowitsch {
147647c6ae99SBarry Smith   PetscFunctionBegin;
1477171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14784f572ea9SToby Isaac   PetscAssertPointer(mat, 2);
14799566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14809566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1481dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
148276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1483c6b011d8SStefano Zampini     DM mdm;
1484c6b011d8SStefano Zampini 
14859566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14867a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1487c6b011d8SStefano Zampini   }
1488e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1489e5e52638SMatthew G. Knepley   if (dm->Nf) {
1490e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1491649ef022SMatthew Knepley     PetscInt     Nf, f;
1492e571a35bSMatthew G. Knepley 
14939566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1494649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1495649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14969566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14979566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14989566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1499649ef022SMatthew Knepley         break;
1500e571a35bSMatthew G. Knepley       }
1501649ef022SMatthew Knepley     }
1502649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1503649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15049566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15059566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15069566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1507e571a35bSMatthew G. Knepley       }
1508e571a35bSMatthew G. Knepley     }
1509e571a35bSMatthew G. Knepley   }
15109566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
151247c6ae99SBarry Smith }
151347c6ae99SBarry Smith 
1514732e2eb9SMatthew G Knepley /*@
1515a4e35b19SJacob Faibussowitsch   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and
1516a4e35b19SJacob Faibussowitsch   `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the
1517a4e35b19SJacob Faibussowitsch   matrices will not be preallocated.
1518aa0f6e3cSJed Brown 
151920f4b53cSBarry Smith   Logically Collective
1520aa0f6e3cSJed Brown 
1521aa0f6e3cSJed Brown   Input Parameters:
1522bb7acecfSBarry Smith + dm   - the `DM`
1523bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1524aa0f6e3cSJed Brown 
1525aa0f6e3cSJed Brown   Level: developer
1526aa0f6e3cSJed Brown 
1527a4e35b19SJacob Faibussowitsch   Notes:
1528a4e35b19SJacob Faibussowitsch   This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and
1529a4e35b19SJacob Faibussowitsch   `MatSetValuesCOO()` will be used.
1530a4e35b19SJacob Faibussowitsch 
15311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1532aa0f6e3cSJed Brown @*/
1533d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1534d71ae5a4SJacob Faibussowitsch {
1535aa0f6e3cSJed Brown   PetscFunctionBegin;
1536aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1537aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1539aa0f6e3cSJed Brown }
1540aa0f6e3cSJed Brown 
1541aa0f6e3cSJed Brown /*@
1542bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1543732e2eb9SMatthew G Knepley   preallocated but the nonzero structure and zero values will not be set.
1544732e2eb9SMatthew G Knepley 
154520f4b53cSBarry Smith   Logically Collective
1546732e2eb9SMatthew G Knepley 
1547d8d19677SJose E. Roman   Input Parameters:
1548bb7acecfSBarry Smith + dm   - the `DM`
1549bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1550732e2eb9SMatthew G Knepley 
155120f4b53cSBarry Smith   Options Database Key:
1552bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1553f27dd7c6SMatthew G. Knepley 
155420f4b53cSBarry Smith   Level: developer
155520f4b53cSBarry Smith 
15561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1557732e2eb9SMatthew G Knepley @*/
1558d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1559d71ae5a4SJacob Faibussowitsch {
1560732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1561732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1562732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1564732e2eb9SMatthew G Knepley }
1565732e2eb9SMatthew G Knepley 
1566b06ff27eSHong Zhang /*@
1567bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1568bb7acecfSBarry Smith   but the array for numerical values will not be allocated.
1569b06ff27eSHong Zhang 
157020f4b53cSBarry Smith   Logically Collective
1571b06ff27eSHong Zhang 
1572d8d19677SJose E. Roman   Input Parameters:
1573bb7acecfSBarry Smith + dm   - the `DM`
1574da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1575b06ff27eSHong Zhang 
1576b06ff27eSHong Zhang   Level: developer
1577bb7acecfSBarry Smith 
15781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1579b06ff27eSHong Zhang @*/
1580d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1581d71ae5a4SJacob Faibussowitsch {
1582b06ff27eSHong Zhang   PetscFunctionBegin;
1583b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1584b06ff27eSHong Zhang   dm->structure_only = only;
15853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1586b06ff27eSHong Zhang }
1587b06ff27eSHong Zhang 
1588863027abSJed Brown /*@
1589863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1590863027abSJed Brown 
159120f4b53cSBarry Smith   Logically Collective
1592863027abSJed Brown 
1593863027abSJed Brown   Input Parameters:
1594863027abSJed Brown + dm    - the `DM`
1595863027abSJed Brown - btype - block by topological point or field node
1596863027abSJed Brown 
159720f4b53cSBarry Smith   Options Database Key:
15980e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1599863027abSJed Brown 
160020f4b53cSBarry Smith   Level: advanced
160120f4b53cSBarry Smith 
16021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1603863027abSJed Brown @*/
1604863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1605863027abSJed Brown {
1606863027abSJed Brown   PetscFunctionBegin;
1607863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1608863027abSJed Brown   dm->blocking_type = btype;
16093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1610863027abSJed Brown }
1611863027abSJed Brown 
1612863027abSJed Brown /*@
1613863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1614863027abSJed Brown 
1615863027abSJed Brown   Not Collective
1616863027abSJed Brown 
16172fe279fdSBarry Smith   Input Parameter:
1618863027abSJed Brown . dm - the `DM`
1619863027abSJed Brown 
16202fe279fdSBarry Smith   Output Parameter:
1621863027abSJed Brown . btype - block by topological point or field node
1622863027abSJed Brown 
1623863027abSJed Brown   Level: advanced
1624863027abSJed Brown 
16251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1626863027abSJed Brown @*/
1627863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1628863027abSJed Brown {
1629863027abSJed Brown   PetscFunctionBegin;
1630863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16314f572ea9SToby Isaac   PetscAssertPointer(btype, 2);
1632863027abSJed Brown   *btype = dm->blocking_type;
16333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1634863027abSJed Brown }
1635863027abSJed Brown 
1636a89ea682SMatthew G Knepley /*@C
1637bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1638a89ea682SMatthew G Knepley 
1639a89ea682SMatthew G Knepley   Not Collective
1640a89ea682SMatthew G Knepley 
1641a89ea682SMatthew G Knepley   Input Parameters:
1642bb7acecfSBarry Smith + dm    - the `DM` object
1643a5b23f4aSJose E. Roman . count - The minimum size
164420f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1645a89ea682SMatthew G Knepley 
1646a89ea682SMatthew G Knepley   Output Parameter:
164760225df5SJacob Faibussowitsch . mem - the work array
1648a89ea682SMatthew G Knepley 
1649a89ea682SMatthew G Knepley   Level: developer
1650a89ea682SMatthew G Knepley 
1651bb7acecfSBarry Smith   Note:
1652da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1653bb7acecfSBarry Smith 
1654bb7acecfSBarry Smith   The array may contain nonzero values
1655bb7acecfSBarry Smith 
16561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1657a89ea682SMatthew G Knepley @*/
1658d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1659d71ae5a4SJacob Faibussowitsch {
1660aa1993deSMatthew G Knepley   DMWorkLink  link;
166169291d52SBarry Smith   PetscMPIInt dsize;
1662a89ea682SMatthew G Knepley 
1663a89ea682SMatthew G Knepley   PetscFunctionBegin;
1664a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16654f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1666442f3b32SStefano Zampini   if (!count) {
1667442f3b32SStefano Zampini     *(void **)mem = NULL;
1668442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1669442f3b32SStefano Zampini   }
1670aa1993deSMatthew G Knepley   if (dm->workin) {
1671aa1993deSMatthew G Knepley     link       = dm->workin;
1672aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1673aa1993deSMatthew G Knepley   } else {
16744dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1675a89ea682SMatthew G Knepley   }
1676b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1677b77fa653SStefano Zampini      Get size directly */
1678b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1679b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1680b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1681b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1682b77fa653SStefano Zampini #endif
1683b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1684b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1685b77fa653SStefano Zampini #endif
1686b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1687b77fa653SStefano Zampini 
16885056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16899566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16909566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1691854ce69bSBarry Smith     link->bytes = dsize * count;
1692aa1993deSMatthew G Knepley   }
1693aa1993deSMatthew G Knepley   link->next  = dm->workout;
1694aa1993deSMatthew G Knepley   dm->workout = link;
1695cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
169600d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
169700d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
169800d952a4SJed Brown #endif
1699aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
17003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1701a89ea682SMatthew G Knepley }
1702a89ea682SMatthew G Knepley 
1703aa1993deSMatthew G Knepley /*@C
1704bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1705aa1993deSMatthew G Knepley 
1706aa1993deSMatthew G Knepley   Not Collective
1707aa1993deSMatthew G Knepley 
1708aa1993deSMatthew G Knepley   Input Parameters:
1709bb7acecfSBarry Smith + dm    - the `DM` object
1710a5b23f4aSJose E. Roman . count - The minimum size
171120f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1712aa1993deSMatthew G Knepley 
1713aa1993deSMatthew G Knepley   Output Parameter:
171460225df5SJacob Faibussowitsch . mem - the work array
1715aa1993deSMatthew G Knepley 
1716aa1993deSMatthew G Knepley   Level: developer
1717aa1993deSMatthew G Knepley 
171860225df5SJacob Faibussowitsch   Developer Notes:
1719bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1720147403d9SBarry Smith 
17211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1722aa1993deSMatthew G Knepley @*/
1723d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1724d71ae5a4SJacob Faibussowitsch {
1725aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1726aa1993deSMatthew G Knepley 
1727aa1993deSMatthew G Knepley   PetscFunctionBegin;
1728aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17294f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1730a4e35b19SJacob Faibussowitsch   (void)count;
1731a4e35b19SJacob Faibussowitsch   (void)dtype;
1732442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1733aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1734aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1735aa1993deSMatthew G Knepley       *p            = link->next;
1736aa1993deSMatthew G Knepley       link->next    = dm->workin;
1737aa1993deSMatthew G Knepley       dm->workin    = link;
17380298fd71SBarry Smith       *(void **)mem = NULL;
17393ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1740aa1993deSMatthew G Knepley     }
1741aa1993deSMatthew G Knepley   }
1742aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1743aa1993deSMatthew G Knepley }
1744e7c4fc90SDmitry Karpeev 
17458cda7954SMatthew G. Knepley /*@C
1746bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1747bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17488cda7954SMatthew G. Knepley 
174920f4b53cSBarry Smith   Logically Collective; No Fortran Support
17508cda7954SMatthew G. Knepley 
17518cda7954SMatthew G. Knepley   Input Parameters:
1752bb7acecfSBarry Smith + dm     - The `DM`
17538cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17548cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17558cda7954SMatthew G. Knepley 
175620f4b53cSBarry Smith   Calling sequence of `nullsp`:
1757bb7acecfSBarry Smith + dm        - The present `DM`
1758bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1759147403d9SBarry Smith . field     - The field number in dm
1760147403d9SBarry Smith - nullSpace - The nullspace for the given field
17618cda7954SMatthew G. Knepley 
176249762cbcSSatish Balay   Level: intermediate
176349762cbcSSatish Balay 
17641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1765147403d9SBarry Smith @*/
1766a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1767d71ae5a4SJacob Faibussowitsch {
1768435a35e8SMatthew G Knepley   PetscFunctionBegin;
1769435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17707a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1771435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1773435a35e8SMatthew G Knepley }
1774435a35e8SMatthew G Knepley 
17758cda7954SMatthew G. Knepley /*@C
1776bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17778cda7954SMatthew G. Knepley 
177820f4b53cSBarry Smith   Not Collective; No Fortran Support
17798cda7954SMatthew G. Knepley 
17808cda7954SMatthew G. Knepley   Input Parameters:
1781bb7acecfSBarry Smith + dm    - The `DM`
17828cda7954SMatthew G. Knepley - field - The field number for the nullspace
17838cda7954SMatthew G. Knepley 
17848cda7954SMatthew G. Knepley   Output Parameter:
17858cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17868cda7954SMatthew G. Knepley 
178720f4b53cSBarry Smith   Calling sequence of `nullsp`:
1788147403d9SBarry Smith + dm        - The present DM
1789147403d9SBarry Smith . origField - The field number given above, in the original DM
1790147403d9SBarry Smith . field     - The field number in dm
1791147403d9SBarry Smith - nullSpace - The nullspace for the given field
17928cda7954SMatthew G. Knepley 
179349762cbcSSatish Balay   Level: intermediate
179449762cbcSSatish Balay 
17951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1796147403d9SBarry Smith @*/
1797a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1798d71ae5a4SJacob Faibussowitsch {
17990a50eb56SMatthew G. Knepley   PetscFunctionBegin;
18000a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18014f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18027a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
18030a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
18043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18050a50eb56SMatthew G. Knepley }
18060a50eb56SMatthew G. Knepley 
18078cda7954SMatthew G. Knepley /*@C
1808bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18098cda7954SMatthew G. Knepley 
181020f4b53cSBarry Smith   Logically Collective; No Fortran Support
18118cda7954SMatthew G. Knepley 
18128cda7954SMatthew G. Knepley   Input Parameters:
1813bb7acecfSBarry Smith + dm     - The `DM`
18148cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18158cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18168cda7954SMatthew G. Knepley 
181720f4b53cSBarry Smith   Calling sequence of `nullsp`:
1818bb7acecfSBarry Smith + dm        - The present `DM`
1819bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1820147403d9SBarry Smith . field     - The field number in dm
1821147403d9SBarry Smith - nullSpace - The nullspace for the given field
18228cda7954SMatthew G. Knepley 
182349762cbcSSatish Balay   Level: intermediate
182449762cbcSSatish Balay 
18251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1826bb7acecfSBarry Smith           `MatNullSpace`
1827147403d9SBarry Smith @*/
1828a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1829d71ae5a4SJacob Faibussowitsch {
1830f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1831f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18327a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1833f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1835f9d4088aSMatthew G. Knepley }
1836f9d4088aSMatthew G. Knepley 
18378cda7954SMatthew G. Knepley /*@C
1838bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18398cda7954SMatthew G. Knepley 
184020f4b53cSBarry Smith   Not Collective; No Fortran Support
18418cda7954SMatthew G. Knepley 
18428cda7954SMatthew G. Knepley   Input Parameters:
1843bb7acecfSBarry Smith + dm    - The `DM`
18448cda7954SMatthew G. Knepley - field - The field number for the nullspace
18458cda7954SMatthew G. Knepley 
18468cda7954SMatthew G. Knepley   Output Parameter:
18478cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18488cda7954SMatthew G. Knepley 
184920f4b53cSBarry Smith   Calling sequence of `nullsp`:
1850bb7acecfSBarry Smith + dm        - The present `DM`
1851bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1852147403d9SBarry Smith . field     - The field number in dm
1853147403d9SBarry Smith - nullSpace - The nullspace for the given field
18548cda7954SMatthew G. Knepley 
185549762cbcSSatish Balay   Level: intermediate
185649762cbcSSatish Balay 
18571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1858bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1859147403d9SBarry Smith @*/
1860a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1861d71ae5a4SJacob Faibussowitsch {
1862f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1863f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18644f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18657a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1866f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1868f9d4088aSMatthew G. Knepley }
1869f9d4088aSMatthew G. Knepley 
18704f3b5142SJed Brown /*@C
1871bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18724d343eeaSMatthew G Knepley 
187320f4b53cSBarry Smith   Not Collective; No Fortran Support
18744d343eeaSMatthew G Knepley 
18754d343eeaSMatthew G Knepley   Input Parameter:
1876bb7acecfSBarry Smith . dm - the `DM` object
18774d343eeaSMatthew G Knepley 
18784d343eeaSMatthew G Knepley   Output Parameters:
187920f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
188020f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
188120f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
18824d343eeaSMatthew G Knepley 
18834d343eeaSMatthew G Knepley   Level: intermediate
18844d343eeaSMatthew G Knepley 
1885bb7acecfSBarry Smith   Note:
188621c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1887bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1888bb7acecfSBarry Smith   `PetscFree()`.
188921c9b008SJed Brown 
189060225df5SJacob Faibussowitsch   Developer Notes:
1891bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1892bb7acecfSBarry Smith   likely be removed.
1893bb7acecfSBarry Smith 
18941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1895bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18964d343eeaSMatthew G Knepley @*/
1897d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1898d71ae5a4SJacob Faibussowitsch {
189937d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
19004d343eeaSMatthew G Knepley 
19014d343eeaSMatthew G Knepley   PetscFunctionBegin;
19024d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
190369ca1f37SDmitry Karpeev   if (numFields) {
19044f572ea9SToby Isaac     PetscAssertPointer(numFields, 2);
190569ca1f37SDmitry Karpeev     *numFields = 0;
190669ca1f37SDmitry Karpeev   }
190737d0c07bSMatthew G Knepley   if (fieldNames) {
19084f572ea9SToby Isaac     PetscAssertPointer(fieldNames, 3);
19090298fd71SBarry Smith     *fieldNames = NULL;
191069ca1f37SDmitry Karpeev   }
191169ca1f37SDmitry Karpeev   if (fields) {
19124f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
19130298fd71SBarry Smith     *fields = NULL;
191469ca1f37SDmitry Karpeev   }
19159566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
191637d0c07bSMatthew G Knepley   if (section) {
19173a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
191837d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
191937d0c07bSMatthew G Knepley 
19209566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19219566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19229566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19239566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
192437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
192537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19269566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
192737d0c07bSMatthew G Knepley     }
192837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
192937d0c07bSMatthew G Knepley       PetscInt gdof;
193037d0c07bSMatthew G Knepley 
19319566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
193237d0c07bSMatthew G Knepley       if (gdof > 0) {
193337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19343a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
193537d0c07bSMatthew G Knepley 
19369566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19379566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19383a544194SStefano Zampini           fpdof = fdof - fcdof;
19393a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19403a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19413a544194SStefano Zampini             fieldNc[f] = 1;
19423a544194SStefano Zampini           }
19433a544194SStefano Zampini           fieldSizes[f] += fpdof;
194437d0c07bSMatthew G Knepley         }
194537d0c07bSMatthew G Knepley       }
194637d0c07bSMatthew G Knepley     }
194737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19489566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
194937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
195037d0c07bSMatthew G Knepley     }
195137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
195237d0c07bSMatthew G Knepley       PetscInt gdof, goff;
195337d0c07bSMatthew G Knepley 
19549566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
195537d0c07bSMatthew G Knepley       if (gdof > 0) {
19569566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
195737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
195837d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
195937d0c07bSMatthew G Knepley 
19609566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19619566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1962ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
196337d0c07bSMatthew G Knepley         }
196437d0c07bSMatthew G Knepley       }
196537d0c07bSMatthew G Knepley     }
19668865f1eaSKarl Rupp     if (numFields) *numFields = nF;
196737d0c07bSMatthew G Knepley     if (fieldNames) {
19689566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
196937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
197037d0c07bSMatthew G Knepley         const char *fieldName;
197137d0c07bSMatthew G Knepley 
19729566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19739566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
197437d0c07bSMatthew G Knepley       }
197537d0c07bSMatthew G Knepley     }
197637d0c07bSMatthew G Knepley     if (fields) {
19779566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
197837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19793a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19803a544194SStefano Zampini 
19819566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19823a544194SStefano Zampini         in[0] = -fieldNc[f];
19833a544194SStefano Zampini         in[1] = fieldNc[f];
19841c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19853a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19869566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
198737d0c07bSMatthew G Knepley       }
198837d0c07bSMatthew G Knepley     }
19899566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1990dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19924d343eeaSMatthew G Knepley }
19934d343eeaSMatthew G Knepley 
199416621825SDmitry Karpeev /*@C
1995bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1996a4e35b19SJacob Faibussowitsch   corresponding to different fields.
1997e7c4fc90SDmitry Karpeev 
199820f4b53cSBarry Smith   Not Collective; No Fortran Support
1999e7c4fc90SDmitry Karpeev 
2000e7c4fc90SDmitry Karpeev   Input Parameter:
2001bb7acecfSBarry Smith . dm - the `DM` object
2002e7c4fc90SDmitry Karpeev 
2003e7c4fc90SDmitry Karpeev   Output Parameters:
200420f4b53cSBarry Smith + len      - The number of fields (or `NULL` if not requested)
200520f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested)
200620f4b53cSBarry Smith . islist   - The global indices for each field (or `NULL` if not requested)
200720f4b53cSBarry Smith - dmlist   - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
2008e7c4fc90SDmitry Karpeev 
2009e7c4fc90SDmitry Karpeev   Level: intermediate
2010e7c4fc90SDmitry Karpeev 
2011a4e35b19SJacob Faibussowitsch   Notes:
2012a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding field, defined by
2013a4e35b19SJacob Faibussowitsch   `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2014a4e35b19SJacob Faibussowitsch 
2015a4e35b19SJacob Faibussowitsch   The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2016a4e35b19SJacob Faibussowitsch 
2017e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2018bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2019bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2020e7c4fc90SDmitry Karpeev 
202160225df5SJacob Faibussowitsch   Developer Notes:
2022bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2023bb7acecfSBarry Smith 
202460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2025e7c4fc90SDmitry Karpeev @*/
2026d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2027d71ae5a4SJacob Faibussowitsch {
2028e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2029e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20308865f1eaSKarl Rupp   if (len) {
20314f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20328865f1eaSKarl Rupp     *len = 0;
20338865f1eaSKarl Rupp   }
20348865f1eaSKarl Rupp   if (namelist) {
20354f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2036ea78f98cSLisandro Dalcin     *namelist = NULL;
20378865f1eaSKarl Rupp   }
20388865f1eaSKarl Rupp   if (islist) {
20394f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2040ea78f98cSLisandro Dalcin     *islist = NULL;
20418865f1eaSKarl Rupp   }
20428865f1eaSKarl Rupp   if (dmlist) {
20434f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2044ea78f98cSLisandro Dalcin     *dmlist = NULL;
20458865f1eaSKarl Rupp   }
2046f3f0edfdSDmitry Karpeev   /*
2047f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2048f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2049f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2050f3f0edfdSDmitry Karpeev    */
20517a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
205216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2053435a35e8SMatthew G Knepley     PetscSection section;
2054435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2055435a35e8SMatthew G Knepley 
20569566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20579566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2058435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2059f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20609566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20619566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20629566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2063435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2064435a35e8SMatthew G Knepley         const char *fieldName;
2065435a35e8SMatthew G Knepley 
20669566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
206703dc3394SMatthew G. Knepley         if (namelist) {
20689566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20699566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2070435a35e8SMatthew G Knepley         }
207103dc3394SMatthew G. Knepley       }
2072435a35e8SMatthew G Knepley     } else {
20739566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2074e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20750298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2076e7c4fc90SDmitry Karpeev     }
2077dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
207916621825SDmitry Karpeev }
208016621825SDmitry Karpeev 
2081412a4547SAlexis Marboeuf /*@C
208220f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
208320f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2084435a35e8SMatthew G Knepley 
2085435a35e8SMatthew G Knepley   Not collective
2086435a35e8SMatthew G Knepley 
2087435a35e8SMatthew G Knepley   Input Parameters:
2088bb7acecfSBarry Smith + dm        - The `DM` object
2089bb7acecfSBarry Smith . numFields - The number of fields to select
20902adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2091435a35e8SMatthew G Knepley 
2092435a35e8SMatthew G Knepley   Output Parameters:
2093bb7acecfSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`
2094bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2095435a35e8SMatthew G Knepley 
209620f4b53cSBarry Smith   Level: intermediate
209720f4b53cSBarry Smith 
2098bb7acecfSBarry Smith   Note:
2099bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21005d3b26e6SMatthew G. Knepley 
210160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2102435a35e8SMatthew G Knepley @*/
2103d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2104d71ae5a4SJacob Faibussowitsch {
2105435a35e8SMatthew G Knepley   PetscFunctionBegin;
2106435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21074f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
21084f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
21094f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2110dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2112435a35e8SMatthew G Knepley }
2113435a35e8SMatthew G Knepley 
21142adcc780SMatthew G. Knepley /*@C
2115bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21162adcc780SMatthew G. Knepley 
21172adcc780SMatthew G. Knepley   Not collective
21182adcc780SMatthew G. Knepley 
2119d8d19677SJose E. Roman   Input Parameters:
2120bb7acecfSBarry Smith + dms - The `DM` objects
2121bb7acecfSBarry Smith - n   - The number of `DM`s
21222adcc780SMatthew G. Knepley 
21232adcc780SMatthew G. Knepley   Output Parameters:
2124bb7acecfSBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or NULL
2125bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21262adcc780SMatthew G. Knepley 
212720f4b53cSBarry Smith   Level: intermediate
212820f4b53cSBarry Smith 
2129bb7acecfSBarry Smith   Note:
2130bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21315d3b26e6SMatthew G. Knepley 
21321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
21332adcc780SMatthew G. Knepley @*/
2134d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
2135d71ae5a4SJacob Faibussowitsch {
21362adcc780SMatthew G. Knepley   PetscInt i;
21372adcc780SMatthew G. Knepley 
21382adcc780SMatthew G. Knepley   PetscFunctionBegin;
21394f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2140ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21414f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
21424f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2143bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2144bb7acecfSBarry Smith   if (n) {
2145b9d85ea2SLisandro Dalcin     DM dm = dms[0];
214600045ab3SPierre Jolivet     PetscCheck(dm->ops->createsuperdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No method createsuperdm for DM of type %s", ((PetscObject)dm)->type_name);
2147dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21482adcc780SMatthew G. Knepley   }
21493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21502adcc780SMatthew G. Knepley }
21512adcc780SMatthew G. Knepley 
215216621825SDmitry Karpeev /*@C
2153a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2154a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
215516621825SDmitry Karpeev 
215620f4b53cSBarry Smith   Not Collective
215716621825SDmitry Karpeev 
215816621825SDmitry Karpeev   Input Parameter:
2159bb7acecfSBarry Smith . dm - the `DM` object
216016621825SDmitry Karpeev 
216116621825SDmitry Karpeev   Output Parameters:
216220f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
216320f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
21640298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
21650298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
216620f4b53cSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if `NULL` is returned, no `DM`s are defined)
216716621825SDmitry Karpeev 
216816621825SDmitry Karpeev   Level: intermediate
216916621825SDmitry Karpeev 
2170bb7acecfSBarry Smith   Note:
2171a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2172a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2173a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2174a4e35b19SJacob Faibussowitsch 
2175a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2176a4e35b19SJacob Faibussowitsch 
217716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2178bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2179bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
218016621825SDmitry Karpeev 
2181a4e35b19SJacob Faibussowitsch   Developer Notes:
218220f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2183bb7acecfSBarry Smith 
218460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
218516621825SDmitry Karpeev @*/
2186d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2187d71ae5a4SJacob Faibussowitsch {
2188be081cd6SPeter Brune   DMSubDomainHookLink link;
2189be081cd6SPeter Brune   PetscInt            i, l;
219016621825SDmitry Karpeev 
219116621825SDmitry Karpeev   PetscFunctionBegin;
219216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21939371c9d4SSatish Balay   if (n) {
21944f572ea9SToby Isaac     PetscAssertPointer(n, 2);
21959371c9d4SSatish Balay     *n = 0;
21969371c9d4SSatish Balay   }
21979371c9d4SSatish Balay   if (namelist) {
21984f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
21999371c9d4SSatish Balay     *namelist = NULL;
22009371c9d4SSatish Balay   }
22019371c9d4SSatish Balay   if (innerislist) {
22024f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
22039371c9d4SSatish Balay     *innerislist = NULL;
22049371c9d4SSatish Balay   }
22059371c9d4SSatish Balay   if (outerislist) {
22064f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
22079371c9d4SSatish Balay     *outerislist = NULL;
22089371c9d4SSatish Balay   }
22099371c9d4SSatish Balay   if (dmlist) {
22104f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
22119371c9d4SSatish Balay     *dmlist = NULL;
22129371c9d4SSatish Balay   }
2213f3f0edfdSDmitry Karpeev   /*
2214f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2215f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2216f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2217f3f0edfdSDmitry Karpeev    */
22187a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
221916621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2220dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
222114a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2222f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2223be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2224be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22259566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2226be081cd6SPeter Brune         }
2227648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2228e7c4fc90SDmitry Karpeev       }
222914a18fd3SPeter Brune     }
2230bb7acecfSBarry Smith     if (n) *n = l;
223114a18fd3SPeter Brune   }
22323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2233e30e807fSPeter Brune }
2234e30e807fSPeter Brune 
2235e30e807fSPeter Brune /*@C
2236e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2237e30e807fSPeter Brune 
223820f4b53cSBarry Smith   Not Collective
2239e30e807fSPeter Brune 
2240e30e807fSPeter Brune   Input Parameters:
2241bb7acecfSBarry Smith + dm     - the `DM` object
2242e30e807fSPeter Brune . n      - the number of subdomain scatters
2243e30e807fSPeter Brune - subdms - the local subdomains
2244e30e807fSPeter Brune 
2245e30e807fSPeter Brune   Output Parameters:
22466b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2247e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2248e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2249e30e807fSPeter Brune 
225020f4b53cSBarry Smith   Level: developer
225120f4b53cSBarry Smith 
2252bb7acecfSBarry Smith   Note:
2253bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2254e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2255e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2256e30e807fSPeter Brune   solution and residual data.
2257e30e807fSPeter Brune 
2258a4e35b19SJacob Faibussowitsch   Developer Notes:
2259a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2260a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2261bb7acecfSBarry Smith 
22621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2263e30e807fSPeter Brune @*/
2264d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat)
2265d71ae5a4SJacob Faibussowitsch {
2266e30e807fSPeter Brune   PetscFunctionBegin;
2267e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22684f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2269dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2271e7c4fc90SDmitry Karpeev }
2272e7c4fc90SDmitry Karpeev 
227347c6ae99SBarry Smith /*@
2274bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
227547c6ae99SBarry Smith 
227620f4b53cSBarry Smith   Collective
227747c6ae99SBarry Smith 
2278d8d19677SJose E. Roman   Input Parameters:
2279bb7acecfSBarry Smith + dm   - the `DM` object
2280bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
228147c6ae99SBarry Smith 
228247c6ae99SBarry Smith   Output Parameter:
228320f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2284ae0a1c52SMatthew G Knepley 
228520f4b53cSBarry Smith   Options Database Key:
2286412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2287412e9a14SMatthew G. Knepley 
228847c6ae99SBarry Smith   Level: developer
228947c6ae99SBarry Smith 
229020f4b53cSBarry Smith   Note:
229120f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
229220f4b53cSBarry Smith 
22931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
229447c6ae99SBarry Smith @*/
2295d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2296d71ae5a4SJacob Faibussowitsch {
2297c833c3b5SJed Brown   DMRefineHookLink link;
229847c6ae99SBarry Smith 
229947c6ae99SBarry Smith   PetscFunctionBegin;
2300732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23019566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2302dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
23034057135bSMatthew G Knepley   if (*dmf) {
230443842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
23058865f1eaSKarl Rupp 
23069566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23078865f1eaSKarl Rupp 
2308644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23090598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2310656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23118865f1eaSKarl Rupp 
23129566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2313c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23141baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2315c833c3b5SJed Brown     }
2316c833c3b5SJed Brown   }
23179566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2319c833c3b5SJed Brown }
2320c833c3b5SJed Brown 
2321bb9467b5SJed Brown /*@C
2322c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2323c833c3b5SJed Brown 
232420f4b53cSBarry Smith   Logically Collective; No Fortran Support
2325c833c3b5SJed Brown 
23264165533cSJose E. Roman   Input Parameters:
2327bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2328bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2329f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
233020f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2331c833c3b5SJed Brown 
233220f4b53cSBarry Smith   Calling sequence of `refinehook`:
2333bb7acecfSBarry Smith + coarse - coarse level `DM`
2334bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2335c833c3b5SJed Brown - ctx    - optional user-defined function context
2336c833c3b5SJed Brown 
233720f4b53cSBarry Smith   Calling sequence of `interphook`:
2338bb7acecfSBarry Smith + coarse - coarse level `DM`
2339c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2340bb7acecfSBarry Smith . fine   - fine level `DM` to update
2341c833c3b5SJed Brown - ctx    - optional user-defined function context
2342c833c3b5SJed Brown 
2343c833c3b5SJed Brown   Level: advanced
2344c833c3b5SJed Brown 
2345c833c3b5SJed Brown   Notes:
2346bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2347bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2348bb7acecfSBarry Smith 
2349bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2350c833c3b5SJed Brown 
2351c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2352c833c3b5SJed Brown 
23531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2354c833c3b5SJed Brown @*/
2355a4e35b19SJacob 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)
2356d71ae5a4SJacob Faibussowitsch {
2357c833c3b5SJed Brown   DMRefineHookLink link, *p;
2358c833c3b5SJed Brown 
2359c833c3b5SJed Brown   PetscFunctionBegin;
2360c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23613d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23623ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23633d8e3701SJed Brown   }
23649566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2365c833c3b5SJed Brown   link->refinehook = refinehook;
2366c833c3b5SJed Brown   link->interphook = interphook;
2367c833c3b5SJed Brown   link->ctx        = ctx;
23680298fd71SBarry Smith   link->next       = NULL;
2369c833c3b5SJed Brown   *p               = link;
23703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2371c833c3b5SJed Brown }
2372c833c3b5SJed Brown 
23733d8e3701SJed Brown /*@C
2374bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2375bb7acecfSBarry Smith   a nonlinear problem to a finer grid
23763d8e3701SJed Brown 
237720f4b53cSBarry Smith   Logically Collective; No Fortran Support
23783d8e3701SJed Brown 
23794165533cSJose E. Roman   Input Parameters:
2380bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2381bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2382bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
238320f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
23843d8e3701SJed Brown 
23853d8e3701SJed Brown   Level: advanced
23863d8e3701SJed Brown 
2387bb7acecfSBarry Smith   Note:
23883d8e3701SJed Brown   This function does nothing if the hook is not in the list.
23893d8e3701SJed Brown 
23901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
23913d8e3701SJed Brown @*/
2392d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2393d71ae5a4SJacob Faibussowitsch {
23943d8e3701SJed Brown   DMRefineHookLink link, *p;
23953d8e3701SJed Brown 
23963d8e3701SJed Brown   PetscFunctionBegin;
23973d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23983d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
23993d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
24003d8e3701SJed Brown       link = *p;
24013d8e3701SJed Brown       *p   = link->next;
24029566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
24033d8e3701SJed Brown       break;
24043d8e3701SJed Brown     }
24053d8e3701SJed Brown   }
24063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24073d8e3701SJed Brown }
24083d8e3701SJed Brown 
2409c833c3b5SJed Brown /*@
2410bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2411c833c3b5SJed Brown 
2412c833c3b5SJed Brown   Collective if any hooks are
2413c833c3b5SJed Brown 
24144165533cSJose E. Roman   Input Parameters:
2415bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2416bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2417bb7acecfSBarry Smith - fine   - finer `DM` to update
2418c833c3b5SJed Brown 
2419c833c3b5SJed Brown   Level: developer
2420c833c3b5SJed Brown 
242160225df5SJacob Faibussowitsch   Developer Notes:
2422bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2423bb7acecfSBarry Smith   an API with consistent terminology.
2424bb7acecfSBarry Smith 
24251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2426c833c3b5SJed Brown @*/
2427d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2428d71ae5a4SJacob Faibussowitsch {
2429c833c3b5SJed Brown   DMRefineHookLink link;
2430c833c3b5SJed Brown 
2431c833c3b5SJed Brown   PetscFunctionBegin;
2432c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24331baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24344057135bSMatthew G Knepley   }
24353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
243647c6ae99SBarry Smith }
243747c6ae99SBarry Smith 
2438eb3f98d2SBarry Smith /*@
24391f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24401f3379b2SToby Isaac 
244120f4b53cSBarry Smith   Collective
24421f3379b2SToby Isaac 
24434165533cSJose E. Roman   Input Parameters:
2444bb7acecfSBarry Smith + coarse    - coarse `DM`
2445bb7acecfSBarry Smith . fine      - fine `DM`
2446bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2447bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2448bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24491f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
24501f3379b2SToby Isaac 
24514165533cSJose E. Roman   Output Parameter:
24521f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
24531f3379b2SToby Isaac 
24541f3379b2SToby Isaac   Level: developer
24551f3379b2SToby Isaac 
2456bb7acecfSBarry Smith   Note:
2457bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
24581f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24591f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24601f3379b2SToby Isaac   slope-limiting reconstruction.
24611f3379b2SToby Isaac 
246260225df5SJacob Faibussowitsch   Developer Notes:
2463bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2464bb7acecfSBarry Smith 
24651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24661f3379b2SToby Isaac @*/
2467d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2468d71ae5a4SJacob Faibussowitsch {
24691f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24701f3379b2SToby Isaac 
24711f3379b2SToby Isaac   PetscFunctionBegin;
24721f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24731f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24741f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24751f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24761f3379b2SToby Isaac 
24779566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24781f3379b2SToby Isaac   if (interpsol) {
24799566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24801f3379b2SToby Isaac   } else if (interp) {
24819566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
248298921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24841f3379b2SToby Isaac }
24851f3379b2SToby Isaac 
24861f3379b2SToby Isaac /*@
2487bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2488eb3f98d2SBarry Smith 
2489eb3f98d2SBarry Smith   Not Collective
2490eb3f98d2SBarry Smith 
2491eb3f98d2SBarry Smith   Input Parameter:
2492bb7acecfSBarry Smith . dm - the `DM` object
2493eb3f98d2SBarry Smith 
2494eb3f98d2SBarry Smith   Output Parameter:
2495eb3f98d2SBarry Smith . level - number of refinements
2496eb3f98d2SBarry Smith 
2497eb3f98d2SBarry Smith   Level: developer
2498eb3f98d2SBarry Smith 
2499bb7acecfSBarry Smith   Note:
2500bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2501bb7acecfSBarry Smith 
25021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2503eb3f98d2SBarry Smith @*/
2504d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2505d71ae5a4SJacob Faibussowitsch {
2506eb3f98d2SBarry Smith   PetscFunctionBegin;
2507eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2508eb3f98d2SBarry Smith   *level = dm->levelup;
25093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2510eb3f98d2SBarry Smith }
2511eb3f98d2SBarry Smith 
2512fef3a512SBarry Smith /*@
2513bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2514fef3a512SBarry Smith 
2515fef3a512SBarry Smith   Not Collective
2516fef3a512SBarry Smith 
2517d8d19677SJose E. Roman   Input Parameters:
2518bb7acecfSBarry Smith + dm    - the `DM` object
2519fef3a512SBarry Smith - level - number of refinements
2520fef3a512SBarry Smith 
2521fef3a512SBarry Smith   Level: advanced
2522fef3a512SBarry Smith 
252395452b02SPatrick Sanan   Notes:
2524bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2525fef3a512SBarry Smith 
2526bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2527bb7acecfSBarry Smith 
25281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2529fef3a512SBarry Smith @*/
2530d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2531d71ae5a4SJacob Faibussowitsch {
2532fef3a512SBarry Smith   PetscFunctionBegin;
2533fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2534fef3a512SBarry Smith   dm->levelup = level;
25353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2536fef3a512SBarry Smith }
2537fef3a512SBarry Smith 
2538d410b0cfSMatthew G. Knepley /*@
2539bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2540d410b0cfSMatthew G. Knepley 
254120f4b53cSBarry Smith   Collective
2542d410b0cfSMatthew G. Knepley 
2543f1a722f8SMatthew G. Knepley   Input Parameters:
2544bb7acecfSBarry Smith + dm     - the `DM` object
2545d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2546d410b0cfSMatthew G. Knepley 
2547d410b0cfSMatthew G. Knepley   Output Parameter:
254820f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2549d410b0cfSMatthew G. Knepley 
2550d410b0cfSMatthew G. Knepley   Level: developer
2551d410b0cfSMatthew G. Knepley 
255220f4b53cSBarry Smith   Note:
255320f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
255420f4b53cSBarry Smith 
25551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2556d410b0cfSMatthew G. Knepley @*/
2557d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2558d71ae5a4SJacob Faibussowitsch {
2559d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2560d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2561dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2562d410b0cfSMatthew G. Knepley   if (*dme) {
2563d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25649566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2565d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25669566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2567d410b0cfSMatthew G. Knepley   }
25683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2569d410b0cfSMatthew G. Knepley }
2570d410b0cfSMatthew G. Knepley 
2571d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2572d71ae5a4SJacob Faibussowitsch {
2573ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2574ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25754f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2576ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
25773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2578ca3d3a14SMatthew G. Knepley }
2579ca3d3a14SMatthew G. Knepley 
2580d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2581d71ae5a4SJacob Faibussowitsch {
2582ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2583ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25844f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2585ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
25863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2587ca3d3a14SMatthew G. Knepley }
2588ca3d3a14SMatthew G. Knepley 
2589ca3d3a14SMatthew G. Knepley /*@
2590bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2591ca3d3a14SMatthew G. Knepley 
2592ca3d3a14SMatthew G. Knepley   Input Parameter:
259320f4b53cSBarry Smith . dm - The `DM`
2594ca3d3a14SMatthew G. Knepley 
2595ca3d3a14SMatthew G. Knepley   Output Parameter:
259620f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2597ca3d3a14SMatthew G. Knepley 
2598ca3d3a14SMatthew G. Knepley   Level: developer
2599ca3d3a14SMatthew G. Knepley 
26001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2601ca3d3a14SMatthew G. Knepley @*/
2602d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2603d71ae5a4SJacob Faibussowitsch {
2604ca3d3a14SMatthew G. Knepley   Vec tv;
2605ca3d3a14SMatthew G. Knepley 
2606ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2607ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26084f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
26099566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2610ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2612ca3d3a14SMatthew G. Knepley }
2613ca3d3a14SMatthew G. Knepley 
2614d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2615d71ae5a4SJacob Faibussowitsch {
2616ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2617ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2618ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2619ca3d3a14SMatthew G. Knepley 
2620ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26219566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26229566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26239566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26249566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26259566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26269566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26279566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26289566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2629ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26309566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2631ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2632ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2633ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26349566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2635ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26369566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26379566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2638ca3d3a14SMatthew G. Knepley     }
2639ca3d3a14SMatthew G. Knepley   }
26409566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26419566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26429566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2643ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2644ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26459566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2646ca3d3a14SMatthew G. Knepley       if (dof) {
2647ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2648ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2649ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2650ca3d3a14SMatthew G. Knepley 
2651ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26529566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26539566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26549566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2655ca3d3a14SMatthew G. Knepley       }
2656ca3d3a14SMatthew G. Knepley     }
2657ca3d3a14SMatthew G. Knepley   }
26589566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2660ca3d3a14SMatthew G. Knepley }
2661ca3d3a14SMatthew G. Knepley 
2662d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2663d71ae5a4SJacob Faibussowitsch {
2664ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2665ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2666ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2667ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2668ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2669ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2670ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26719566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2673ca3d3a14SMatthew G. Knepley }
2674ca3d3a14SMatthew G. Knepley 
2675bb9467b5SJed Brown /*@C
2676bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2677baf369e7SPeter Brune 
267820f4b53cSBarry Smith   Logically Collective
2679baf369e7SPeter Brune 
26804165533cSJose E. Roman   Input Parameters:
2681bb7acecfSBarry Smith + dm        - the `DM`
2682bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2683bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
268420f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2685baf369e7SPeter Brune 
268620f4b53cSBarry Smith   Calling sequence of `beginhook`:
2687a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2688baf369e7SPeter Brune . g    - global vector
2689baf369e7SPeter Brune . mode - mode
2690baf369e7SPeter Brune . l    - local vector
2691baf369e7SPeter Brune - ctx  - optional user-defined function context
2692baf369e7SPeter Brune 
269320f4b53cSBarry Smith   Calling sequence of `endhook`:
2694a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2695a4e35b19SJacob Faibussowitsch . g    - global vector
2696a4e35b19SJacob Faibussowitsch . mode - mode
2697a4e35b19SJacob Faibussowitsch . l    - local vector
2698baf369e7SPeter Brune - ctx  - optional user-defined function context
2699baf369e7SPeter Brune 
2700baf369e7SPeter Brune   Level: advanced
2701baf369e7SPeter Brune 
2702bb7acecfSBarry Smith   Note:
2703bb7acecfSBarry 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.
2704bb7acecfSBarry Smith 
27051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2706baf369e7SPeter Brune @*/
2707a4e35b19SJacob 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)
2708d71ae5a4SJacob Faibussowitsch {
2709baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2710baf369e7SPeter Brune 
2711baf369e7SPeter Brune   PetscFunctionBegin;
2712baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2713baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27149566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2715baf369e7SPeter Brune   link->beginhook = beginhook;
2716baf369e7SPeter Brune   link->endhook   = endhook;
2717baf369e7SPeter Brune   link->ctx       = ctx;
27180298fd71SBarry Smith   link->next      = NULL;
2719baf369e7SPeter Brune   *p              = link;
27203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2721baf369e7SPeter Brune }
2722baf369e7SPeter Brune 
2723d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2724d71ae5a4SJacob Faibussowitsch {
27254c274da1SToby Isaac   Mat          cMat;
272679769bd5SJed Brown   Vec          cVec, cBias;
27274c274da1SToby Isaac   PetscSection section, cSec;
27284c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27294c274da1SToby Isaac 
27304c274da1SToby Isaac   PetscFunctionBegin;
2731a4e35b19SJacob Faibussowitsch   (void)g;
2732a4e35b19SJacob Faibussowitsch   (void)ctx;
27334c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27349566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27354c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27365db9a05bSToby Isaac     PetscInt nRows;
27375db9a05bSToby Isaac 
27389566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27393ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27409566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27419566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27429566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27439566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27449566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27454c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27469566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27474c274da1SToby Isaac       if (dof) {
27484c274da1SToby Isaac         PetscScalar *vals;
27499566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27509566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27514c274da1SToby Isaac       }
27524c274da1SToby Isaac     }
27539566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27544c274da1SToby Isaac   }
27553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27564c274da1SToby Isaac }
27574c274da1SToby Isaac 
275847c6ae99SBarry Smith /*@
275901729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
276001729b5cSPatrick Sanan 
276120f4b53cSBarry Smith   Neighbor-wise Collective
276201729b5cSPatrick Sanan 
276301729b5cSPatrick Sanan   Input Parameters:
2764bb7acecfSBarry Smith + dm   - the `DM` object
276501729b5cSPatrick Sanan . g    - the global vector
2766bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
276701729b5cSPatrick Sanan - l    - the local vector
276801729b5cSPatrick Sanan 
276920f4b53cSBarry Smith   Level: beginner
277020f4b53cSBarry Smith 
277101729b5cSPatrick Sanan   Notes:
2772bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2773bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2774bb7acecfSBarry Smith 
2775bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
277601729b5cSPatrick Sanan 
27771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
277860225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2779bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
278001729b5cSPatrick Sanan @*/
2781d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2782d71ae5a4SJacob Faibussowitsch {
278301729b5cSPatrick Sanan   PetscFunctionBegin;
27849566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27859566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
27863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
278701729b5cSPatrick Sanan }
278801729b5cSPatrick Sanan 
278901729b5cSPatrick Sanan /*@
279047c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
279147c6ae99SBarry Smith 
279220f4b53cSBarry Smith   Neighbor-wise Collective
279347c6ae99SBarry Smith 
279447c6ae99SBarry Smith   Input Parameters:
2795bb7acecfSBarry Smith + dm   - the `DM` object
279647c6ae99SBarry Smith . g    - the global vector
2797bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
279847c6ae99SBarry Smith - l    - the local vector
279947c6ae99SBarry Smith 
280001729b5cSPatrick Sanan   Level: intermediate
280147c6ae99SBarry Smith 
2802bb7acecfSBarry Smith   Notes:
2803bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2804bb7acecfSBarry Smith 
2805bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2806bb7acecfSBarry Smith 
2807bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2808bb7acecfSBarry Smith 
2809bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2810bb7acecfSBarry Smith 
281160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
281247c6ae99SBarry Smith @*/
2813d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2814d71ae5a4SJacob Faibussowitsch {
28157128ae9fSMatthew G Knepley   PetscSF                 sf;
2816baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
281747c6ae99SBarry Smith 
281847c6ae99SBarry Smith   PetscFunctionBegin;
2819171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2820baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28211baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2822baf369e7SPeter Brune   }
28239566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28247128ae9fSMatthew G Knepley   if (sf) {
2825ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2826ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2827d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28287128ae9fSMatthew G Knepley 
28297a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28309566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28319566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28329566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28339566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28349566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28357128ae9fSMatthew G Knepley   } else {
2836fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28377128ae9fSMatthew G Knepley   }
28383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
283947c6ae99SBarry Smith }
284047c6ae99SBarry Smith 
284147c6ae99SBarry Smith /*@
284247c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
284347c6ae99SBarry Smith 
284420f4b53cSBarry Smith   Neighbor-wise Collective
284547c6ae99SBarry Smith 
284647c6ae99SBarry Smith   Input Parameters:
2847bb7acecfSBarry Smith + dm   - the `DM` object
284847c6ae99SBarry Smith . g    - the global vector
2849bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
285047c6ae99SBarry Smith - l    - the local vector
285147c6ae99SBarry Smith 
285201729b5cSPatrick Sanan   Level: intermediate
285347c6ae99SBarry Smith 
2854bb7acecfSBarry Smith   Note:
2855bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2856bb7acecfSBarry Smith 
285760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
285847c6ae99SBarry Smith @*/
2859d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2860d71ae5a4SJacob Faibussowitsch {
28617128ae9fSMatthew G Knepley   PetscSF                 sf;
2862ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2863ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2864ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2865baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2866d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
286747c6ae99SBarry Smith 
286847c6ae99SBarry Smith   PetscFunctionBegin;
2869171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28709566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28719566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28727128ae9fSMatthew G Knepley   if (sf) {
28737a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28747128ae9fSMatthew G Knepley 
28759566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28769566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28779566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28789566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28799566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28809566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28817128ae9fSMatthew G Knepley   } else {
2882fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28837128ae9fSMatthew G Knepley   }
28849566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2885baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28869566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2887baf369e7SPeter Brune   }
28883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
288947c6ae99SBarry Smith }
289047c6ae99SBarry Smith 
2891d4d07f1eSToby Isaac /*@C
2892d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2893d4d07f1eSToby Isaac 
289420f4b53cSBarry Smith   Logically Collective
2895d4d07f1eSToby Isaac 
28964165533cSJose E. Roman   Input Parameters:
2897bb7acecfSBarry Smith + dm        - the `DM`
2898bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2899bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
290020f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2901d4d07f1eSToby Isaac 
290220f4b53cSBarry Smith   Calling sequence of `beginhook`:
2903a4e35b19SJacob Faibussowitsch + global - global `DM`
2904d4d07f1eSToby Isaac . l      - local vector
2905d4d07f1eSToby Isaac . mode   - mode
2906d4d07f1eSToby Isaac . g      - global vector
2907d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2908d4d07f1eSToby Isaac 
290920f4b53cSBarry Smith   Calling sequence of `endhook`:
2910bb7acecfSBarry Smith + global - global `DM`
2911d4d07f1eSToby Isaac . l      - local vector
2912d4d07f1eSToby Isaac . mode   - mode
2913d4d07f1eSToby Isaac . g      - global vector
2914d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2915d4d07f1eSToby Isaac 
2916d4d07f1eSToby Isaac   Level: advanced
2917d4d07f1eSToby Isaac 
29181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2919d4d07f1eSToby Isaac @*/
2920a4e35b19SJacob 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)
2921d71ae5a4SJacob Faibussowitsch {
2922d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2923d4d07f1eSToby Isaac 
2924d4d07f1eSToby Isaac   PetscFunctionBegin;
2925d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2926d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29279566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2928d4d07f1eSToby Isaac   link->beginhook = beginhook;
2929d4d07f1eSToby Isaac   link->endhook   = endhook;
2930d4d07f1eSToby Isaac   link->ctx       = ctx;
2931d4d07f1eSToby Isaac   link->next      = NULL;
2932d4d07f1eSToby Isaac   *p              = link;
29333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2934d4d07f1eSToby Isaac }
2935d4d07f1eSToby Isaac 
2936d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2937d71ae5a4SJacob Faibussowitsch {
29384c274da1SToby Isaac   PetscFunctionBegin;
2939a4e35b19SJacob Faibussowitsch   (void)g;
2940a4e35b19SJacob Faibussowitsch   (void)ctx;
29414c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29422b6b7d09SToby Isaac   if (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES) {
29432b6b7d09SToby Isaac     Mat          cMat;
29442b6b7d09SToby Isaac     Vec          cVec;
29455db9a05bSToby Isaac     PetscInt     nRows;
29462b6b7d09SToby Isaac     PetscSection section, cSec;
29472b6b7d09SToby Isaac     PetscInt     pStart, pEnd, p, dof;
29482b6b7d09SToby Isaac 
29492b6b7d09SToby Isaac     PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29502b6b7d09SToby Isaac     if (!cMat) PetscFunctionReturn(PETSC_SUCCESS);
29515db9a05bSToby Isaac 
29529566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29533ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29549566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29559566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29569566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29574c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29589566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29594c274da1SToby Isaac       if (dof) {
29604c274da1SToby Isaac         PetscInt     d;
29614c274da1SToby Isaac         PetscScalar *vals;
29629566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29639566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29644c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29654c274da1SToby Isaac          * we just extracted */
2966ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29674c274da1SToby Isaac       }
29684c274da1SToby Isaac     }
29699566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29709566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29714c274da1SToby Isaac   }
29723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29734c274da1SToby Isaac }
297401729b5cSPatrick Sanan /*@
297501729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
297601729b5cSPatrick Sanan 
297720f4b53cSBarry Smith   Neighbor-wise Collective
297801729b5cSPatrick Sanan 
297901729b5cSPatrick Sanan   Input Parameters:
2980bb7acecfSBarry Smith + dm   - the `DM` object
298101729b5cSPatrick Sanan . l    - the local vector
2982bb7acecfSBarry 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.
298301729b5cSPatrick Sanan - g    - the global vector
298401729b5cSPatrick Sanan 
298520f4b53cSBarry Smith   Level: beginner
298620f4b53cSBarry Smith 
298701729b5cSPatrick Sanan   Notes:
298801729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
2989bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
299001729b5cSPatrick Sanan 
2991bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2992bb7acecfSBarry Smith 
2993bb7acecfSBarry 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.
2994bb7acecfSBarry Smith 
2995bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
299601729b5cSPatrick Sanan 
29971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
299801729b5cSPatrick Sanan @*/
2999d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
3000d71ae5a4SJacob Faibussowitsch {
300101729b5cSPatrick Sanan   PetscFunctionBegin;
30029566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
30039566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
30043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
300501729b5cSPatrick Sanan }
30064c274da1SToby Isaac 
300747c6ae99SBarry Smith /*@
300801729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
30099a42bb27SBarry Smith 
301020f4b53cSBarry Smith   Neighbor-wise Collective
30119a42bb27SBarry Smith 
30129a42bb27SBarry Smith   Input Parameters:
3013bb7acecfSBarry Smith + dm   - the `DM` object
3014f6813fd5SJed Brown . l    - the local vector
3015aa624791SPierre 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.
30161eb28f2eSBarry Smith - g    - the global vector
30179a42bb27SBarry Smith 
301820f4b53cSBarry Smith   Level: intermediate
301920f4b53cSBarry Smith 
302095452b02SPatrick Sanan   Notes:
3021bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3022bb7acecfSBarry Smith 
3023bb7acecfSBarry 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.
3024bb7acecfSBarry Smith 
3025bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3026bb7acecfSBarry Smith 
3027bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3028bb7acecfSBarry Smith 
3029bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30309a42bb27SBarry Smith 
30311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30329a42bb27SBarry Smith @*/
3033d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3034d71ae5a4SJacob Faibussowitsch {
30357128ae9fSMatthew G Knepley   PetscSF                 sf;
303684330215SMatthew G. Knepley   PetscSection            s, gs;
3037d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3038ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3039ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3040ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3041fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3042d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30439a42bb27SBarry Smith 
30449a42bb27SBarry Smith   PetscFunctionBegin;
3045171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3046d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30471baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3048d4d07f1eSToby Isaac   }
30499566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30509566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30519566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30527128ae9fSMatthew G Knepley   switch (mode) {
30537128ae9fSMatthew G Knepley   case INSERT_VALUES:
30547128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3055d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3056d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3057d71ae5a4SJacob Faibussowitsch     break;
30587128ae9fSMatthew G Knepley   case ADD_VALUES:
30597128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3060d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3061d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3062d71ae5a4SJacob Faibussowitsch     break;
3063d71ae5a4SJacob Faibussowitsch   default:
3064d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30657128ae9fSMatthew G Knepley   }
3066ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30679566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3068ca3d3a14SMatthew G. Knepley     if (transform) {
30699566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30709566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30719566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30729566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3073fa88e482SJed Brown     } else if (isInsert) {
30749566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3075fa88e482SJed Brown     } else {
30769566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3077fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3078ca3d3a14SMatthew G. Knepley     }
3079fa88e482SJed Brown     if (s && isInsert) {
30809566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3081fa88e482SJed Brown     } else {
30829566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3083fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3084fa88e482SJed Brown     }
3085ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30869566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
308784330215SMatthew G. Knepley     } else if (s && isInsert) {
308884330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
308984330215SMatthew G. Knepley 
30909566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
30919566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
30929566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
309384330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3094b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
309584330215SMatthew G. Knepley 
30969566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
30979566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
30989566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
30999566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
31009566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
31019566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3102b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
310303442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
31047a8be351SBarry 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);
3105b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3106b3b16f48SMatthew G. Knepley         if (!gcdof) {
310784330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3108b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3109b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
311084330215SMatthew G. Knepley           const PetscInt *cdofs;
311184330215SMatthew G. Knepley           PetscInt        cind = 0;
311284330215SMatthew G. Knepley 
31139566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3114b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31159371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31169371c9d4SSatish Balay               ++cind;
31179371c9d4SSatish Balay               continue;
31189371c9d4SSatish Balay             }
3119b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
312084330215SMatthew G. Knepley           }
31217a8be351SBarry 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);
312284330215SMatthew G. Knepley       }
3123ca3d3a14SMatthew G. Knepley     }
3124fa88e482SJed Brown     if (g_inplace) {
31259566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3126fa88e482SJed Brown     } else {
31279566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3128fa88e482SJed Brown     }
3129ca3d3a14SMatthew G. Knepley     if (transform) {
31309566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31319566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3132fa88e482SJed Brown     } else if (l_inplace) {
31339566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3134ca3d3a14SMatthew G. Knepley     } else {
31359566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3136ca3d3a14SMatthew G. Knepley     }
31377128ae9fSMatthew G Knepley   } else {
3138fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalbegin, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
31397128ae9fSMatthew G Knepley   }
31403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31419a42bb27SBarry Smith }
31429a42bb27SBarry Smith 
31439a42bb27SBarry Smith /*@
31449a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
314547c6ae99SBarry Smith 
314620f4b53cSBarry Smith   Neighbor-wise Collective
314747c6ae99SBarry Smith 
314847c6ae99SBarry Smith   Input Parameters:
3149bb7acecfSBarry Smith + dm   - the `DM` object
3150f6813fd5SJed Brown . l    - the local vector
3151bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3152f6813fd5SJed Brown - g    - the global vector
315347c6ae99SBarry Smith 
315401729b5cSPatrick Sanan   Level: intermediate
315547c6ae99SBarry Smith 
3156bb7acecfSBarry Smith   Note:
3157bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3158bb7acecfSBarry Smith 
315960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
316047c6ae99SBarry Smith @*/
3161d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3162d71ae5a4SJacob Faibussowitsch {
31637128ae9fSMatthew G Knepley   PetscSF                 sf;
316484330215SMatthew G. Knepley   PetscSection            s;
3165d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3166ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
316747c6ae99SBarry Smith 
316847c6ae99SBarry Smith   PetscFunctionBegin;
3169171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31709566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31719566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31727128ae9fSMatthew G Knepley   switch (mode) {
31737128ae9fSMatthew G Knepley   case INSERT_VALUES:
3174d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3175d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3176d71ae5a4SJacob Faibussowitsch     break;
31777128ae9fSMatthew G Knepley   case ADD_VALUES:
3178d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3179d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3180d71ae5a4SJacob Faibussowitsch     break;
3181d71ae5a4SJacob Faibussowitsch   default:
3182d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31837128ae9fSMatthew G Knepley   }
318484330215SMatthew G. Knepley   if (sf && !isInsert) {
3185ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3186ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3187ca3d3a14SMatthew G. Knepley     Vec                tmpl;
318884330215SMatthew G. Knepley 
31899566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3190ca3d3a14SMatthew G. Knepley     if (transform) {
31919566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31929566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3193ca3d3a14SMatthew G. Knepley     } else {
31949566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3195ca3d3a14SMatthew G. Knepley     }
31969566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
31979566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3198ca3d3a14SMatthew G. Knepley     if (transform) {
31999566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
32009566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3201ca3d3a14SMatthew G. Knepley     } else {
32029566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3203ca3d3a14SMatthew G. Knepley     }
32049566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
320584330215SMatthew G. Knepley   } else if (s && isInsert) {
32067128ae9fSMatthew G Knepley   } else {
3207fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalend, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
32087128ae9fSMatthew G Knepley   }
3209d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32109566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3211d4d07f1eSToby Isaac   }
32123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
321347c6ae99SBarry Smith }
321447c6ae99SBarry Smith 
3215f089877aSRichard Tran Mills /*@
3216a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3217a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3218a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3219f089877aSRichard Tran Mills 
322020f4b53cSBarry Smith   Neighbor-wise Collective
3221f089877aSRichard Tran Mills 
3222f089877aSRichard Tran Mills   Input Parameters:
3223bb7acecfSBarry Smith + dm   - the `DM` object
3224bc0a1609SRichard Tran Mills . g    - the original local vector
3225bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3226f089877aSRichard Tran Mills 
3227bc0a1609SRichard Tran Mills   Output Parameter:
3228bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3229f089877aSRichard Tran Mills 
3230f089877aSRichard Tran Mills   Level: intermediate
3231f089877aSRichard Tran Mills 
3232a4e35b19SJacob Faibussowitsch   Notes:
3233a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3234a4e35b19SJacob Faibussowitsch 
323560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3236f089877aSRichard Tran Mills @*/
3237d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3238d71ae5a4SJacob Faibussowitsch {
3239f089877aSRichard Tran Mills   PetscFunctionBegin;
3240f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32419f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32429f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32439f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3245f089877aSRichard Tran Mills }
3246f089877aSRichard Tran Mills 
3247f089877aSRichard Tran Mills /*@
3248bb7acecfSBarry Smith   DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3249bb7acecfSBarry Smith   points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3250f089877aSRichard Tran Mills 
325120f4b53cSBarry Smith   Neighbor-wise Collective
3252f089877aSRichard Tran Mills 
3253f089877aSRichard Tran Mills   Input Parameters:
325460225df5SJacob Faibussowitsch + dm   - the `DM` object
3255bc0a1609SRichard Tran Mills . g    - the original local vector
3256bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3257f089877aSRichard Tran Mills 
3258bc0a1609SRichard Tran Mills   Output Parameter:
3259bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3260f089877aSRichard Tran Mills 
3261f089877aSRichard Tran Mills   Level: intermediate
3262f089877aSRichard Tran Mills 
326360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3264f089877aSRichard Tran Mills @*/
3265d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3266d71ae5a4SJacob Faibussowitsch {
3267f089877aSRichard Tran Mills   PetscFunctionBegin;
3268f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32699f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32709f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32719f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3273f089877aSRichard Tran Mills }
3274f089877aSRichard Tran Mills 
327547c6ae99SBarry Smith /*@
3276bb7acecfSBarry Smith   DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
327747c6ae99SBarry Smith 
327820f4b53cSBarry Smith   Collective
327947c6ae99SBarry Smith 
3280d8d19677SJose E. Roman   Input Parameters:
3281bb7acecfSBarry Smith + dm   - the `DM` object
328220f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
328347c6ae99SBarry Smith 
328447c6ae99SBarry Smith   Output Parameter:
3285bb7acecfSBarry Smith . dmc - the coarsened `DM`
328647c6ae99SBarry Smith 
328747c6ae99SBarry Smith   Level: developer
328847c6ae99SBarry Smith 
32891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
329047c6ae99SBarry Smith @*/
3291d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3292d71ae5a4SJacob Faibussowitsch {
3293b17ce1afSJed Brown   DMCoarsenHookLink link;
329447c6ae99SBarry Smith 
329547c6ae99SBarry Smith   PetscFunctionBegin;
3296171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32979566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3298dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3299b9d85ea2SLisandro Dalcin   if (*dmc) {
3300a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
33019566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
330243842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
33039566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3304644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
33050598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3306656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
33079566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3308b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33099566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3310b17ce1afSJed Brown     }
3311b9d85ea2SLisandro Dalcin   }
33129566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33137a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3315b17ce1afSJed Brown }
3316b17ce1afSJed Brown 
3317bb9467b5SJed Brown /*@C
3318b17ce1afSJed Brown   DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3319b17ce1afSJed Brown 
332020f4b53cSBarry Smith   Logically Collective; No Fortran Support
3321b17ce1afSJed Brown 
33224165533cSJose E. Roman   Input Parameters:
3323bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3324b17ce1afSJed Brown . coarsenhook  - function to run when setting up a coarser level
3325bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
332620f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3327b17ce1afSJed Brown 
332820f4b53cSBarry Smith   Calling sequence of `coarsenhook`:
3329bb7acecfSBarry Smith + fine   - fine level `DM`
3330bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3331b17ce1afSJed Brown - ctx    - optional user-defined function context
3332b17ce1afSJed Brown 
333320f4b53cSBarry Smith   Calling sequence of `restricthook`:
3334bb7acecfSBarry Smith + fine      - fine level `DM`
3335bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3336c833c3b5SJed Brown . rscale    - scaling vector for restriction
3337c833c3b5SJed Brown . inject    - matrix restricting by injection
3338b17ce1afSJed Brown . coarse    - coarse level DM to update
3339b17ce1afSJed Brown - ctx       - optional user-defined function context
3340b17ce1afSJed Brown 
3341b17ce1afSJed Brown   Level: advanced
3342b17ce1afSJed Brown 
3343b17ce1afSJed Brown   Notes:
3344bb7acecfSBarry 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`.
3345b17ce1afSJed Brown 
3346b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3347b17ce1afSJed Brown 
3348b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3349bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3350b17ce1afSJed Brown 
3351bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3352bb7acecfSBarry Smith 
33531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3354b17ce1afSJed Brown @*/
3355a4e35b19SJacob 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)
3356d71ae5a4SJacob Faibussowitsch {
3357b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3358b17ce1afSJed Brown 
3359b17ce1afSJed Brown   PetscFunctionBegin;
3360b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33611e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33623ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33631e3d8eccSJed Brown   }
33649566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3365b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3366b17ce1afSJed Brown   link->restricthook = restricthook;
3367b17ce1afSJed Brown   link->ctx          = ctx;
33680298fd71SBarry Smith   link->next         = NULL;
3369b17ce1afSJed Brown   *p                 = link;
33703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3371b17ce1afSJed Brown }
3372b17ce1afSJed Brown 
3373dc822a44SJed Brown /*@C
3374bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3375dc822a44SJed Brown 
337620f4b53cSBarry Smith   Logically Collective; No Fortran Support
3377dc822a44SJed Brown 
33784165533cSJose E. Roman   Input Parameters:
3379bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3380dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3381bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
338220f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3383dc822a44SJed Brown 
3384dc822a44SJed Brown   Level: advanced
3385dc822a44SJed Brown 
3386bb7acecfSBarry Smith   Note:
3387dc822a44SJed Brown   This function does nothing if the hook is not in the list.
3388dc822a44SJed Brown 
33891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3390dc822a44SJed Brown @*/
3391d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3392d71ae5a4SJacob Faibussowitsch {
3393dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3394dc822a44SJed Brown 
3395dc822a44SJed Brown   PetscFunctionBegin;
3396dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3397dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3398dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3399dc822a44SJed Brown       link = *p;
3400dc822a44SJed Brown       *p   = link->next;
34019566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3402dc822a44SJed Brown       break;
3403dc822a44SJed Brown     }
3404dc822a44SJed Brown   }
34053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3406dc822a44SJed Brown }
3407dc822a44SJed Brown 
3408b17ce1afSJed Brown /*@
3409bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3410b17ce1afSJed Brown 
3411b17ce1afSJed Brown   Collective if any hooks are
3412b17ce1afSJed Brown 
34134165533cSJose E. Roman   Input Parameters:
3414bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3415bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3416e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3417bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
341820f4b53cSBarry Smith - coarse  - coarser `DM` to update
3419b17ce1afSJed Brown 
3420b17ce1afSJed Brown   Level: developer
3421b17ce1afSJed Brown 
342260225df5SJacob Faibussowitsch   Developer Notes:
3423bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3424bb7acecfSBarry Smith 
34251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3426b17ce1afSJed Brown @*/
3427d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3428d71ae5a4SJacob Faibussowitsch {
3429b17ce1afSJed Brown   DMCoarsenHookLink link;
3430b17ce1afSJed Brown 
3431b17ce1afSJed Brown   PetscFunctionBegin;
3432b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34331baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3434b17ce1afSJed Brown   }
34353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
343647c6ae99SBarry Smith }
343747c6ae99SBarry Smith 
3438bb9467b5SJed Brown /*@C
3439be081cd6SPeter Brune   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
34405dbd56e3SPeter Brune 
344120f4b53cSBarry Smith   Logically Collective; No Fortran Support
34425dbd56e3SPeter Brune 
34434165533cSJose E. Roman   Input Parameters:
3444bb7acecfSBarry Smith + global       - global `DM`
3445bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
34465dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
344720f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34485dbd56e3SPeter Brune 
344920f4b53cSBarry Smith   Calling sequence of `ddhook`:
3450bb7acecfSBarry Smith + global - global `DM`
3451bb7acecfSBarry Smith . block  - block `DM`
3452ec4806b8SPeter Brune - ctx    - optional user-defined function context
3453ec4806b8SPeter Brune 
345420f4b53cSBarry Smith   Calling sequence of `restricthook`:
3455bb7acecfSBarry Smith + global - global `DM`
34565dbd56e3SPeter Brune . out    - scatter to the outer (with ghost and overlap points) block vector
34575dbd56e3SPeter Brune . in     - scatter to block vector values only owned locally
3458bb7acecfSBarry Smith . block  - block `DM`
34595dbd56e3SPeter Brune - ctx    - optional user-defined function context
34605dbd56e3SPeter Brune 
34615dbd56e3SPeter Brune   Level: advanced
34625dbd56e3SPeter Brune 
34635dbd56e3SPeter Brune   Notes:
3464bb7acecfSBarry Smith   This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
34655dbd56e3SPeter Brune 
34665dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
34675dbd56e3SPeter Brune 
34685dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3469bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
34705dbd56e3SPeter Brune 
34711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
34725dbd56e3SPeter Brune @*/
3473a4e35b19SJacob 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)
3474d71ae5a4SJacob Faibussowitsch {
3475be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34765dbd56e3SPeter Brune 
34775dbd56e3SPeter Brune   PetscFunctionBegin;
34785dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3479b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
34803ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3481b3a6b972SJed Brown   }
34829566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
34835dbd56e3SPeter Brune   link->restricthook = restricthook;
3484be081cd6SPeter Brune   link->ddhook       = ddhook;
34855dbd56e3SPeter Brune   link->ctx          = ctx;
34860298fd71SBarry Smith   link->next         = NULL;
34875dbd56e3SPeter Brune   *p                 = link;
34883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
34895dbd56e3SPeter Brune }
34905dbd56e3SPeter Brune 
3491b3a6b972SJed Brown /*@C
3492b3a6b972SJed Brown   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3493b3a6b972SJed Brown 
349420f4b53cSBarry Smith   Logically Collective; No Fortran Support
3495b3a6b972SJed Brown 
34964165533cSJose E. Roman   Input Parameters:
3497bb7acecfSBarry Smith + global       - global `DM`
3498bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3499b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
350020f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3501b3a6b972SJed Brown 
3502b3a6b972SJed Brown   Level: advanced
3503b3a6b972SJed Brown 
35041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3505b3a6b972SJed Brown @*/
3506d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3507d71ae5a4SJacob Faibussowitsch {
3508b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3509b3a6b972SJed Brown 
3510b3a6b972SJed Brown   PetscFunctionBegin;
3511b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3512b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3513b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3514b3a6b972SJed Brown       link = *p;
3515b3a6b972SJed Brown       *p   = link->next;
35169566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3517b3a6b972SJed Brown       break;
3518b3a6b972SJed Brown     }
3519b3a6b972SJed Brown   }
35203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3521b3a6b972SJed Brown }
3522b3a6b972SJed Brown 
35235dbd56e3SPeter Brune /*@
3524bb7acecfSBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
35255dbd56e3SPeter Brune 
35265dbd56e3SPeter Brune   Collective if any hooks are
35275dbd56e3SPeter Brune 
35284165533cSJose E. Roman   Input Parameters:
3529a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3530a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3531a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3532a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
35335dbd56e3SPeter Brune 
35345dbd56e3SPeter Brune   Level: developer
35355dbd56e3SPeter Brune 
35361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`
35375dbd56e3SPeter Brune @*/
3538d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3539d71ae5a4SJacob Faibussowitsch {
3540be081cd6SPeter Brune   DMSubDomainHookLink link;
35415dbd56e3SPeter Brune 
35425dbd56e3SPeter Brune   PetscFunctionBegin;
3543be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35441baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35455dbd56e3SPeter Brune   }
35463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35475dbd56e3SPeter Brune }
35485dbd56e3SPeter Brune 
35495fe1f584SPeter Brune /*@
3550bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35515fe1f584SPeter Brune 
35525fe1f584SPeter Brune   Not Collective
35535fe1f584SPeter Brune 
35545fe1f584SPeter Brune   Input Parameter:
3555bb7acecfSBarry Smith . dm - the `DM` object
35565fe1f584SPeter Brune 
35575fe1f584SPeter Brune   Output Parameter:
35586a7d9d85SPeter Brune . level - number of coarsenings
35595fe1f584SPeter Brune 
35605fe1f584SPeter Brune   Level: developer
35615fe1f584SPeter Brune 
35621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35635fe1f584SPeter Brune @*/
3564d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3565d71ae5a4SJacob Faibussowitsch {
35665fe1f584SPeter Brune   PetscFunctionBegin;
35675fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35684f572ea9SToby Isaac   PetscAssertPointer(level, 2);
35695fe1f584SPeter Brune   *level = dm->leveldown;
35703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35715fe1f584SPeter Brune }
35725fe1f584SPeter Brune 
35739a64c4a8SMatthew G. Knepley /*@
3574bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
35759a64c4a8SMatthew G. Knepley 
357620f4b53cSBarry Smith   Collective
35779a64c4a8SMatthew G. Knepley 
35789a64c4a8SMatthew G. Knepley   Input Parameters:
3579bb7acecfSBarry Smith + dm    - the `DM` object
35809a64c4a8SMatthew G. Knepley - level - number of coarsenings
35819a64c4a8SMatthew G. Knepley 
35829a64c4a8SMatthew G. Knepley   Level: developer
35839a64c4a8SMatthew G. Knepley 
3584bb7acecfSBarry Smith   Note:
3585bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3586bb7acecfSBarry Smith 
358742747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35889a64c4a8SMatthew G. Knepley @*/
3589d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3590d71ae5a4SJacob Faibussowitsch {
35919a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
35929a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35939a64c4a8SMatthew G. Knepley   dm->leveldown = level;
35943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35959a64c4a8SMatthew G. Knepley }
35969a64c4a8SMatthew G. Knepley 
359747c6ae99SBarry Smith /*@C
3598bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
359947c6ae99SBarry Smith 
360020f4b53cSBarry Smith   Collective
360147c6ae99SBarry Smith 
3602d8d19677SJose E. Roman   Input Parameters:
3603bb7acecfSBarry Smith + dm      - the `DM` object
360447c6ae99SBarry Smith - nlevels - the number of levels of refinement
360547c6ae99SBarry Smith 
360647c6ae99SBarry Smith   Output Parameter:
3607bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
360847c6ae99SBarry Smith 
360947c6ae99SBarry Smith   Level: developer
361047c6ae99SBarry Smith 
36111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
361247c6ae99SBarry Smith @*/
3613d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3614d71ae5a4SJacob Faibussowitsch {
361547c6ae99SBarry Smith   PetscFunctionBegin;
3616171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36177a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36183ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36194f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
3620213acdd3SPierre Jolivet   if (dm->ops->refine && !dm->ops->refinehierarchy) {
362147c6ae99SBarry Smith     PetscInt i;
362247c6ae99SBarry Smith 
36239566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
362448a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3625213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
36263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
362747c6ae99SBarry Smith }
362847c6ae99SBarry Smith 
362947c6ae99SBarry Smith /*@C
3630bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
363147c6ae99SBarry Smith 
363220f4b53cSBarry Smith   Collective
363347c6ae99SBarry Smith 
3634d8d19677SJose E. Roman   Input Parameters:
3635bb7acecfSBarry Smith + dm      - the `DM` object
363647c6ae99SBarry Smith - nlevels - the number of levels of coarsening
363747c6ae99SBarry Smith 
363847c6ae99SBarry Smith   Output Parameter:
3639bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
364047c6ae99SBarry Smith 
364147c6ae99SBarry Smith   Level: developer
364247c6ae99SBarry Smith 
36431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
364447c6ae99SBarry Smith @*/
3645d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3646d71ae5a4SJacob Faibussowitsch {
364747c6ae99SBarry Smith   PetscFunctionBegin;
3648171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36497a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36503ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36514f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
3652213acdd3SPierre Jolivet   if (dm->ops->coarsen && !dm->ops->coarsenhierarchy) {
365347c6ae99SBarry Smith     PetscInt i;
365447c6ae99SBarry Smith 
36559566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
365648a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3657213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
36583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
365947c6ae99SBarry Smith }
366047c6ae99SBarry Smith 
36611a266240SBarry Smith /*@C
3662bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36631a266240SBarry Smith 
3664bb7acecfSBarry Smith   Logically Collective if the function is collective
36651a266240SBarry Smith 
36661a266240SBarry Smith   Input Parameters:
3667bb7acecfSBarry Smith + dm      - the `DM` object
36681a266240SBarry Smith - destroy - the destroy function
36691a266240SBarry Smith 
36701a266240SBarry Smith   Level: intermediate
36711a266240SBarry Smith 
36721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3673f07f9ceaSJed Brown @*/
3674d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3675d71ae5a4SJacob Faibussowitsch {
36761a266240SBarry Smith   PetscFunctionBegin;
3677171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36781a266240SBarry Smith   dm->ctxdestroy = destroy;
36793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36801a266240SBarry Smith }
36811a266240SBarry Smith 
3682b07ff414SBarry Smith /*@
3683bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
368447c6ae99SBarry Smith 
368547c6ae99SBarry Smith   Not Collective
368647c6ae99SBarry Smith 
368747c6ae99SBarry Smith   Input Parameters:
3688bb7acecfSBarry Smith + dm  - the `DM` object
368947c6ae99SBarry Smith - ctx - the user context
369047c6ae99SBarry Smith 
369147c6ae99SBarry Smith   Level: intermediate
369247c6ae99SBarry Smith 
3693bb7acecfSBarry Smith   Note:
369435cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3695bb7acecfSBarry Smith 
369660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
369747c6ae99SBarry Smith @*/
3698d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3699d71ae5a4SJacob Faibussowitsch {
370047c6ae99SBarry Smith   PetscFunctionBegin;
3701171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
370247c6ae99SBarry Smith   dm->ctx = ctx;
37033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
370447c6ae99SBarry Smith }
370547c6ae99SBarry Smith 
370647c6ae99SBarry Smith /*@
3707bb7acecfSBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object
370847c6ae99SBarry Smith 
370947c6ae99SBarry Smith   Not Collective
371047c6ae99SBarry Smith 
371147c6ae99SBarry Smith   Input Parameter:
3712bb7acecfSBarry Smith . dm - the `DM` object
371347c6ae99SBarry Smith 
371447c6ae99SBarry Smith   Output Parameter:
371547c6ae99SBarry Smith . ctx - the user context
371647c6ae99SBarry Smith 
371747c6ae99SBarry Smith   Level: intermediate
371847c6ae99SBarry Smith 
3719bb7acecfSBarry Smith   Note:
372035cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3721bb7acecfSBarry Smith 
372242747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
372347c6ae99SBarry Smith @*/
3724d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3725d71ae5a4SJacob Faibussowitsch {
372647c6ae99SBarry Smith   PetscFunctionBegin;
3727171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37281b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
373047c6ae99SBarry Smith }
373147c6ae99SBarry Smith 
373208da532bSDmitry Karpeev /*@C
3733bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
373408da532bSDmitry Karpeev 
373520f4b53cSBarry Smith   Logically Collective
373608da532bSDmitry Karpeev 
3737d8d19677SJose E. Roman   Input Parameters:
373808da532bSDmitry Karpeev + dm - the DM object
373920f4b53cSBarry Smith - f  - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
374008da532bSDmitry Karpeev 
374108da532bSDmitry Karpeev   Level: intermediate
374208da532bSDmitry Karpeev 
37431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3744db781477SPatrick Sanan          `DMSetJacobian()`
374508da532bSDmitry Karpeev @*/
3746d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3747d71ae5a4SJacob Faibussowitsch {
374808da532bSDmitry Karpeev   PetscFunctionBegin;
37495a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
375008da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
375208da532bSDmitry Karpeev }
375308da532bSDmitry Karpeev 
375408da532bSDmitry Karpeev /*@
3755bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
375608da532bSDmitry Karpeev 
375708da532bSDmitry Karpeev   Not Collective
375808da532bSDmitry Karpeev 
375908da532bSDmitry Karpeev   Input Parameter:
3760bb7acecfSBarry Smith . dm - the `DM` object to destroy
376108da532bSDmitry Karpeev 
376208da532bSDmitry Karpeev   Output Parameter:
3763bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
376408da532bSDmitry Karpeev 
376508da532bSDmitry Karpeev   Level: developer
376608da532bSDmitry Karpeev 
37671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
376808da532bSDmitry Karpeev @*/
3769d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3770d71ae5a4SJacob Faibussowitsch {
377108da532bSDmitry Karpeev   PetscFunctionBegin;
37725a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37734f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
377408da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
37753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
377608da532bSDmitry Karpeev }
377708da532bSDmitry Karpeev 
377808da532bSDmitry Karpeev /*@C
3779bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
378008da532bSDmitry Karpeev 
378120f4b53cSBarry Smith   Logically Collective
378208da532bSDmitry Karpeev 
3783f899ff85SJose E. Roman   Input Parameter:
3784bb7acecfSBarry Smith . dm - the `DM` object
378508da532bSDmitry Karpeev 
378660225df5SJacob Faibussowitsch   Output Parameters:
378708da532bSDmitry Karpeev + xl - lower bound
378808da532bSDmitry Karpeev - xu - upper bound
378908da532bSDmitry Karpeev 
3790907376e6SBarry Smith   Level: advanced
3791907376e6SBarry Smith 
379220f4b53cSBarry Smith   Note:
379395452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
379408da532bSDmitry Karpeev 
37951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
379608da532bSDmitry Karpeev @*/
3797d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3798d71ae5a4SJacob Faibussowitsch {
379908da532bSDmitry Karpeev   PetscFunctionBegin;
38005a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
380108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
38025a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3803dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
380508da532bSDmitry Karpeev }
380608da532bSDmitry Karpeev 
3807b0ae01b7SPeter Brune /*@
3808bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3809b0ae01b7SPeter Brune 
3810b0ae01b7SPeter Brune   Not Collective
3811b0ae01b7SPeter Brune 
3812b0ae01b7SPeter Brune   Input Parameter:
3813b0ae01b7SPeter Brune . dm - the DM object
3814b0ae01b7SPeter Brune 
3815b0ae01b7SPeter Brune   Output Parameter:
3816bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3817b0ae01b7SPeter Brune 
3818b0ae01b7SPeter Brune   Level: developer
3819b0ae01b7SPeter Brune 
38201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3821b0ae01b7SPeter Brune @*/
3822d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3823d71ae5a4SJacob Faibussowitsch {
3824b0ae01b7SPeter Brune   PetscFunctionBegin;
38255a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38264f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3827b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3829b0ae01b7SPeter Brune }
3830b0ae01b7SPeter Brune 
38313ad4599aSBarry Smith /*@
3832bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38333ad4599aSBarry Smith 
38343ad4599aSBarry Smith   Not Collective
38353ad4599aSBarry Smith 
38363ad4599aSBarry Smith   Input Parameter:
3837bb7acecfSBarry Smith . dm - the `DM` object
38383ad4599aSBarry Smith 
38393ad4599aSBarry Smith   Output Parameter:
3840bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38413ad4599aSBarry Smith 
38423ad4599aSBarry Smith   Level: developer
38433ad4599aSBarry Smith 
38441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38453ad4599aSBarry Smith @*/
3846d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3847d71ae5a4SJacob Faibussowitsch {
38483ad4599aSBarry Smith   PetscFunctionBegin;
38495a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38504f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
38513ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38533ad4599aSBarry Smith }
38543ad4599aSBarry Smith 
3855a7058e45SLawrence Mitchell /*@
3856bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3857a7058e45SLawrence Mitchell 
3858a7058e45SLawrence Mitchell   Not Collective
3859a7058e45SLawrence Mitchell 
3860a7058e45SLawrence Mitchell   Input Parameter:
3861bb7acecfSBarry Smith . dm - the `DM` object
3862a7058e45SLawrence Mitchell 
3863a7058e45SLawrence Mitchell   Output Parameter:
3864bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3865a7058e45SLawrence Mitchell 
3866a7058e45SLawrence Mitchell   Level: developer
3867a7058e45SLawrence Mitchell 
38681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3869a7058e45SLawrence Mitchell @*/
3870d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3871d71ae5a4SJacob Faibussowitsch {
3872a7058e45SLawrence Mitchell   PetscFunctionBegin;
38735a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38744f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3875dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3876ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
38773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3878a7058e45SLawrence Mitchell }
3879a7058e45SLawrence Mitchell 
38800298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3881264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3882264ace61SBarry Smith 
3883264ace61SBarry Smith /*@C
3884bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3885264ace61SBarry Smith 
388620f4b53cSBarry Smith   Collective
3887264ace61SBarry Smith 
3888264ace61SBarry Smith   Input Parameters:
3889bb7acecfSBarry Smith + dm     - The `DM` object
3890bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3891264ace61SBarry Smith 
3892264ace61SBarry Smith   Options Database Key:
3893bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3894264ace61SBarry Smith 
3895264ace61SBarry Smith   Level: intermediate
3896264ace61SBarry Smith 
3897bb7acecfSBarry Smith   Note:
38980462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3899bb7acecfSBarry Smith 
39001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3901264ace61SBarry Smith @*/
3902d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3903d71ae5a4SJacob Faibussowitsch {
3904264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3905264ace61SBarry Smith   PetscBool match;
3906264ace61SBarry Smith 
3907264ace61SBarry Smith   PetscFunctionBegin;
3908264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39099566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39103ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3911264ace61SBarry Smith 
39129566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39139566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39147a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3915264ace61SBarry Smith 
3916dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39179566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39189566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39199566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3921264ace61SBarry Smith }
3922264ace61SBarry Smith 
3923264ace61SBarry Smith /*@C
3924bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3925264ace61SBarry Smith 
3926264ace61SBarry Smith   Not Collective
3927264ace61SBarry Smith 
3928264ace61SBarry Smith   Input Parameter:
3929bb7acecfSBarry Smith . dm - The `DM`
3930264ace61SBarry Smith 
3931264ace61SBarry Smith   Output Parameter:
3932bb7acecfSBarry Smith . type - The `DMType` name
3933264ace61SBarry Smith 
3934264ace61SBarry Smith   Level: intermediate
3935264ace61SBarry Smith 
39361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3937264ace61SBarry Smith @*/
3938d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3939d71ae5a4SJacob Faibussowitsch {
3940264ace61SBarry Smith   PetscFunctionBegin;
3941264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39424f572ea9SToby Isaac   PetscAssertPointer(type, 2);
39439566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3944264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3946264ace61SBarry Smith }
3947264ace61SBarry Smith 
394867a56275SMatthew G Knepley /*@C
3949bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
395067a56275SMatthew G Knepley 
395120f4b53cSBarry Smith   Collective
395267a56275SMatthew G Knepley 
395367a56275SMatthew G Knepley   Input Parameters:
3954bb7acecfSBarry Smith + dm      - the `DM`
3955bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
395667a56275SMatthew G Knepley 
395767a56275SMatthew G Knepley   Output Parameter:
3958bb7acecfSBarry Smith . M - pointer to new `DM`
395967a56275SMatthew G Knepley 
396020f4b53cSBarry Smith   Level: intermediate
396120f4b53cSBarry Smith 
396267a56275SMatthew G Knepley   Notes:
3963bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3964bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3965bb7acecfSBarry Smith   of the input `DM`.
396667a56275SMatthew G Knepley 
39671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
396867a56275SMatthew G Knepley @*/
3969d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3970d71ae5a4SJacob Faibussowitsch {
397167a56275SMatthew G Knepley   DM        B;
397267a56275SMatthew G Knepley   char      convname[256];
3973c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
397467a56275SMatthew G Knepley 
397567a56275SMatthew G Knepley   PetscFunctionBegin;
397667a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
397767a56275SMatthew G Knepley   PetscValidType(dm, 1);
39784f572ea9SToby Isaac   PetscAssertPointer(M, 3);
39799566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
39809566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3981c067b6caSMatthew G. Knepley   if (sametype) {
3982c067b6caSMatthew G. Knepley     *M = dm;
39839566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
39843ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3985c067b6caSMatthew G. Knepley   } else {
39860298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
398767a56275SMatthew G Knepley 
398867a56275SMatthew G Knepley     /*
398967a56275SMatthew G Knepley        Order of precedence:
399067a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
399167a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
399267a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
399367a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
399467a56275SMatthew G Knepley        5) Use a really basic converter.
399567a56275SMatthew G Knepley     */
399667a56275SMatthew G Knepley 
399767a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
39989566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39999566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40009566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40019566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40029566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40039566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
400467a56275SMatthew G Knepley     if (conv) goto foundconv;
400567a56275SMatthew G Knepley 
400667a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40079566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40089566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40099566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40109566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40119566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40129566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40139566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40149566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
401567a56275SMatthew G Knepley     if (conv) {
40169566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
401767a56275SMatthew G Knepley       goto foundconv;
401867a56275SMatthew G Knepley     }
401967a56275SMatthew G Knepley 
402067a56275SMatthew G Knepley #if 0
402167a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
402267a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40239566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
402467a56275SMatthew G Knepley     if (conv) goto foundconv;
402567a56275SMatthew G Knepley 
402667a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
402767a56275SMatthew G Knepley     if (dm->ops->convert) {
402867a56275SMatthew G Knepley       conv = dm->ops->convert;
402967a56275SMatthew G Knepley     }
403067a56275SMatthew G Knepley     if (conv) goto foundconv;
403167a56275SMatthew G Knepley #endif
403267a56275SMatthew G Knepley 
403367a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
403498921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
403567a56275SMatthew G Knepley 
403667a56275SMatthew G Knepley   foundconv:
40379566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40389566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
403912fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
404090b157c4SStefano Zampini     {
40414fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40426858538eSMatthew G. Knepley 
40434fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40444fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4045c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40469566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40479566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40489566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40499566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
405012fa691eSMatthew G. Knepley     }
40519566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
405267a56275SMatthew G Knepley   }
40539566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
405567a56275SMatthew G Knepley }
4056264ace61SBarry Smith 
4057264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4058264ace61SBarry Smith 
4059264ace61SBarry Smith /*@C
4060bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40611c84c290SBarry Smith 
40621c84c290SBarry Smith   Not Collective
40631c84c290SBarry Smith 
40641c84c290SBarry Smith   Input Parameters:
406520f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
406620f4b53cSBarry Smith - function - The creation routine itself
406720f4b53cSBarry Smith 
406820f4b53cSBarry Smith   Level: advanced
40691c84c290SBarry Smith 
40701c84c290SBarry Smith   Notes:
407120f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
40721c84c290SBarry Smith 
407360225df5SJacob Faibussowitsch   Example Usage:
40741c84c290SBarry Smith .vb
4075bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40761c84c290SBarry Smith .ve
40771c84c290SBarry Smith 
407820f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
40791c84c290SBarry Smith .vb
40801c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
40811c84c290SBarry Smith     DMSetType(DM,"my_da");
40821c84c290SBarry Smith .ve
40831c84c290SBarry Smith   or at runtime via the option
40841c84c290SBarry Smith .vb
40851c84c290SBarry Smith     -da_type my_da
40861c84c290SBarry Smith .ve
4087264ace61SBarry Smith 
40881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4089264ace61SBarry Smith @*/
4090d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4091d71ae5a4SJacob Faibussowitsch {
4092264ace61SBarry Smith   PetscFunctionBegin;
40939566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
40949566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
40953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4096264ace61SBarry Smith }
4097264ace61SBarry Smith 
4098b859378eSBarry Smith /*@C
4099bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4100b859378eSBarry Smith 
410120f4b53cSBarry Smith   Collective
4102b859378eSBarry Smith 
4103b859378eSBarry Smith   Input Parameters:
4104bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4105bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4106bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4107bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4108b859378eSBarry Smith 
4109b859378eSBarry Smith   Level: intermediate
4110b859378eSBarry Smith 
4111b859378eSBarry Smith   Notes:
411255849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4113b859378eSBarry Smith 
4114bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4115bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4116bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4117cd7e8a5eSksagiyam 
41181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4119b859378eSBarry Smith @*/
4120d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4121d71ae5a4SJacob Faibussowitsch {
41229331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4123b859378eSBarry Smith 
4124b859378eSBarry Smith   PetscFunctionBegin;
4125b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4126b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41279566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41289566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41299566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41309566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41319331c7a4SMatthew G. Knepley   if (isbinary) {
41329331c7a4SMatthew G. Knepley     PetscInt classid;
41339331c7a4SMatthew G. Knepley     char     type[256];
4134b859378eSBarry Smith 
41359566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41367a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41379566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41389566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4139dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41409331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4141dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41429331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41439566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4145b859378eSBarry Smith }
4146b859378eSBarry Smith 
41477da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41487da65231SMatthew G Knepley 
41492ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[])
41502ecf6ec3SMatthew G. Knepley {
41512ecf6ec3SMatthew G. Knepley   PetscInt f;
41522ecf6ec3SMatthew G. Knepley 
41532ecf6ec3SMatthew G. Knepley   PetscFunctionBegin;
41542ecf6ec3SMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41552ecf6ec3SMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %" PetscInt_FMT " |\n", x[f]));
41562ecf6ec3SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41572ecf6ec3SMatthew G. Knepley }
41582ecf6ec3SMatthew G. Knepley 
4159d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4160d71ae5a4SJacob Faibussowitsch {
41611d47ebbbSSatish Balay   PetscInt f;
41621b30c384SMatthew G Knepley 
41637da65231SMatthew G Knepley   PetscFunctionBegin;
416463a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
416548a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41677da65231SMatthew G Knepley }
41687da65231SMatthew G Knepley 
41695962854dSMatthew G. Knepley PetscErrorCode DMPrintCellVectorReal(PetscInt c, const char name[], PetscInt len, const PetscReal x[])
41705962854dSMatthew G. Knepley {
41715962854dSMatthew G. Knepley   PetscInt f;
41725962854dSMatthew G. Knepley 
41735962854dSMatthew G. Knepley   PetscFunctionBegin;
41745962854dSMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41755962854dSMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)x[f]));
41765962854dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41775962854dSMatthew G. Knepley }
41785962854dSMatthew G. Knepley 
4179d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4180d71ae5a4SJacob Faibussowitsch {
41811b30c384SMatthew G Knepley   PetscInt f, g;
41827da65231SMatthew G Knepley 
41837da65231SMatthew G Knepley   PetscFunctionBegin;
418463a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41851d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
41869566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
418748a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
41889566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
41897da65231SMatthew G Knepley   }
41903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41917da65231SMatthew G Knepley }
4192e7c4fc90SDmitry Karpeev 
4193d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4194d71ae5a4SJacob Faibussowitsch {
41950c5b8624SToby Isaac   PetscInt           localSize, bs;
41960c5b8624SToby Isaac   PetscMPIInt        size;
41970c5b8624SToby Isaac   Vec                x, xglob;
41980c5b8624SToby Isaac   const PetscScalar *xarray;
4199e759306cSMatthew G. Knepley 
4200e759306cSMatthew G. Knepley   PetscFunctionBegin;
42019566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
42029566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
42039566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
420493ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
42059566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
42060c5b8624SToby Isaac   if (size > 1) {
42079566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
42089566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
42099566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
42109566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42110c5b8624SToby Isaac   } else {
42120c5b8624SToby Isaac     xglob = x;
42130c5b8624SToby Isaac   }
42149566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42150c5b8624SToby Isaac   if (size > 1) {
42169566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42179566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42180c5b8624SToby Isaac   }
42199566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
42203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4221e759306cSMatthew G. Knepley }
4222e759306cSMatthew G. Knepley 
422388ed4aceSMatthew G Knepley /*@
4224bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4225061576a5SJed Brown 
4226061576a5SJed Brown   Input Parameter:
4227bb7acecfSBarry Smith . dm - The `DM`
4228061576a5SJed Brown 
4229061576a5SJed Brown   Output Parameter:
4230bb7acecfSBarry Smith . section - The `PetscSection`
4231061576a5SJed Brown 
423220f4b53cSBarry Smith   Options Database Key:
4233bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4234061576a5SJed Brown 
4235061576a5SJed Brown   Level: advanced
4236061576a5SJed Brown 
4237061576a5SJed Brown   Notes:
4238bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4239061576a5SJed Brown 
4240bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4241061576a5SJed Brown 
42421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4243061576a5SJed Brown @*/
4244d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4245d71ae5a4SJacob Faibussowitsch {
4246061576a5SJed Brown   PetscFunctionBegin;
42479566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4249061576a5SJed Brown }
4250061576a5SJed Brown 
4251061576a5SJed Brown /*@
4252bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
425388ed4aceSMatthew G Knepley 
425488ed4aceSMatthew G Knepley   Input Parameter:
4255bb7acecfSBarry Smith . dm - The `DM`
425688ed4aceSMatthew G Knepley 
425788ed4aceSMatthew G Knepley   Output Parameter:
4258bb7acecfSBarry Smith . section - The `PetscSection`
425988ed4aceSMatthew G Knepley 
426020f4b53cSBarry Smith   Options Database Key:
4261bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4262e5893cccSMatthew G. Knepley 
426388ed4aceSMatthew G Knepley   Level: intermediate
426488ed4aceSMatthew G Knepley 
4265bb7acecfSBarry Smith   Note:
4266bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
426788ed4aceSMatthew G Knepley 
42681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
426988ed4aceSMatthew G Knepley @*/
4270d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4271d71ae5a4SJacob Faibussowitsch {
427288ed4aceSMatthew G Knepley   PetscFunctionBegin;
427388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42744f572ea9SToby Isaac   PetscAssertPointer(section, 2);
42751bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4276e5e52638SMatthew G. Knepley     PetscInt d;
4277e5e52638SMatthew G. Knepley 
427845480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
427945480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
428045480ffeSMatthew G. Knepley       PetscViewer       viewer;
428145480ffeSMatthew G. Knepley       PetscViewerFormat format;
428245480ffeSMatthew G. Knepley       PetscBool         flg;
428345480ffeSMatthew G. Knepley 
42849566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
42859566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
428645480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
42879566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
42889566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
428945480ffeSMatthew G. Knepley       }
429045480ffeSMatthew G. Knepley       if (flg) {
42919566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
42929566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
4293cd791dc2SBarry Smith         PetscCall(PetscOptionsRestoreViewer(&viewer));
429445480ffeSMatthew G. Knepley       }
429545480ffeSMatthew G. Knepley     }
4296dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
42979566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
42982f0f8703SMatthew G. Knepley   }
42991bb6d2a8SBarry Smith   *section = dm->localSection;
43003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
430188ed4aceSMatthew G Knepley }
430288ed4aceSMatthew G Knepley 
430388ed4aceSMatthew G Knepley /*@
4304bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4305061576a5SJed Brown 
4306061576a5SJed Brown   Input Parameters:
4307bb7acecfSBarry Smith + dm      - The `DM`
4308bb7acecfSBarry Smith - section - The `PetscSection`
4309061576a5SJed Brown 
4310061576a5SJed Brown   Level: advanced
4311061576a5SJed Brown 
4312061576a5SJed Brown   Notes:
4313bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4314061576a5SJed Brown 
4315bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4316061576a5SJed Brown 
43171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4318061576a5SJed Brown @*/
4319d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4320d71ae5a4SJacob Faibussowitsch {
4321061576a5SJed Brown   PetscFunctionBegin;
43229566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
43233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4324061576a5SJed Brown }
4325061576a5SJed Brown 
4326061576a5SJed Brown /*@
4327bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
432888ed4aceSMatthew G Knepley 
432988ed4aceSMatthew G Knepley   Input Parameters:
4330bb7acecfSBarry Smith + dm      - The `DM`
4331bb7acecfSBarry Smith - section - The `PetscSection`
433288ed4aceSMatthew G Knepley 
433388ed4aceSMatthew G Knepley   Level: intermediate
433488ed4aceSMatthew G Knepley 
4335bb7acecfSBarry Smith   Note:
4336bb7acecfSBarry Smith   Any existing Section will be destroyed
433788ed4aceSMatthew G Knepley 
43381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
433988ed4aceSMatthew G Knepley @*/
4340d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4341d71ae5a4SJacob Faibussowitsch {
4342c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4343af122d2aSMatthew G Knepley   PetscInt f;
434488ed4aceSMatthew G Knepley 
434588ed4aceSMatthew G Knepley   PetscFunctionBegin;
434688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4347b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43489566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43499566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43501bb6d2a8SBarry Smith   dm->localSection = section;
43519566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4352af122d2aSMatthew G Knepley   if (numFields) {
43539566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4354af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43550f21e855SMatthew G. Knepley       PetscObject disc;
4356af122d2aSMatthew G Knepley       const char *name;
4357af122d2aSMatthew G Knepley 
43589566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43599566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43609566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4361af122d2aSMatthew G Knepley     }
4362af122d2aSMatthew G Knepley   }
436350350c8eSStefano Zampini   /* The global section and the SectionSF will be rebuilt
436450350c8eSStefano Zampini      in the next call to DMGetGlobalSection() and DMGetSectionSF(). */
43659566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
436650350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
436752947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
436850350c8eSStefano Zampini 
436950350c8eSStefano Zampini   /* Clear scratch vectors */
437050350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
437150350c8eSStefano Zampini   PetscCall(DMClearLocalVectors(dm));
437250350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
437350350c8eSStefano Zampini   PetscCall(DMClearNamedLocalVectors(dm));
43743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
437588ed4aceSMatthew G Knepley }
437688ed4aceSMatthew G Knepley 
4377adc21957SMatthew G. Knepley /*@C
4378adc21957SMatthew G. Knepley   DMCreateSectionPermutation - Create a permutation of the `PetscSection` chart and optionally a blokc structure.
4379adc21957SMatthew G. Knepley 
4380adc21957SMatthew G. Knepley   Input Parameter:
4381adc21957SMatthew G. Knepley . dm - The `DM`
4382adc21957SMatthew G. Knepley 
4383adc21957SMatthew G. Knepley   Output Parameter:
4384adc21957SMatthew G. Knepley + perm        - A permutation of the mesh points in the chart
4385adc21957SMatthew G. Knepley - blockStarts - A high bit is set for the point that begins every block, or NULL for default blocking
4386adc21957SMatthew G. Knepley 
4387adc21957SMatthew G. Knepley   Level: developer
4388adc21957SMatthew G. Knepley 
4389adc21957SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMGetGlobalSection()`
4390adc21957SMatthew G. Knepley @*/
4391adc21957SMatthew G. Knepley PetscErrorCode DMCreateSectionPermutation(DM dm, IS *perm, PetscBT *blockStarts)
4392adc21957SMatthew G. Knepley {
4393adc21957SMatthew G. Knepley   PetscFunctionBegin;
4394adc21957SMatthew G. Knepley   *perm        = NULL;
4395adc21957SMatthew G. Knepley   *blockStarts = NULL;
4396adc21957SMatthew G. Knepley   PetscTryTypeMethod(dm, createsectionpermutation, perm, blockStarts);
4397adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
4398adc21957SMatthew G. Knepley }
4399adc21957SMatthew G. Knepley 
44009435951eSToby Isaac /*@
4401bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
44029435951eSToby Isaac 
440320f4b53cSBarry Smith   not Collective
4404e228b242SToby Isaac 
44059435951eSToby Isaac   Input Parameter:
4406bb7acecfSBarry Smith . dm - The `DM`
44079435951eSToby Isaac 
4408d8d19677SJose E. Roman   Output Parameters:
440920f4b53cSBarry 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.
441020f4b53cSBarry 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.
441179769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
44129435951eSToby Isaac 
44139435951eSToby Isaac   Level: advanced
44149435951eSToby Isaac 
4415bb7acecfSBarry Smith   Note:
4416bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
44179435951eSToby Isaac 
44181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
44199435951eSToby Isaac @*/
4420d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4421d71ae5a4SJacob Faibussowitsch {
44229435951eSToby Isaac   PetscFunctionBegin;
44239435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4424dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44253b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44263b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
442779769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44299435951eSToby Isaac }
44309435951eSToby Isaac 
44319435951eSToby Isaac /*@
4432bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44339435951eSToby Isaac 
443420f4b53cSBarry Smith   Collective
4435e228b242SToby Isaac 
44369435951eSToby Isaac   Input Parameters:
4437bb7acecfSBarry Smith + dm      - The `DM`
4438bb7acecfSBarry 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).
443920f4b53cSBarry 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).
444020f4b53cSBarry 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).
44419435951eSToby Isaac 
44429435951eSToby Isaac   Level: advanced
44439435951eSToby Isaac 
444420f4b53cSBarry Smith   Notes:
444520f4b53cSBarry 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()`.
444620f4b53cSBarry Smith 
444720f4b53cSBarry 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.
444820f4b53cSBarry Smith 
4449bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44509435951eSToby Isaac 
44511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
44529435951eSToby Isaac @*/
4453d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4454d71ae5a4SJacob Faibussowitsch {
4455e228b242SToby Isaac   PetscMPIInt result;
44569435951eSToby Isaac 
44579435951eSToby Isaac   PetscFunctionBegin;
44589435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4459e228b242SToby Isaac   if (section) {
4460e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44619566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44627a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4463e228b242SToby Isaac   }
4464e228b242SToby Isaac   if (mat) {
4465e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44669566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44677a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4468e228b242SToby Isaac   }
446979769bd5SJed Brown   if (bias) {
447079769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44719566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
447279769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
447379769bd5SJed Brown   }
44749566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44759566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44763b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44779566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44789566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44793b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44809566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44819566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
448279769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44849435951eSToby Isaac }
44859435951eSToby Isaac 
4486497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4487507e4973SMatthew G. Knepley /*
4488bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4489507e4973SMatthew G. Knepley 
4490507e4973SMatthew G. Knepley   Input Parameters:
4491bb7acecfSBarry Smith + dm - The `DM`
4492bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4493bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4494507e4973SMatthew G. Knepley 
4495507e4973SMatthew G. Knepley   Level: intermediate
4496507e4973SMatthew G. Knepley 
44971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4498507e4973SMatthew G. Knepley */
4499d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4500d71ae5a4SJacob Faibussowitsch {
4501507e4973SMatthew G. Knepley   MPI_Comm        comm;
4502507e4973SMatthew G. Knepley   PetscLayout     layout;
4503507e4973SMatthew G. Knepley   const PetscInt *ranges;
4504507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4505507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4506507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4507507e4973SMatthew G. Knepley 
4508507e4973SMatthew G. Knepley   PetscFunctionBegin;
45099566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4510507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45119566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
45129566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
45139566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
45149566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
45159566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
45169566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
45179566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
45189566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45199566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4520507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4521f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4522507e4973SMatthew G. Knepley 
45239566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45259566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45269566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45279566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45289566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4529507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45309371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45319371c9d4SSatish 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));
45329371c9d4SSatish Balay       valid = PETSC_FALSE;
45339371c9d4SSatish Balay     }
45349371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45359371c9d4SSatish 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));
45369371c9d4SSatish Balay       valid = PETSC_FALSE;
45379371c9d4SSatish Balay     }
4538507e4973SMatthew G. Knepley     if (gdof < 0) {
4539507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4540507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4541507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4542507e4973SMatthew G. Knepley 
45439566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4544507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45459371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45469371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45479371c9d4SSatish Balay           valid = PETSC_FALSE;
45489371c9d4SSatish Balay           break;
45499371c9d4SSatish Balay         }
4550507e4973SMatthew G. Knepley       }
4551507e4973SMatthew G. Knepley     }
4552507e4973SMatthew G. Knepley   }
45539566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45549566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
45551c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4556507e4973SMatthew G. Knepley   if (!gvalid) {
45579566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4558507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4559507e4973SMatthew G. Knepley   }
45603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4561507e4973SMatthew G. Knepley }
4562f741bcd2SMatthew G. Knepley #endif
4563507e4973SMatthew G. Knepley 
45645f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45656725e60dSJed Brown {
45666725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45674d86920dSPierre Jolivet 
45686725e60dSJed Brown   PetscFunctionBegin;
45696725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45704f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
45715f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45726725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
45736725e60dSJed Brown   else *sf = dm->sf;
45743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45756725e60dSJed Brown }
45766725e60dSJed Brown 
457788ed4aceSMatthew G Knepley /*@
4578bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
457988ed4aceSMatthew G Knepley 
458020f4b53cSBarry Smith   Collective
45818b1ab98fSJed Brown 
458288ed4aceSMatthew G Knepley   Input Parameter:
4583bb7acecfSBarry Smith . dm - The `DM`
458488ed4aceSMatthew G Knepley 
458588ed4aceSMatthew G Knepley   Output Parameter:
4586bb7acecfSBarry Smith . section - The `PetscSection`
458788ed4aceSMatthew G Knepley 
458888ed4aceSMatthew G Knepley   Level: intermediate
458988ed4aceSMatthew G Knepley 
4590bb7acecfSBarry Smith   Note:
4591bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
459288ed4aceSMatthew G Knepley 
45931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
459488ed4aceSMatthew G Knepley @*/
4595d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4596d71ae5a4SJacob Faibussowitsch {
459788ed4aceSMatthew G Knepley   PetscFunctionBegin;
459888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45994f572ea9SToby Isaac   PetscAssertPointer(section, 2);
46001bb6d2a8SBarry Smith   if (!dm->globalSection) {
4601fd59a867SMatthew G. Knepley     PetscSection s;
46026725e60dSJed Brown     PetscSF      sf;
4603fd59a867SMatthew G. Knepley 
46049566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
46057a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
46067a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
46075f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
4608eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
46099566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
46109566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
46119566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
461288ed4aceSMatthew G Knepley   }
46131bb6d2a8SBarry Smith   *section = dm->globalSection;
46143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
461588ed4aceSMatthew G Knepley }
461688ed4aceSMatthew G Knepley 
4617b21d0597SMatthew G Knepley /*@
4618bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4619b21d0597SMatthew G Knepley 
4620b21d0597SMatthew G Knepley   Input Parameters:
4621bb7acecfSBarry Smith + dm      - The `DM`
46222fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4623b21d0597SMatthew G Knepley 
4624b21d0597SMatthew G Knepley   Level: intermediate
4625b21d0597SMatthew G Knepley 
4626bb7acecfSBarry Smith   Note:
4627bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4628b21d0597SMatthew G Knepley 
46291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4630b21d0597SMatthew G Knepley @*/
4631d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4632d71ae5a4SJacob Faibussowitsch {
4633b21d0597SMatthew G Knepley   PetscFunctionBegin;
4634b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46355080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46369566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46379566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46381bb6d2a8SBarry Smith   dm->globalSection = section;
4639497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46409566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4641507e4973SMatthew G. Knepley #endif
464250350c8eSStefano Zampini   /* Clear global scratch vectors and sectionSF */
464350350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
464452947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
464550350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
464650350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
46473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4648b21d0597SMatthew G Knepley }
4649b21d0597SMatthew G Knepley 
465088ed4aceSMatthew G Knepley /*@
4651bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4652bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
465388ed4aceSMatthew G Knepley 
465488ed4aceSMatthew G Knepley   Input Parameter:
4655bb7acecfSBarry Smith . dm - The `DM`
465688ed4aceSMatthew G Knepley 
465788ed4aceSMatthew G Knepley   Output Parameter:
4658bb7acecfSBarry Smith . sf - The `PetscSF`
465988ed4aceSMatthew G Knepley 
466088ed4aceSMatthew G Knepley   Level: intermediate
466188ed4aceSMatthew G Knepley 
4662bb7acecfSBarry Smith   Note:
4663bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
466488ed4aceSMatthew G Knepley 
46651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
466688ed4aceSMatthew G Knepley @*/
4667d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4668d71ae5a4SJacob Faibussowitsch {
466988ed4aceSMatthew G Knepley   PetscInt nroots;
467088ed4aceSMatthew G Knepley 
467188ed4aceSMatthew G Knepley   PetscFunctionBegin;
467288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46734f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
467448a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
46759566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
467688ed4aceSMatthew G Knepley   if (nroots < 0) {
467788ed4aceSMatthew G Knepley     PetscSection section, gSection;
467888ed4aceSMatthew G Knepley 
46799566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
468031ea6d37SMatthew G Knepley     if (section) {
46819566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
46829566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
468331ea6d37SMatthew G Knepley     } else {
46840298fd71SBarry Smith       *sf = NULL;
46853ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
468631ea6d37SMatthew G Knepley     }
468788ed4aceSMatthew G Knepley   }
46881bb6d2a8SBarry Smith   *sf = dm->sectionSF;
46893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
469088ed4aceSMatthew G Knepley }
469188ed4aceSMatthew G Knepley 
469288ed4aceSMatthew G Knepley /*@
4693bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
469488ed4aceSMatthew G Knepley 
469588ed4aceSMatthew G Knepley   Input Parameters:
4696bb7acecfSBarry Smith + dm - The `DM`
4697bb7acecfSBarry Smith - sf - The `PetscSF`
469888ed4aceSMatthew G Knepley 
469988ed4aceSMatthew G Knepley   Level: intermediate
470088ed4aceSMatthew G Knepley 
4701bb7acecfSBarry Smith   Note:
4702bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
470388ed4aceSMatthew G Knepley 
47041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
470588ed4aceSMatthew G Knepley @*/
4706d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4707d71ae5a4SJacob Faibussowitsch {
470888ed4aceSMatthew G Knepley   PetscFunctionBegin;
470988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4710b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47119566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47129566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
47131bb6d2a8SBarry Smith   dm->sectionSF = sf;
47143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
471588ed4aceSMatthew G Knepley }
471688ed4aceSMatthew G Knepley 
471788ed4aceSMatthew G Knepley /*@C
4718bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
471988ed4aceSMatthew G Knepley   describing the data layout.
472088ed4aceSMatthew G Knepley 
472188ed4aceSMatthew G Knepley   Input Parameters:
4722bb7acecfSBarry Smith + dm            - The `DM`
4723bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4724bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
472588ed4aceSMatthew G Knepley 
47261bb6d2a8SBarry Smith   Level: developer
47271bb6d2a8SBarry Smith 
4728bb7acecfSBarry Smith   Note:
4729bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4730bb7acecfSBarry Smith 
473160225df5SJacob Faibussowitsch   Developer Notes:
4732bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4733bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4734bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
47351bb6d2a8SBarry Smith 
47361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
473788ed4aceSMatthew G Knepley @*/
4738d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4739d71ae5a4SJacob Faibussowitsch {
474088ed4aceSMatthew G Knepley   PetscFunctionBegin;
474188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47429566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
47433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
474488ed4aceSMatthew G Knepley }
4745af122d2aSMatthew G Knepley 
4746b21d0597SMatthew G Knepley /*@
4747bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4748bb7acecfSBarry Smith 
4749bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4750b21d0597SMatthew G Knepley 
4751b21d0597SMatthew G Knepley   Input Parameter:
4752bb7acecfSBarry Smith . dm - The `DM`
4753b21d0597SMatthew G Knepley 
4754b21d0597SMatthew G Knepley   Output Parameter:
4755bb7acecfSBarry Smith . sf - The `PetscSF`
4756b21d0597SMatthew G Knepley 
4757b21d0597SMatthew G Knepley   Level: intermediate
4758b21d0597SMatthew G Knepley 
4759bb7acecfSBarry Smith   Note:
4760bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4761b21d0597SMatthew G Knepley 
47621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4763b21d0597SMatthew G Knepley @*/
4764d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4765d71ae5a4SJacob Faibussowitsch {
4766b21d0597SMatthew G Knepley   PetscFunctionBegin;
4767b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47684f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4769b21d0597SMatthew G Knepley   *sf = dm->sf;
47703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4771b21d0597SMatthew G Knepley }
4772b21d0597SMatthew G Knepley 
4773057b4bcdSMatthew G Knepley /*@
4774bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4775bb7acecfSBarry Smith 
477620f4b53cSBarry Smith   Collective
4777057b4bcdSMatthew G Knepley 
4778057b4bcdSMatthew G Knepley   Input Parameters:
4779bb7acecfSBarry Smith + dm - The `DM`
4780bb7acecfSBarry Smith - sf - The `PetscSF`
4781057b4bcdSMatthew G Knepley 
4782057b4bcdSMatthew G Knepley   Level: intermediate
4783057b4bcdSMatthew G Knepley 
47841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4785057b4bcdSMatthew G Knepley @*/
4786d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4787d71ae5a4SJacob Faibussowitsch {
4788057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4789057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4790b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47919566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47929566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4793057b4bcdSMatthew G Knepley   dm->sf = sf;
47943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4795057b4bcdSMatthew G Knepley }
4796057b4bcdSMatthew G Knepley 
47974f37162bSMatthew G. Knepley /*@
4798bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
47994f37162bSMatthew G. Knepley 
48004f37162bSMatthew G. Knepley   Input Parameter:
4801bb7acecfSBarry Smith . dm - The `DM`
48024f37162bSMatthew G. Knepley 
48034f37162bSMatthew G. Knepley   Output Parameter:
4804bb7acecfSBarry Smith . sf - The `PetscSF`
48054f37162bSMatthew G. Knepley 
48064f37162bSMatthew G. Knepley   Level: intermediate
48074f37162bSMatthew G. Knepley 
4808bb7acecfSBarry Smith   Note:
4809bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
48104f37162bSMatthew G. Knepley 
48111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48124f37162bSMatthew G. Knepley @*/
4813d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4814d71ae5a4SJacob Faibussowitsch {
48154f37162bSMatthew G. Knepley   PetscFunctionBegin;
48164f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48174f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
48184f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
48193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48204f37162bSMatthew G. Knepley }
48214f37162bSMatthew G. Knepley 
48224f37162bSMatthew G. Knepley /*@
48234f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48244f37162bSMatthew G. Knepley 
48254f37162bSMatthew G. Knepley   Input Parameters:
48264f37162bSMatthew G. Knepley + dm - The DM
48274f37162bSMatthew G. Knepley - sf - The PetscSF
48284f37162bSMatthew G. Knepley 
48294f37162bSMatthew G. Knepley   Level: intermediate
48304f37162bSMatthew G. Knepley 
48311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48324f37162bSMatthew G. Knepley @*/
4833d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4834d71ae5a4SJacob Faibussowitsch {
48354f37162bSMatthew G. Knepley   PetscFunctionBegin;
48364f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48374f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48389566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48399566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48404f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48424f37162bSMatthew G. Knepley }
48434f37162bSMatthew G. Knepley 
4844d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4845d71ae5a4SJacob Faibussowitsch {
484634aa8a36SMatthew G. Knepley   PetscClassId id;
484734aa8a36SMatthew G. Knepley 
484834aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48499566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
485034aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48519566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
485234aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48539566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
485417c1d62eSMatthew G. Knepley   } else {
48559566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
485634aa8a36SMatthew G. Knepley   }
48573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
485834aa8a36SMatthew G. Knepley }
485934aa8a36SMatthew G. Knepley 
4860d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4861d71ae5a4SJacob Faibussowitsch {
486244a7f3ddSMatthew G. Knepley   RegionField *tmpr;
486344a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
486444a7f3ddSMatthew G. Knepley 
486544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
48663ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
48679566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
486844a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
48699371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
48709371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48719371c9d4SSatish Balay     tmpr[f].label       = NULL;
48729371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
48739371c9d4SSatish Balay   }
48749566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
487544a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
487644a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
48773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
487844a7f3ddSMatthew G. Knepley }
487944a7f3ddSMatthew G. Knepley 
488044a7f3ddSMatthew G. Knepley /*@
488120f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
488244a7f3ddSMatthew G. Knepley 
488320f4b53cSBarry Smith   Logically Collective
488444a7f3ddSMatthew G. Knepley 
488544a7f3ddSMatthew G. Knepley   Input Parameter:
488620f4b53cSBarry Smith . dm - The `DM`
488744a7f3ddSMatthew G. Knepley 
488844a7f3ddSMatthew G. Knepley   Level: intermediate
488944a7f3ddSMatthew G. Knepley 
48901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
489144a7f3ddSMatthew G. Knepley @*/
4892d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4893d71ae5a4SJacob Faibussowitsch {
489444a7f3ddSMatthew G. Knepley   PetscInt f;
489544a7f3ddSMatthew G. Knepley 
489644a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
489744a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
489844a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
48999566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
49009566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
490144a7f3ddSMatthew G. Knepley   }
49029566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
490344a7f3ddSMatthew G. Knepley   dm->fields = NULL;
490444a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
49053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
490644a7f3ddSMatthew G. Knepley }
490744a7f3ddSMatthew G. Knepley 
4908689b5837SMatthew G. Knepley /*@
490920f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4910689b5837SMatthew G. Knepley 
491120f4b53cSBarry Smith   Not Collective
4912689b5837SMatthew G. Knepley 
4913689b5837SMatthew G. Knepley   Input Parameter:
491420f4b53cSBarry Smith . dm - The `DM`
4915689b5837SMatthew G. Knepley 
4916689b5837SMatthew G. Knepley   Output Parameter:
491760225df5SJacob Faibussowitsch . numFields - The number of fields
4918689b5837SMatthew G. Knepley 
4919689b5837SMatthew G. Knepley   Level: intermediate
4920689b5837SMatthew G. Knepley 
49211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4922689b5837SMatthew G. Knepley @*/
4923d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4924d71ae5a4SJacob Faibussowitsch {
49250f21e855SMatthew G. Knepley   PetscFunctionBegin;
49260f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49274f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
492844a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
49293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4930af122d2aSMatthew G Knepley }
4931af122d2aSMatthew G Knepley 
4932689b5837SMatthew G. Knepley /*@
493320f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4934689b5837SMatthew G. Knepley 
493520f4b53cSBarry Smith   Logically Collective
4936689b5837SMatthew G. Knepley 
4937689b5837SMatthew G. Knepley   Input Parameters:
493820f4b53cSBarry Smith + dm        - The `DM`
493960225df5SJacob Faibussowitsch - numFields - The number of fields
4940689b5837SMatthew G. Knepley 
4941689b5837SMatthew G. Knepley   Level: intermediate
4942689b5837SMatthew G. Knepley 
49431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4944689b5837SMatthew G. Knepley @*/
4945d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4946d71ae5a4SJacob Faibussowitsch {
49470f21e855SMatthew G. Knepley   PetscInt Nf, f;
4948af122d2aSMatthew G Knepley 
4949af122d2aSMatthew G Knepley   PetscFunctionBegin;
4950af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49519566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49520f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49530f21e855SMatthew G. Knepley     PetscContainer obj;
49540f21e855SMatthew G. Knepley 
49559566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49569566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
49579566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4958af122d2aSMatthew G Knepley   }
49593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4960af122d2aSMatthew G Knepley }
4961af122d2aSMatthew G Knepley 
4962c1929be8SMatthew G. Knepley /*@
4963bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4964c1929be8SMatthew G. Knepley 
496520f4b53cSBarry Smith   Not Collective
4966c1929be8SMatthew G. Knepley 
4967c1929be8SMatthew G. Knepley   Input Parameters:
4968bb7acecfSBarry Smith + dm - The `DM`
4969c1929be8SMatthew G. Knepley - f  - The field number
4970c1929be8SMatthew G. Knepley 
497144a7f3ddSMatthew G. Knepley   Output Parameters:
497220f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
497320f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
4974c1929be8SMatthew G. Knepley 
497544a7f3ddSMatthew G. Knepley   Level: intermediate
4976c1929be8SMatthew G. Knepley 
49771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
4978c1929be8SMatthew G. Knepley @*/
4979d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4980d71ae5a4SJacob Faibussowitsch {
4981af122d2aSMatthew G Knepley   PetscFunctionBegin;
4982af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49834f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
49847a8be351SBarry 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);
498544a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4986bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
49873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4988decb47aaSMatthew G. Knepley }
4989decb47aaSMatthew G. Knepley 
4990083401c6SMatthew G. Knepley /* Does not clear the DS */
4991d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4992d71ae5a4SJacob Faibussowitsch {
4993083401c6SMatthew G. Knepley   PetscFunctionBegin;
49949566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
49959566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
49969566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4997083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4998bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
49999566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5000bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
50013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5002083401c6SMatthew G. Knepley }
5003083401c6SMatthew G. Knepley 
500446560f82SMatthew G. Knepley /*@C
5005bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
5006bb7acecfSBarry Smith   the field numbering.
5007c1929be8SMatthew G. Knepley 
500820f4b53cSBarry Smith   Logically Collective
5009c1929be8SMatthew G. Knepley 
5010c1929be8SMatthew G. Knepley   Input Parameters:
5011bb7acecfSBarry Smith + dm    - The `DM`
5012c1929be8SMatthew G. Knepley . f     - The field number
501320f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5014bb7acecfSBarry Smith - disc  - The discretization object
5015c1929be8SMatthew G. Knepley 
501644a7f3ddSMatthew G. Knepley   Level: intermediate
5017c1929be8SMatthew G. Knepley 
50181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
5019c1929be8SMatthew G. Knepley @*/
5020d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5021d71ae5a4SJacob Faibussowitsch {
5022decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5023decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5024e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
5025bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50267a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5027bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5028bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50299566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
503144a7f3ddSMatthew G. Knepley }
503244a7f3ddSMatthew G. Knepley 
503346560f82SMatthew G. Knepley /*@C
5034bb7acecfSBarry 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)
5035bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
503644a7f3ddSMatthew G. Knepley 
503720f4b53cSBarry Smith   Logically Collective
503844a7f3ddSMatthew G. Knepley 
503944a7f3ddSMatthew G. Knepley   Input Parameters:
5040bb7acecfSBarry Smith + dm    - The `DM`
504120f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5042bb7acecfSBarry Smith - disc  - The discretization object
504344a7f3ddSMatthew G. Knepley 
504444a7f3ddSMatthew G. Knepley   Level: intermediate
504544a7f3ddSMatthew G. Knepley 
5046bb7acecfSBarry Smith   Notes:
5047bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5048bb7acecfSBarry Smith 
5049da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5050bb7acecfSBarry 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
5051bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5052bb7acecfSBarry Smith 
50531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
505444a7f3ddSMatthew G. Knepley @*/
5055d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5056d71ae5a4SJacob Faibussowitsch {
505744a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
505844a7f3ddSMatthew G. Knepley 
505944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
506044a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5061064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5062bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50639566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
506444a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5065bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50669566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5067bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5068bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50699566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5071af122d2aSMatthew G Knepley }
50726636e97aSMatthew G Knepley 
5073e5e52638SMatthew G. Knepley /*@
5074e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5075e0b68406SMatthew Knepley 
507620f4b53cSBarry Smith   Logically Collective
5077e0b68406SMatthew Knepley 
5078e0b68406SMatthew Knepley   Input Parameters:
5079bb7acecfSBarry Smith + dm          - The `DM`
5080e0b68406SMatthew Knepley . f           - The field index
5081bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5082e0b68406SMatthew Knepley 
5083e0b68406SMatthew Knepley   Level: intermediate
5084e0b68406SMatthew Knepley 
50851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5086e0b68406SMatthew Knepley @*/
5087d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5088d71ae5a4SJacob Faibussowitsch {
5089e0b68406SMatthew Knepley   PetscFunctionBegin;
509063a3b9bcSJacob 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);
5091e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
50923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5093e0b68406SMatthew Knepley }
5094e0b68406SMatthew Knepley 
5095e0b68406SMatthew Knepley /*@
5096e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5097e0b68406SMatthew Knepley 
509820f4b53cSBarry Smith   Not Collective
5099e0b68406SMatthew Knepley 
5100e0b68406SMatthew Knepley   Input Parameters:
5101bb7acecfSBarry Smith + dm - The `DM`
5102e0b68406SMatthew Knepley - f  - The field index
5103e0b68406SMatthew Knepley 
5104e0b68406SMatthew Knepley   Output Parameter:
5105e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5106e0b68406SMatthew Knepley 
5107e0b68406SMatthew Knepley   Level: intermediate
5108e0b68406SMatthew Knepley 
510960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5110e0b68406SMatthew Knepley @*/
5111d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5112d71ae5a4SJacob Faibussowitsch {
5113e0b68406SMatthew Knepley   PetscFunctionBegin;
511463a3b9bcSJacob 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);
5115e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
51163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5117e0b68406SMatthew Knepley }
5118e0b68406SMatthew Knepley 
5119e0b68406SMatthew Knepley /*@
5120bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5121e5e52638SMatthew G. Knepley 
512220f4b53cSBarry Smith   Collective
5123e5e52638SMatthew G. Knepley 
5124e5e52638SMatthew G. Knepley   Input Parameter:
5125bb7acecfSBarry Smith . dm - The `DM`
5126e5e52638SMatthew G. Knepley 
5127e5e52638SMatthew G. Knepley   Output Parameter:
5128bb7acecfSBarry Smith . newdm - The `DM`
5129e5e52638SMatthew G. Knepley 
5130e5e52638SMatthew G. Knepley   Level: advanced
5131e5e52638SMatthew G. Knepley 
51321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5133e5e52638SMatthew G. Knepley @*/
5134d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5135d71ae5a4SJacob Faibussowitsch {
5136e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5137e5e52638SMatthew G. Knepley 
5138e5e52638SMatthew G. Knepley   PetscFunctionBegin;
51393ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
51409566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
51419566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5142e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5143e5e52638SMatthew G. Knepley     DMLabel     label;
5144e5e52638SMatthew G. Knepley     PetscObject field;
514534aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5146e5e52638SMatthew G. Knepley 
51479566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
51489566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
51499566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
51509566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
515134aa8a36SMatthew G. Knepley   }
51523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
515334aa8a36SMatthew G. Knepley }
515434aa8a36SMatthew G. Knepley 
515534aa8a36SMatthew G. Knepley /*@
515634aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
515734aa8a36SMatthew G. Knepley 
515820f4b53cSBarry Smith   Not Collective
515934aa8a36SMatthew G. Knepley 
516034aa8a36SMatthew G. Knepley   Input Parameters:
516120f4b53cSBarry Smith + dm - The `DM` object
516220f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
516334aa8a36SMatthew G. Knepley 
5164d8d19677SJose E. Roman   Output Parameters:
516534aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
516634aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
516734aa8a36SMatthew G. Knepley 
516834aa8a36SMatthew G. Knepley   Level: developer
516934aa8a36SMatthew G. Knepley 
517020f4b53cSBarry Smith   Notes:
517120f4b53cSBarry Smith .vb
517220f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
517320f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
517420f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
517520f4b53cSBarry Smith .ve
517620f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
517720f4b53cSBarry Smith 
51781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
517934aa8a36SMatthew G. Knepley @*/
5180d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5181d71ae5a4SJacob Faibussowitsch {
518234aa8a36SMatthew G. Knepley   PetscFunctionBegin;
518334aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51844f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
51854f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
518634aa8a36SMatthew G. Knepley   if (f < 0) {
518734aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
518834aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
518934aa8a36SMatthew G. Knepley   } else {
519034aa8a36SMatthew G. Knepley     PetscInt Nf;
519134aa8a36SMatthew G. Knepley 
51929566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51937a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
519434aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
519534aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
519634aa8a36SMatthew G. Knepley   }
51973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
519834aa8a36SMatthew G. Knepley }
519934aa8a36SMatthew G. Knepley 
520034aa8a36SMatthew G. Knepley /*@
520134aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
520234aa8a36SMatthew G. Knepley 
520320f4b53cSBarry Smith   Not Collective
520434aa8a36SMatthew G. Knepley 
520534aa8a36SMatthew G. Knepley   Input Parameters:
520620f4b53cSBarry Smith + dm         - The `DM` object
520734aa8a36SMatthew G. Knepley . f          - The field number
520834aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
520934aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
521034aa8a36SMatthew G. Knepley 
521134aa8a36SMatthew G. Knepley   Level: developer
521234aa8a36SMatthew G. Knepley 
521320f4b53cSBarry Smith   Notes:
521420f4b53cSBarry Smith .vb
521520f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
521620f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
521720f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
521820f4b53cSBarry Smith .ve
521920f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
522020f4b53cSBarry Smith 
52211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
522234aa8a36SMatthew G. Knepley @*/
5223d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5224d71ae5a4SJacob Faibussowitsch {
522534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
522634aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
522734aa8a36SMatthew G. Knepley   if (f < 0) {
522834aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
522934aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
523034aa8a36SMatthew G. Knepley   } else {
523134aa8a36SMatthew G. Knepley     PetscInt Nf;
523234aa8a36SMatthew G. Knepley 
52339566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52347a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
523534aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
523634aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5237e5e52638SMatthew G. Knepley   }
52383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5239e5e52638SMatthew G. Knepley }
5240e5e52638SMatthew G. Knepley 
5241b0441da4SMatthew G. Knepley /*@
5242b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5243b0441da4SMatthew G. Knepley 
5244b0441da4SMatthew G. Knepley   Not collective
5245b0441da4SMatthew G. Knepley 
5246f899ff85SJose E. Roman   Input Parameter:
524720f4b53cSBarry Smith . dm - The `DM` object
5248b0441da4SMatthew G. Knepley 
5249d8d19677SJose E. Roman   Output Parameters:
5250b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5251b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5252b0441da4SMatthew G. Knepley 
5253b0441da4SMatthew G. Knepley   Level: developer
5254b0441da4SMatthew G. Knepley 
525520f4b53cSBarry Smith   Notes:
525620f4b53cSBarry Smith .vb
525720f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
525820f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
525920f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
526020f4b53cSBarry Smith .ve
526120f4b53cSBarry Smith 
52621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5263b0441da4SMatthew G. Knepley @*/
5264d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5265d71ae5a4SJacob Faibussowitsch {
5266b0441da4SMatthew G. Knepley   PetscInt Nf;
5267b0441da4SMatthew G. Knepley 
5268b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5269b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52704f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
52714f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
52729566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5273b0441da4SMatthew G. Knepley   if (!Nf) {
52749566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5275b0441da4SMatthew G. Knepley   } else {
52769566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5277b0441da4SMatthew G. Knepley   }
52783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5279b0441da4SMatthew G. Knepley }
5280b0441da4SMatthew G. Knepley 
5281b0441da4SMatthew G. Knepley /*@
5282b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5283b0441da4SMatthew G. Knepley 
528420f4b53cSBarry Smith   Not Collective
5285b0441da4SMatthew G. Knepley 
5286b0441da4SMatthew G. Knepley   Input Parameters:
528720f4b53cSBarry Smith + dm         - The `DM` object
5288b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5289b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5290b0441da4SMatthew G. Knepley 
5291b0441da4SMatthew G. Knepley   Level: developer
5292b0441da4SMatthew G. Knepley 
529320f4b53cSBarry Smith   Notes:
529420f4b53cSBarry Smith .vb
529520f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
529620f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
529720f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
529820f4b53cSBarry Smith .ve
529920f4b53cSBarry Smith 
53001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5301b0441da4SMatthew G. Knepley @*/
5302d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5303d71ae5a4SJacob Faibussowitsch {
5304b0441da4SMatthew G. Knepley   PetscInt Nf;
5305b0441da4SMatthew G. Knepley 
5306b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5307b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53089566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5309b0441da4SMatthew G. Knepley   if (!Nf) {
53109566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5311b0441da4SMatthew G. Knepley   } else {
53129566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5313e5e52638SMatthew G. Knepley   }
53143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5315e5e52638SMatthew G. Knepley }
5316e5e52638SMatthew G. Knepley 
5317d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5318d71ae5a4SJacob Faibussowitsch {
5319799db056SMatthew G. Knepley   DM           plex;
5320799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5321799db056SMatthew G. Knepley   const char **names;
5322799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5323799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5324799db056SMatthew G. Knepley   size_t       len;
5325799db056SMatthew G. Knepley   MPI_Comm     comm;
5326799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5327783e2ec8SMatthew G. Knepley 
5328783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5329799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5330799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5331799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5332799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5333799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5334799db056SMatthew G. Knepley     PetscDS  dsBC;
5335799db056SMatthew G. Knepley     PetscInt numBd;
5336799db056SMatthew G. Knepley 
533707218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5338799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5339799db056SMatthew G. Knepley     maxLabels += numBd;
5340799db056SMatthew G. Knepley   }
5341799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5342799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5343799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5344799db056SMatthew G. Knepley     PetscDS  dsBC;
5345799db056SMatthew G. Knepley     PetscInt numBd, bd;
5346799db056SMatthew G. Knepley 
534707218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5348799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5349799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5350799db056SMatthew G. Knepley       DMLabel      label;
5351799db056SMatthew G. Knepley       PetscInt     field;
5352799db056SMatthew G. Knepley       PetscObject  obj;
5353799db056SMatthew G. Knepley       PetscClassId id;
5354799db056SMatthew G. Knepley 
5355799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
53569566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
53579566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5358799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
53599371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
53609371c9d4SSatish Balay         if (labels[l] == label) break;
5361799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5362783e2ec8SMatthew G. Knepley     }
5363799db056SMatthew G. Knepley   }
5364799db056SMatthew G. Knepley   /* Get label names */
5365799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5366799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
53679371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
53689371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
53699371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
53709371c9d4SSatish Balay   }
5371799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5372712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5373799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5374c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5375799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5376799db056SMatthew G. Knepley   /* Put all names on all processes */
5377799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5378799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5379799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5380799db056SMatthew G. Knepley   gNl = displs[size];
53819371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
53829371c9d4SSatish Balay     counts[p] *= gmaxLen;
53839371c9d4SSatish Balay     displs[p] *= gmaxLen;
53849371c9d4SSatish Balay   }
5385799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5386799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5387799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5388799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5389799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5390799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5391799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
53929371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
53939371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
53949566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5395799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
53969566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5397799db056SMatthew G. Knepley     ++gl;
5398783e2ec8SMatthew G. Knepley   }
5399799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
54003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5401783e2ec8SMatthew G. Knepley }
5402783e2ec8SMatthew G. Knepley 
5403d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5404d71ae5a4SJacob Faibussowitsch {
5405e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5406e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5407e5e52638SMatthew G. Knepley 
5408e5e52638SMatthew G. Knepley   PetscFunctionBegin;
54093ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
54109566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5411e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
54129371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
54139371c9d4SSatish Balay     tmpd[s].ds     = NULL;
54149371c9d4SSatish Balay     tmpd[s].label  = NULL;
54159371c9d4SSatish Balay     tmpd[s].fields = NULL;
54169371c9d4SSatish Balay   }
54179566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5418e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5419e5e52638SMatthew G. Knepley   dm->probs = tmpd;
54203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5421e5e52638SMatthew G. Knepley }
5422e5e52638SMatthew G. Knepley 
5423e5e52638SMatthew G. Knepley /*@
542420f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5425e5e52638SMatthew G. Knepley 
542620f4b53cSBarry Smith   Not Collective
5427e5e52638SMatthew G. Knepley 
5428e5e52638SMatthew G. Knepley   Input Parameter:
542920f4b53cSBarry Smith . dm - The `DM`
5430e5e52638SMatthew G. Knepley 
5431e5e52638SMatthew G. Knepley   Output Parameter:
543220f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5433e5e52638SMatthew G. Knepley 
5434e5e52638SMatthew G. Knepley   Level: intermediate
5435e5e52638SMatthew G. Knepley 
54361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5437e5e52638SMatthew G. Knepley @*/
5438d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5439d71ae5a4SJacob Faibussowitsch {
5440e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5441e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54424f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5443e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
54443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5445e5e52638SMatthew G. Knepley }
5446e5e52638SMatthew G. Knepley 
5447e5e52638SMatthew G. Knepley /*@
544820f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5449e5e52638SMatthew G. Knepley 
545020f4b53cSBarry Smith   Logically Collective
5451e5e52638SMatthew G. Knepley 
5452e5e52638SMatthew G. Knepley   Input Parameter:
545320f4b53cSBarry Smith . dm - The `DM`
5454e5e52638SMatthew G. Knepley 
5455e5e52638SMatthew G. Knepley   Level: intermediate
5456e5e52638SMatthew G. Knepley 
54571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5458e5e52638SMatthew G. Knepley @*/
5459d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5460d71ae5a4SJacob Faibussowitsch {
5461e5e52638SMatthew G. Knepley   PetscInt s;
5462e5e52638SMatthew G. Knepley 
5463e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5464e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5465e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
54669566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
546707218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
54689566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
54699566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5470e5e52638SMatthew G. Knepley   }
54719566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5472e5e52638SMatthew G. Knepley   dm->probs = NULL;
5473e5e52638SMatthew G. Knepley   dm->Nds   = 0;
54743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5475e5e52638SMatthew G. Knepley }
5476e5e52638SMatthew G. Knepley 
5477e5e52638SMatthew G. Knepley /*@
547820f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5479e5e52638SMatthew G. Knepley 
548020f4b53cSBarry Smith   Not Collective
5481e5e52638SMatthew G. Knepley 
5482e5e52638SMatthew G. Knepley   Input Parameter:
548320f4b53cSBarry Smith . dm - The `DM`
5484e5e52638SMatthew G. Knepley 
5485e5e52638SMatthew G. Knepley   Output Parameter:
548607218a29SMatthew G. Knepley . ds - The default `PetscDS`
5487e5e52638SMatthew G. Knepley 
5488e5e52638SMatthew G. Knepley   Level: intermediate
5489e5e52638SMatthew G. Knepley 
54901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5491e5e52638SMatthew G. Knepley @*/
549207218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5493d71ae5a4SJacob Faibussowitsch {
5494e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5495e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54964f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
549707218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
549807218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
54993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5500e5e52638SMatthew G. Knepley }
5501e5e52638SMatthew G. Knepley 
5502e5e52638SMatthew G. Knepley /*@
550320f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5504e5e52638SMatthew G. Knepley 
550520f4b53cSBarry Smith   Not Collective
5506e5e52638SMatthew G. Knepley 
5507e5e52638SMatthew G. Knepley   Input Parameters:
550820f4b53cSBarry Smith + dm    - The `DM`
550920f4b53cSBarry Smith - point - Cell for the `PetscDS`
5510e5e52638SMatthew G. Knepley 
551107218a29SMatthew G. Knepley   Output Parameters:
551207218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
551307218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5514e5e52638SMatthew G. Knepley 
5515e5e52638SMatthew G. Knepley   Level: developer
5516e5e52638SMatthew G. Knepley 
55171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5518e5e52638SMatthew G. Knepley @*/
551907218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5520d71ae5a4SJacob Faibussowitsch {
552107218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5522e5e52638SMatthew G. Knepley   PetscInt s;
5523e5e52638SMatthew G. Knepley 
5524e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5525e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55264f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
55274f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
552863a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
552907218a29SMatthew G. Knepley   if (ds) *ds = NULL;
553007218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5531e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5532e5e52638SMatthew G. Knepley     PetscInt val;
5533e5e52638SMatthew G. Knepley 
55349371c9d4SSatish Balay     if (!dm->probs[s].label) {
553507218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
55369371c9d4SSatish Balay     } else {
55379566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
55389371c9d4SSatish Balay       if (val >= 0) {
553907218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
554007218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
55419371c9d4SSatish Balay         break;
55429371c9d4SSatish Balay       }
5543e5e52638SMatthew G. Knepley     }
5544e5e52638SMatthew G. Knepley   }
554507218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
55463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5547e5e52638SMatthew G. Knepley }
5548e5e52638SMatthew G. Knepley 
5549e5e52638SMatthew G. Knepley /*@
555020f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5551e5e52638SMatthew G. Knepley 
555220f4b53cSBarry Smith   Not Collective
5553e5e52638SMatthew G. Knepley 
5554e5e52638SMatthew G. Knepley   Input Parameters:
555520f4b53cSBarry Smith + dm    - The `DM`
555620f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5557e5e52638SMatthew G. Knepley 
5558b3cf3223SMatthew G. Knepley   Output Parameters:
555920f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
556007218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
556107218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5562e5e52638SMatthew G. Knepley 
5563e5e52638SMatthew G. Knepley   Level: advanced
5564e5e52638SMatthew G. Knepley 
556520f4b53cSBarry Smith   Note:
556620f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
556720f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
556807218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
556920f4b53cSBarry Smith 
55701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5571e5e52638SMatthew G. Knepley @*/
557207218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5573d71ae5a4SJacob Faibussowitsch {
5574e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5575e5e52638SMatthew G. Knepley 
5576e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5577e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5578e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
55799371c9d4SSatish Balay   if (fields) {
55804f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
55819371c9d4SSatish Balay     *fields = NULL;
55829371c9d4SSatish Balay   }
55839371c9d4SSatish Balay   if (ds) {
55844f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
55859371c9d4SSatish Balay     *ds = NULL;
55869371c9d4SSatish Balay   }
558707218a29SMatthew G. Knepley   if (dsIn) {
55884f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
558907218a29SMatthew G. Knepley     *dsIn = NULL;
559007218a29SMatthew G. Knepley   }
5591e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5592154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5593b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5594b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
559507218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
55963ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5597b3cf3223SMatthew G. Knepley     }
5598e5e52638SMatthew G. Knepley   }
55993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5600e5e52638SMatthew G. Knepley }
5601e5e52638SMatthew G. Knepley 
5602e5e52638SMatthew G. Knepley /*@
5603bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5604083401c6SMatthew G. Knepley 
560520f4b53cSBarry Smith   Collective
5606083401c6SMatthew G. Knepley 
5607083401c6SMatthew G. Knepley   Input Parameters:
5608bb7acecfSBarry Smith + dm     - The `DM`
560920f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
561007218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
561107218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
561207218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5613083401c6SMatthew G. Knepley 
561420f4b53cSBarry Smith   Level: advanced
561520f4b53cSBarry Smith 
5616bb7acecfSBarry Smith   Note:
5617bb7acecfSBarry 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,
5618083401c6SMatthew G. Knepley   the fields argument is ignored.
5619083401c6SMatthew G. Knepley 
56201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5621083401c6SMatthew G. Knepley @*/
562207218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5623d71ae5a4SJacob Faibussowitsch {
5624083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5625083401c6SMatthew G. Knepley 
5626083401c6SMatthew G. Knepley   PetscFunctionBegin;
5627083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5628083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
562907218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5630064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
563107218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5632083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5633083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
56349566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
563507218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5636083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
563707218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
56383ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5639083401c6SMatthew G. Knepley     }
5640083401c6SMatthew G. Knepley   }
56419566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
56429566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56439566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
56449566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
564507218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5646083401c6SMatthew G. Knepley   if (!label) {
5647083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5648083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5649083401c6SMatthew G. Knepley     Nds = 0;
5650083401c6SMatthew G. Knepley   }
5651083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5652083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5653083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
565407218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
56553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5656083401c6SMatthew G. Knepley }
5657083401c6SMatthew G. Knepley 
5658083401c6SMatthew G. Knepley /*@
565920f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5660e5e52638SMatthew G. Knepley 
566120f4b53cSBarry Smith   Not Collective
5662e5e52638SMatthew G. Knepley 
5663e5e52638SMatthew G. Knepley   Input Parameters:
566420f4b53cSBarry Smith + dm  - The `DM`
5665e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5666e5e52638SMatthew G. Knepley 
5667e5e52638SMatthew G. Knepley   Output Parameters:
566820f4b53cSBarry Smith + label  - The region label, or `NULL`
566920f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
567007218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
567107218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5672e5e52638SMatthew G. Knepley 
5673e5e52638SMatthew G. Knepley   Level: advanced
5674e5e52638SMatthew G. Knepley 
56751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5676e5e52638SMatthew G. Knepley @*/
567707218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5678d71ae5a4SJacob Faibussowitsch {
5679e5e52638SMatthew G. Knepley   PetscInt Nds;
5680e5e52638SMatthew G. Knepley 
5681e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5682e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56839566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
568463a3b9bcSJacob 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);
5685e5e52638SMatthew G. Knepley   if (label) {
56864f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5687e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5688e5e52638SMatthew G. Knepley   }
5689b3cf3223SMatthew G. Knepley   if (fields) {
56904f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5691b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5692b3cf3223SMatthew G. Knepley   }
5693e5e52638SMatthew G. Knepley   if (ds) {
56944f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5695e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5696e5e52638SMatthew G. Knepley   }
569707218a29SMatthew G. Knepley   if (dsIn) {
56984f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
569907218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
570007218a29SMatthew G. Knepley   }
57013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5702e5e52638SMatthew G. Knepley }
5703e5e52638SMatthew G. Knepley 
5704e5e52638SMatthew G. Knepley /*@
570520f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5706e5e52638SMatthew G. Knepley 
570720f4b53cSBarry Smith   Not Collective
5708e5e52638SMatthew G. Knepley 
5709e5e52638SMatthew G. Knepley   Input Parameters:
571020f4b53cSBarry Smith + dm     - The `DM`
5711083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
571220f4b53cSBarry Smith . label  - The region label, or `NULL`
571307218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
571407218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
571507218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5716e5e52638SMatthew G. Knepley 
5717e5e52638SMatthew G. Knepley   Level: advanced
5718e5e52638SMatthew G. Knepley 
57191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5720e5e52638SMatthew G. Knepley @*/
572107218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5722d71ae5a4SJacob Faibussowitsch {
5723083401c6SMatthew G. Knepley   PetscInt Nds;
5724e5e52638SMatthew G. Knepley 
5725e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5726e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5727ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
57289566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
572963a3b9bcSJacob 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);
57309566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57319566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5732083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5733083401c6SMatthew G. Knepley   if (fields) {
5734083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
57359566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
57369566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5737083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5738e5e52638SMatthew G. Knepley   }
5739083401c6SMatthew G. Knepley   if (ds) {
5740083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
57419566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
57429566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5743083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5744083401c6SMatthew G. Knepley   }
574507218a29SMatthew G. Knepley   if (dsIn) {
574607218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
574707218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
574807218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
574907218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
575007218a29SMatthew G. Knepley   }
57513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5752e5e52638SMatthew G. Knepley }
5753e5e52638SMatthew G. Knepley 
5754e5e52638SMatthew G. Knepley /*@
575520f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
57561d3af9e0SMatthew G. Knepley 
575720f4b53cSBarry Smith   Not Collective
57581d3af9e0SMatthew G. Knepley 
57591d3af9e0SMatthew G. Knepley   Input Parameters:
576020f4b53cSBarry Smith + dm - The `DM`
576120f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
57621d3af9e0SMatthew G. Knepley 
57631d3af9e0SMatthew G. Knepley   Output Parameter:
57641d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
57651d3af9e0SMatthew G. Knepley 
57661d3af9e0SMatthew G. Knepley   Level: advanced
57671d3af9e0SMatthew G. Knepley 
57681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
57691d3af9e0SMatthew G. Knepley @*/
5770d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5771d71ae5a4SJacob Faibussowitsch {
57721d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
57731d3af9e0SMatthew G. Knepley 
57741d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
57751d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57761d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
57774f572ea9SToby Isaac   PetscAssertPointer(num, 3);
57789566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
57799371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
57809371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
57811d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
57821d3af9e0SMatthew G. Knepley   else *num = n;
57833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57841d3af9e0SMatthew G. Knepley }
57851d3af9e0SMatthew G. Knepley 
57862df84da0SMatthew G. Knepley /*@C
5787bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
57882df84da0SMatthew G. Knepley 
578920f4b53cSBarry Smith   Not Collective
57902df84da0SMatthew G. Knepley 
5791f1a722f8SMatthew G. Knepley   Input Parameters:
5792bb7acecfSBarry Smith + dm     - The `DM`
57932df84da0SMatthew G. Knepley . Nc     - The number of components for the field
579420f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5795bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
57962df84da0SMatthew G. Knepley 
57972df84da0SMatthew G. Knepley   Output Parameter:
5798bb7acecfSBarry Smith . fem - The `PetscFE`
57992df84da0SMatthew G. Knepley 
580020f4b53cSBarry Smith   Level: intermediate
580120f4b53cSBarry Smith 
5802bb7acecfSBarry Smith   Note:
5803bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
58042df84da0SMatthew G. Knepley 
58051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
58062df84da0SMatthew G. Knepley @*/
5807d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5808d71ae5a4SJacob Faibussowitsch {
58092df84da0SMatthew G. Knepley   DMPolytopeType ct;
58102df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
58112df84da0SMatthew G. Knepley 
58122df84da0SMatthew G. Knepley   PetscFunctionBegin;
58132df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58142df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
58154f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
58162df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
58174f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
58189566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
58199566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
58209566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
58219566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
58223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58232df84da0SMatthew G. Knepley }
58242df84da0SMatthew G. Knepley 
58251d3af9e0SMatthew G. Knepley /*@
5826bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5827e5e52638SMatthew G. Knepley 
582820f4b53cSBarry Smith   Collective
5829e5e52638SMatthew G. Knepley 
5830e5e52638SMatthew G. Knepley   Input Parameter:
5831bb7acecfSBarry Smith . dm - The `DM`
5832e5e52638SMatthew G. Knepley 
583320f4b53cSBarry Smith   Options Database Key:
5834bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
583545480ffeSMatthew G. Knepley 
583620f4b53cSBarry Smith   Level: intermediate
583720f4b53cSBarry Smith 
58381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5839e5e52638SMatthew G. Knepley @*/
5840d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5841d71ae5a4SJacob Faibussowitsch {
5842e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5843083401c6SMatthew G. Knepley   PetscDS   dsDef;
5844083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5845f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5846f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5847e5e52638SMatthew G. Knepley 
5848e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5849e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58503ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
58519566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
58529566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5853083401c6SMatthew G. Knepley   /* Determine how many regions we have */
58549566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5855083401c6SMatthew G. Knepley   Nl   = 0;
5856083401c6SMatthew G. Knepley   Ndef = 0;
5857083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5858083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5859083401c6SMatthew G. Knepley     PetscInt l;
5860083401c6SMatthew G. Knepley 
5861f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5862f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5863f918ec44SMatthew G. Knepley     {
5864f918ec44SMatthew G. Knepley       PetscClassId id;
5865f918ec44SMatthew G. Knepley 
58669566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5867f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5868f918ec44SMatthew G. Knepley         Ceed ceed;
5869f918ec44SMatthew G. Knepley 
58709566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
58719566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5872f918ec44SMatthew G. Knepley       }
5873f918ec44SMatthew G. Knepley     }
5874f918ec44SMatthew G. Knepley #endif
58759371c9d4SSatish Balay     if (!label) {
58769371c9d4SSatish Balay       ++Ndef;
58779371c9d4SSatish Balay       continue;
58789371c9d4SSatish Balay     }
58799371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
58809371c9d4SSatish Balay       if (label == labelSet[l]) break;
5881083401c6SMatthew G. Knepley     if (l < Nl) continue;
5882083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5883083401c6SMatthew G. Knepley   }
5884083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
588507218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5886083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5887b3cf3223SMatthew G. Knepley     IS        fields;
5888b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5889b3cf3223SMatthew G. Knepley 
58909371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58919371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
58927a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
58939566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
58949371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58959371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
58969566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58979566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58989566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58999566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
590088f0c812SMatthew G. Knepley 
59019566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
590207218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
59039566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59049566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
59052df9ee95SMatthew G. Knepley   }
590607218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
59079566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5908083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5909083401c6SMatthew G. Knepley   if (Ndef && Nl) {
59100122748bSMatthew G. Knepley     DM              plex;
5911083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5912083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5913083401c6SMatthew G. Knepley     PetscInt       *fields;
5914083401c6SMatthew G. Knepley     const PetscInt *cells;
5915083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
59160122748bSMatthew G. Knepley 
59179566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
59189566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
59199566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
59209566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
59215fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5922083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5923083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5924083401c6SMatthew G. Knepley       IS      pointIS;
5925083401c6SMatthew G. Knepley 
59269566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
59279566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
59289566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
59299566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5930083401c6SMatthew G. Knepley     }
59319566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5932083401c6SMatthew G. Knepley 
59339566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
59349566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
59359566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
59369566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
59379566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
59389566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
59399566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5940083401c6SMatthew G. Knepley 
59419566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
59429371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
59439371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
59449566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
59459566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
59469566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
59479566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5948083401c6SMatthew G. Knepley 
59499566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
595007218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
59519566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
59529566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
59539566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59549566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
59559566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5956083401c6SMatthew G. Knepley   }
5957083401c6SMatthew G. Knepley   /* Create label DSes
5958083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5959083401c6SMatthew G. Knepley   */
5960083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5961083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5962083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
596307218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
5964083401c6SMatthew G. Knepley     IS        fields;
5965083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5966083401c6SMatthew G. Knepley 
59679566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
59689371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59699371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
59709566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59719371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59729371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
59739566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59749566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59759566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59769566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
59779566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5978083401c6SMatthew G. Knepley     {
5979083401c6SMatthew G. Knepley       DMPolytopeType ct;
5980083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
59815fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
59820122748bSMatthew G. Knepley 
59839566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5984665f567fSMatthew G. Knepley       if (lStart >= 0) {
59859566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5986412e9a14SMatthew G. Knepley         switch (ct) {
5987412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5988412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5989412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
5990d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5991d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
5992d71ae5a4SJacob Faibussowitsch           break;
5993d71ae5a4SJacob Faibussowitsch         default:
5994d71ae5a4SJacob Faibussowitsch           break;
5995412e9a14SMatthew G. Knepley         }
5996665f567fSMatthew G. Knepley       }
5997712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
599807218a29SMatthew G. Knepley       if (isCohesive) {
599907218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
600007218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
600107218a29SMatthew G. Knepley       }
60025fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
60035fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
60045fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
60059566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
60069566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
600707218a29SMatthew G. Knepley             if (dsIn) {
600807218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
600907218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
601007218a29SMatthew G. Knepley             }
60115fedec97SMatthew G. Knepley           }
60125fedec97SMatthew G. Knepley           ++nf;
60135fedec97SMatthew G. Knepley         }
60145fedec97SMatthew G. Knepley       }
6015e5e52638SMatthew G. Knepley     }
601607218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
601707218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
60189566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
601907218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
6020e5e52638SMatthew G. Knepley   }
60219566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
6022e5e52638SMatthew G. Knepley   /* Set fields in DSes */
6023083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
6024083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
602507218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
6026083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
6027083401c6SMatthew G. Knepley     const PetscInt *fld;
60285fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
60295fedec97SMatthew G. Knepley     PetscBool       isCohesive;
6030e5e52638SMatthew G. Knepley 
60319566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
60329566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
60339566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
60349566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
6035083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
6036083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
60375fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
6038e5e52638SMatthew G. Knepley       PetscClassId id;
6039e5e52638SMatthew G. Knepley 
60405fedec97SMatthew G. Knepley       /* Handle DS with no fields */
60419566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
60425fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
604307218a29SMatthew G. Knepley       if (isCohesive) {
604407218a29SMatthew G. Knepley         if (!isCohesiveField) {
604507218a29SMatthew G. Knepley           PetscObject bdDisc;
604607218a29SMatthew G. Knepley 
604707218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
604807218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
604907218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
605007218a29SMatthew G. Knepley         } else {
60519566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
605207218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
605307218a29SMatthew G. Knepley         }
605407218a29SMatthew G. Knepley       } else {
605507218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
605607218a29SMatthew G. Knepley       }
6057083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
60589566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
6059e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
6060e5e52638SMatthew G. Knepley     }
60619566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6062e5e52638SMatthew G. Knepley   }
6063f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
60649566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6065f9244615SMatthew G. Knepley   if (flg) {
60663b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
60673b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
606807218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
60693b4aee56SMatthew G. Knepley       PetscInt Nf, f;
60703b4aee56SMatthew G. Knepley 
60719566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
607207218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
607307218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
607407218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
607507218a29SMatthew G. Knepley       }
60763b4aee56SMatthew G. Knepley     }
6077f9244615SMatthew G. Knepley   }
6078e5e52638SMatthew G. Knepley   /* Setup DSes */
6079e5e52638SMatthew G. Knepley   if (doSetup) {
608007218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
6081cf1363e4SMatthew G. Knepley       if (dm->setfromoptionscalled) {
6082cf1363e4SMatthew G. Knepley         PetscCall(PetscDSSetFromOptions(dm->probs[s].ds));
6083cf1363e4SMatthew G. Knepley         if (dm->probs[s].dsIn) PetscCall(PetscDSSetFromOptions(dm->probs[s].dsIn));
6084cf1363e4SMatthew G. Knepley       }
608507218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
608607218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
608707218a29SMatthew G. Knepley     }
6088e5e52638SMatthew G. Knepley   }
60893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6090e5e52638SMatthew G. Knepley }
6091e5e52638SMatthew G. Knepley 
6092e5e52638SMatthew G. Knepley /*@
6093d2b2dc1eSMatthew G. Knepley   DMUseTensorOrder - Use a tensor product closure ordering for the default section
6094d2b2dc1eSMatthew G. Knepley 
6095d2b2dc1eSMatthew G. Knepley   Input Parameters:
6096d2b2dc1eSMatthew G. Knepley + dm     - The DM
6097d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order
6098d2b2dc1eSMatthew G. Knepley 
6099d2b2dc1eSMatthew G. Knepley   Level: developer
6100d2b2dc1eSMatthew G. Knepley 
6101d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()`
6102d2b2dc1eSMatthew G. Knepley @*/
6103d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor)
6104d2b2dc1eSMatthew G. Knepley {
6105d2b2dc1eSMatthew G. Knepley   PetscInt  Nf;
6106d2b2dc1eSMatthew G. Knepley   PetscBool reorder = PETSC_TRUE, isPlex;
6107d2b2dc1eSMatthew G. Knepley 
6108d2b2dc1eSMatthew G. Knepley   PetscFunctionBegin;
6109d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex));
6110d2b2dc1eSMatthew G. Knepley   PetscCall(DMGetNumFields(dm, &Nf));
6111d2b2dc1eSMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
6112d2b2dc1eSMatthew G. Knepley     PetscObject  obj;
6113d2b2dc1eSMatthew G. Knepley     PetscClassId id;
6114d2b2dc1eSMatthew G. Knepley 
6115d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetField(dm, f, NULL, &obj));
6116d2b2dc1eSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
6117d2b2dc1eSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
6118d2b2dc1eSMatthew G. Knepley       PetscSpace sp;
6119d2b2dc1eSMatthew G. Knepley       PetscBool  tensor;
6120d2b2dc1eSMatthew G. Knepley 
6121d2b2dc1eSMatthew G. Knepley       PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp));
6122d2b2dc1eSMatthew G. Knepley       PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor));
6123d2b2dc1eSMatthew G. Knepley       reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE;
6124d2b2dc1eSMatthew G. Knepley     } else reorder = PETSC_FALSE;
6125d2b2dc1eSMatthew G. Knepley   }
6126d2b2dc1eSMatthew G. Knepley   if (tensor) {
6127d2b2dc1eSMatthew G. Knepley     if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
6128d2b2dc1eSMatthew G. Knepley   } else {
6129d2b2dc1eSMatthew G. Knepley     PetscSection s;
6130d2b2dc1eSMatthew G. Knepley 
6131d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetLocalSection(dm, &s));
6132d2b2dc1eSMatthew G. Knepley     if (s) PetscCall(PetscSectionResetClosurePermutation(s));
6133d2b2dc1eSMatthew G. Knepley   }
6134d2b2dc1eSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6135d2b2dc1eSMatthew G. Knepley }
6136d2b2dc1eSMatthew G. Knepley 
6137d2b2dc1eSMatthew G. Knepley /*@
6138bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
61397f96f943SMatthew G. Knepley 
614020f4b53cSBarry Smith   Collective
6141f2cacb80SMatthew G. Knepley 
61427f96f943SMatthew G. Knepley   Input Parameters:
6143bb7acecfSBarry Smith + dm   - The `DM`
61447f96f943SMatthew G. Knepley - time - The time
61457f96f943SMatthew G. Knepley 
61467f96f943SMatthew G. Knepley   Output Parameters:
614720f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
614820f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
614920f4b53cSBarry Smith 
615020f4b53cSBarry Smith   Level: developer
61517f96f943SMatthew G. Knepley 
6152bb7acecfSBarry Smith   Note:
6153bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
61547f96f943SMatthew G. Knepley 
61551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
61567f96f943SMatthew G. Knepley @*/
6157d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6158d71ae5a4SJacob Faibussowitsch {
61597f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
61607f96f943SMatthew G. Knepley   void   **ectxs;
6161f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
61627f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
61637f96f943SMatthew G. Knepley 
61647f96f943SMatthew G. Knepley   PetscFunctionBegin;
6165f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6166f60fa741SMatthew G. Knepley   if (u) {
6167f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6168f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6169f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6170f60fa741SMatthew G. Knepley   }
6171f60fa741SMatthew G. Knepley   if (u_t) {
6172f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6173f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6174f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6175f60fa741SMatthew G. Knepley   }
61769566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
61779566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
61789566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
61797f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
61807f96f943SMatthew G. Knepley     PetscDS         ds;
61817f96f943SMatthew G. Knepley     DMLabel         label;
61827f96f943SMatthew G. Knepley     IS              fieldIS;
61837f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
61847f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
61857f96f943SMatthew G. Knepley 
618607218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
61879566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
61889566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
61899566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
61909566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6191f2cacb80SMatthew G. Knepley     if (u) {
6192f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6193f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6194f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
61957f96f943SMatthew G. Knepley     }
6196f2cacb80SMatthew G. Knepley     if (u_t) {
61979566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
61989566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6199f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6200f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6201f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6202f2cacb80SMatthew G. Knepley     }
62039566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6204f2cacb80SMatthew G. Knepley   }
6205f2cacb80SMatthew G. Knepley   if (u) {
62069566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
62079566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6208f2cacb80SMatthew G. Knepley   }
6209f2cacb80SMatthew G. Knepley   if (u_t) {
62109566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
62119566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6212f2cacb80SMatthew G. Knepley   }
62139566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6214f60fa741SMatthew G. Knepley   if (u) {
6215f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6216f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6217f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6218f60fa741SMatthew G. Knepley   }
6219f60fa741SMatthew G. Knepley   if (u_t) {
6220f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6221f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6222f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6223f60fa741SMatthew G. Knepley   }
62243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
62257f96f943SMatthew G. Knepley }
62267f96f943SMatthew G. Knepley 
622707218a29SMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
6228d71ae5a4SJacob Faibussowitsch {
622907218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
623045480ffeSMatthew G. Knepley 
623145480ffeSMatthew G. Knepley   PetscFunctionBegin;
62329566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
623307218a29SMatthew G. Knepley   PetscCall(PetscDSCopy(ds, dm, dsNew));
623407218a29SMatthew G. Knepley   if (dsIn) {
623507218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
623607218a29SMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, dm, dsInNew));
623745480ffeSMatthew G. Knepley   }
623807218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
62399566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
624007218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
62413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
624245480ffeSMatthew G. Knepley }
624345480ffeSMatthew G. Knepley 
62447f96f943SMatthew G. Knepley /*@
6245bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6246e5e52638SMatthew G. Knepley 
624720f4b53cSBarry Smith   Collective
6248e5e52638SMatthew G. Knepley 
6249e5e52638SMatthew G. Knepley   Input Parameter:
6250bb7acecfSBarry Smith . dm - The `DM`
6251e5e52638SMatthew G. Knepley 
6252e5e52638SMatthew G. Knepley   Output Parameter:
6253bb7acecfSBarry Smith . newdm - The `DM`
6254e5e52638SMatthew G. Knepley 
6255e5e52638SMatthew G. Knepley   Level: advanced
6256e5e52638SMatthew G. Knepley 
62571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6258e5e52638SMatthew G. Knepley @*/
6259d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6260d71ae5a4SJacob Faibussowitsch {
6261e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6262e5e52638SMatthew G. Knepley 
6263e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62643ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
62659566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62669566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6267e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6268e5e52638SMatthew G. Knepley     DMLabel  label;
6269b3cf3223SMatthew G. Knepley     IS       fields;
627007218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6271783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6272e5e52638SMatthew G. Knepley 
627307218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6274b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
627507218a29SMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds, dsIn));
6276d5b43468SJose E. Roman     /* Complete new labels in the new DS */
627707218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
62789566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6279783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6280b8025e53SMatthew G. Knepley       PetscWeakForm wf;
628145480ffeSMatthew G. Knepley       DMLabel       label;
6282783e2ec8SMatthew G. Knepley       PetscInt      field;
6283783e2ec8SMatthew G. Knepley 
62849566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
62859566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6286783e2ec8SMatthew G. Knepley     }
6287e5e52638SMatthew G. Knepley   }
6288799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
62893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6290e5e52638SMatthew G. Knepley }
6291e5e52638SMatthew G. Knepley 
6292e5e52638SMatthew G. Knepley /*@
6293bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6294e5e52638SMatthew G. Knepley 
629520f4b53cSBarry Smith   Collective
6296e5e52638SMatthew G. Knepley 
6297e5e52638SMatthew G. Knepley   Input Parameter:
6298bb7acecfSBarry Smith . dm - The `DM`
6299e5e52638SMatthew G. Knepley 
6300e5e52638SMatthew G. Knepley   Output Parameter:
6301bb7acecfSBarry Smith . newdm - The `DM`
6302e5e52638SMatthew G. Knepley 
6303e5e52638SMatthew G. Knepley   Level: advanced
6304e5e52638SMatthew G. Knepley 
630560225df5SJacob Faibussowitsch   Developer Notes:
6306bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6307bb7acecfSBarry Smith 
63081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6309e5e52638SMatthew G. Knepley @*/
6310d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6311d71ae5a4SJacob Faibussowitsch {
6312e5e52638SMatthew G. Knepley   PetscFunctionBegin;
63139566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
63149566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
63153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6316e5e52638SMatthew G. Knepley }
6317e5e52638SMatthew G. Knepley 
6318c73cfb54SMatthew G. Knepley /*@
6319bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6320c73cfb54SMatthew G. Knepley 
632120f4b53cSBarry Smith   Not Collective
6322c73cfb54SMatthew G. Knepley 
6323c73cfb54SMatthew G. Knepley   Input Parameter:
6324bb7acecfSBarry Smith . dm - The `DM`
6325c73cfb54SMatthew G. Knepley 
6326c73cfb54SMatthew G. Knepley   Output Parameter:
6327c73cfb54SMatthew G. Knepley . dim - The topological dimension
6328c73cfb54SMatthew G. Knepley 
6329c73cfb54SMatthew G. Knepley   Level: beginner
6330c73cfb54SMatthew G. Knepley 
63311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6332c73cfb54SMatthew G. Knepley @*/
6333d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6334d71ae5a4SJacob Faibussowitsch {
6335c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6336c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63374f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6338c73cfb54SMatthew G. Knepley   *dim = dm->dim;
63393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6340c73cfb54SMatthew G. Knepley }
6341c73cfb54SMatthew G. Knepley 
6342c73cfb54SMatthew G. Knepley /*@
6343bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6344c73cfb54SMatthew G. Knepley 
634520f4b53cSBarry Smith   Collective
6346c73cfb54SMatthew G. Knepley 
6347c73cfb54SMatthew G. Knepley   Input Parameters:
6348bb7acecfSBarry Smith + dm  - The `DM`
6349c73cfb54SMatthew G. Knepley - dim - The topological dimension
6350c73cfb54SMatthew G. Knepley 
6351c73cfb54SMatthew G. Knepley   Level: beginner
6352c73cfb54SMatthew G. Knepley 
63531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6354c73cfb54SMatthew G. Knepley @*/
6355d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6356d71ae5a4SJacob Faibussowitsch {
6357e5e52638SMatthew G. Knepley   PetscDS  ds;
635845480ffeSMatthew G. Knepley   PetscInt Nds, n;
6359f17e8794SMatthew G. Knepley 
6360c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6361c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6362c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6363c73cfb54SMatthew G. Knepley   dm->dim = dim;
6364d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
63659566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
636645480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
636707218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
63689566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
636945480ffeSMatthew G. Knepley     }
6370d17bd122SMatthew G. Knepley   }
63713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6372c73cfb54SMatthew G. Knepley }
6373c73cfb54SMatthew G. Knepley 
6374793f3fe5SMatthew G. Knepley /*@
6375793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6376793f3fe5SMatthew G. Knepley 
637720f4b53cSBarry Smith   Collective
6378793f3fe5SMatthew G. Knepley 
6379793f3fe5SMatthew G. Knepley   Input Parameters:
6380bb7acecfSBarry Smith + dm  - the `DM`
6381793f3fe5SMatthew G. Knepley - dim - the dimension
6382793f3fe5SMatthew G. Knepley 
6383793f3fe5SMatthew G. Knepley   Output Parameters:
6384793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6385aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6386793f3fe5SMatthew G. Knepley 
638720f4b53cSBarry Smith   Level: intermediate
638820f4b53cSBarry Smith 
6389793f3fe5SMatthew G. Knepley   Note:
6390793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6391a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6392793f3fe5SMatthew G. Knepley   then the interval is empty.
6393793f3fe5SMatthew G. Knepley 
63941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6395793f3fe5SMatthew G. Knepley @*/
6396d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6397d71ae5a4SJacob Faibussowitsch {
6398793f3fe5SMatthew G. Knepley   PetscInt d;
6399793f3fe5SMatthew G. Knepley 
6400793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6401793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64029566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
64037a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6404dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
64053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6406793f3fe5SMatthew G. Knepley }
6407793f3fe5SMatthew G. Knepley 
64086636e97aSMatthew G Knepley /*@
6409bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6410f4d763aaSMatthew G. Knepley 
641120f4b53cSBarry Smith   Collective
64128f700142SStefano Zampini 
6413f4d763aaSMatthew G. Knepley   Input Parameter:
6414bb7acecfSBarry Smith . dm - The original `DM`
6415f4d763aaSMatthew G. Knepley 
6416f4d763aaSMatthew G. Knepley   Output Parameter:
6417bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6418f4d763aaSMatthew G. Knepley 
6419f4d763aaSMatthew G. Knepley   Level: intermediate
6420f4d763aaSMatthew G. Knepley 
6421bb7acecfSBarry Smith   Note:
6422bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6423bb7acecfSBarry 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
6424bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6425bb7acecfSBarry Smith 
64261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6427f4d763aaSMatthew G. Knepley @*/
6428d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6429d71ae5a4SJacob Faibussowitsch {
6430c26acbdeSMatthew G. Knepley   PetscSection section;
6431eb9d3e4dSMatthew G. Knepley   IS           perm;
6432eb9d3e4dSMatthew G. Knepley   PetscBool    hasConstraints, newDM, gnewDM;
643314f150ffSMatthew G. Knepley 
643414f150ffSMatthew G. Knepley   PetscFunctionBegin;
643514f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64364f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
64379566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
64389566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6439eb9d3e4dSMatthew G. Knepley   PetscCall(PetscSectionGetPermutation(section, &perm));
6440eb9d3e4dSMatthew G. Knepley   newDM = hasConstraints || perm ? PETSC_TRUE : PETSC_FALSE;
6441eb9d3e4dSMatthew G. Knepley   PetscCall(MPIU_Allreduce(&newDM, &gnewDM, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
6442eb9d3e4dSMatthew G. Knepley   if (!gnewDM) {
6443c26acbdeSMatthew G. Knepley     *odm = dm;
64443ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6445c26acbdeSMatthew G. Knepley   }
644614f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6447c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
644814f150ffSMatthew G. Knepley     PetscSF      sf;
6449eb9d3e4dSMatthew G. Knepley     PetscBool    usePerm = dm->ignorePermOutput ? PETSC_FALSE : PETSC_TRUE;
645014f150ffSMatthew G. Knepley 
64519566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
64529566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
64539566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
64549566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
64559566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
64569566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
6457eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(section, sf, usePerm, PETSC_TRUE, PETSC_FALSE, &gsection));
64589566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
64599566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
646014f150ffSMatthew G. Knepley   }
646114f150ffSMatthew G. Knepley   *odm = dm->dmBC;
64623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
646314f150ffSMatthew G. Knepley }
6464f4d763aaSMatthew G. Knepley 
6465f4d763aaSMatthew G. Knepley /*@
6466cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6467f4d763aaSMatthew G. Knepley 
6468f4d763aaSMatthew G. Knepley   Input Parameter:
6469bb7acecfSBarry Smith . dm - The original `DM`
6470f4d763aaSMatthew G. Knepley 
6471cdb7a50dSMatthew G. Knepley   Output Parameters:
6472cdb7a50dSMatthew G. Knepley + num - The output sequence number
6473cdb7a50dSMatthew G. Knepley - val - The output sequence value
6474f4d763aaSMatthew G. Knepley 
6475f4d763aaSMatthew G. Knepley   Level: intermediate
6476f4d763aaSMatthew G. Knepley 
6477bb7acecfSBarry Smith   Note:
6478bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6479bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6480bb7acecfSBarry Smith 
648160225df5SJacob Faibussowitsch   Developer Notes:
6482bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6483bb7acecfSBarry Smith   not directly related to the `DM`.
6484f4d763aaSMatthew G. Knepley 
64851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6486f4d763aaSMatthew G. Knepley @*/
6487d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6488d71ae5a4SJacob Faibussowitsch {
6489f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6490f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64919371c9d4SSatish Balay   if (num) {
64924f572ea9SToby Isaac     PetscAssertPointer(num, 2);
64939371c9d4SSatish Balay     *num = dm->outputSequenceNum;
64949371c9d4SSatish Balay   }
64959371c9d4SSatish Balay   if (val) {
64964f572ea9SToby Isaac     PetscAssertPointer(val, 3);
64979371c9d4SSatish Balay     *val = dm->outputSequenceVal;
64989371c9d4SSatish Balay   }
64993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6500f4d763aaSMatthew G. Knepley }
6501f4d763aaSMatthew G. Knepley 
6502f4d763aaSMatthew G. Knepley /*@
6503cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6504f4d763aaSMatthew G. Knepley 
6505f4d763aaSMatthew G. Knepley   Input Parameters:
6506bb7acecfSBarry Smith + dm  - The original `DM`
6507cdb7a50dSMatthew G. Knepley . num - The output sequence number
6508cdb7a50dSMatthew G. Knepley - val - The output sequence value
6509f4d763aaSMatthew G. Knepley 
6510f4d763aaSMatthew G. Knepley   Level: intermediate
6511f4d763aaSMatthew G. Knepley 
6512bb7acecfSBarry Smith   Note:
6513bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6514bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6515f4d763aaSMatthew G. Knepley 
65161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6517f4d763aaSMatthew G. Knepley @*/
6518d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6519d71ae5a4SJacob Faibussowitsch {
6520f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6521f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6522f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6523cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
65243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6525cdb7a50dSMatthew G. Knepley }
6526cdb7a50dSMatthew G. Knepley 
6527cdb7a50dSMatthew G. Knepley /*@C
6528bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6529cdb7a50dSMatthew G. Knepley 
6530cdb7a50dSMatthew G. Knepley   Input Parameters:
6531bb7acecfSBarry Smith + dm     - The original `DM`
6532a4e35b19SJacob Faibussowitsch . viewer - The viewer to get it from
6533cdb7a50dSMatthew G. Knepley . name   - The sequence name
6534cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6535cdb7a50dSMatthew G. Knepley 
6536cdb7a50dSMatthew G. Knepley   Output Parameter:
6537cdb7a50dSMatthew G. Knepley . val - The output sequence value
6538cdb7a50dSMatthew G. Knepley 
6539cdb7a50dSMatthew G. Knepley   Level: intermediate
6540cdb7a50dSMatthew G. Knepley 
6541bb7acecfSBarry Smith   Note:
6542bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6543bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6544bb7acecfSBarry Smith 
654560225df5SJacob Faibussowitsch   Developer Notes:
6546bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6547cdb7a50dSMatthew G. Knepley 
65481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6549cdb7a50dSMatthew G. Knepley @*/
6550d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6551d71ae5a4SJacob Faibussowitsch {
6552cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6553cdb7a50dSMatthew G. Knepley 
6554cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6555cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6556cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
65574f572ea9SToby Isaac   PetscAssertPointer(val, 5);
65589566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6559cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6560cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6561cdb7a50dSMatthew G. Knepley     PetscScalar value;
6562cdb7a50dSMatthew G. Knepley 
65639566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
65644aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6565cdb7a50dSMatthew G. Knepley #endif
6566cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
65673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6568f4d763aaSMatthew G. Knepley }
65698e4ac7eaSMatthew G. Knepley 
65708e4ac7eaSMatthew G. Knepley /*@
6571bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
65728e4ac7eaSMatthew G. Knepley 
657320f4b53cSBarry Smith   Not Collective
65748e4ac7eaSMatthew G. Knepley 
65758e4ac7eaSMatthew G. Knepley   Input Parameter:
6576bb7acecfSBarry Smith . dm - The `DM`
65778e4ac7eaSMatthew G. Knepley 
65788e4ac7eaSMatthew G. Knepley   Output Parameter:
6579bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
65808e4ac7eaSMatthew G. Knepley 
65818e4ac7eaSMatthew G. Knepley   Level: beginner
65828e4ac7eaSMatthew G. Knepley 
65831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
65848e4ac7eaSMatthew G. Knepley @*/
6585d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6586d71ae5a4SJacob Faibussowitsch {
65878e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
65888e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65894f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
65908e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
65913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65928e4ac7eaSMatthew G. Knepley }
65938e4ac7eaSMatthew G. Knepley 
65948e4ac7eaSMatthew G. Knepley /*@
6595bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
65968e4ac7eaSMatthew G. Knepley 
659720f4b53cSBarry Smith   Collective
65988e4ac7eaSMatthew G. Knepley 
65998e4ac7eaSMatthew G. Knepley   Input Parameters:
6600bb7acecfSBarry Smith + dm         - The `DM`
6601bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66028e4ac7eaSMatthew G. Knepley 
6603bb7acecfSBarry Smith   Note:
6604bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
66055d3b26e6SMatthew G. Knepley 
66068e4ac7eaSMatthew G. Knepley   Level: beginner
66078e4ac7eaSMatthew G. Knepley 
66081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
66098e4ac7eaSMatthew G. Knepley @*/
6610d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6611d71ae5a4SJacob Faibussowitsch {
66128e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66138e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66148833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
66158e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
66163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66178e4ac7eaSMatthew G. Knepley }
6618c58f1c22SToby Isaac 
6619c58f1c22SToby Isaac /*@C
6620bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6621c58f1c22SToby Isaac 
6622c58f1c22SToby Isaac   Not Collective
6623c58f1c22SToby Isaac 
6624c58f1c22SToby Isaac   Input Parameters:
6625bb7acecfSBarry Smith + dm   - The `DM` object
6626c58f1c22SToby Isaac - name - The label name
6627c58f1c22SToby Isaac 
6628c58f1c22SToby Isaac   Level: intermediate
6629c58f1c22SToby Isaac 
66301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6631c58f1c22SToby Isaac @*/
6632d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6633d71ae5a4SJacob Faibussowitsch {
66345d80c0bfSVaclav Hapla   PetscBool flg;
66355d80c0bfSVaclav Hapla   DMLabel   label;
6636c58f1c22SToby Isaac 
6637c58f1c22SToby Isaac   PetscFunctionBegin;
6638c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66394f572ea9SToby Isaac   PetscAssertPointer(name, 2);
66409566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6641c58f1c22SToby Isaac   if (!flg) {
66429566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
66439566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
66449566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6645c58f1c22SToby Isaac   }
66463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6647c58f1c22SToby Isaac }
6648c58f1c22SToby Isaac 
6649c58f1c22SToby Isaac /*@C
6650bb7acecfSBarry 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.
66510fdc7489SMatthew Knepley 
66520fdc7489SMatthew Knepley   Not Collective
66530fdc7489SMatthew Knepley 
66540fdc7489SMatthew Knepley   Input Parameters:
6655bb7acecfSBarry Smith + dm   - The `DM` object
66560fdc7489SMatthew Knepley . l    - The index for the label
66570fdc7489SMatthew Knepley - name - The label name
66580fdc7489SMatthew Knepley 
66590fdc7489SMatthew Knepley   Level: intermediate
66600fdc7489SMatthew Knepley 
66611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
66620fdc7489SMatthew Knepley @*/
6663d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6664d71ae5a4SJacob Faibussowitsch {
66650fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
66660fdc7489SMatthew Knepley   DMLabel     label;
66670fdc7489SMatthew Knepley   PetscInt    Nl, m;
66680fdc7489SMatthew Knepley   PetscBool   flg, match;
66690fdc7489SMatthew Knepley   const char *lname;
66700fdc7489SMatthew Knepley 
66710fdc7489SMatthew Knepley   PetscFunctionBegin;
66720fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66734f572ea9SToby Isaac   PetscAssertPointer(name, 3);
66749566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
66750fdc7489SMatthew Knepley   if (!flg) {
66769566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
66779566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
66789566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
66790fdc7489SMatthew Knepley   }
66809566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
668163a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
66820fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
66839566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
66849566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
66850fdc7489SMatthew Knepley     if (match) break;
66860fdc7489SMatthew Knepley   }
66873ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
66880fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
66890fdc7489SMatthew Knepley   else prev->next = orig->next;
66900fdc7489SMatthew Knepley   if (!l) {
66910fdc7489SMatthew Knepley     orig->next = dm->labels;
66920fdc7489SMatthew Knepley     dm->labels = orig;
66930fdc7489SMatthew Knepley   } else {
6694fbccb6d4SPierre Jolivet     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next);
66950fdc7489SMatthew Knepley     orig->next = prev->next;
66960fdc7489SMatthew Knepley     prev->next = orig;
66970fdc7489SMatthew Knepley   }
66983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66990fdc7489SMatthew Knepley }
67000fdc7489SMatthew Knepley 
67010fdc7489SMatthew Knepley /*@C
6702bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6703c58f1c22SToby Isaac 
6704c58f1c22SToby Isaac   Not Collective
6705c58f1c22SToby Isaac 
6706c58f1c22SToby Isaac   Input Parameters:
6707bb7acecfSBarry Smith + dm    - The `DM` object
6708c58f1c22SToby Isaac . name  - The label name
6709c58f1c22SToby Isaac - point - The mesh point
6710c58f1c22SToby Isaac 
6711c58f1c22SToby Isaac   Output Parameter:
6712c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6713c58f1c22SToby Isaac 
6714c58f1c22SToby Isaac   Level: beginner
6715c58f1c22SToby Isaac 
67161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6717c58f1c22SToby Isaac @*/
6718d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6719d71ae5a4SJacob Faibussowitsch {
6720c58f1c22SToby Isaac   DMLabel label;
6721c58f1c22SToby Isaac 
6722c58f1c22SToby Isaac   PetscFunctionBegin;
6723c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67244f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67259566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67267a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
67279566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
67283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6729c58f1c22SToby Isaac }
6730c58f1c22SToby Isaac 
6731c58f1c22SToby Isaac /*@C
6732bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6733c58f1c22SToby Isaac 
6734c58f1c22SToby Isaac   Not Collective
6735c58f1c22SToby Isaac 
6736c58f1c22SToby Isaac   Input Parameters:
6737bb7acecfSBarry Smith + dm    - The `DM` object
6738c58f1c22SToby Isaac . name  - The label name
6739c58f1c22SToby Isaac . point - The mesh point
6740c58f1c22SToby Isaac - value - The label value for this point
6741c58f1c22SToby Isaac 
6742c58f1c22SToby Isaac   Output Parameter:
6743c58f1c22SToby Isaac 
6744c58f1c22SToby Isaac   Level: beginner
6745c58f1c22SToby Isaac 
67461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6747c58f1c22SToby Isaac @*/
6748d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6749d71ae5a4SJacob Faibussowitsch {
6750c58f1c22SToby Isaac   DMLabel label;
6751c58f1c22SToby Isaac 
6752c58f1c22SToby Isaac   PetscFunctionBegin;
6753c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67544f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67559566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6756c58f1c22SToby Isaac   if (!label) {
67579566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
67589566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6759c58f1c22SToby Isaac   }
67609566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
67613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6762c58f1c22SToby Isaac }
6763c58f1c22SToby Isaac 
6764c58f1c22SToby Isaac /*@C
6765bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6766c58f1c22SToby Isaac 
6767c58f1c22SToby Isaac   Not Collective
6768c58f1c22SToby Isaac 
6769c58f1c22SToby Isaac   Input Parameters:
6770bb7acecfSBarry Smith + dm    - The `DM` object
6771c58f1c22SToby Isaac . name  - The label name
6772c58f1c22SToby Isaac . point - The mesh point
6773c58f1c22SToby Isaac - value - The label value for this point
6774c58f1c22SToby Isaac 
6775c58f1c22SToby Isaac   Level: beginner
6776c58f1c22SToby Isaac 
67771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6778c58f1c22SToby Isaac @*/
6779d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6780d71ae5a4SJacob Faibussowitsch {
6781c58f1c22SToby Isaac   DMLabel label;
6782c58f1c22SToby Isaac 
6783c58f1c22SToby Isaac   PetscFunctionBegin;
6784c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67854f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67869566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67873ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67889566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
67893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6790c58f1c22SToby Isaac }
6791c58f1c22SToby Isaac 
6792c58f1c22SToby Isaac /*@C
6793bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6794c58f1c22SToby Isaac 
6795c58f1c22SToby Isaac   Not Collective
6796c58f1c22SToby Isaac 
6797c58f1c22SToby Isaac   Input Parameters:
6798bb7acecfSBarry Smith + dm   - The `DM` object
6799c58f1c22SToby Isaac - name - The label name
6800c58f1c22SToby Isaac 
6801c58f1c22SToby Isaac   Output Parameter:
6802c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6803c58f1c22SToby Isaac 
6804c58f1c22SToby Isaac   Level: beginner
6805c58f1c22SToby Isaac 
680660225df5SJacob Faibussowitsch   Developer Notes:
6807bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6808bb7acecfSBarry Smith 
68091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6810c58f1c22SToby Isaac @*/
6811d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6812d71ae5a4SJacob Faibussowitsch {
6813c58f1c22SToby Isaac   DMLabel label;
6814c58f1c22SToby Isaac 
6815c58f1c22SToby Isaac   PetscFunctionBegin;
6816c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68174f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68184f572ea9SToby Isaac   PetscAssertPointer(size, 3);
68199566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6820c58f1c22SToby Isaac   *size = 0;
68213ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68229566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
68233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6824c58f1c22SToby Isaac }
6825c58f1c22SToby Isaac 
6826c58f1c22SToby Isaac /*@C
6827bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6828c58f1c22SToby Isaac 
6829c58f1c22SToby Isaac   Not Collective
6830c58f1c22SToby Isaac 
6831c58f1c22SToby Isaac   Input Parameters:
683260225df5SJacob Faibussowitsch + dm   - The `DM` object
6833c58f1c22SToby Isaac - name - The label name
6834c58f1c22SToby Isaac 
6835c58f1c22SToby Isaac   Output Parameter:
683620f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6837c58f1c22SToby Isaac 
6838c58f1c22SToby Isaac   Level: beginner
6839c58f1c22SToby Isaac 
68401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6841c58f1c22SToby Isaac @*/
6842d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6843d71ae5a4SJacob Faibussowitsch {
6844c58f1c22SToby Isaac   DMLabel label;
6845c58f1c22SToby Isaac 
6846c58f1c22SToby Isaac   PetscFunctionBegin;
6847c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68484f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68494f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
68509566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6851c58f1c22SToby Isaac   *ids = NULL;
6852dab2e251SBlaise Bourdin   if (label) {
68539566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6854dab2e251SBlaise Bourdin   } else {
6855dab2e251SBlaise Bourdin     /* returning an empty IS */
68569566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6857dab2e251SBlaise Bourdin   }
68583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6859c58f1c22SToby Isaac }
6860c58f1c22SToby Isaac 
6861c58f1c22SToby Isaac /*@C
6862c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6863c58f1c22SToby Isaac 
6864c58f1c22SToby Isaac   Not Collective
6865c58f1c22SToby Isaac 
6866c58f1c22SToby Isaac   Input Parameters:
6867bb7acecfSBarry Smith + dm    - The `DM` object
6868c58f1c22SToby Isaac . name  - The label name
6869c58f1c22SToby Isaac - value - The stratum value
6870c58f1c22SToby Isaac 
6871c58f1c22SToby Isaac   Output Parameter:
6872bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6873c58f1c22SToby Isaac 
6874c58f1c22SToby Isaac   Level: beginner
6875c58f1c22SToby Isaac 
68761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6877c58f1c22SToby Isaac @*/
6878d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6879d71ae5a4SJacob Faibussowitsch {
6880c58f1c22SToby Isaac   DMLabel label;
6881c58f1c22SToby Isaac 
6882c58f1c22SToby Isaac   PetscFunctionBegin;
6883c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68844f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68854f572ea9SToby Isaac   PetscAssertPointer(size, 4);
68869566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6887c58f1c22SToby Isaac   *size = 0;
68883ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68899566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
68903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6891c58f1c22SToby Isaac }
6892c58f1c22SToby Isaac 
6893c58f1c22SToby Isaac /*@C
6894c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6895c58f1c22SToby Isaac 
6896c58f1c22SToby Isaac   Not Collective
6897c58f1c22SToby Isaac 
6898c58f1c22SToby Isaac   Input Parameters:
6899bb7acecfSBarry Smith + dm    - The `DM` object
6900c58f1c22SToby Isaac . name  - The label name
6901c58f1c22SToby Isaac - value - The stratum value
6902c58f1c22SToby Isaac 
6903c58f1c22SToby Isaac   Output Parameter:
690420f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
6905c58f1c22SToby Isaac 
6906c58f1c22SToby Isaac   Level: beginner
6907c58f1c22SToby Isaac 
69081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6909c58f1c22SToby Isaac @*/
6910d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6911d71ae5a4SJacob Faibussowitsch {
6912c58f1c22SToby Isaac   DMLabel label;
6913c58f1c22SToby Isaac 
6914c58f1c22SToby Isaac   PetscFunctionBegin;
6915c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69164f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69174f572ea9SToby Isaac   PetscAssertPointer(points, 4);
69189566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6919c58f1c22SToby Isaac   *points = NULL;
69203ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69219566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
69223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6923c58f1c22SToby Isaac }
6924c58f1c22SToby Isaac 
69254de306b1SToby Isaac /*@C
69269044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
69274de306b1SToby Isaac 
69284de306b1SToby Isaac   Not Collective
69294de306b1SToby Isaac 
69304de306b1SToby Isaac   Input Parameters:
6931bb7acecfSBarry Smith + dm     - The `DM` object
69324de306b1SToby Isaac . name   - The label name
69334de306b1SToby Isaac . value  - The stratum value
69344de306b1SToby Isaac - points - The stratum points
69354de306b1SToby Isaac 
69364de306b1SToby Isaac   Level: beginner
69374de306b1SToby Isaac 
69381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
69394de306b1SToby Isaac @*/
6940d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6941d71ae5a4SJacob Faibussowitsch {
69424de306b1SToby Isaac   DMLabel label;
69434de306b1SToby Isaac 
69444de306b1SToby Isaac   PetscFunctionBegin;
69454de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69464f572ea9SToby Isaac   PetscAssertPointer(name, 2);
6947292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
69489566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
69493ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69509566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
69513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
69524de306b1SToby Isaac }
69534de306b1SToby Isaac 
6954c58f1c22SToby Isaac /*@C
6955bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6956c58f1c22SToby Isaac 
6957c58f1c22SToby Isaac   Not Collective
6958c58f1c22SToby Isaac 
6959c58f1c22SToby Isaac   Input Parameters:
6960bb7acecfSBarry Smith + dm    - The `DM` object
6961c58f1c22SToby Isaac . name  - The label name
6962c58f1c22SToby Isaac - value - The label value for this point
6963c58f1c22SToby Isaac 
6964c58f1c22SToby Isaac   Output Parameter:
6965c58f1c22SToby Isaac 
6966c58f1c22SToby Isaac   Level: beginner
6967c58f1c22SToby Isaac 
69681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6969c58f1c22SToby Isaac @*/
6970d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6971d71ae5a4SJacob Faibussowitsch {
6972c58f1c22SToby Isaac   DMLabel label;
6973c58f1c22SToby Isaac 
6974c58f1c22SToby Isaac   PetscFunctionBegin;
6975c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69764f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69779566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
69783ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69799566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
69803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6981c58f1c22SToby Isaac }
6982c58f1c22SToby Isaac 
6983c58f1c22SToby Isaac /*@
6984bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6985c58f1c22SToby Isaac 
6986c58f1c22SToby Isaac   Not Collective
6987c58f1c22SToby Isaac 
6988c58f1c22SToby Isaac   Input Parameter:
6989bb7acecfSBarry Smith . dm - The `DM` object
6990c58f1c22SToby Isaac 
6991c58f1c22SToby Isaac   Output Parameter:
6992c58f1c22SToby Isaac . numLabels - the number of Labels
6993c58f1c22SToby Isaac 
6994c58f1c22SToby Isaac   Level: intermediate
6995c58f1c22SToby Isaac 
69961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6997c58f1c22SToby Isaac @*/
6998d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6999d71ae5a4SJacob Faibussowitsch {
70005d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7001c58f1c22SToby Isaac   PetscInt    n    = 0;
7002c58f1c22SToby Isaac 
7003c58f1c22SToby Isaac   PetscFunctionBegin;
7004c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70054f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
70069371c9d4SSatish Balay   while (next) {
70079371c9d4SSatish Balay     ++n;
70089371c9d4SSatish Balay     next = next->next;
70099371c9d4SSatish Balay   }
7010c58f1c22SToby Isaac   *numLabels = n;
70113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7012c58f1c22SToby Isaac }
7013c58f1c22SToby Isaac 
7014c58f1c22SToby Isaac /*@C
7015c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7016c58f1c22SToby Isaac 
7017c58f1c22SToby Isaac   Not Collective
7018c58f1c22SToby Isaac 
7019c58f1c22SToby Isaac   Input Parameters:
7020bb7acecfSBarry Smith + dm - The `DM` object
7021c58f1c22SToby Isaac - n  - the label number
7022c58f1c22SToby Isaac 
7023c58f1c22SToby Isaac   Output Parameter:
7024c58f1c22SToby Isaac . name - the label name
7025c58f1c22SToby Isaac 
7026c58f1c22SToby Isaac   Level: intermediate
7027c58f1c22SToby Isaac 
702860225df5SJacob Faibussowitsch   Developer Notes:
7029bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
7030bb7acecfSBarry Smith 
70311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7032c58f1c22SToby Isaac @*/
7033d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7034d71ae5a4SJacob Faibussowitsch {
70355d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7036c58f1c22SToby Isaac   PetscInt    l    = 0;
7037c58f1c22SToby Isaac 
7038c58f1c22SToby Isaac   PetscFunctionBegin;
7039c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70404f572ea9SToby Isaac   PetscAssertPointer(name, 3);
7041c58f1c22SToby Isaac   while (next) {
7042c58f1c22SToby Isaac     if (l == n) {
70439566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
70443ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7045c58f1c22SToby Isaac     }
7046c58f1c22SToby Isaac     ++l;
7047c58f1c22SToby Isaac     next = next->next;
7048c58f1c22SToby Isaac   }
704963a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7050c58f1c22SToby Isaac }
7051c58f1c22SToby Isaac 
7052c58f1c22SToby Isaac /*@C
7053bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
7054c58f1c22SToby Isaac 
7055c58f1c22SToby Isaac   Not Collective
7056c58f1c22SToby Isaac 
7057c58f1c22SToby Isaac   Input Parameters:
7058bb7acecfSBarry Smith + dm   - The `DM` object
7059c58f1c22SToby Isaac - name - The label name
7060c58f1c22SToby Isaac 
7061c58f1c22SToby Isaac   Output Parameter:
7062bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
7063c58f1c22SToby Isaac 
7064c58f1c22SToby Isaac   Level: intermediate
7065c58f1c22SToby Isaac 
70661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7067c58f1c22SToby Isaac @*/
7068d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7069d71ae5a4SJacob Faibussowitsch {
70705d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7071d67d17b1SMatthew G. Knepley   const char *lname;
7072c58f1c22SToby Isaac 
7073c58f1c22SToby Isaac   PetscFunctionBegin;
7074c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70754f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70764f572ea9SToby Isaac   PetscAssertPointer(hasLabel, 3);
7077c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7078c58f1c22SToby Isaac   while (next) {
70799566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70809566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7081c58f1c22SToby Isaac     if (*hasLabel) break;
7082c58f1c22SToby Isaac     next = next->next;
7083c58f1c22SToby Isaac   }
70843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7085c58f1c22SToby Isaac }
7086c58f1c22SToby Isaac 
7087a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7088c58f1c22SToby Isaac /*@C
708920f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
7090c58f1c22SToby Isaac 
7091c58f1c22SToby Isaac   Not Collective
7092c58f1c22SToby Isaac 
7093c58f1c22SToby Isaac   Input Parameters:
7094bb7acecfSBarry Smith + dm   - The `DM` object
7095c58f1c22SToby Isaac - name - The label name
7096c58f1c22SToby Isaac 
7097c58f1c22SToby Isaac   Output Parameter:
709820f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
7099c58f1c22SToby Isaac 
7100bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7101bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7102bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7103bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7104bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7105bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7106bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
71076d7c9049SMatthew G. Knepley 
7108c58f1c22SToby Isaac   Level: intermediate
7109c58f1c22SToby Isaac 
711060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7111c58f1c22SToby Isaac @*/
7112d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7113d71ae5a4SJacob Faibussowitsch {
71145d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7115c58f1c22SToby Isaac   PetscBool   hasLabel;
7116d67d17b1SMatthew G. Knepley   const char *lname;
7117c58f1c22SToby Isaac 
7118c58f1c22SToby Isaac   PetscFunctionBegin;
7119c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71204f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71214f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7122c58f1c22SToby Isaac   *label = NULL;
7123c58f1c22SToby Isaac   while (next) {
71249566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71259566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7126c58f1c22SToby Isaac     if (hasLabel) {
7127c58f1c22SToby Isaac       *label = next->label;
7128c58f1c22SToby Isaac       break;
7129c58f1c22SToby Isaac     }
7130c58f1c22SToby Isaac     next = next->next;
7131c58f1c22SToby Isaac   }
71323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7133c58f1c22SToby Isaac }
7134c58f1c22SToby Isaac 
7135c58f1c22SToby Isaac /*@C
7136bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7137c58f1c22SToby Isaac 
7138c58f1c22SToby Isaac   Not Collective
7139c58f1c22SToby Isaac 
7140c58f1c22SToby Isaac   Input Parameters:
7141bb7acecfSBarry Smith + dm - The `DM` object
7142c58f1c22SToby Isaac - n  - the label number
7143c58f1c22SToby Isaac 
7144c58f1c22SToby Isaac   Output Parameter:
7145c58f1c22SToby Isaac . label - the label
7146c58f1c22SToby Isaac 
7147c58f1c22SToby Isaac   Level: intermediate
7148c58f1c22SToby Isaac 
71491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7150c58f1c22SToby Isaac @*/
7151d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7152d71ae5a4SJacob Faibussowitsch {
71535d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7154c58f1c22SToby Isaac   PetscInt    l    = 0;
7155c58f1c22SToby Isaac 
7156c58f1c22SToby Isaac   PetscFunctionBegin;
7157c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71584f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7159c58f1c22SToby Isaac   while (next) {
7160c58f1c22SToby Isaac     if (l == n) {
7161c58f1c22SToby Isaac       *label = next->label;
71623ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7163c58f1c22SToby Isaac     }
7164c58f1c22SToby Isaac     ++l;
7165c58f1c22SToby Isaac     next = next->next;
7166c58f1c22SToby Isaac   }
716763a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7168c58f1c22SToby Isaac }
7169c58f1c22SToby Isaac 
7170c58f1c22SToby Isaac /*@C
7171bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7172c58f1c22SToby Isaac 
7173c58f1c22SToby Isaac   Not Collective
7174c58f1c22SToby Isaac 
7175c58f1c22SToby Isaac   Input Parameters:
7176bb7acecfSBarry Smith + dm    - The `DM` object
7177bb7acecfSBarry Smith - label - The `DMLabel`
7178c58f1c22SToby Isaac 
7179c58f1c22SToby Isaac   Level: developer
7180c58f1c22SToby Isaac 
718160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7182c58f1c22SToby Isaac @*/
7183d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7184d71ae5a4SJacob Faibussowitsch {
71855d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7186c58f1c22SToby Isaac   PetscBool   hasLabel;
7187d67d17b1SMatthew G. Knepley   const char *lname;
71885d80c0bfSVaclav Hapla   PetscBool   flg;
7189c58f1c22SToby Isaac 
7190c58f1c22SToby Isaac   PetscFunctionBegin;
7191c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71929566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
71939566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
71947a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
71959566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7196c58f1c22SToby Isaac   tmpLabel->label  = label;
7197c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
71985d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
71995d80c0bfSVaclav Hapla   *p = tmpLabel;
72009566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
72019566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
72025d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
72039566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7204ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
72053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7206c58f1c22SToby Isaac }
7207c58f1c22SToby Isaac 
7208a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7209c58f1c22SToby Isaac /*@C
72104a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
72114a7ee7d0SMatthew G. Knepley 
72124a7ee7d0SMatthew G. Knepley   Not Collective
72134a7ee7d0SMatthew G. Knepley 
72144a7ee7d0SMatthew G. Knepley   Input Parameters:
7215bb7acecfSBarry Smith + dm    - The `DM` object
7216bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
72174a7ee7d0SMatthew G. Knepley 
7218bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7219bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7220bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7221bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7222bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7223bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7224bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
72254a7ee7d0SMatthew G. Knepley 
72264a7ee7d0SMatthew G. Knepley   Level: intermediate
72274a7ee7d0SMatthew G. Knepley 
72281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
72294a7ee7d0SMatthew G. Knepley @*/
7230d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7231d71ae5a4SJacob Faibussowitsch {
72324a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
72334a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
72344a7ee7d0SMatthew G. Knepley   const char *name, *lname;
72354a7ee7d0SMatthew G. Knepley 
72364a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
72374a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72384a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
72399566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
72404a7ee7d0SMatthew G. Knepley   while (next) {
72419566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72429566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
72434a7ee7d0SMatthew G. Knepley     if (hasLabel) {
72449566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
72459566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
72464a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
72479566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
72484a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
72499566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
72504a7ee7d0SMatthew G. Knepley       next->label = label;
72514a7ee7d0SMatthew G. Knepley       break;
72524a7ee7d0SMatthew G. Knepley     }
72534a7ee7d0SMatthew G. Knepley     next = next->next;
72544a7ee7d0SMatthew G. Knepley   }
72553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
72564a7ee7d0SMatthew G. Knepley }
72574a7ee7d0SMatthew G. Knepley 
72584a7ee7d0SMatthew G. Knepley /*@C
7259bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7260c58f1c22SToby Isaac 
7261c58f1c22SToby Isaac   Not Collective
7262c58f1c22SToby Isaac 
7263c58f1c22SToby Isaac   Input Parameters:
7264bb7acecfSBarry Smith + dm   - The `DM` object
7265c58f1c22SToby Isaac - name - The label name
7266c58f1c22SToby Isaac 
7267c58f1c22SToby Isaac   Output Parameter:
726820f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7269bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7270c58f1c22SToby Isaac 
7271c58f1c22SToby Isaac   Level: developer
7272c58f1c22SToby Isaac 
72731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7274c58f1c22SToby Isaac @*/
7275d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7276d71ae5a4SJacob Faibussowitsch {
727795d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7278c58f1c22SToby Isaac   PetscBool   hasLabel;
7279d67d17b1SMatthew G. Knepley   const char *lname;
7280c58f1c22SToby Isaac 
7281c58f1c22SToby Isaac   PetscFunctionBegin;
7282c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72834f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7284e5472504SVaclav Hapla   if (label) {
72854f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7286c58f1c22SToby Isaac     *label = NULL;
7287e5472504SVaclav Hapla   }
72885d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
72899566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
72909566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7291c58f1c22SToby Isaac     if (hasLabel) {
729295d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
72939566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
729495d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
72959566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7296ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
729795d578d6SVaclav Hapla       if (label) *label = link->label;
72989566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
72999566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7300c58f1c22SToby Isaac       break;
7301c58f1c22SToby Isaac     }
7302c58f1c22SToby Isaac   }
73033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7304c58f1c22SToby Isaac }
7305c58f1c22SToby Isaac 
7306306894acSVaclav Hapla /*@
7307bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7308306894acSVaclav Hapla 
7309306894acSVaclav Hapla   Not Collective
7310306894acSVaclav Hapla 
7311306894acSVaclav Hapla   Input Parameters:
7312bb7acecfSBarry Smith + dm           - The `DM` object
7313bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
731420f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7315306894acSVaclav Hapla 
7316306894acSVaclav Hapla   Level: developer
7317306894acSVaclav Hapla 
7318bb7acecfSBarry Smith   Note:
7319306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7320bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7321306894acSVaclav Hapla   *label nullified.
7322306894acSVaclav Hapla 
73231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7324306894acSVaclav Hapla @*/
7325d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7326d71ae5a4SJacob Faibussowitsch {
732743e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7328306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7329306894acSVaclav Hapla 
7330306894acSVaclav Hapla   PetscFunctionBegin;
7331306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73324f572ea9SToby Isaac   PetscAssertPointer(label, 2);
73333ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7334306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7335306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
73365d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
733743e45a93SVaclav Hapla     if (*label == link->label) {
7338306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
733943e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7340306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7341ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
734243e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
73439566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
73449566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7345306894acSVaclav Hapla       break;
7346306894acSVaclav Hapla     }
7347306894acSVaclav Hapla   }
73487a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
73493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7350306894acSVaclav Hapla }
7351306894acSVaclav Hapla 
7352c58f1c22SToby Isaac /*@C
7353c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7354c58f1c22SToby Isaac 
7355c58f1c22SToby Isaac   Not Collective
7356c58f1c22SToby Isaac 
7357c58f1c22SToby Isaac   Input Parameters:
7358bb7acecfSBarry Smith + dm   - The `DM` object
7359c58f1c22SToby Isaac - name - The label name
7360c58f1c22SToby Isaac 
7361c58f1c22SToby Isaac   Output Parameter:
7362c58f1c22SToby Isaac . output - The flag for output
7363c58f1c22SToby Isaac 
7364c58f1c22SToby Isaac   Level: developer
7365c58f1c22SToby Isaac 
73661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7367c58f1c22SToby Isaac @*/
7368d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7369d71ae5a4SJacob Faibussowitsch {
73705d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7371d67d17b1SMatthew G. Knepley   const char *lname;
7372c58f1c22SToby Isaac 
7373c58f1c22SToby Isaac   PetscFunctionBegin;
7374c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73754f572ea9SToby Isaac   PetscAssertPointer(name, 2);
73764f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7377c58f1c22SToby Isaac   while (next) {
7378c58f1c22SToby Isaac     PetscBool flg;
7379c58f1c22SToby Isaac 
73809566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73819566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
73829371c9d4SSatish Balay     if (flg) {
73839371c9d4SSatish Balay       *output = next->output;
73843ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
73859371c9d4SSatish Balay     }
7386c58f1c22SToby Isaac     next = next->next;
7387c58f1c22SToby Isaac   }
738898921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7389c58f1c22SToby Isaac }
7390c58f1c22SToby Isaac 
7391c58f1c22SToby Isaac /*@C
7392bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7393c58f1c22SToby Isaac 
7394c58f1c22SToby Isaac   Not Collective
7395c58f1c22SToby Isaac 
7396c58f1c22SToby Isaac   Input Parameters:
7397bb7acecfSBarry Smith + dm     - The `DM` object
7398c58f1c22SToby Isaac . name   - The label name
7399bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7400c58f1c22SToby Isaac 
7401c58f1c22SToby Isaac   Level: developer
7402c58f1c22SToby Isaac 
74031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7404c58f1c22SToby Isaac @*/
7405d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7406d71ae5a4SJacob Faibussowitsch {
74075d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7408d67d17b1SMatthew G. Knepley   const char *lname;
7409c58f1c22SToby Isaac 
7410c58f1c22SToby Isaac   PetscFunctionBegin;
7411c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74124f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7413c58f1c22SToby Isaac   while (next) {
7414c58f1c22SToby Isaac     PetscBool flg;
7415c58f1c22SToby Isaac 
74169566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74179566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74189371c9d4SSatish Balay     if (flg) {
74199371c9d4SSatish Balay       next->output = output;
74203ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74219371c9d4SSatish Balay     }
7422c58f1c22SToby Isaac     next = next->next;
7423c58f1c22SToby Isaac   }
742498921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7425c58f1c22SToby Isaac }
7426c58f1c22SToby Isaac 
7427c58f1c22SToby Isaac /*@
7428bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7429c58f1c22SToby Isaac 
743020f4b53cSBarry Smith   Collective
7431c58f1c22SToby Isaac 
7432d8d19677SJose E. Roman   Input Parameters:
7433bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7434bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7435bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7436bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7437bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7438c58f1c22SToby Isaac 
7439c58f1c22SToby Isaac   Level: intermediate
7440c58f1c22SToby Isaac 
7441bb7acecfSBarry Smith   Note:
74422cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7443c58f1c22SToby Isaac 
74441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7445c58f1c22SToby Isaac @*/
7446d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7447d71ae5a4SJacob Faibussowitsch {
74482cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7449c58f1c22SToby Isaac   const char *name;
7450c58f1c22SToby Isaac   PetscBool   flg;
74515d80c0bfSVaclav Hapla   DMLabelLink link;
7452c58f1c22SToby Isaac 
74535d80c0bfSVaclav Hapla   PetscFunctionBegin;
74545d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
74555d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
74565d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
74575d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
74587a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
74593ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
74605d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
74615d80c0bfSVaclav Hapla     label = link->label;
74629566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
74635d80c0bfSVaclav Hapla     if (!all) {
74649566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7465c58f1c22SToby Isaac       if (flg) continue;
74669566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
74677d5acc75SStefano Zampini       if (flg) continue;
74689566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7469ba2698f1SMatthew G. Knepley       if (flg) continue;
74705d80c0bfSVaclav Hapla     }
74719566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
74722cbb9b06SVaclav Hapla     if (labelOld) {
74732cbb9b06SVaclav Hapla       switch (emode) {
7474d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7475d71ae5a4SJacob Faibussowitsch         continue;
7476d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7477d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7478d71ae5a4SJacob Faibussowitsch         break;
7479d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7480d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7481d71ae5a4SJacob Faibussowitsch       default:
7482d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
74832cbb9b06SVaclav Hapla       }
74842cbb9b06SVaclav Hapla     }
74855d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
74869566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
74875d80c0bfSVaclav Hapla     } else {
74885d80c0bfSVaclav Hapla       labelNew = label;
74895d80c0bfSVaclav Hapla     }
74909566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
74919566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7492c58f1c22SToby Isaac   }
74933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7494c58f1c22SToby Isaac }
7495461a15a0SLisandro Dalcin 
7496609dae6eSVaclav Hapla /*@C
7497b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7498609dae6eSVaclav Hapla 
749920f4b53cSBarry Smith   Collective; No Fortran Support
7500609dae6eSVaclav Hapla 
7501609dae6eSVaclav Hapla   Input Parameters:
7502bb7acecfSBarry Smith + dm0 - First `DM` object
7503bb7acecfSBarry Smith - dm1 - Second `DM` object
7504609dae6eSVaclav Hapla 
7505a4e35b19SJacob Faibussowitsch   Output Parameters:
75065efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
750720f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7508609dae6eSVaclav Hapla 
7509609dae6eSVaclav Hapla   Level: intermediate
7510609dae6eSVaclav Hapla 
7511609dae6eSVaclav Hapla   Notes:
7512bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7513bb7acecfSBarry Smith 
751420f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7515bb7acecfSBarry Smith 
751620f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7517609dae6eSVaclav Hapla 
75185efe38ccSVaclav Hapla   The output message is set independently on each rank.
7519bb7acecfSBarry Smith 
7520bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7521bb7acecfSBarry Smith 
752220f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7523bb7acecfSBarry Smith 
752420f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7525609dae6eSVaclav Hapla 
7526609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7527bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7528609dae6eSVaclav Hapla 
75291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7530609dae6eSVaclav Hapla @*/
7531d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7532d71ae5a4SJacob Faibussowitsch {
75335efe38ccSVaclav Hapla   PetscInt    n, i;
7534609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
75355efe38ccSVaclav Hapla   PetscBool   eq;
7536609dae6eSVaclav Hapla   MPI_Comm    comm;
75375efe38ccSVaclav Hapla   PetscMPIInt rank;
7538609dae6eSVaclav Hapla 
7539609dae6eSVaclav Hapla   PetscFunctionBegin;
7540609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7541609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7542609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
75434f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
75444f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
75459566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
75469566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
75475efe38ccSVaclav Hapla   {
75485efe38ccSVaclav Hapla     PetscInt n1;
75495efe38ccSVaclav Hapla 
75509566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
75519566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
75525efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
755348a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7554712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
75555efe38ccSVaclav Hapla     if (!eq) goto finish;
75565efe38ccSVaclav Hapla   }
75575efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7558609dae6eSVaclav Hapla     DMLabel     l0, l1;
7559609dae6eSVaclav Hapla     const char *name;
7560609dae6eSVaclav Hapla     char       *msgInner;
7561609dae6eSVaclav Hapla 
7562609dae6eSVaclav Hapla     /* Ignore label order */
75639566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
75649566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
75659566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7566609dae6eSVaclav Hapla     if (!l1) {
756763a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
75685efe38ccSVaclav Hapla       eq = PETSC_FALSE;
75695efe38ccSVaclav Hapla       break;
7570609dae6eSVaclav Hapla     }
75719566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
75729566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
75739566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
75745efe38ccSVaclav Hapla     if (!eq) break;
7575609dae6eSVaclav Hapla   }
7576712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7577609dae6eSVaclav Hapla finish:
75785efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7579609dae6eSVaclav Hapla   if (message) {
7580609dae6eSVaclav Hapla     *message = NULL;
758148a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
75825efe38ccSVaclav Hapla   } else {
758348a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
75849566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
75855efe38ccSVaclav Hapla   }
75865efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
75875efe38ccSVaclav Hapla   if (equal) *equal = eq;
75887a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
75893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7590609dae6eSVaclav Hapla }
7591609dae6eSVaclav Hapla 
7592d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7593d71ae5a4SJacob Faibussowitsch {
7594461a15a0SLisandro Dalcin   PetscFunctionBegin;
75954f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7596461a15a0SLisandro Dalcin   if (!*label) {
75979566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
75989566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7599461a15a0SLisandro Dalcin   }
76009566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
76013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7602461a15a0SLisandro Dalcin }
7603461a15a0SLisandro Dalcin 
76040fdc7489SMatthew Knepley /*
76050fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
76060fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
76070fdc7489SMatthew Knepley   (label, id) pair in the DM.
76080fdc7489SMatthew Knepley 
76090fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
76100fdc7489SMatthew Knepley   each label.
76110fdc7489SMatthew Knepley */
7612d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7613d71ae5a4SJacob Faibussowitsch {
76140fdc7489SMatthew Knepley   DMUniversalLabel ul;
76150fdc7489SMatthew Knepley   PetscBool       *active;
76160fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
76170fdc7489SMatthew Knepley 
76180fdc7489SMatthew Knepley   PetscFunctionBegin;
76199566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
76209566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
76219566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
76229566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
76230fdc7489SMatthew Knepley   ul->Nl = 0;
76240fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
76250fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
76260fdc7489SMatthew Knepley     const char *name;
76270fdc7489SMatthew Knepley 
76289566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
76299566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
76309566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
76310fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
76320fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
76330fdc7489SMatthew Knepley   }
76349566063dSJacob 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));
76350fdc7489SMatthew Knepley   ul->Nv = 0;
76360fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
76370fdc7489SMatthew Knepley     DMLabel     label;
76380fdc7489SMatthew Knepley     PetscInt    nv;
76390fdc7489SMatthew Knepley     const char *name;
76400fdc7489SMatthew Knepley 
76410fdc7489SMatthew Knepley     if (!active[l]) continue;
76429566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
76439566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
76449566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
76459566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
76460fdc7489SMatthew Knepley     ul->indices[m] = l;
76470fdc7489SMatthew Knepley     ul->Nv += nv;
76480fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
76490fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
76500fdc7489SMatthew Knepley     ++m;
76510fdc7489SMatthew Knepley   }
76520fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
76530fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
76540fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
76550fdc7489SMatthew Knepley   }
76560fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
76570fdc7489SMatthew Knepley     PetscInt b;
76580fdc7489SMatthew Knepley 
76590fdc7489SMatthew Knepley     ul->masks[l] = 0;
76600fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
76610fdc7489SMatthew Knepley   }
76629566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
76630fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
76640fdc7489SMatthew Knepley     DMLabel         label;
76650fdc7489SMatthew Knepley     IS              valueIS;
76660fdc7489SMatthew Knepley     const PetscInt *varr;
76670fdc7489SMatthew Knepley     PetscInt        nv, v;
76680fdc7489SMatthew Knepley 
76690fdc7489SMatthew Knepley     if (!active[l]) continue;
76709566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
76719566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
76729566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
76739566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7674ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
76759566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
76769566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
76779566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
76780fdc7489SMatthew Knepley     ++m;
76790fdc7489SMatthew Knepley   }
76809566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
76810fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
76820fdc7489SMatthew Knepley     PetscInt  uval   = 0;
76830fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
76840fdc7489SMatthew Knepley 
76850fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
76860fdc7489SMatthew Knepley       DMLabel  label;
76870649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
76880fdc7489SMatthew Knepley 
76890fdc7489SMatthew Knepley       if (!active[l]) continue;
76909566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
76919566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
76929566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
76939371c9d4SSatish Balay       if (val == defval) {
76949371c9d4SSatish Balay         ++m;
76959371c9d4SSatish Balay         continue;
76969371c9d4SSatish Balay       }
76970649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
76980fdc7489SMatthew Knepley       marked = PETSC_TRUE;
76999566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
770063a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
77010fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
77020fdc7489SMatthew Knepley       ++m;
77030fdc7489SMatthew Knepley     }
77049566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
77050fdc7489SMatthew Knepley   }
77069566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
77070fdc7489SMatthew Knepley   *universal = ul;
77083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77090fdc7489SMatthew Knepley }
77100fdc7489SMatthew Knepley 
7711d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7712d71ae5a4SJacob Faibussowitsch {
77130fdc7489SMatthew Knepley   PetscInt l;
77140fdc7489SMatthew Knepley 
77150fdc7489SMatthew Knepley   PetscFunctionBegin;
77169566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
77179566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
77189566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
77199566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
77209566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
77210fdc7489SMatthew Knepley   *universal = NULL;
77223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77230fdc7489SMatthew Knepley }
77240fdc7489SMatthew Knepley 
7725d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7726d71ae5a4SJacob Faibussowitsch {
77270fdc7489SMatthew Knepley   PetscFunctionBegin;
77284f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
77290fdc7489SMatthew Knepley   *ulabel = ul->label;
77303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77310fdc7489SMatthew Knepley }
77320fdc7489SMatthew Knepley 
7733d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7734d71ae5a4SJacob Faibussowitsch {
77350fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
77360fdc7489SMatthew Knepley 
77370fdc7489SMatthew Knepley   PetscFunctionBegin;
7738064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
77390fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
77409566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
77419566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
77420fdc7489SMatthew Knepley   }
77430fdc7489SMatthew Knepley   if (preserveOrder) {
77440fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
77450fdc7489SMatthew Knepley       const char *name;
77460fdc7489SMatthew Knepley       PetscBool   match;
77470fdc7489SMatthew Knepley 
77489566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
77499566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
775063a3b9bcSJacob 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]);
77510fdc7489SMatthew Knepley     }
77520fdc7489SMatthew Knepley   }
77533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77540fdc7489SMatthew Knepley }
77550fdc7489SMatthew Knepley 
7756d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7757d71ae5a4SJacob Faibussowitsch {
77580fdc7489SMatthew Knepley   PetscInt l;
77590fdc7489SMatthew Knepley 
77600fdc7489SMatthew Knepley   PetscFunctionBegin;
77610fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
77620fdc7489SMatthew Knepley     DMLabel  label;
77630fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
77640fdc7489SMatthew Knepley 
77650fdc7489SMatthew Knepley     if (lval) {
77669566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
77679566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
77689566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
77690fdc7489SMatthew Knepley     }
77700fdc7489SMatthew Knepley   }
77713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77720fdc7489SMatthew Knepley }
7773a8fb8f29SToby Isaac 
7774a8fb8f29SToby Isaac /*@
7775bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7776bb7acecfSBarry Smith 
777720f4b53cSBarry Smith   Not Collective
7778a8fb8f29SToby Isaac 
7779a8fb8f29SToby Isaac   Input Parameter:
7780bb7acecfSBarry Smith . dm - The `DM` object
7781a8fb8f29SToby Isaac 
7782a8fb8f29SToby Isaac   Output Parameter:
7783bb7acecfSBarry Smith . cdm - The coarse `DM`
7784a8fb8f29SToby Isaac 
7785a8fb8f29SToby Isaac   Level: intermediate
7786a8fb8f29SToby Isaac 
77871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7788a8fb8f29SToby Isaac @*/
7789d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7790d71ae5a4SJacob Faibussowitsch {
7791a8fb8f29SToby Isaac   PetscFunctionBegin;
7792a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77934f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7794a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
77953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7796a8fb8f29SToby Isaac }
7797a8fb8f29SToby Isaac 
7798a8fb8f29SToby Isaac /*@
7799bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7800a8fb8f29SToby Isaac 
7801a8fb8f29SToby Isaac   Input Parameters:
7802bb7acecfSBarry Smith + dm  - The `DM` object
7803bb7acecfSBarry Smith - cdm - The coarse `DM`
7804a8fb8f29SToby Isaac 
7805a8fb8f29SToby Isaac   Level: intermediate
7806a8fb8f29SToby Isaac 
7807bb7acecfSBarry Smith   Note:
7808bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7809bb7acecfSBarry Smith 
78101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7811a8fb8f29SToby Isaac @*/
7812d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7813d71ae5a4SJacob Faibussowitsch {
7814a8fb8f29SToby Isaac   PetscFunctionBegin;
7815a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7816a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
781789d734beSBarry Smith   if (dm == cdm) cdm = NULL;
78189566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
78199566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7820a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
78213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7822a8fb8f29SToby Isaac }
7823a8fb8f29SToby Isaac 
782488bdff64SToby Isaac /*@
7825bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
782688bdff64SToby Isaac 
782788bdff64SToby Isaac   Input Parameter:
7828bb7acecfSBarry Smith . dm - The `DM` object
782988bdff64SToby Isaac 
783088bdff64SToby Isaac   Output Parameter:
7831bb7acecfSBarry Smith . fdm - The fine `DM`
783288bdff64SToby Isaac 
783388bdff64SToby Isaac   Level: intermediate
783488bdff64SToby Isaac 
78351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
783688bdff64SToby Isaac @*/
7837d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7838d71ae5a4SJacob Faibussowitsch {
783988bdff64SToby Isaac   PetscFunctionBegin;
784088bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78414f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
784288bdff64SToby Isaac   *fdm = dm->fineMesh;
78433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
784488bdff64SToby Isaac }
784588bdff64SToby Isaac 
784688bdff64SToby Isaac /*@
7847bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
784888bdff64SToby Isaac 
784988bdff64SToby Isaac   Input Parameters:
7850bb7acecfSBarry Smith + dm  - The `DM` object
7851bb7acecfSBarry Smith - fdm - The fine `DM`
785288bdff64SToby Isaac 
7853bb7acecfSBarry Smith   Level: developer
785488bdff64SToby Isaac 
7855bb7acecfSBarry Smith   Note:
7856bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7857bb7acecfSBarry Smith 
78581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
785988bdff64SToby Isaac @*/
7860d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7861d71ae5a4SJacob Faibussowitsch {
786288bdff64SToby Isaac   PetscFunctionBegin;
786388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
786488bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
786589d734beSBarry Smith   if (dm == fdm) fdm = NULL;
78669566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
78679566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
786888bdff64SToby Isaac   dm->fineMesh = fdm;
78693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
787088bdff64SToby Isaac }
787188bdff64SToby Isaac 
7872a6ba4734SToby Isaac /*@C
7873bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7874a6ba4734SToby Isaac 
787520f4b53cSBarry Smith   Collective
7876783e2ec8SMatthew G. Knepley 
7877a6ba4734SToby Isaac   Input Parameters:
7878bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7879bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7880a6ba4734SToby Isaac . name     - The BC name
788145480ffeSMatthew G. Knepley . label    - The label defining constrained points
7882bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
788345480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7884a6ba4734SToby Isaac . field    - The field to constrain
788545480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7886a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7887a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
788856cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7889a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7890a6ba4734SToby Isaac 
789145480ffeSMatthew G. Knepley   Output Parameter:
789245480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
789345480ffeSMatthew G. Knepley 
7894a6ba4734SToby Isaac   Options Database Keys:
7895a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
7896a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7897a6ba4734SToby Isaac 
789820f4b53cSBarry Smith   Level: intermediate
789920f4b53cSBarry Smith 
7900bb7acecfSBarry Smith   Notes:
790137fdd005SBarry 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\:
790256cf3b9cSMatthew G. Knepley 
790320f4b53cSBarry Smith $ void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
790456cf3b9cSMatthew G. Knepley 
7905a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
790656cf3b9cSMatthew G. Knepley 
790720f4b53cSBarry Smith .vb
790820f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
790920f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
791020f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
791120f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
791220f4b53cSBarry Smith .ve
791356cf3b9cSMatthew G. Knepley + dim - the spatial dimension
791456cf3b9cSMatthew G. Knepley . Nf - the number of fields
791556cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
791656cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
791756cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
791856cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
791956cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
792056cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
792156cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
792256cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
792356cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
792456cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
792556cf3b9cSMatthew G. Knepley . t - current time
792656cf3b9cSMatthew G. Knepley . x - coordinates of the current point
792756cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
792856cf3b9cSMatthew G. Knepley . constants - constant parameters
792956cf3b9cSMatthew G. Knepley - bcval - output values at the current point
793056cf3b9cSMatthew G. Knepley 
79311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
7932a6ba4734SToby Isaac @*/
7933d71ae5a4SJacob 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)
7934d71ae5a4SJacob Faibussowitsch {
7935e5e52638SMatthew G. Knepley   PetscDS ds;
7936a6ba4734SToby Isaac 
7937a6ba4734SToby Isaac   PetscFunctionBegin;
7938a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7939783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
794045480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
794145480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
794245480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
794345480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
794401a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
79459566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7946799db056SMatthew G. Knepley   /* Complete label */
7947799db056SMatthew G. Knepley   if (label) {
7948799db056SMatthew G. Knepley     PetscObject  obj;
7949799db056SMatthew G. Knepley     PetscClassId id;
7950799db056SMatthew G. Knepley 
7951799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7952799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7953799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7954799db056SMatthew G. Knepley       DM plex;
7955799db056SMatthew G. Knepley 
7956799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7957799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7958799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7959799db056SMatthew G. Knepley     }
7960799db056SMatthew G. Knepley   }
79619566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
79623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7963a6ba4734SToby Isaac }
7964a6ba4734SToby Isaac 
796545480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7966d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
7967d71ae5a4SJacob Faibussowitsch {
7968e5e52638SMatthew G. Knepley   PetscDS     ds;
7969dff059c6SToby Isaac   DMBoundary *lastnext;
7970e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7971e6f8dbb6SToby Isaac 
7972e6f8dbb6SToby Isaac   PetscFunctionBegin;
79739566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7974e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
797547a1f5adSToby Isaac   if (dm->boundary) {
797647a1f5adSToby Isaac     DMBoundary next = dm->boundary;
797747a1f5adSToby Isaac 
797847a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
79793ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
798047a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
798147a1f5adSToby Isaac     while (next) {
798247a1f5adSToby Isaac       DMBoundary b = next;
798347a1f5adSToby Isaac 
798447a1f5adSToby Isaac       next = b->next;
79859566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7986a6ba4734SToby Isaac     }
798747a1f5adSToby Isaac     dm->boundary = NULL;
7988a6ba4734SToby Isaac   }
798947a1f5adSToby Isaac 
7990f4f49eeaSPierre Jolivet   lastnext = &dm->boundary;
7991e6f8dbb6SToby Isaac   while (dsbound) {
7992e6f8dbb6SToby Isaac     DMBoundary dmbound;
7993e6f8dbb6SToby Isaac 
79949566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7995e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
799645480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
799747a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7998dff059c6SToby Isaac     *lastnext = dmbound;
7999f4f49eeaSPierre Jolivet     lastnext  = &dmbound->next;
8000dff059c6SToby Isaac     dsbound   = dsbound->next;
8001a6ba4734SToby Isaac   }
80023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8003a6ba4734SToby Isaac }
8004a6ba4734SToby Isaac 
8005bb7acecfSBarry Smith /* TODO: missing manual page */
8006d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8007d71ae5a4SJacob Faibussowitsch {
8008b95f2879SToby Isaac   DMBoundary b;
8009a6ba4734SToby Isaac 
8010a6ba4734SToby Isaac   PetscFunctionBegin;
8011a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80124f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
8013a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
80149566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
8015b95f2879SToby Isaac   b = dm->boundary;
8016a6ba4734SToby Isaac   while (b && !(*isBd)) {
8017e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8018e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8019a6ba4734SToby Isaac     PetscInt   i;
8020a6ba4734SToby Isaac 
802145480ffeSMatthew G. Knepley     if (label) {
80229566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8023a6ba4734SToby Isaac     }
8024a6ba4734SToby Isaac     b = b->next;
8025a6ba4734SToby Isaac   }
80263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8027a6ba4734SToby Isaac }
80284d6f44ffSToby Isaac 
80294d6f44ffSToby Isaac /*@C
8030bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
8031a6e0b375SMatthew G. Knepley 
803220f4b53cSBarry Smith   Collective
80334d6f44ffSToby Isaac 
80344d6f44ffSToby Isaac   Input Parameters:
8035bb7acecfSBarry Smith + dm    - The `DM`
80360709b2feSToby Isaac . time  - The time
80374d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
80384d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
80394d6f44ffSToby Isaac - mode  - The insertion mode for values
80404d6f44ffSToby Isaac 
80414d6f44ffSToby Isaac   Output Parameter:
80424d6f44ffSToby Isaac . X - vector
80434d6f44ffSToby Isaac 
804420f4b53cSBarry Smith   Calling sequence of `funcs`:
80454d6f44ffSToby Isaac + dim  - The spatial dimension
80468ec8862eSJed Brown . time - The time at which to sample
80474d6f44ffSToby Isaac . x    - The coordinates
804877b739a6SMatthew Knepley . Nc   - The number of components
80494d6f44ffSToby Isaac . u    - The output field values
80504d6f44ffSToby Isaac - ctx  - optional user-defined function context
80514d6f44ffSToby Isaac 
80524d6f44ffSToby Isaac   Level: developer
80534d6f44ffSToby Isaac 
8054bb7acecfSBarry Smith   Developer Notes:
8055bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8056bb7acecfSBarry Smith 
8057bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8058bb7acecfSBarry Smith 
80591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
80604d6f44ffSToby Isaac @*/
8061a4e35b19SJacob 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)
8062d71ae5a4SJacob Faibussowitsch {
80634d6f44ffSToby Isaac   Vec localX;
80644d6f44ffSToby Isaac 
80654d6f44ffSToby Isaac   PetscFunctionBegin;
80664d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8067708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
80689566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8069f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
80709566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
80719566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
80729566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
80739566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
8074708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
80753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80764d6f44ffSToby Isaac }
80774d6f44ffSToby Isaac 
8078a6e0b375SMatthew G. Knepley /*@C
8079bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
8080a6e0b375SMatthew G. Knepley 
808120f4b53cSBarry Smith   Not Collective
8082a6e0b375SMatthew G. Knepley 
8083a6e0b375SMatthew G. Knepley   Input Parameters:
8084bb7acecfSBarry Smith + dm    - The `DM`
8085a6e0b375SMatthew G. Knepley . time  - The time
8086a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
8087a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8088a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
8089a6e0b375SMatthew G. Knepley 
8090a6e0b375SMatthew G. Knepley   Output Parameter:
8091a6e0b375SMatthew G. Knepley . localX - vector
8092a6e0b375SMatthew G. Knepley 
809320f4b53cSBarry Smith   Calling sequence of `funcs`:
8094a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8095a4e35b19SJacob Faibussowitsch . time - The current timestep
8096a6e0b375SMatthew G. Knepley . x    - The coordinates
809777b739a6SMatthew Knepley . Nc   - The number of components
8098a6e0b375SMatthew G. Knepley . u    - The output field values
8099a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8100a6e0b375SMatthew G. Knepley 
8101a6e0b375SMatthew G. Knepley   Level: developer
8102a6e0b375SMatthew G. Knepley 
8103bb7acecfSBarry Smith   Developer Notes:
8104bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8105bb7acecfSBarry Smith 
8106bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8107bb7acecfSBarry Smith 
81081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8109a6e0b375SMatthew G. Knepley @*/
8110a4e35b19SJacob 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)
8111d71ae5a4SJacob Faibussowitsch {
81124d6f44ffSToby Isaac   PetscFunctionBegin;
81134d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8114064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8115fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlocal, time, funcs, ctxs, mode, localX);
81163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81174d6f44ffSToby Isaac }
81184d6f44ffSToby Isaac 
8119a6e0b375SMatthew G. Knepley /*@C
8120bb7acecfSBarry 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.
8121a6e0b375SMatthew G. Knepley 
812220f4b53cSBarry Smith   Collective
8123a6e0b375SMatthew G. Knepley 
8124a6e0b375SMatthew G. Knepley   Input Parameters:
8125bb7acecfSBarry Smith + dm     - The `DM`
8126a6e0b375SMatthew G. Knepley . time   - The time
8127a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8128a4e35b19SJacob Faibussowitsch . ids    - The ids
8129a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8130a4e35b19SJacob Faibussowitsch . comps  - The components
8131bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8132a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8133bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8134a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8135a6e0b375SMatthew G. Knepley 
8136a6e0b375SMatthew G. Knepley   Output Parameter:
8137a6e0b375SMatthew G. Knepley . X - vector
8138a6e0b375SMatthew G. Knepley 
813920f4b53cSBarry Smith   Calling sequence of `funcs`:
8140a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8141a4e35b19SJacob Faibussowitsch . time - The current timestep
8142a6e0b375SMatthew G. Knepley . x    - The coordinates
814377b739a6SMatthew Knepley . Nc   - The number of components
8144a6e0b375SMatthew G. Knepley . u    - The output field values
8145a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8146a6e0b375SMatthew G. Knepley 
8147a6e0b375SMatthew G. Knepley   Level: developer
8148a6e0b375SMatthew G. Knepley 
8149bb7acecfSBarry Smith   Developer Notes:
8150bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8151bb7acecfSBarry Smith 
8152bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8153bb7acecfSBarry Smith 
81541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8155a6e0b375SMatthew G. Knepley @*/
8156a4e35b19SJacob 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)
8157d71ae5a4SJacob Faibussowitsch {
81582c53366bSMatthew G. Knepley   Vec localX;
81592c53366bSMatthew G. Knepley 
81602c53366bSMatthew G. Knepley   PetscFunctionBegin;
81612c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81629566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8163f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
81649566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
81659566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
81669566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
81679566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
81683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81692c53366bSMatthew G. Knepley }
81702c53366bSMatthew G. Knepley 
8171a6e0b375SMatthew G. Knepley /*@C
8172bb7acecfSBarry 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.
8173a6e0b375SMatthew G. Knepley 
817420f4b53cSBarry Smith   Not Collective
8175a6e0b375SMatthew G. Knepley 
8176a6e0b375SMatthew G. Knepley   Input Parameters:
8177bb7acecfSBarry Smith + dm     - The `DM`
8178a6e0b375SMatthew G. Knepley . time   - The time
8179bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8180a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8181a4e35b19SJacob Faibussowitsch . ids    - The ids
8182a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8183a4e35b19SJacob Faibussowitsch . comps  - The components
8184a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8185a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8186a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8187a6e0b375SMatthew G. Knepley 
8188a6e0b375SMatthew G. Knepley   Output Parameter:
8189a6e0b375SMatthew G. Knepley . localX - vector
8190a6e0b375SMatthew G. Knepley 
819120f4b53cSBarry Smith   Calling sequence of `funcs`:
8192a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8193a4e35b19SJacob Faibussowitsch . time - The current time
8194a6e0b375SMatthew G. Knepley . x    - The coordinates
819577b739a6SMatthew Knepley . Nc   - The number of components
8196a6e0b375SMatthew G. Knepley . u    - The output field values
8197a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8198a6e0b375SMatthew G. Knepley 
8199a6e0b375SMatthew G. Knepley   Level: developer
8200a6e0b375SMatthew G. Knepley 
8201bb7acecfSBarry Smith   Developer Notes:
8202bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8203bb7acecfSBarry Smith 
8204bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8205bb7acecfSBarry Smith 
82061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8207a6e0b375SMatthew G. Knepley @*/
8208a4e35b19SJacob 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)
8209d71ae5a4SJacob Faibussowitsch {
82104d6f44ffSToby Isaac   PetscFunctionBegin;
82114d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8212064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8213fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlabellocal, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);
82143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82154d6f44ffSToby Isaac }
82162716604bSToby Isaac 
8217a6e0b375SMatthew G. Knepley /*@C
8218bb7acecfSBarry 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.
8219a6e0b375SMatthew G. Knepley 
822020f4b53cSBarry Smith   Not Collective
8221a6e0b375SMatthew G. Knepley 
8222a6e0b375SMatthew G. Knepley   Input Parameters:
8223bb7acecfSBarry Smith + dm     - The `DM`
8224a6e0b375SMatthew G. Knepley . time   - The time
822520f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8226a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8227a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8228a6e0b375SMatthew G. Knepley 
8229a6e0b375SMatthew G. Knepley   Output Parameter:
8230a6e0b375SMatthew G. Knepley . localX - The output vector
8231a6e0b375SMatthew G. Knepley 
823220f4b53cSBarry Smith   Calling sequence of `funcs`:
8233a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8234a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8235a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8236a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8237a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8238a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8239a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8240a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8241a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8242a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8243a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8244a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8245a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8246a6e0b375SMatthew G. Knepley . t            - The current time
8247a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8248a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8249a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8250a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8251a6e0b375SMatthew G. Knepley 
8252bb7acecfSBarry Smith   Note:
8253bb7acecfSBarry 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.
8254bb7acecfSBarry 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
8255bb7acecfSBarry 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
8256a6e0b375SMatthew 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.
8257a6e0b375SMatthew G. Knepley 
8258a6e0b375SMatthew G. Knepley   Level: intermediate
8259a6e0b375SMatthew G. Knepley 
8260bb7acecfSBarry Smith   Developer Notes:
8261bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8262bb7acecfSBarry Smith 
8263bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8264bb7acecfSBarry Smith 
8265a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8266a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8267a6e0b375SMatthew G. Knepley @*/
8268a4e35b19SJacob 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)
8269d71ae5a4SJacob Faibussowitsch {
82708c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82718c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8272eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
82738c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8274fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlocal, time, localU, funcs, mode, localX);
82753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82768c6c5593SMatthew G. Knepley }
82778c6c5593SMatthew G. Knepley 
8278a6e0b375SMatthew G. Knepley /*@C
8279a6e0b375SMatthew 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.
8280a6e0b375SMatthew G. Knepley 
828120f4b53cSBarry Smith   Not Collective
8282a6e0b375SMatthew G. Knepley 
8283a6e0b375SMatthew G. Knepley   Input Parameters:
8284bb7acecfSBarry Smith + dm     - The `DM`
8285a6e0b375SMatthew G. Knepley . time   - The time
8286bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8287a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8288a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8289bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
829020f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8291a6e0b375SMatthew G. Knepley . localU - The input field vector
8292a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8293a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8294a6e0b375SMatthew G. Knepley 
8295a6e0b375SMatthew G. Knepley   Output Parameter:
8296a6e0b375SMatthew G. Knepley . localX - The output vector
8297a6e0b375SMatthew G. Knepley 
829820f4b53cSBarry Smith   Calling sequence of `funcs`:
8299a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8300a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8301a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8302a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8303a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8304a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8305a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8306a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8307a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8308a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8309a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8310a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8311a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8312a6e0b375SMatthew G. Knepley . t            - The current time
8313a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8314a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8315a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8316a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8317a6e0b375SMatthew G. Knepley 
8318bb7acecfSBarry Smith   Note:
8319bb7acecfSBarry 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.
8320bb7acecfSBarry 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
8321bb7acecfSBarry 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
8322a6e0b375SMatthew 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.
8323a6e0b375SMatthew G. Knepley 
8324a6e0b375SMatthew G. Knepley   Level: intermediate
8325a6e0b375SMatthew G. Knepley 
8326bb7acecfSBarry Smith   Developer Notes:
8327bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8328bb7acecfSBarry Smith 
8329bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8330bb7acecfSBarry Smith 
83311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8332a6e0b375SMatthew G. Knepley @*/
8333a4e35b19SJacob 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)
8334d71ae5a4SJacob Faibussowitsch {
83358c6c5593SMatthew G. Knepley   PetscFunctionBegin;
83368c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8337064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8338064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8339fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
83403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83418c6c5593SMatthew G. Knepley }
83428c6c5593SMatthew G. Knepley 
83432716604bSToby Isaac /*@C
8344d29d7c6eSMatthew 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.
8345d29d7c6eSMatthew G. Knepley 
834620f4b53cSBarry Smith   Not Collective
8347d29d7c6eSMatthew G. Knepley 
8348d29d7c6eSMatthew G. Knepley   Input Parameters:
8349bb7acecfSBarry Smith + dm     - The `DM`
8350d29d7c6eSMatthew G. Knepley . time   - The time
8351bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8352d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8353d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8354bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
835520f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8356d29d7c6eSMatthew G. Knepley . U      - The input field vector
8357d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8358d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8359d29d7c6eSMatthew G. Knepley 
8360d29d7c6eSMatthew G. Knepley   Output Parameter:
8361d29d7c6eSMatthew G. Knepley . X - The output vector
8362d29d7c6eSMatthew G. Knepley 
836320f4b53cSBarry Smith   Calling sequence of `funcs`:
8364d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8365d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8366d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8367d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8368d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8369d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
8370d29d7c6eSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8371d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8372d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8373d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8374d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8375d29d7c6eSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8376d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8377d29d7c6eSMatthew G. Knepley . t            - The current time
8378d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8379d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8380d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8381d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8382d29d7c6eSMatthew G. Knepley 
8383bb7acecfSBarry Smith   Note:
8384bb7acecfSBarry 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.
8385bb7acecfSBarry 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
8386bb7acecfSBarry 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
8387d29d7c6eSMatthew 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.
8388d29d7c6eSMatthew G. Knepley 
8389d29d7c6eSMatthew G. Knepley   Level: intermediate
8390d29d7c6eSMatthew G. Knepley 
8391bb7acecfSBarry Smith   Developer Notes:
8392bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8393bb7acecfSBarry Smith 
8394bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8395bb7acecfSBarry Smith 
83961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8397d29d7c6eSMatthew G. Knepley @*/
8398a4e35b19SJacob 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)
8399d71ae5a4SJacob Faibussowitsch {
8400d29d7c6eSMatthew G. Knepley   DM  dmIn;
8401d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8402d29d7c6eSMatthew G. Knepley 
8403d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8404d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8405d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8406d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8407d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8408f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
840972fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
841072fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8411d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8412d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8413d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8414d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8415d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
84163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8417d29d7c6eSMatthew G. Knepley }
8418d29d7c6eSMatthew G. Knepley 
8419d29d7c6eSMatthew G. Knepley /*@C
8420ece3a9fcSMatthew 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.
8421ece3a9fcSMatthew G. Knepley 
842220f4b53cSBarry Smith   Not Collective
8423ece3a9fcSMatthew G. Knepley 
8424ece3a9fcSMatthew G. Knepley   Input Parameters:
8425bb7acecfSBarry Smith + dm     - The `DM`
8426ece3a9fcSMatthew G. Knepley . time   - The time
8427bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8428ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8429ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8430bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
843120f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8432ece3a9fcSMatthew G. Knepley . localU - The input field vector
8433ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8434ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8435ece3a9fcSMatthew G. Knepley 
8436ece3a9fcSMatthew G. Knepley   Output Parameter:
8437ece3a9fcSMatthew G. Knepley . localX - The output vector
8438ece3a9fcSMatthew G. Knepley 
843920f4b53cSBarry Smith   Calling sequence of `funcs`:
8440ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8441ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8442ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8443ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8444ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8445ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
8446ece3a9fcSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8447ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8448ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8449ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8450ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8451ece3a9fcSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8452ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8453ece3a9fcSMatthew G. Knepley . t            - The current time
8454ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8455ece3a9fcSMatthew G. Knepley . n            - The face normal
8456ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8457ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8458ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8459ece3a9fcSMatthew G. Knepley 
8460ece3a9fcSMatthew G. Knepley   Note:
8461bb7acecfSBarry 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.
8462bb7acecfSBarry 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
8463bb7acecfSBarry 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
8464ece3a9fcSMatthew 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.
8465ece3a9fcSMatthew G. Knepley 
8466ece3a9fcSMatthew G. Knepley   Level: intermediate
8467ece3a9fcSMatthew G. Knepley 
8468bb7acecfSBarry Smith   Developer Notes:
8469bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8470bb7acecfSBarry Smith 
8471bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8472bb7acecfSBarry Smith 
84731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8474ece3a9fcSMatthew G. Knepley @*/
8475a4e35b19SJacob 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)
8476d71ae5a4SJacob Faibussowitsch {
8477ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8478ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8479064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8480064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8481fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectbdfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
84823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8483ece3a9fcSMatthew G. Knepley }
8484ece3a9fcSMatthew G. Knepley 
8485ece3a9fcSMatthew G. Knepley /*@C
84862716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
84872716604bSToby Isaac 
848820f4b53cSBarry Smith   Collective
8489bb7acecfSBarry Smith 
84902716604bSToby Isaac   Input Parameters:
8491bb7acecfSBarry Smith + dm    - The `DM`
84920709b2feSToby Isaac . time  - The time
84932716604bSToby Isaac . funcs - The functions to evaluate for each field component
84942716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8495574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84962716604bSToby Isaac 
84972716604bSToby Isaac   Output Parameter:
84982716604bSToby Isaac . diff - The diff ||u - u_h||_2
84992716604bSToby Isaac 
85002716604bSToby Isaac   Level: developer
85012716604bSToby Isaac 
8502bb7acecfSBarry Smith   Developer Notes:
8503bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8504bb7acecfSBarry Smith 
8505bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8506bb7acecfSBarry Smith 
85071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
85082716604bSToby Isaac @*/
8509d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8510d71ae5a4SJacob Faibussowitsch {
85112716604bSToby Isaac   PetscFunctionBegin;
85122716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8513b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8514fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2diff, time, funcs, ctxs, X, diff);
85153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
85162716604bSToby Isaac }
8517b698f381SToby Isaac 
8518b698f381SToby Isaac /*@C
8519b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8520b698f381SToby Isaac 
852120f4b53cSBarry Smith   Collective
8522d083f849SBarry Smith 
8523b698f381SToby Isaac   Input Parameters:
8524bb7acecfSBarry Smith + dm    - The `DM`
8525a4e35b19SJacob Faibussowitsch . time  - The time
8526b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8527b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8528574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8529b698f381SToby Isaac - n     - The vector to project along
8530b698f381SToby Isaac 
8531b698f381SToby Isaac   Output Parameter:
8532b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8533b698f381SToby Isaac 
8534b698f381SToby Isaac   Level: developer
8535b698f381SToby Isaac 
8536bb7acecfSBarry Smith   Developer Notes:
8537bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8538bb7acecfSBarry Smith 
8539bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8540bb7acecfSBarry Smith 
85411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8542b698f381SToby Isaac @*/
8543d71ae5a4SJacob 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)
8544d71ae5a4SJacob Faibussowitsch {
8545b698f381SToby Isaac   PetscFunctionBegin;
8546b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8547b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8548fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2gradientdiff, time, funcs, ctxs, X, n, diff);
85493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8550b698f381SToby Isaac }
8551b698f381SToby Isaac 
85522a16baeaSToby Isaac /*@C
85532a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
85542a16baeaSToby Isaac 
855520f4b53cSBarry Smith   Collective
8556d083f849SBarry Smith 
85572a16baeaSToby Isaac   Input Parameters:
8558bb7acecfSBarry Smith + dm    - The `DM`
85592a16baeaSToby Isaac . time  - The time
85602a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
85612a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8562574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
85632a16baeaSToby Isaac 
85642a16baeaSToby Isaac   Output Parameter:
85652a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
85662a16baeaSToby Isaac 
85672a16baeaSToby Isaac   Level: developer
85682a16baeaSToby Isaac 
8569bb7acecfSBarry Smith   Developer Notes:
8570bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8571bb7acecfSBarry Smith 
8572bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8573bb7acecfSBarry Smith 
857442747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
85752a16baeaSToby Isaac @*/
8576d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8577d71ae5a4SJacob Faibussowitsch {
85782a16baeaSToby Isaac   PetscFunctionBegin;
85792a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85802a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8581fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2fielddiff, time, funcs, ctxs, X, diff);
85823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
85832a16baeaSToby Isaac }
85842a16baeaSToby Isaac 
8585df0b854cSToby Isaac /*@C
8586bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8587502a2867SDave May 
8588502a2867SDave May   Not Collective
8589502a2867SDave May 
8590502a2867SDave May   Input Parameter:
8591bb7acecfSBarry Smith . dm - The `DM`
8592502a2867SDave May 
85930a19bb7dSprj-   Output Parameters:
85940a19bb7dSprj- + nranks - the number of neighbours
85950a19bb7dSprj- - ranks  - the neighbors ranks
8596502a2867SDave May 
85979bdbcad8SBarry Smith   Level: beginner
85989bdbcad8SBarry Smith 
8599bb7acecfSBarry Smith   Note:
8600bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8601502a2867SDave May 
86021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8603502a2867SDave May @*/
8604d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8605d71ae5a4SJacob Faibussowitsch {
8606502a2867SDave May   PetscFunctionBegin;
8607502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8608fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, getneighbors, nranks, ranks);
86093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8610502a2867SDave May }
8611502a2867SDave May 
8612531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8613531c7667SBarry Smith 
8614531c7667SBarry Smith /*
8615531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
86162b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8617531c7667SBarry Smith */
861866976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8619d71ae5a4SJacob Faibussowitsch {
8620531c7667SBarry Smith   PetscFunctionBegin;
8621531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8622531c7667SBarry Smith     Vec x1local;
8623531c7667SBarry Smith     DM  dm;
86249566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
86257a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
86269566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
86279566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
86289566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8629531c7667SBarry Smith     x1 = x1local;
8630531c7667SBarry Smith   }
86319566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8632531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8633531c7667SBarry Smith     DM dm;
86349566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
86359566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8636531c7667SBarry Smith   }
86373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8638531c7667SBarry Smith }
8639531c7667SBarry Smith 
8640531c7667SBarry Smith /*@
8641bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8642531c7667SBarry Smith 
8643a4e35b19SJacob Faibussowitsch   Input Parameters:
8644a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8645a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8646531c7667SBarry Smith 
86479bdbcad8SBarry Smith   Level: advanced
86489bdbcad8SBarry Smith 
864960225df5SJacob Faibussowitsch   Developer Notes:
8650bb7acecfSBarry Smith   this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8651531c7667SBarry Smith 
86521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8653531c7667SBarry Smith @*/
8654d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8655d71ae5a4SJacob Faibussowitsch {
8656531c7667SBarry Smith   PetscFunctionBegin;
8657531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
86583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8659531c7667SBarry Smith }
86608320bc6fSPatrick Sanan 
86618320bc6fSPatrick Sanan /*@
8662bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
86638320bc6fSPatrick Sanan 
86648320bc6fSPatrick Sanan   Collective
86658320bc6fSPatrick Sanan 
86668320bc6fSPatrick Sanan   Input Parameters:
8667bb7acecfSBarry Smith + dm1 - the first `DM`
8668bb7acecfSBarry Smith - dm2 - the second `DM`
86698320bc6fSPatrick Sanan 
86708320bc6fSPatrick Sanan   Output Parameters:
8671bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8672bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
86738320bc6fSPatrick Sanan 
867420f4b53cSBarry Smith   Level: advanced
867520f4b53cSBarry Smith 
86768320bc6fSPatrick Sanan   Notes:
8677bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
86783d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
86798320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8680bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
86813d862458SPatrick Sanan   decomposition, but hold different data.
86828320bc6fSPatrick Sanan 
86838320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8684bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
86858320bc6fSPatrick Sanan 
8686bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
86878320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
86888320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8689bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
86908320bc6fSPatrick Sanan 
8691bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8692bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
86938320bc6fSPatrick Sanan .vb
86948320bc6fSPatrick Sanan   ...
86959566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
86968320bc6fSPatrick Sanan   if (set && compatible)  {
86979566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
86989566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
86999566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
87008320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
87018320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
87028320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
87038320bc6fSPatrick Sanan       }
87048320bc6fSPatrick Sanan     }
87059566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
87069566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
87078320bc6fSPatrick Sanan   } else {
87088320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
87098320bc6fSPatrick Sanan   }
87108320bc6fSPatrick Sanan   ...
87118320bc6fSPatrick Sanan .ve
87128320bc6fSPatrick Sanan 
8713bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
87148320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
87158320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
87168320bc6fSPatrick Sanan   always check the "set" output parameter.
87178320bc6fSPatrick Sanan 
8718bb7acecfSBarry Smith   A `DM` is always compatible with itself.
87198320bc6fSPatrick Sanan 
8720bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
87218320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
87228320bc6fSPatrick Sanan   incompatible.
87238320bc6fSPatrick Sanan 
87248320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8725bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
87268320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
87278320bc6fSPatrick Sanan 
872860225df5SJacob Faibussowitsch   Developer Notes:
8729bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
87303d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8731a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8732bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
87338320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
87343d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8735bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
87368320bc6fSPatrick Sanan 
87371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
87388320bc6fSPatrick Sanan @*/
8739d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8740d71ae5a4SJacob Faibussowitsch {
87418320bc6fSPatrick Sanan   PetscMPIInt compareResult;
87428320bc6fSPatrick Sanan   DMType      type, type2;
87438320bc6fSPatrick Sanan   PetscBool   sameType;
87448320bc6fSPatrick Sanan 
87458320bc6fSPatrick Sanan   PetscFunctionBegin;
8746a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
87478320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
87488320bc6fSPatrick Sanan 
87498320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8750a5bc1bf3SBarry Smith   if (dm1 == dm2) {
87518320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
87528320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
87533ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
87548320bc6fSPatrick Sanan   }
87558320bc6fSPatrick Sanan 
87568320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
87578320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
87588320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
87598320bc6fSPatrick Sanan      determined by the implementation-specific logic */
87609566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
87618320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
87628320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
87638320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
87643ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
87658320bc6fSPatrick Sanan   }
87668320bc6fSPatrick Sanan 
87678320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8768a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8769dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
87703ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
87718320bc6fSPatrick Sanan   }
87728320bc6fSPatrick Sanan 
8773a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
87748320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
87759566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
87769566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
87779566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
87788320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8779dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
87808320bc6fSPatrick Sanan   } else {
87818320bc6fSPatrick Sanan     *set = PETSC_FALSE;
87828320bc6fSPatrick Sanan   }
87833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
87848320bc6fSPatrick Sanan }
8785c0f0dcc3SMatthew G. Knepley 
8786c0f0dcc3SMatthew G. Knepley /*@C
8787bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8788c0f0dcc3SMatthew G. Knepley 
878920f4b53cSBarry Smith   Logically Collective
8790c0f0dcc3SMatthew G. Knepley 
8791c0f0dcc3SMatthew G. Knepley   Input Parameters:
879260225df5SJacob Faibussowitsch + dm             - the `DM`
8793c0f0dcc3SMatthew G. Knepley . f              - the monitor function
879420f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
879520f4b53cSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`)
8796c0f0dcc3SMatthew G. Knepley 
879720f4b53cSBarry Smith   Options Database Key:
879860225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8799c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8800c0f0dcc3SMatthew G. Knepley 
88019bdbcad8SBarry Smith   Level: intermediate
88029bdbcad8SBarry Smith 
8803bb7acecfSBarry Smith   Note:
8804c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8805bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8806c0f0dcc3SMatthew G. Knepley   order in which they were set.
8807c0f0dcc3SMatthew G. Knepley 
880860225df5SJacob Faibussowitsch   Fortran Notes:
8809bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8810bb7acecfSBarry Smith 
881160225df5SJacob Faibussowitsch   Developer Notes:
8812bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8813c0f0dcc3SMatthew G. Knepley 
88141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8815c0f0dcc3SMatthew G. Knepley @*/
8816d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8817d71ae5a4SJacob Faibussowitsch {
8818c0f0dcc3SMatthew G. Knepley   PetscInt m;
8819c0f0dcc3SMatthew G. Knepley 
8820c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8821c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8822c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8823c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8824c0f0dcc3SMatthew G. Knepley 
88259566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
88263ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8827c0f0dcc3SMatthew G. Knepley   }
88287a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8829c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8830c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8831c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
88323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8833c0f0dcc3SMatthew G. Knepley }
8834c0f0dcc3SMatthew G. Knepley 
8835c0f0dcc3SMatthew G. Knepley /*@
8836bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8837c0f0dcc3SMatthew G. Knepley 
883820f4b53cSBarry Smith   Logically Collective
8839c0f0dcc3SMatthew G. Knepley 
8840c0f0dcc3SMatthew G. Knepley   Input Parameter:
8841c0f0dcc3SMatthew G. Knepley . dm - the DM
8842c0f0dcc3SMatthew G. Knepley 
8843c0f0dcc3SMatthew G. Knepley   Options Database Key:
8844c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8845bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8846c0f0dcc3SMatthew G. Knepley   set via the options database
8847c0f0dcc3SMatthew G. Knepley 
88489bdbcad8SBarry Smith   Level: intermediate
88499bdbcad8SBarry Smith 
8850bb7acecfSBarry Smith   Note:
8851bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8852c0f0dcc3SMatthew G. Knepley 
88531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8854c0f0dcc3SMatthew G. Knepley @*/
8855d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8856d71ae5a4SJacob Faibussowitsch {
8857c0f0dcc3SMatthew G. Knepley   PetscInt m;
8858c0f0dcc3SMatthew G. Knepley 
8859c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8860c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8861c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
88629566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8863c0f0dcc3SMatthew G. Knepley   }
8864c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
88653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8866c0f0dcc3SMatthew G. Knepley }
8867c0f0dcc3SMatthew G. Knepley 
8868c0f0dcc3SMatthew G. Knepley /*@C
8869c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8870c0f0dcc3SMatthew G. Knepley 
887120f4b53cSBarry Smith   Collective
8872c0f0dcc3SMatthew G. Knepley 
8873c0f0dcc3SMatthew G. Knepley   Input Parameters:
8874bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
8875c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
8876c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
8877c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
8878c0f0dcc3SMatthew G. Knepley . monitor      - the monitor function
8879bb7acecfSBarry 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
8880c0f0dcc3SMatthew G. Knepley 
8881c0f0dcc3SMatthew G. Knepley   Output Parameter:
8882c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8883c0f0dcc3SMatthew G. Knepley 
8884c0f0dcc3SMatthew G. Knepley   Level: developer
8885c0f0dcc3SMatthew G. Knepley 
88861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8887db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
888860225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
8889db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8890c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8891db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8892bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8893c0f0dcc3SMatthew G. Knepley @*/
8894d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8895d71ae5a4SJacob Faibussowitsch {
8896c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8897c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8898c0f0dcc3SMatthew G. Knepley 
8899c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8900c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8902c0f0dcc3SMatthew G. Knepley   if (*flg) {
8903c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8904c0f0dcc3SMatthew G. Knepley 
89059566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
8906cd791dc2SBarry Smith     PetscCall(PetscOptionsRestoreViewer(&viewer));
89079566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
89089566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8909c0f0dcc3SMatthew G. Knepley   }
89103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8911c0f0dcc3SMatthew G. Knepley }
8912c0f0dcc3SMatthew G. Knepley 
8913c0f0dcc3SMatthew G. Knepley /*@
8914c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
8915c0f0dcc3SMatthew G. Knepley 
891620f4b53cSBarry Smith   Collective
8917c0f0dcc3SMatthew G. Knepley 
89182fe279fdSBarry Smith   Input Parameter:
8919bb7acecfSBarry Smith . dm - The `DM`
8920c0f0dcc3SMatthew G. Knepley 
8921c0f0dcc3SMatthew G. Knepley   Level: developer
8922c0f0dcc3SMatthew G. Knepley 
8923a4e35b19SJacob Faibussowitsch   Developer Notes:
8924a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
8925a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
8926a4e35b19SJacob Faibussowitsch   concept of discretization.
8927bb7acecfSBarry Smith 
89281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8929c0f0dcc3SMatthew G. Knepley @*/
8930d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8931d71ae5a4SJacob Faibussowitsch {
8932c0f0dcc3SMatthew G. Knepley   PetscInt m;
8933c0f0dcc3SMatthew G. Knepley 
8934c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
89353ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
8936c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
893748a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
89383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8939c0f0dcc3SMatthew G. Knepley }
89402e4af2aeSMatthew G. Knepley 
89412e4af2aeSMatthew G. Knepley /*@
8942bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
89432e4af2aeSMatthew G. Knepley 
894420f4b53cSBarry Smith   Collective
89452e4af2aeSMatthew G. Knepley 
89462e4af2aeSMatthew G. Knepley   Input Parameters:
8947bb7acecfSBarry Smith + dm  - The `DM`
89486b867d5aSJose E. Roman - sol - The solution vector
89492e4af2aeSMatthew G. Knepley 
89506b867d5aSJose E. Roman   Input/Output Parameter:
895120f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
89526b867d5aSJose E. Roman            contains the error in each field
89536b867d5aSJose E. Roman 
89546b867d5aSJose E. Roman   Output Parameter:
895520f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
895620f4b53cSBarry Smith 
895720f4b53cSBarry Smith   Level: developer
89582e4af2aeSMatthew G. Knepley 
8959bb7acecfSBarry Smith   Note:
8960bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
89612e4af2aeSMatthew G. Knepley 
89621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
89632e4af2aeSMatthew G. Knepley @*/
8964d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
8965d71ae5a4SJacob Faibussowitsch {
89662e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
89672e4af2aeSMatthew G. Knepley   void    **ctxs;
89682e4af2aeSMatthew G. Knepley   PetscReal time;
89692e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
89702e4af2aeSMatthew G. Knepley 
89712e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
89729566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
89739566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
89749566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
89752e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
89762e4af2aeSMatthew G. Knepley     PetscDS         ds;
89772e4af2aeSMatthew G. Knepley     DMLabel         label;
89782e4af2aeSMatthew G. Knepley     IS              fieldIS;
89792e4af2aeSMatthew G. Knepley     const PetscInt *fields;
89802e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
89812e4af2aeSMatthew G. Knepley 
898207218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
89839566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
89849566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
89852e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
89862e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
89879566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
89882e4af2aeSMatthew G. Knepley     }
89899566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
89902e4af2aeSMatthew G. Knepley   }
8991ad540459SPierre 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);
89929566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
89939566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
89942e4af2aeSMatthew G. Knepley   if (errorVec) {
89952e4af2aeSMatthew G. Knepley     DM             edm;
89962e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
89972e4af2aeSMatthew G. Knepley     PetscBool      simplex;
89982e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
89992e4af2aeSMatthew G. Knepley 
90009566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
90019566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
90029566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
90039566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
90042e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
90059566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
90062e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
90072e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
90082e4af2aeSMatthew G. Knepley       PetscQuadrature q;
90092e4af2aeSMatthew G. Knepley       const char     *name;
90102e4af2aeSMatthew G. Knepley 
90119566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
90129566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
90139566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
90149566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
90159566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
90169566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
90179566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
90189566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
90192e4af2aeSMatthew G. Knepley     }
90209566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
90212e4af2aeSMatthew G. Knepley 
90229566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
90239566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
90249566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
90259566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
90262e4af2aeSMatthew G. Knepley   }
90279566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
90283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90292e4af2aeSMatthew G. Knepley }
90309a2a23afSMatthew G. Knepley 
90319a2a23afSMatthew G. Knepley /*@
9032bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
90339a2a23afSMatthew G. Knepley 
903420f4b53cSBarry Smith   Not Collective
90359a2a23afSMatthew G. Knepley 
90369a2a23afSMatthew G. Knepley   Input Parameter:
9037bb7acecfSBarry Smith . dm - The `DM`
90389a2a23afSMatthew G. Knepley 
90399a2a23afSMatthew G. Knepley   Output Parameter:
9040a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
90419a2a23afSMatthew G. Knepley 
90429a2a23afSMatthew G. Knepley   Level: advanced
90439a2a23afSMatthew G. Knepley 
9044e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
90459a2a23afSMatthew G. Knepley @*/
9046d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
9047d71ae5a4SJacob Faibussowitsch {
90489a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90499a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90509566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
90513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90529a2a23afSMatthew G. Knepley }
90539a2a23afSMatthew G. Knepley 
90549a2a23afSMatthew G. Knepley /*@
9055ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
90569a2a23afSMatthew G. Knepley 
905720f4b53cSBarry Smith   Not Collective
90589a2a23afSMatthew G. Knepley 
90599a2a23afSMatthew G. Knepley   Input Parameters:
9060bb7acecfSBarry Smith + dm    - The `DM`
9061bb7acecfSBarry Smith . label - The `DMLabel`
9062ac17215fSMatthew G. Knepley . value - The label value indicating the region
9063ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
90649a2a23afSMatthew G. Knepley 
90659a2a23afSMatthew G. Knepley   Output Parameter:
9066bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
90679a2a23afSMatthew G. Knepley 
90689bdbcad8SBarry Smith   Level: advanced
90699bdbcad8SBarry Smith 
9070bb7acecfSBarry Smith   Note:
9071bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
907204c51a94SMatthew G. Knepley 
9073e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
90749a2a23afSMatthew G. Knepley @*/
9075d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
9076d71ae5a4SJacob Faibussowitsch {
9077ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
907804c51a94SMatthew G. Knepley   PetscBool       has;
90799a2a23afSMatthew G. Knepley 
90809a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90819a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90829a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
90839a2a23afSMatthew G. Knepley   key.label = label;
90849a2a23afSMatthew G. Knepley   key.value = value;
9085ac17215fSMatthew G. Knepley   key.part  = part;
90869566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
90879566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
90889566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
90893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90909a2a23afSMatthew G. Knepley }
90919a2a23afSMatthew G. Knepley 
90929a2a23afSMatthew G. Knepley /*@
9093bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
90949a2a23afSMatthew G. Knepley 
909520f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
90969a2a23afSMatthew G. Knepley 
90979a2a23afSMatthew G. Knepley   Input Parameters:
9098bb7acecfSBarry Smith + dm    - The `DM`
9099bb7acecfSBarry Smith . label - The `DMLabel`
91009a2a23afSMatthew G. Knepley . value - The label value indicating the region
9101ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
9102bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
91039a2a23afSMatthew G. Knepley 
91049a2a23afSMatthew G. Knepley   Level: advanced
91059a2a23afSMatthew G. Knepley 
9106e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
91079a2a23afSMatthew G. Knepley @*/
9108d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9109d71ae5a4SJacob Faibussowitsch {
91109a2a23afSMatthew G. Knepley   Vec             old;
91119a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
91129a2a23afSMatthew G. Knepley 
91139a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91149a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91159a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
91169a2a23afSMatthew G. Knepley   key.label = label;
91179a2a23afSMatthew G. Knepley   key.value = value;
9118ac17215fSMatthew G. Knepley   key.part  = part;
91199566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
91209566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
91219566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
91229566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
9123d705d0eaSStefano Zampini   PetscCall(VecDestroy(&old));
91243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91259a2a23afSMatthew G. Knepley }
91269a2a23afSMatthew G. Knepley 
91279a2a23afSMatthew G. Knepley /*@C
9128bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
91299a2a23afSMatthew G. Knepley 
913020f4b53cSBarry Smith   Not Collective
91319a2a23afSMatthew G. Knepley 
91329a2a23afSMatthew G. Knepley   Input Parameter:
9133bb7acecfSBarry Smith . dm - The `DM`
91349a2a23afSMatthew G. Knepley 
91359a2a23afSMatthew G. Knepley   Output Parameters:
9136bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9137bb7acecfSBarry Smith . values - The label values for each `Vec`
9138bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
91399a2a23afSMatthew G. Knepley 
91409bdbcad8SBarry Smith   Level: advanced
91419bdbcad8SBarry Smith 
9142bb7acecfSBarry Smith   Note:
9143bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
91449a2a23afSMatthew G. Knepley 
9145e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
91469a2a23afSMatthew G. Knepley @*/
9147d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9148d71ae5a4SJacob Faibussowitsch {
91499a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
91509a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
91519a2a23afSMatthew G. Knepley 
91529a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91539a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91544f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
91554f572ea9SToby Isaac   PetscAssertPointer(values, 3);
91564f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
91579566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
91589566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
91599566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
91609371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
91619371c9d4SSatish Balay     labels[i] = keys[i].label;
91629371c9d4SSatish Balay     values[i] = keys[i].value;
91639371c9d4SSatish Balay     parts[i]  = keys[i].part;
91649371c9d4SSatish Balay   }
91659566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
91663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91679a2a23afSMatthew G. Knepley }
91689a2a23afSMatthew G. Knepley 
91699a2a23afSMatthew G. Knepley /*@
9170bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
91719a2a23afSMatthew G. Knepley 
917220f4b53cSBarry Smith   Not Collective
91739a2a23afSMatthew G. Knepley 
91749a2a23afSMatthew G. Knepley   Input Parameter:
9175bb7acecfSBarry Smith . dm - The `DM`
91769a2a23afSMatthew G. Knepley 
91779a2a23afSMatthew G. Knepley   Output Parameter:
9178bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
91799a2a23afSMatthew G. Knepley 
91809a2a23afSMatthew G. Knepley   Level: advanced
91819a2a23afSMatthew G. Knepley 
9182bb7acecfSBarry Smith   Note:
9183bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9184bb7acecfSBarry Smith 
9185e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
91869a2a23afSMatthew G. Knepley @*/
9187d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9188d71ae5a4SJacob Faibussowitsch {
91899a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91909a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9191d705d0eaSStefano Zampini   PetscValidHeaderSpecific(dmNew, DM_CLASSID, 2);
9192d705d0eaSStefano Zampini   if (dm == dmNew) PetscFunctionReturn(PETSC_SUCCESS);
9193e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(dmNew));
9194e4d5475eSStefano Zampini 
9195e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
91969566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
9197d705d0eaSStefano Zampini   {
9198d705d0eaSStefano Zampini     Vec     *auxData;
9199d705d0eaSStefano Zampini     PetscInt n, i, off = 0;
9200d705d0eaSStefano Zampini 
9201d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetSize(dmNew->auxData, &n));
9202d705d0eaSStefano Zampini     PetscCall(PetscMalloc1(n, &auxData));
9203d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetVals(dmNew->auxData, &off, auxData));
9204d705d0eaSStefano Zampini     for (i = 0; i < n; ++i) PetscCall(PetscObjectReference((PetscObject)auxData[i]));
9205d705d0eaSStefano Zampini     PetscCall(PetscFree(auxData));
9206e4d5475eSStefano Zampini   }
9207e4d5475eSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
9208e4d5475eSStefano Zampini }
9209e4d5475eSStefano Zampini 
9210e4d5475eSStefano Zampini /*@
9211e4d5475eSStefano Zampini   DMClearAuxiliaryVec - Destroys the auxiliary vector information and creates a new empty one
9212e4d5475eSStefano Zampini 
9213e4d5475eSStefano Zampini   Not Collective
9214e4d5475eSStefano Zampini 
9215e4d5475eSStefano Zampini   Input Parameter:
9216e4d5475eSStefano Zampini . dm - The `DM`
9217e4d5475eSStefano Zampini 
9218e4d5475eSStefano Zampini   Level: advanced
9219e4d5475eSStefano Zampini 
9220e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMCopyAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
9221e4d5475eSStefano Zampini @*/
9222e4d5475eSStefano Zampini PetscErrorCode DMClearAuxiliaryVec(DM dm)
9223e4d5475eSStefano Zampini {
9224e4d5475eSStefano Zampini   Vec     *auxData;
9225e4d5475eSStefano Zampini   PetscInt n, i, off = 0;
9226e4d5475eSStefano Zampini 
9227e4d5475eSStefano Zampini   PetscFunctionBegin;
9228e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetSize(dm->auxData, &n));
9229d705d0eaSStefano Zampini   PetscCall(PetscMalloc1(n, &auxData));
9230e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetVals(dm->auxData, &off, auxData));
9231d705d0eaSStefano Zampini   for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
9232d705d0eaSStefano Zampini   PetscCall(PetscFree(auxData));
9233e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dm->auxData));
9234e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxCreate(&dm->auxData));
92353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92369a2a23afSMatthew G. Knepley }
9237b5a892a1SMatthew G. Knepley 
9238b5a892a1SMatthew G. Knepley /*@C
9239bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9240b5a892a1SMatthew G. Knepley 
924120f4b53cSBarry Smith   Not Collective
9242b5a892a1SMatthew G. Knepley 
9243b5a892a1SMatthew G. Knepley   Input Parameters:
9244bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9245b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9246b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9247b5a892a1SMatthew G. Knepley 
9248b5a892a1SMatthew G. Knepley   Output Parameters:
9249bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9250b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9251b5a892a1SMatthew G. Knepley 
9252b5a892a1SMatthew G. Knepley   Level: advanced
9253b5a892a1SMatthew G. Knepley 
9254bb7acecfSBarry Smith   Note:
9255bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9256bb7acecfSBarry Smith 
925785036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9258bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9259bb7acecfSBarry Smith 
9260bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9261bb7acecfSBarry Smith 
92621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9263b5a892a1SMatthew G. Knepley @*/
9264d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9265d71ae5a4SJacob Faibussowitsch {
9266b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
926785036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9268b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9269b5a892a1SMatthew G. Knepley 
9270b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92719371c9d4SSatish Balay   if (!nO) {
92729371c9d4SSatish Balay     *ornt  = 0;
92739371c9d4SSatish Balay     *found = PETSC_TRUE;
92743ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
92759371c9d4SSatish Balay   }
9276b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
927785036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
9278b5a892a1SMatthew G. Knepley 
92799371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
92809371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
92819371c9d4SSatish Balay     if (c == cS) {
92829371c9d4SSatish Balay       *ornt = o;
92839371c9d4SSatish Balay       break;
92849371c9d4SSatish Balay     }
9285b5a892a1SMatthew G. Knepley   }
9286b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
92873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9288b5a892a1SMatthew G. Knepley }
9289b5a892a1SMatthew G. Knepley 
9290b5a892a1SMatthew G. Knepley /*@C
9291bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9292b5a892a1SMatthew G. Knepley 
929320f4b53cSBarry Smith   Not Collective
9294b5a892a1SMatthew G. Knepley 
9295b5a892a1SMatthew G. Knepley   Input Parameters:
9296bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9297b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9298b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9299b5a892a1SMatthew G. Knepley 
93002fe279fdSBarry Smith   Output Parameter:
9301bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9302b5a892a1SMatthew G. Knepley 
9303b5a892a1SMatthew G. Knepley   Level: advanced
9304b5a892a1SMatthew G. Knepley 
9305bb7acecfSBarry Smith   Note:
9306bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9307bb7acecfSBarry Smith 
930860225df5SJacob Faibussowitsch   Developer Notes:
9309bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9310bb7acecfSBarry Smith 
93111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9312b5a892a1SMatthew G. Knepley @*/
9313d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9314d71ae5a4SJacob Faibussowitsch {
9315b5a892a1SMatthew G. Knepley   PetscBool found;
9316b5a892a1SMatthew G. Knepley 
9317b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93189566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
93197a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
93203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9321b5a892a1SMatthew G. Knepley }
9322b5a892a1SMatthew G. Knepley 
9323b5a892a1SMatthew G. Knepley /*@C
9324bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9325b5a892a1SMatthew G. Knepley 
932620f4b53cSBarry Smith   Not Collective
9327b5a892a1SMatthew G. Knepley 
9328b5a892a1SMatthew G. Knepley   Input Parameters:
9329bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9330b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9331b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9332b5a892a1SMatthew G. Knepley 
9333b5a892a1SMatthew G. Knepley   Output Parameters:
9334bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9335b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9336b5a892a1SMatthew G. Knepley 
9337b5a892a1SMatthew G. Knepley   Level: advanced
9338b5a892a1SMatthew G. Knepley 
9339bb7acecfSBarry Smith   Note:
9340bb7acecfSBarry Smith   An arrangement is a vertex order
9341bb7acecfSBarry Smith 
934285036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9343bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9344bb7acecfSBarry Smith 
9345bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9346bb7acecfSBarry Smith 
934785036b15SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangement()`
9348b5a892a1SMatthew G. Knepley @*/
9349d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9350d71ae5a4SJacob Faibussowitsch {
9351b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
935285036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9353b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9354b5a892a1SMatthew G. Knepley 
9355b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93569371c9d4SSatish Balay   if (!nO) {
93579371c9d4SSatish Balay     *ornt  = 0;
93589371c9d4SSatish Balay     *found = PETSC_TRUE;
93593ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
93609371c9d4SSatish Balay   }
9361b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
936285036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangement(ct, o);
9363b5a892a1SMatthew G. Knepley 
93649371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
93659371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
93669371c9d4SSatish Balay     if (c == cS) {
93679371c9d4SSatish Balay       *ornt = o;
93689371c9d4SSatish Balay       break;
93699371c9d4SSatish Balay     }
9370b5a892a1SMatthew G. Knepley   }
9371b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
93723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9373b5a892a1SMatthew G. Knepley }
9374b5a892a1SMatthew G. Knepley 
9375b5a892a1SMatthew G. Knepley /*@C
9376bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9377b5a892a1SMatthew G. Knepley 
937820f4b53cSBarry Smith   Not Collective
9379b5a892a1SMatthew G. Knepley 
9380b5a892a1SMatthew G. Knepley   Input Parameters:
9381bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9382b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9383b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9384b5a892a1SMatthew G. Knepley 
93852fe279fdSBarry Smith   Output Parameter:
9386bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9387b5a892a1SMatthew G. Knepley 
9388b5a892a1SMatthew G. Knepley   Level: advanced
9389b5a892a1SMatthew G. Knepley 
9390bb7acecfSBarry Smith   Note:
9391bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9392bb7acecfSBarry Smith 
939360225df5SJacob Faibussowitsch   Developer Notes:
9394bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9395bb7acecfSBarry Smith 
93961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9397b5a892a1SMatthew G. Knepley @*/
9398d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9399d71ae5a4SJacob Faibussowitsch {
9400b5a892a1SMatthew G. Knepley   PetscBool found;
9401b5a892a1SMatthew G. Knepley 
9402b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94039566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
94047a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
94053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9406b5a892a1SMatthew G. Knepley }
9407012bc364SMatthew G. Knepley 
9408012bc364SMatthew G. Knepley /*@C
9409012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9410012bc364SMatthew G. Knepley 
941120f4b53cSBarry Smith   Not Collective
9412012bc364SMatthew G. Knepley 
9413012bc364SMatthew G. Knepley   Input Parameters:
9414bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9415012bc364SMatthew G. Knepley - point - Coordinates of the point
9416012bc364SMatthew G. Knepley 
94172fe279fdSBarry Smith   Output Parameter:
9418012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9419012bc364SMatthew G. Knepley 
9420012bc364SMatthew G. Knepley   Level: advanced
9421012bc364SMatthew G. Knepley 
94221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9423012bc364SMatthew G. Knepley @*/
9424d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9425d71ae5a4SJacob Faibussowitsch {
9426012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9427012bc364SMatthew G. Knepley   PetscInt  d;
9428012bc364SMatthew G. Knepley 
9429012bc364SMatthew G. Knepley   PetscFunctionBegin;
9430012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9431012bc364SMatthew G. Knepley   switch (ct) {
9432012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9433012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9434012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
94359371c9d4SSatish Balay       if (point[d] < -1.0) {
94369371c9d4SSatish Balay         *inside = PETSC_FALSE;
94379371c9d4SSatish Balay         break;
94389371c9d4SSatish Balay       }
9439012bc364SMatthew G. Knepley       sum += point[d];
9440012bc364SMatthew G. Knepley     }
94419371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
94429371c9d4SSatish Balay       *inside = PETSC_FALSE;
94439371c9d4SSatish Balay       break;
94449371c9d4SSatish Balay     }
9445012bc364SMatthew G. Knepley     break;
9446012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9447012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9448012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
94499371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
94509371c9d4SSatish Balay         *inside = PETSC_FALSE;
9451012bc364SMatthew G. Knepley         break;
94529371c9d4SSatish Balay       }
94539371c9d4SSatish Balay     break;
9454d71ae5a4SJacob Faibussowitsch   default:
9455d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9456012bc364SMatthew G. Knepley   }
94573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9458012bc364SMatthew G. Knepley }
9459adc21957SMatthew G. Knepley 
9460adc21957SMatthew G. Knepley /*@
9461adc21957SMatthew G. Knepley   DMReorderSectionSetDefault - Set flag indicating whether the local section should be reordered by default
9462adc21957SMatthew G. Knepley 
9463adc21957SMatthew G. Knepley   Logically collective
9464adc21957SMatthew G. Knepley 
9465adc21957SMatthew G. Knepley   Input Parameters:
9466adc21957SMatthew G. Knepley + dm      - The DM
9467adc21957SMatthew G. Knepley - reorder - Flag for reordering
9468adc21957SMatthew G. Knepley 
9469adc21957SMatthew G. Knepley   Level: intermediate
9470adc21957SMatthew G. Knepley 
9471adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetDefault()`
9472adc21957SMatthew G. Knepley @*/
9473adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetDefault(DM dm, DMReorderDefaultFlag reorder)
9474adc21957SMatthew G. Knepley {
9475adc21957SMatthew G. Knepley   PetscFunctionBegin;
9476adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9477adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetDefault_C", (DM, DMReorderDefaultFlag), (dm, reorder));
9478adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9479adc21957SMatthew G. Knepley }
9480adc21957SMatthew G. Knepley 
9481adc21957SMatthew G. Knepley /*@
9482adc21957SMatthew G. Knepley   DMReorderSectionGetDefault - Get flag indicating whether the local section should be reordered by default
9483adc21957SMatthew G. Knepley 
9484adc21957SMatthew G. Knepley   Not collective
9485adc21957SMatthew G. Knepley 
9486adc21957SMatthew G. Knepley   Input Parameter:
9487adc21957SMatthew G. Knepley . dm - The DM
9488adc21957SMatthew G. Knepley 
9489adc21957SMatthew G. Knepley   Output Parameter:
9490adc21957SMatthew G. Knepley . reorder - Flag for reordering
9491adc21957SMatthew G. Knepley 
9492adc21957SMatthew G. Knepley   Level: intermediate
9493adc21957SMatthew G. Knepley 
9494adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`
9495adc21957SMatthew G. Knepley @*/
9496adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetDefault(DM dm, DMReorderDefaultFlag *reorder)
9497adc21957SMatthew G. Knepley {
9498adc21957SMatthew G. Knepley   PetscFunctionBegin;
9499adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9500adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9501adc21957SMatthew G. Knepley   *reorder = DM_REORDER_DEFAULT_NOTSET;
9502adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetDefault_C", (DM, DMReorderDefaultFlag *), (dm, reorder));
9503adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9504adc21957SMatthew G. Knepley }
9505adc21957SMatthew G. Knepley 
9506adc21957SMatthew G. Knepley /*@C
9507adc21957SMatthew G. Knepley   DMReorderSectionSetType - Set the type of local section reordering
9508adc21957SMatthew G. Knepley 
9509adc21957SMatthew G. Knepley   Logically collective
9510adc21957SMatthew G. Knepley 
9511adc21957SMatthew G. Knepley   Input Parameters:
9512adc21957SMatthew G. Knepley + dm      - The DM
9513adc21957SMatthew G. Knepley - reorder - The reordering method
9514adc21957SMatthew G. Knepley 
9515adc21957SMatthew G. Knepley   Level: intermediate
9516adc21957SMatthew G. Knepley 
9517adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetType()`, `DMReorderSectionSetDefault()`
9518adc21957SMatthew G. Knepley @*/
9519adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetType(DM dm, MatOrderingType reorder)
9520adc21957SMatthew G. Knepley {
9521adc21957SMatthew G. Knepley   PetscFunctionBegin;
9522adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9523adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetType_C", (DM, MatOrderingType), (dm, reorder));
9524adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9525adc21957SMatthew G. Knepley }
9526adc21957SMatthew G. Knepley 
9527adc21957SMatthew G. Knepley /*@C
9528adc21957SMatthew G. Knepley   DMReorderSectionGetType - Get the reordering type for the local section
9529adc21957SMatthew G. Knepley 
9530adc21957SMatthew G. Knepley   Not collective
9531adc21957SMatthew G. Knepley 
9532adc21957SMatthew G. Knepley   Input Parameter:
9533adc21957SMatthew G. Knepley . dm - The DM
9534adc21957SMatthew G. Knepley 
9535adc21957SMatthew G. Knepley   Output Parameter:
9536adc21957SMatthew G. Knepley . reorder - The reordering method
9537adc21957SMatthew G. Knepley 
9538adc21957SMatthew G. Knepley   Level: intermediate
9539adc21957SMatthew G. Knepley 
9540adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`, `DMReorderSectionGetDefault()`
9541adc21957SMatthew G. Knepley @*/
9542adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetType(DM dm, MatOrderingType *reorder)
9543adc21957SMatthew G. Knepley {
9544adc21957SMatthew G. Knepley   PetscFunctionBegin;
9545adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9546adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9547adc21957SMatthew G. Knepley   *reorder = NULL;
9548adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetType_C", (DM, MatOrderingType *), (dm, reorder));
9549adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9550adc21957SMatthew G. Knepley }
9551