xref: /petsc/src/dm/interface/dm.c (revision a36b0f0f346e718409f8ecb84f382e8cd0966bc7)
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 
15732e2eb9SMatthew G Knepley PetscClassId DM_CLASSID;
16d67d17b1SMatthew G. Knepley PetscClassId DMLABEL_CLASSID;
1742d0c57aSJames Wright 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_View, DM_AdaptInterpolator, DM_ProjectFunction;
1867a56275SMatthew G Knepley 
19ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[]          = {"NONE", "GHOSTED", "MIRROR", "PERIODIC", "TWIST", "DMBoundaryType", "DM_BOUNDARY_", NULL};
2078e542f3SJed Brown const char *const DMBoundaryConditionTypes[] = {"INVALID", "ESSENTIAL", "NATURAL", "INVALID", "LOWER_BOUND", "ESSENTIAL_FIELD", "NATURAL_FIELD", "INVALID", "UPPER_BOUND", "ESSENTIAL_BD_FIELD", "NATURAL_RIEMANN", "DMBoundaryConditionType",
2178e542f3SJed Brown                                                 "DM_BC_",  NULL};
220e762ea3SJed Brown const char *const DMBlockingTypes[]          = {"TOPOLOGICAL_POINT", "FIELD_NODE", "DMBlockingType", "DM_BLOCKING_", NULL};
23476787b7SMatthew G. Knepley const char *const DMPolytopeTypes[] =
24476787b7SMatthew 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",
25476787b7SMatthew G. Knepley    "unknown", "unknown_cell", "unknown_face",   "invalid",  "DMPolytopeType", "DM_POLYTOPE_", NULL};
262cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace", "keep", "fail", "DMCopyLabelsMode", "DM_COPY_LABELS_", NULL};
2760c22052SBarry Smith 
28a4121054SBarry Smith /*@
29bb7acecfSBarry Smith   DMCreate - Creates an empty `DM` object. `DM`s are the abstract objects in PETSc that mediate between meshes and discretizations and the
300b4b7b1cSBarry Smith   algebraic solvers, time integrators, and optimization algorithms in PETSc.
31a4121054SBarry Smith 
32d083f849SBarry Smith   Collective
33a4121054SBarry Smith 
34a4121054SBarry Smith   Input Parameter:
35bb7acecfSBarry Smith . comm - The communicator for the `DM` object
36a4121054SBarry Smith 
37a4121054SBarry Smith   Output Parameter:
38bb7acecfSBarry Smith . dm - The `DM` object
39a4121054SBarry Smith 
40a4121054SBarry Smith   Level: beginner
41a4121054SBarry Smith 
42bb7acecfSBarry Smith   Notes:
43bb7acecfSBarry Smith   See `DMType` for a brief summary of available `DM`.
44bb7acecfSBarry Smith 
45bb7acecfSBarry Smith   The type must then be set with `DMSetType()`. If you never call `DMSetType()` it will generate an
460b4b7b1cSBarry Smith   error when you try to use the `dm`.
470b4b7b1cSBarry Smith 
480b4b7b1cSBarry Smith   `DM` is an orphan initialism or orphan acronym, the letters have no meaning and never did.
49bb7acecfSBarry Smith 
501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMType`, `DMDACreate()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK`
51a4121054SBarry Smith @*/
52d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreate(MPI_Comm comm, DM *dm)
53d71ae5a4SJacob Faibussowitsch {
54a4121054SBarry Smith   DM      v;
55e5e52638SMatthew G. Knepley   PetscDS ds;
56a4121054SBarry Smith 
57a4121054SBarry Smith   PetscFunctionBegin;
584f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
59377f809aSBarry Smith 
609566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
619566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
6262e5d2d2SJDBetteridge   ((PetscObject)v)->non_cyclic_references = &DMCountNonCyclicReferences;
6349be4549SMatthew G. Knepley   v->setupcalled                          = PETSC_FALSE;
6449be4549SMatthew G. Knepley   v->setfromoptionscalled                 = PETSC_FALSE;
650298fd71SBarry Smith   v->ltogmap                              = NULL;
66a4ea9b21SRichard Tran Mills   v->bind_below                           = 0;
671411c6eeSJed Brown   v->bs                                   = 1;
68171400e9SBarry Smith   v->coloringtype                         = IS_COLORING_GLOBAL;
699566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
709566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
71c58f1c22SToby Isaac   v->labels                    = NULL;
7234aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7334aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
74c58f1c22SToby Isaac   v->depthLabel                = NULL;
75ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
761bb6d2a8SBarry Smith   v->localSection              = NULL;
771bb6d2a8SBarry Smith   v->globalSection             = NULL;
783b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
793b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
8079769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
816858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
826858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
836858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8496173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
85435a35e8SMatthew G Knepley   {
86435a35e8SMatthew G Knepley     PetscInt i;
87435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
880298fd71SBarry Smith       v->nullspaceConstructors[i]     = NULL;
89f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
90435a35e8SMatthew G Knepley     }
91435a35e8SMatthew G Knepley   }
929566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
9307218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(v, NULL, NULL, ds, NULL));
949566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
959566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
9614f150ffSMatthew G. Knepley   v->dmBC              = NULL;
97a8fb8f29SToby Isaac   v->coarseMesh        = NULL;
98f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
99cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
1009566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v, VECSTANDARD));
1019566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v, MATAIJ));
1024a7a4c06SLawrence Mitchell 
1031411c6eeSJed Brown   *dm = v;
1043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
105a4121054SBarry Smith }
106a4121054SBarry Smith 
10738221697SMatthew G. Knepley /*@
108bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
10938221697SMatthew G. Knepley 
110d083f849SBarry Smith   Collective
11138221697SMatthew G. Knepley 
11238221697SMatthew G. Knepley   Input Parameter:
113bb7acecfSBarry Smith . dm - The original `DM` object
11438221697SMatthew G. Knepley 
11538221697SMatthew G. Knepley   Output Parameter:
116bb7acecfSBarry Smith . newdm - The new `DM` object
11738221697SMatthew G. Knepley 
11838221697SMatthew G. Knepley   Level: beginner
11938221697SMatthew G. Knepley 
1201cb8cacdSPatrick Sanan   Notes:
121bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
122bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
123bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1241bb6d2a8SBarry Smith 
125bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
12689706ed2SPatrick Sanan 
127bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
128bb7acecfSBarry Smith 
12960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMConvert()`
13038221697SMatthew G. Knepley @*/
131d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm)
132d71ae5a4SJacob Faibussowitsch {
13338221697SMatthew G. Knepley   PetscSF              sf;
13438221697SMatthew G. Knepley   Vec                  coords;
13538221697SMatthew G. Knepley   void                *ctx;
136ec196627SMatthew G. Knepley   MatOrderingType      otype;
137ec196627SMatthew G. Knepley   DMReorderDefaultFlag flg;
1386858538eSMatthew G. Knepley   PetscInt             dim, cdim, i;
13938221697SMatthew G. Knepley 
14038221697SMatthew G. Knepley   PetscFunctionBegin;
14138221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1424f572ea9SToby Isaac   PetscAssertPointer(newdm, 2);
1439566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1449566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
145ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
146ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
147c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
148fc214432SJed Brown   (*newdm)->prealloc_skip = dm->prealloc_skip;
1499566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1509566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1519566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1529566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1539566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1549566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
155dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1563f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1579566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1589566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1599566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1609566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
161ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionGetDefault(dm, &flg));
162ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionSetDefault(*newdm, flg));
163ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionGetType(dm, &otype));
164ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionSetType(*newdm, otype));
1656858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1666858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
167be4c1c3eSMatthew G. Knepley       DM           ncdm;
168be4c1c3eSMatthew G. Knepley       PetscSection cs;
1695a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
170be4c1c3eSMatthew G. Knepley 
1716858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1729566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
173462c564dSBarry Smith       PetscCallMPI(MPIU_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1745a0206caSToby Isaac       if (pEndMax >= 0) {
1756858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1766858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1779566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1785e50ca95SJames Wright         if (dm->coordinates[i].dm->periodic.setup) {
1795e50ca95SJames Wright           ncdm->periodic.setup = dm->coordinates[i].dm->periodic.setup;
1805e50ca95SJames Wright           PetscCall(ncdm->periodic.setup(ncdm));
1815e50ca95SJames Wright         }
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 
2195d83a8b1SBarry Smith /*@
2205d83a8b1SBarry Smith   DMSetVecType - Sets the type of vector to be 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 
2495d83a8b1SBarry Smith /*@
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 
31073ff1848SBarry Smith   Notes:
311bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
312d9805387SMatthew G. Knepley 
313bb7acecfSBarry Smith   This is NOT the same as `DMCreateGlobalVector()` since it does not change the view methods or perform other customization, but merely sets the `DM` member.
314bb7acecfSBarry Smith 
3151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3165f1ad066SMatthew G Knepley @*/
317d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
318d71ae5a4SJacob Faibussowitsch {
3195f1ad066SMatthew G Knepley   PetscFunctionBegin;
3205f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
321d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3229566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3245f1ad066SMatthew G Knepley }
3255f1ad066SMatthew G Knepley 
326cc4c1da9SBarry Smith /*@
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 
351cc4c1da9SBarry Smith /*@
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 
378cc4c1da9SBarry Smith /*@
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 
407cc4c1da9SBarry Smith /*@
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 
44673ff1848SBarry Smith   Developer Note:
447bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4488f1509bcSBarry Smith 
4491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
450c688c046SMatthew G Knepley @*/
451d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
452d71ae5a4SJacob Faibussowitsch {
453c688c046SMatthew G Knepley   PetscFunctionBegin;
454c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4554f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
4569566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
4573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
458c688c046SMatthew G Knepley }
459c688c046SMatthew G Knepley 
460c688c046SMatthew G Knepley /*@
461bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
462c688c046SMatthew G Knepley 
46320f4b53cSBarry Smith   Not Collective
464c688c046SMatthew G Knepley 
465c688c046SMatthew G Knepley   Input Parameters:
46620f4b53cSBarry Smith + A  - The `Mat`
46720f4b53cSBarry Smith - dm - The `DM`
468c688c046SMatthew G Knepley 
469bb7acecfSBarry Smith   Level: developer
470c688c046SMatthew G Knepley 
471bb7acecfSBarry Smith   Note:
472bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
473bb7acecfSBarry Smith 
47473ff1848SBarry Smith   Developer Note:
475bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
476bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4778f1509bcSBarry Smith 
4781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
479c688c046SMatthew G Knepley @*/
480d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
481d71ae5a4SJacob Faibussowitsch {
482c688c046SMatthew G Knepley   PetscFunctionBegin;
483c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4848865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4859566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
4863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
487c688c046SMatthew G Knepley }
488c688c046SMatthew G Knepley 
489cc4c1da9SBarry Smith /*@
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 
516cc4c1da9SBarry Smith /*@
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 
543cc4c1da9SBarry Smith /*@
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 
5571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
55831697293SDave May @*/
559d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
560d71ae5a4SJacob Faibussowitsch {
56131697293SDave May   PetscFunctionBegin;
56231697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5639566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
5643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
56531697293SDave May }
56631697293SDave May 
56762e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
568d71ae5a4SJacob Faibussowitsch {
5696eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
57088bdff64SToby Isaac 
57188bdff64SToby Isaac   PetscFunctionBegin;
572aab5bcd8SJed Brown   *ncrefct = 0;
57388bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
57488bdff64SToby Isaac     refct--;
57588bdff64SToby Isaac     if (recurseCoarse) {
57688bdff64SToby Isaac       PetscInt coarseCount;
57788bdff64SToby Isaac 
57862e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
57988bdff64SToby Isaac       refct += coarseCount;
58088bdff64SToby Isaac     }
58188bdff64SToby Isaac   }
58288bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
58388bdff64SToby Isaac     refct--;
58488bdff64SToby Isaac     if (recurseFine) {
58588bdff64SToby Isaac       PetscInt fineCount;
58688bdff64SToby Isaac 
58762e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
58888bdff64SToby Isaac       refct += fineCount;
58988bdff64SToby Isaac     }
59088bdff64SToby Isaac   }
59188bdff64SToby Isaac   *ncrefct = refct;
5923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59388bdff64SToby Isaac }
59488bdff64SToby Isaac 
59562e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */
59662e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct)
59762e5d2d2SJDBetteridge {
59862e5d2d2SJDBetteridge   PetscFunctionBegin;
59962e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct));
6003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
60162e5d2d2SJDBetteridge }
60262e5d2d2SJDBetteridge 
603d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
604d71ae5a4SJacob Faibussowitsch {
6055d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
606354557abSToby Isaac 
607354557abSToby Isaac   PetscFunctionBegin;
608354557abSToby Isaac   /* destroy the labels */
609354557abSToby Isaac   while (next) {
610354557abSToby Isaac     DMLabelLink tmp = next->next;
611354557abSToby Isaac 
6125d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
613ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
6149566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
6159566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
616354557abSToby Isaac     next = tmp;
617354557abSToby Isaac   }
6185d80c0bfSVaclav Hapla   dm->labels = NULL;
6193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
620354557abSToby Isaac }
621354557abSToby Isaac 
62266976f2fSJacob Faibussowitsch static PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
623d71ae5a4SJacob Faibussowitsch {
6246858538eSMatthew G. Knepley   PetscFunctionBegin;
6256858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6266858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6276858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6286858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6296858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6316858538eSMatthew G. Knepley }
6326858538eSMatthew G. Knepley 
6330764c050SBarry Smith /*@
634bb7acecfSBarry Smith   DMDestroy - Destroys a `DM`.
63547c6ae99SBarry Smith 
63620f4b53cSBarry Smith   Collective
63747c6ae99SBarry Smith 
63847c6ae99SBarry Smith   Input Parameter:
639bb7acecfSBarry Smith . dm - the `DM` object to destroy
64047c6ae99SBarry Smith 
64147c6ae99SBarry Smith   Level: developer
64247c6ae99SBarry Smith 
6431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
64447c6ae99SBarry Smith @*/
645d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
646d71ae5a4SJacob Faibussowitsch {
6476eb26441SStefano Zampini   PetscInt cnt;
64847c6ae99SBarry Smith 
64947c6ae99SBarry Smith   PetscFunctionBegin;
6503ba16761SJacob Faibussowitsch   if (!*dm) PetscFunctionReturn(PETSC_SUCCESS);
651f4f49eeaSPierre Jolivet   PetscValidHeaderSpecific(*dm, DM_CLASSID, 1);
65287e657c6SBarry Smith 
65388bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
65462e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
655f4f49eeaSPierre Jolivet   --((PetscObject)*dm)->refct;
6569371c9d4SSatish Balay   if (--cnt > 0) {
6579371c9d4SSatish Balay     *dm = NULL;
6583ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6599371c9d4SSatish Balay   }
660f4f49eeaSPierre Jolivet   if (((PetscObject)*dm)->refct < 0) PetscFunctionReturn(PETSC_SUCCESS);
661f4f49eeaSPierre Jolivet   ((PetscObject)*dm)->refct = 0;
6626eb26441SStefano Zampini 
6639566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6649566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
665974ca4ecSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(*dm));
666974ca4ecSStefano Zampini   PetscCall(DMClearNamedLocalVectors(*dm));
6672348bcf4SPeter Brune 
668b17ce1afSJed Brown   /* Destroy the list of hooks */
669c833c3b5SJed Brown   {
670c833c3b5SJed Brown     DMCoarsenHookLink link, next;
671b17ce1afSJed Brown     for (link = (*dm)->coarsenhook; link; link = next) {
672b17ce1afSJed Brown       next = link->next;
6739566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
674b17ce1afSJed Brown     }
6750298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
676c833c3b5SJed Brown   }
677c833c3b5SJed Brown   {
678c833c3b5SJed Brown     DMRefineHookLink link, next;
679c833c3b5SJed Brown     for (link = (*dm)->refinehook; link; link = next) {
680c833c3b5SJed Brown       next = link->next;
6819566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
682c833c3b5SJed Brown     }
6830298fd71SBarry Smith     (*dm)->refinehook = NULL;
684c833c3b5SJed Brown   }
685be081cd6SPeter Brune   {
686be081cd6SPeter Brune     DMSubDomainHookLink link, next;
687be081cd6SPeter Brune     for (link = (*dm)->subdomainhook; link; link = next) {
688be081cd6SPeter Brune       next = link->next;
6899566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
690be081cd6SPeter Brune     }
6910298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
692be081cd6SPeter Brune   }
693baf369e7SPeter Brune   {
694baf369e7SPeter Brune     DMGlobalToLocalHookLink link, next;
695baf369e7SPeter Brune     for (link = (*dm)->gtolhook; link; link = next) {
696baf369e7SPeter Brune       next = link->next;
6979566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
698baf369e7SPeter Brune     }
6990298fd71SBarry Smith     (*dm)->gtolhook = NULL;
700baf369e7SPeter Brune   }
701d4d07f1eSToby Isaac   {
702d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link, next;
703d4d07f1eSToby Isaac     for (link = (*dm)->ltoghook; link; link = next) {
704d4d07f1eSToby Isaac       next = link->next;
7059566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
706d4d07f1eSToby Isaac     }
707d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
708d4d07f1eSToby Isaac   }
709aa1993deSMatthew G Knepley   /* Destroy the work arrays */
710aa1993deSMatthew G Knepley   {
711aa1993deSMatthew G Knepley     DMWorkLink link, next;
712835f2295SStefano Zampini     PetscCheck(!(*dm)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out %p %p", (void *)(*dm)->workout, (*dm)->workout->mem);
713aa1993deSMatthew G Knepley     for (link = (*dm)->workin; link; link = next) {
714aa1993deSMatthew G Knepley       next = link->next;
7159566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
7169566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
717aa1993deSMatthew G Knepley     }
7180298fd71SBarry Smith     (*dm)->workin = NULL;
719aa1993deSMatthew G Knepley   }
720c58f1c22SToby Isaac   /* destroy the labels */
7219566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
722f4cdcedcSVaclav Hapla   /* destroy the fields */
7239566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
724f4cdcedcSVaclav Hapla   /* destroy the boundaries */
725e6f8dbb6SToby Isaac   {
726e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
727e6f8dbb6SToby Isaac     while (next) {
728e6f8dbb6SToby Isaac       DMBoundary b = next;
729e6f8dbb6SToby Isaac 
730e6f8dbb6SToby Isaac       next = b->next;
7319566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
732e6f8dbb6SToby Isaac     }
733e6f8dbb6SToby Isaac   }
734b17ce1afSJed Brown 
7359566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7369566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7379566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
73852536dc3SBarry Smith 
73948a46eb9SPierre Jolivet   if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7409566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7419566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7429566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7439566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
74488ed4aceSMatthew G Knepley 
7459566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7469566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
747adc21957SMatthew G. Knepley   PetscCall(PetscFree((*dm)->reorderSectionType));
7489566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7499566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7509566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7519566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7529566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
75348a46eb9SPierre Jolivet   if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7549566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
755e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(*dm));
7569566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
75748a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7586eb26441SStefano Zampini 
7599566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
76048a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7619566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7624fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7639566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7649566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7656858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7666858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7679566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7689566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7699566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
770ddedc8f6SJames Wright   for (PetscInt i = 0; i < (*dm)->periodic.num_affines; i++) {
771ddedc8f6SJames Wright     PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local[i]));
772ddedc8f6SJames Wright     PetscCall(VecDestroy(&(*dm)->periodic.affine[i]));
773ddedc8f6SJames Wright   }
774ddedc8f6SJames Wright   if ((*dm)->periodic.num_affines > 0) PetscCall(PetscFree2((*dm)->periodic.affine_to_local, (*dm)->periodic.affine));
7756636e97aSMatthew G Knepley 
7769566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7779566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
778e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7799566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
780732e2eb9SMatthew G Knepley 
781213acdd3SPierre Jolivet   PetscTryTypeMethod(*dm, destroy);
7829566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
783d2b2dc1eSMatthew G. Knepley   PetscCall(DMCeedDestroy(&(*dm)->dmceed));
784f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7859566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7869566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
787f918ec44SMatthew G. Knepley #endif
788435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7899566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
7903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79147c6ae99SBarry Smith }
79247c6ae99SBarry Smith 
793d7bf68aeSBarry Smith /*@
794bb7acecfSBarry Smith   DMSetUp - sets up the data structures inside a `DM` object
795d7bf68aeSBarry Smith 
79620f4b53cSBarry Smith   Collective
797d7bf68aeSBarry Smith 
798d7bf68aeSBarry Smith   Input Parameter:
799bb7acecfSBarry Smith . dm - the `DM` object to setup
800d7bf68aeSBarry Smith 
801bb7acecfSBarry Smith   Level: intermediate
802d7bf68aeSBarry Smith 
803bb7acecfSBarry Smith   Note:
804bb7acecfSBarry Smith   This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
805bb7acecfSBarry Smith 
8061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
807d7bf68aeSBarry Smith @*/
808d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
809d71ae5a4SJacob Faibussowitsch {
810d7bf68aeSBarry Smith   PetscFunctionBegin;
811171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8123ba16761SJacob Faibussowitsch   if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
813dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8148387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
8153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
816d7bf68aeSBarry Smith }
817d7bf68aeSBarry Smith 
818d7bf68aeSBarry Smith /*@
819bb7acecfSBarry Smith   DMSetFromOptions - sets parameters in a `DM` from the options database
820d7bf68aeSBarry Smith 
82120f4b53cSBarry Smith   Collective
822d7bf68aeSBarry Smith 
823d7bf68aeSBarry Smith   Input Parameter:
824bb7acecfSBarry Smith . dm - the `DM` object to set options for
825d7bf68aeSBarry Smith 
82620f4b53cSBarry Smith   Options Database Keys:
827bb7acecfSBarry Smith + -dm_preallocate_only                               - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
828bb7acecfSBarry Smith . -dm_vec_type <type>                                - type of vector to create inside `DM`
829bb7acecfSBarry Smith . -dm_mat_type <type>                                - type of matrix to create inside `DM`
830a4ea9b21SRichard Tran Mills . -dm_is_coloring_type                               - <global or local>
83120f4b53cSBarry 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`
83222d6dc08SStefano Zampini . -dm_plex_option_phases <ph0_, ph1_, ...>           - List of prefixes for option processing phases
83320f4b53cSBarry Smith . -dm_plex_filename <str>                            - File containing a mesh
8349318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>                   - File containing a mesh boundary
835cd7e8a5eSksagiyam . -dm_plex_name <str>                                - Name of the mesh in the file
8365dca41c3SJed Brown . -dm_plex_shape <shape>                             - The domain shape, such as `BOX`, `SPHERE`, etc.
8379318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                                 - Cell shape
8389318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool>              - Use a reference cell domain
8399318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                                 - Set the topological dimension
840bb7acecfSBarry Smith . -dm_plex_simplex <bool>                            - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
841bb7acecfSBarry Smith . -dm_plex_interpolate <bool>                        - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
842b9da1bb3SMatthew G. Knepley . -dm_plex_orient <bool>                             - `PETSC_TRUE` turns on topological orientation (flipping edges and faces)
8439318fe57SMatthew G. Knepley . -dm_plex_scale <sc>                                - Scale factor for mesh coordinates
844be664eb1SMatthew G. Knepley . -dm_coord_remap <bool>                             - Map coordinates using a function
845ac9d17c7SMatthew G. Knepley . -dm_plex_coordinate_dim <dim>                      - Change the coordinate dimension of a mesh (usually given with cdm_ prefix)
846be664eb1SMatthew G. Knepley . -dm_coord_map <mapname>                            - Select a builtin coordinate map
847be664eb1SMatthew G. Knepley . -dm_coord_map_params <p0,p1,p2,...>                - Set coordinate mapping parameters
8489318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>                         - Number of faces along each dimension
8499318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>                         - Specify lower-left-bottom coordinates for the box
8509318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>                         - Specify upper-right-top coordinates for the box
851bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>                         - Specify the `DMBoundaryType` for each direction
8529318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>                         - The sphere radius
8539318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>                           - Radius of the ball
8549318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>                          - Boundary type in the z direction
8559318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>                   - Number of wedges around the cylinder
856bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>                           - Reorder the mesh using the specified algorithm
8579318fe57SMatthew G. Knepley . -dm_refine_pre <n>                                 - The number of refinements before distribution
8589318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>                      - Flag for uniform refinement before distribution
8599318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>                    - The maximum cell volume after refinement before distribution
8609318fe57SMatthew G. Knepley . -dm_refine <n>                                     - The number of refinements after distribution
861bdf63967SMatthew G. Knepley . -dm_extrude <l>                                    - Activate extrusion and specify the number of layers to extrude
86261f058f9SMatthew G. Knepley . -dm_plex_save_transform <bool>                     - Save the `DMPlexTransform` that produced this mesh
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 
8858e704042SBarry Smith   Note:
8868e704042SBarry Smith   For some `DMType` such as `DMDA` this cannot be called after `DMSetUp()` has been called.
8878e704042SBarry Smith 
8881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
889bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
8908e704042SBarry Smith          `DMSetOptionsPrefix()`, `DMType`, `DMPLEX`, `DMDA`, `DMSetUp()`
891d7bf68aeSBarry Smith @*/
892d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
893d71ae5a4SJacob Faibussowitsch {
8947781c08eSBarry Smith   char      typeName[256];
895ca266f36SBarry Smith   PetscBool flg;
896d7bf68aeSBarry Smith 
897d7bf68aeSBarry Smith   PetscFunctionBegin;
898171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89949be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
9009566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
9019566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
902dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
903d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
9049566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
9059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9061baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9081baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
909863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9109566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-dm_bind_below", "Set the size threshold (in entries) below which the Vec is bound to the CPU", "VecBindToCPU", dm->bind_below, &dm->bind_below, &flg));
912eb9d3e4dSMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_ignore_perm_output", "Ignore the local section permutation on output", "DMGetOutputDM", dm->ignorePermOutput, &dm->ignorePermOutput, NULL));
913dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
914f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
915dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
916d0609cedSBarry Smith   PetscOptionsEnd();
9173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
918d7bf68aeSBarry Smith }
919d7bf68aeSBarry Smith 
920ffeef943SBarry Smith /*@
921bb7acecfSBarry Smith   DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
922fe2efc57SMark 
92320f4b53cSBarry Smith   Collective
924fe2efc57SMark 
925fe2efc57SMark   Input Parameters:
926bb7acecfSBarry Smith + dm   - the `DM` object
927ce78bad3SBarry Smith . obj  - optional object that provides the prefix for the options database (if `NULL` then the prefix in `obj` is used)
92860225df5SJacob Faibussowitsch - name - option string that is used to activate viewing
929fe2efc57SMark 
930fe2efc57SMark   Level: intermediate
931bb7acecfSBarry Smith 
932bb7acecfSBarry Smith   Note:
933bb7acecfSBarry Smith   See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
934bb7acecfSBarry Smith 
93560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`
936fe2efc57SMark @*/
937ce78bad3SBarry Smith PetscErrorCode DMViewFromOptions(DM dm, PeOp PetscObject obj, const char name[])
938d71ae5a4SJacob Faibussowitsch {
939fe2efc57SMark   PetscFunctionBegin;
940fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9419566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
943fe2efc57SMark }
944fe2efc57SMark 
945ffeef943SBarry Smith /*@
946bb7acecfSBarry 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
947bb7acecfSBarry Smith   save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
94847c6ae99SBarry Smith 
94920f4b53cSBarry Smith   Collective
95047c6ae99SBarry Smith 
951d8d19677SJose E. Roman   Input Parameters:
952bb7acecfSBarry Smith + dm - the `DM` object to view
95347c6ae99SBarry Smith - v  - the viewer
95447c6ae99SBarry Smith 
955c8a418c4SMatthew G. Knepley   Options Database Keys:
956c8a418c4SMatthew G. Knepley + -view_pyvista_warp <f>                 - Warps the mesh by the active scalar with factor f
957*a36b0f0fSMatthew G. Knepley . -view_pyvista_clip <xl,xu,yl,yu,zl,zu> - Defines the clipping box
958*a36b0f0fSMatthew G. Knepley . -dm_view_draw_line_color <int>         - Specify the X-window color for cell borders
959*a36b0f0fSMatthew G. Knepley . -dm_view_draw_cell_color <int>         - Specify the X-window color for cells
960*a36b0f0fSMatthew G. Knepley - -dm_view_draw_affine <bool>            - Flag to ignore high-order edges
961c8a418c4SMatthew G. Knepley 
96220f4b53cSBarry Smith   Level: beginner
96320f4b53cSBarry Smith 
96449c89c76SBlaise Bourdin   Notes:
96549c89c76SBlaise Bourdin 
96649c89c76SBlaise Bourdin   `PetscViewer` = `PETSCVIEWERHDF5` i.e. HDF5 format can be used with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` to save multiple `DMPLEX`
967bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
968bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
969cd7e8a5eSksagiyam 
97049c89c76SBlaise Bourdin   `PetscViewer` = `PETSCVIEWEREXODUSII` i.e. ExodusII format assumes that element blocks (mapped to "Cell sets" labels)
97149c89c76SBlaise Bourdin   consists of sequentially numbered cells.
97249c89c76SBlaise Bourdin 
97349c89c76SBlaise Bourdin   If `dm` has been distributed, only the part of the `DM` on MPI rank 0 (including "ghost" cells and vertices) will be written.
97449c89c76SBlaise Bourdin 
975c8a418c4SMatthew G. Knepley   Only TRI, TET, QUAD, and HEX cells are supported in ExodusII.
97649c89c76SBlaise Bourdin 
97749c89c76SBlaise Bourdin   `DMPLEX` only represents geometry while most post-processing software expect that a mesh also provides information on the discretization space. This function assumes that the file represents Lagrange finite elements of order 1 or 2.
97849c89c76SBlaise Bourdin   The order of the mesh shall be set using `PetscViewerExodusIISetOrder()`
97949c89c76SBlaise Bourdin 
980d7c1f440SPierre Jolivet   Variable names can be set and queried using `PetscViewerExodusII[Set/Get][Nodal/Zonal]VariableNames[s]`.
98149c89c76SBlaise Bourdin 
9821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
98347c6ae99SBarry Smith @*/
984d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
985d71ae5a4SJacob Faibussowitsch {
98632c0f0efSBarry Smith   PetscBool         isbinary;
98776a8abe0SBarry Smith   PetscMPIInt       size;
98876a8abe0SBarry Smith   PetscViewerFormat format;
98947c6ae99SBarry Smith 
99047c6ae99SBarry Smith   PetscFunctionBegin;
991171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
99248a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
993b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
99474903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
99574903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
99674903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
99774903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
99874903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
99974903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
100074903a4fSStefano Zampini      in an error here */
100174903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
10029566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
1003b1b135c8SBarry Smith 
100442d0c57aSJames Wright   PetscCall(PetscLogEventBegin(DM_View, v, 0, 0, 0));
10059566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
10069566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
10073ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
10089566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
10099566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
101032c0f0efSBarry Smith   if (isbinary) {
101155849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
101232c0f0efSBarry Smith     char     type[256];
101332c0f0efSBarry Smith 
10149566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
1015c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
10169566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
101732c0f0efSBarry Smith   }
1018dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
101942d0c57aSJames Wright   PetscCall(PetscLogEventEnd(DM_View, v, 0, 0, 0));
10203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
102147c6ae99SBarry Smith }
102247c6ae99SBarry Smith 
102347c6ae99SBarry Smith /*@
1024bb7acecfSBarry 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,
1025bb7acecfSBarry Smith   that is it has no ghost locations.
102647c6ae99SBarry Smith 
102720f4b53cSBarry Smith   Collective
102847c6ae99SBarry Smith 
102947c6ae99SBarry Smith   Input Parameter:
1030bb7acecfSBarry Smith . dm - the `DM` object
103147c6ae99SBarry Smith 
103247c6ae99SBarry Smith   Output Parameter:
103347c6ae99SBarry Smith . vec - the global vector
103447c6ae99SBarry Smith 
1035073dac72SJed Brown   Level: beginner
103647c6ae99SBarry Smith 
10371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1038bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
103947c6ae99SBarry Smith @*/
1040d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1041d71ae5a4SJacob Faibussowitsch {
104247c6ae99SBarry Smith   PetscFunctionBegin;
1043171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10444f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1045dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
104676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1047c6b011d8SStefano Zampini     DM vdm;
1048c6b011d8SStefano Zampini 
10499566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10507a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1051c6b011d8SStefano Zampini   }
10523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
105347c6ae99SBarry Smith }
105447c6ae99SBarry Smith 
105547c6ae99SBarry Smith /*@
1056bb7acecfSBarry Smith   DMCreateLocalVector - Creates a local vector from a `DM` object.
105747c6ae99SBarry Smith 
105847c6ae99SBarry Smith   Not Collective
105947c6ae99SBarry Smith 
106047c6ae99SBarry Smith   Input Parameter:
1061bb7acecfSBarry Smith . dm - the `DM` object
106247c6ae99SBarry Smith 
106347c6ae99SBarry Smith   Output Parameter:
106447c6ae99SBarry Smith . vec - the local vector
106547c6ae99SBarry Smith 
1066073dac72SJed Brown   Level: beginner
106747c6ae99SBarry Smith 
106820f4b53cSBarry Smith   Note:
1069bb7acecfSBarry 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.
1070bb7acecfSBarry Smith 
10711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1072bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
107347c6ae99SBarry Smith @*/
1074d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1075d71ae5a4SJacob Faibussowitsch {
107647c6ae99SBarry Smith   PetscFunctionBegin;
1077171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10784f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1079dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
108076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1081c6b011d8SStefano Zampini     DM vdm;
1082c6b011d8SStefano Zampini 
10839566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10847a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1085c6b011d8SStefano Zampini   }
10863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
108747c6ae99SBarry Smith }
108847c6ae99SBarry Smith 
10891411c6eeSJed Brown /*@
1090bb7acecfSBarry Smith   DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10911411c6eeSJed Brown 
109220f4b53cSBarry Smith   Collective
10931411c6eeSJed Brown 
10941411c6eeSJed Brown   Input Parameter:
1095bb7acecfSBarry Smith . dm - the `DM` that provides the mapping
10961411c6eeSJed Brown 
10971411c6eeSJed Brown   Output Parameter:
10981411c6eeSJed Brown . ltog - the mapping
10991411c6eeSJed Brown 
1100bb7acecfSBarry Smith   Level: advanced
11011411c6eeSJed Brown 
11021411c6eeSJed Brown   Notes:
1103bb7acecfSBarry Smith   The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
11041411c6eeSJed Brown 
1105bb7acecfSBarry Smith   Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1106bb7acecfSBarry Smith   need to use this function with those objects.
1107bb7acecfSBarry Smith 
1108bb7acecfSBarry Smith   This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1109bb7acecfSBarry Smith 
111060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1111bb7acecfSBarry Smith           `DMCreateMatrix()`
11121411c6eeSJed Brown @*/
1113d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1114d71ae5a4SJacob Faibussowitsch {
11150be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
11161411c6eeSJed Brown 
11171411c6eeSJed Brown   PetscFunctionBegin;
11181411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11194f572ea9SToby Isaac   PetscAssertPointer(ltog, 2);
11201411c6eeSJed Brown   if (!dm->ltogmap) {
112137d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
112237d0c07bSMatthew G Knepley 
11239566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
112437d0c07bSMatthew G Knepley     if (section) {
1125a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
112637d0c07bSMatthew G Knepley       PetscInt       *ltog;
1127ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
112837d0c07bSMatthew G Knepley 
11299566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11309566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11319566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11329566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
113337d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1134e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
113537d0c07bSMatthew G Knepley 
113637d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11379566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11389566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11399566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11409566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11411a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11421a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1143ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11445227eafbSStefano Zampini 
1145e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11465227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1147e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1148e6befd46SJed Brown             cind++;
1149e6befd46SJed Brown           } else {
11505227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1151e6befd46SJed Brown           }
115237d0c07bSMatthew G Knepley         }
115337d0c07bSMatthew G Knepley       }
1154bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11551690c2aeSBarry Smith       bsLocal[0] = bs < 0 ? PETSC_INT_MAX : bs;
11569371c9d4SSatish Balay       bsLocal[1] = bs;
11579566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11589371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11599371c9d4SSatish Balay         bs = 1;
11609371c9d4SSatish Balay       } else {
11619371c9d4SSatish Balay         bs = bsMinMax[0];
11629371c9d4SSatish Balay       }
11637591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11647591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1165ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1166ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1167ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1168ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1169ca469d19SJed Brown         }
1170ccf3bd66SMatthew G. Knepley         n /= bs;
1171ccf3bd66SMatthew G. Knepley       }
11729566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1173dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
117437d0c07bSMatthew G Knepley   }
11751411c6eeSJed Brown   *ltog = dm->ltogmap;
11763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11771411c6eeSJed Brown }
11781411c6eeSJed Brown 
11791411c6eeSJed Brown /*@
1180bb7acecfSBarry Smith   DMGetBlockSize - Gets the inherent block size associated with a `DM`
11811411c6eeSJed Brown 
11821411c6eeSJed Brown   Not Collective
11831411c6eeSJed Brown 
11841411c6eeSJed Brown   Input Parameter:
1185bb7acecfSBarry Smith . dm - the `DM` with block structure
11861411c6eeSJed Brown 
11871411c6eeSJed Brown   Output Parameter:
11881411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure
11891411c6eeSJed Brown 
11901411c6eeSJed Brown   Level: intermediate
11911411c6eeSJed Brown 
119273ff1848SBarry Smith   Notes:
1193bb7acecfSBarry Smith   This might be the number of degrees of freedom at each grid point for a structured grid.
1194bb7acecfSBarry Smith 
1195bb7acecfSBarry Smith   Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1196bb7acecfSBarry Smith   rather different locations in the vectors may have a different block size.
1197bb7acecfSBarry Smith 
11981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11991411c6eeSJed Brown @*/
1200d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1201d71ae5a4SJacob Faibussowitsch {
12021411c6eeSJed Brown   PetscFunctionBegin;
12031411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12044f572ea9SToby Isaac   PetscAssertPointer(bs, 2);
12057a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
12061411c6eeSJed Brown   *bs = dm->bs;
12073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12081411c6eeSJed Brown }
12091411c6eeSJed Brown 
1210ffeef943SBarry Smith /*@
1211bb7acecfSBarry Smith   DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1212bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
121347c6ae99SBarry Smith 
121420f4b53cSBarry Smith   Collective
121547c6ae99SBarry Smith 
1216d8d19677SJose E. Roman   Input Parameters:
1217bb7acecfSBarry Smith + dmc - the `DM` object
1218bb7acecfSBarry Smith - dmf - the second, finer `DM` object
121947c6ae99SBarry Smith 
1220d8d19677SJose E. Roman   Output Parameters:
122147c6ae99SBarry Smith + mat - the interpolation
1222b6971eaeSBarry Smith - vec - the scaling (optional, pass `NULL` if not needed), see `DMCreateInterpolationScale()`
122347c6ae99SBarry Smith 
122447c6ae99SBarry Smith   Level: developer
122547c6ae99SBarry Smith 
122695452b02SPatrick Sanan   Notes:
1227bb7acecfSBarry 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
1228bb7acecfSBarry Smith   DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1229d52bd9f3SBarry Smith 
1230bb7acecfSBarry Smith   For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1231bb7acecfSBarry Smith   vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
123285afcc9aSBarry Smith 
12331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
123447c6ae99SBarry Smith @*/
1235d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1236d71ae5a4SJacob Faibussowitsch {
123747c6ae99SBarry Smith   PetscFunctionBegin;
1238a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1239a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12404f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12419566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1242dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12439566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
124547c6ae99SBarry Smith }
124647c6ae99SBarry Smith 
12473ad4599aSBarry Smith /*@
1248a4e35b19SJacob Faibussowitsch   DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is
1249a4e35b19SJacob Faibussowitsch   the transpose of the interpolation between the `DM`.
12502ed6491fSPatrick Sanan 
12512ed6491fSPatrick Sanan   Input Parameters:
1252bb7acecfSBarry Smith + dac - `DM` that defines a coarse mesh
1253bb7acecfSBarry Smith . daf - `DM` that defines a fine mesh
12542ed6491fSPatrick Sanan - mat - the restriction (or interpolation operator) from fine to coarse
12552ed6491fSPatrick Sanan 
12562ed6491fSPatrick Sanan   Output Parameter:
12572ed6491fSPatrick Sanan . scale - the scaled vector
12582ed6491fSPatrick Sanan 
1259bb7acecfSBarry Smith   Level: advanced
12602ed6491fSPatrick Sanan 
126173ff1848SBarry Smith   Note:
1262a4e35b19SJacob Faibussowitsch   xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual)
1263a4e35b19SJacob Faibussowitsch   restriction. In other words xcoarse is the coarse representation of xfine.
1264a4e35b19SJacob Faibussowitsch 
126573ff1848SBarry Smith   Developer Note:
1266bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1267e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1268e9c74fd6SRichard Tran Mills 
126960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, `DMCreateRestriction()`, `DMCreateGlobalVector()`
12702ed6491fSPatrick Sanan @*/
1271d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1272d71ae5a4SJacob Faibussowitsch {
12732ed6491fSPatrick Sanan   Vec         fine;
12742ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12759704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1276e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12779704db99SRichard Tran Mills #endif
12782ed6491fSPatrick Sanan 
12792ed6491fSPatrick Sanan   PetscFunctionBegin;
12809566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12819566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12829566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12839704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12849704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12859704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12869704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12879566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1288e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12899566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12909566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12919566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
129283aa49f4SRichard Tran Mills   }
12939704db99SRichard Tran Mills #endif
12949566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12959566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12969566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12982ed6491fSPatrick Sanan }
12992ed6491fSPatrick Sanan 
13002ed6491fSPatrick Sanan /*@
1301bb7acecfSBarry Smith   DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1302bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
13033ad4599aSBarry Smith 
130420f4b53cSBarry Smith   Collective
13053ad4599aSBarry Smith 
1306d8d19677SJose E. Roman   Input Parameters:
1307bb7acecfSBarry Smith + dmc - the `DM` object
1308bb7acecfSBarry Smith - dmf - the second, finer `DM` object
13093ad4599aSBarry Smith 
13103ad4599aSBarry Smith   Output Parameter:
13113ad4599aSBarry Smith . mat - the restriction
13123ad4599aSBarry Smith 
13133ad4599aSBarry Smith   Level: developer
13143ad4599aSBarry Smith 
1315bb7acecfSBarry Smith   Note:
1316bb7acecfSBarry Smith   This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1317bb7acecfSBarry Smith   matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
13183ad4599aSBarry Smith 
13191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
13203ad4599aSBarry Smith @*/
1321d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1322d71ae5a4SJacob Faibussowitsch {
13233ad4599aSBarry Smith   PetscFunctionBegin;
1324a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1325a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13264f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13279566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1328dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13299566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13313ad4599aSBarry Smith }
13323ad4599aSBarry Smith 
133347c6ae99SBarry Smith /*@
1334a4e35b19SJacob Faibussowitsch   DMCreateInjection - Gets injection matrix between two `DM` objects.
133547c6ae99SBarry Smith 
133620f4b53cSBarry Smith   Collective
133747c6ae99SBarry Smith 
1338d8d19677SJose E. Roman   Input Parameters:
1339bb7acecfSBarry Smith + dac - the `DM` object
1340bb7acecfSBarry Smith - daf - the second, finer `DM` object
134147c6ae99SBarry Smith 
134247c6ae99SBarry Smith   Output Parameter:
13436dbf9973SLawrence Mitchell . mat - the injection
134447c6ae99SBarry Smith 
134547c6ae99SBarry Smith   Level: developer
134647c6ae99SBarry Smith 
1347a4e35b19SJacob Faibussowitsch   Notes:
1348a4e35b19SJacob Faibussowitsch   This is an operator that applied to a vector obtained with `DMCreateGlobalVector()` on the
1349a4e35b19SJacob Faibussowitsch   fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting
1350a4e35b19SJacob Faibussowitsch   the values on the coarse grid points. This compares to the operator obtained by
1351a4e35b19SJacob Faibussowitsch   `DMCreateRestriction()` or the transpose of the operator obtained by
1352a4e35b19SJacob Faibussowitsch   `DMCreateInterpolation()` that uses a "local weighted average" of the values around the
1353a4e35b19SJacob Faibussowitsch   coarse grid point as the coarse grid value.
1354a4e35b19SJacob Faibussowitsch 
1355bb7acecfSBarry 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
1356bb7acecfSBarry Smith   `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
135785afcc9aSBarry Smith 
13581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1359bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
136047c6ae99SBarry Smith @*/
1361d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1362d71ae5a4SJacob Faibussowitsch {
136347c6ae99SBarry Smith   PetscFunctionBegin;
1364a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1365a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13664f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13679566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1368dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13699566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
137147c6ae99SBarry Smith }
137247c6ae99SBarry Smith 
1373b412c318SBarry Smith /*@
1374bb7acecfSBarry 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
1375bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1376bd041c0cSMatthew G. Knepley 
137720f4b53cSBarry Smith   Collective
1378bd041c0cSMatthew G. Knepley 
1379d8d19677SJose E. Roman   Input Parameters:
1380bb7acecfSBarry Smith + dmc - the target `DM` object
13818c1c0954SStefano Zampini - dmf - the source `DM` object, can be `NULL`
1382bd041c0cSMatthew G. Knepley 
1383bd041c0cSMatthew G. Knepley   Output Parameter:
1384b4937a87SMatthew G. Knepley . mat - the mass matrix
1385bd041c0cSMatthew G. Knepley 
1386bd041c0cSMatthew G. Knepley   Level: developer
1387bd041c0cSMatthew G. Knepley 
1388bb7acecfSBarry Smith   Notes:
1389bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1390bb7acecfSBarry Smith 
13918c1c0954SStefano Zampini   if `dmc` is `dmf` or `NULL`, then x^t M x is an approximation to the L2 norm of the vector x which is obtained by `DMCreateGlobalVector()`
1392bb7acecfSBarry Smith 
139342747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1394bd041c0cSMatthew G. Knepley @*/
1395d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1396d71ae5a4SJacob Faibussowitsch {
1397bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1398b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
13998c1c0954SStefano Zampini   if (!dmf) dmf = dmc;
1400b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
14014f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
14028c1c0954SStefano Zampini   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, dmc, dmf, 0, 0));
1403dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
14048c1c0954SStefano Zampini   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, dmc, dmf, 0, 0));
14053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1406b4937a87SMatthew G. Knepley }
1407b4937a87SMatthew G. Knepley 
1408b4937a87SMatthew G. Knepley /*@
1409bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1410b4937a87SMatthew G. Knepley 
141120f4b53cSBarry Smith   Collective
1412b4937a87SMatthew G. Knepley 
1413b4937a87SMatthew G. Knepley   Input Parameter:
1414bb7acecfSBarry Smith . dm - the `DM` object
1415b4937a87SMatthew G. Knepley 
14167dcfde4dSJose E. Roman   Output Parameters:
14178e9849d2SStefano Zampini + llm - the local lumped mass matrix, which is a diagonal matrix, represented as a vector
14188e9849d2SStefano Zampini - lm  - the global lumped mass matrix, which is a diagonal matrix, represented as a vector
1419b4937a87SMatthew G. Knepley 
1420b4937a87SMatthew G. Knepley   Level: developer
1421b4937a87SMatthew G. Knepley 
1422bb7acecfSBarry Smith   Note:
1423bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1424bb7acecfSBarry Smith 
142560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1426b4937a87SMatthew G. Knepley @*/
14278e9849d2SStefano Zampini PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *llm, Vec *lm)
1428d71ae5a4SJacob Faibussowitsch {
1429b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1430b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14318e9849d2SStefano Zampini   if (llm) PetscAssertPointer(llm, 2);
14328e9849d2SStefano Zampini   if (lm) PetscAssertPointer(lm, 3);
14338e9849d2SStefano Zampini   if (llm || lm) PetscUseTypeMethod(dm, createmassmatrixlumped, llm, lm);
14343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1435bd041c0cSMatthew G. Knepley }
1436bd041c0cSMatthew G. Knepley 
1437bd041c0cSMatthew G. Knepley /*@
1438bb7acecfSBarry Smith   DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1439bb7acecfSBarry Smith   of a PDE on the `DM`.
144047c6ae99SBarry Smith 
144120f4b53cSBarry Smith   Collective
144247c6ae99SBarry Smith 
1443d8d19677SJose E. Roman   Input Parameters:
1444bb7acecfSBarry Smith + dm    - the `DM` object
1445bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
144647c6ae99SBarry Smith 
144747c6ae99SBarry Smith   Output Parameter:
144847c6ae99SBarry Smith . coloring - the coloring
144947c6ae99SBarry Smith 
14501bf8429eSBarry Smith   Level: developer
14511bf8429eSBarry Smith 
1452ec5066bdSBarry Smith   Notes:
1453bb7acecfSBarry 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
1454bb7acecfSBarry Smith   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1455ec5066bdSBarry Smith 
1456bb7acecfSBarry Smith   This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14571bf8429eSBarry 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,
14581bf8429eSBarry Smith   otherwise an error will be generated.
1459ec5066bdSBarry Smith 
14601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1461aab9d709SJed Brown @*/
1462d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1463d71ae5a4SJacob Faibussowitsch {
146447c6ae99SBarry Smith   PetscFunctionBegin;
1465171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14664f572ea9SToby Isaac   PetscAssertPointer(coloring, 3);
1467dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
146947c6ae99SBarry Smith }
147047c6ae99SBarry Smith 
1471b412c318SBarry Smith /*@
1472bb7acecfSBarry Smith   DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
147347c6ae99SBarry Smith 
147420f4b53cSBarry Smith   Collective
147547c6ae99SBarry Smith 
147647c6ae99SBarry Smith   Input Parameter:
1477bb7acecfSBarry Smith . dm - the `DM` object
147847c6ae99SBarry Smith 
147947c6ae99SBarry Smith   Output Parameter:
148047c6ae99SBarry Smith . mat - the empty Jacobian
148147c6ae99SBarry Smith 
148220f4b53cSBarry Smith   Options Database Key:
1483bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1484f27dd7c6SMatthew G. Knepley 
148520f4b53cSBarry Smith   Level: beginner
148620f4b53cSBarry Smith 
148795452b02SPatrick Sanan   Notes:
148895452b02SPatrick Sanan   This properly preallocates the number of nonzeros in the sparse matrix so you
148994013140SBarry Smith   do not need to do it yourself.
149094013140SBarry Smith 
149194013140SBarry Smith   By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1492bb7acecfSBarry Smith   the nonzero pattern call `DMSetMatrixPreallocateOnly()`
149394013140SBarry Smith 
1494bb7acecfSBarry Smith   For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
149594013140SBarry Smith   internally by PETSc.
149694013140SBarry Smith 
1497bb7acecfSBarry Smith   For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1498bb7acecfSBarry Smith   `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
149994013140SBarry Smith 
15001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1501aab9d709SJed Brown @*/
1502d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1503d71ae5a4SJacob Faibussowitsch {
150447c6ae99SBarry Smith   PetscFunctionBegin;
1505171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
15064f572ea9SToby Isaac   PetscAssertPointer(mat, 2);
15079566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
15089566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1509dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
151076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1511c6b011d8SStefano Zampini     DM mdm;
1512c6b011d8SStefano Zampini 
15139566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
15147a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1515c6b011d8SStefano Zampini   }
1516e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1517e5e52638SMatthew G. Knepley   if (dm->Nf) {
1518e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1519649ef022SMatthew Knepley     PetscInt     Nf, f;
1520e571a35bSMatthew G. Knepley 
15219566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1522649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1523649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
15249566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
15259566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
15269566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1527649ef022SMatthew Knepley         break;
1528e571a35bSMatthew G. Knepley       }
1529649ef022SMatthew Knepley     }
1530649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1531649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15329566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15339566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15349566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1535e571a35bSMatthew G. Knepley       }
1536e571a35bSMatthew G. Knepley     }
1537e571a35bSMatthew G. Knepley   }
15389566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
154047c6ae99SBarry Smith }
154147c6ae99SBarry Smith 
1542732e2eb9SMatthew G Knepley /*@
1543a4e35b19SJacob Faibussowitsch   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and
1544a4e35b19SJacob Faibussowitsch   `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the
1545a4e35b19SJacob Faibussowitsch   matrices will not be preallocated.
1546aa0f6e3cSJed Brown 
154720f4b53cSBarry Smith   Logically Collective
1548aa0f6e3cSJed Brown 
1549aa0f6e3cSJed Brown   Input Parameters:
1550bb7acecfSBarry Smith + dm   - the `DM`
1551bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1552aa0f6e3cSJed Brown 
1553aa0f6e3cSJed Brown   Level: developer
1554aa0f6e3cSJed Brown 
155573ff1848SBarry Smith   Note:
1556a4e35b19SJacob Faibussowitsch   This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and
1557a4e35b19SJacob Faibussowitsch   `MatSetValuesCOO()` will be used.
1558a4e35b19SJacob Faibussowitsch 
15591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1560aa0f6e3cSJed Brown @*/
1561d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1562d71ae5a4SJacob Faibussowitsch {
1563aa0f6e3cSJed Brown   PetscFunctionBegin;
1564aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1565aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1567aa0f6e3cSJed Brown }
1568aa0f6e3cSJed Brown 
1569aa0f6e3cSJed Brown /*@
1570bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1571732e2eb9SMatthew G Knepley   preallocated but the nonzero structure and zero values will not be set.
1572732e2eb9SMatthew G Knepley 
157320f4b53cSBarry Smith   Logically Collective
1574732e2eb9SMatthew G Knepley 
1575d8d19677SJose E. Roman   Input Parameters:
1576bb7acecfSBarry Smith + dm   - the `DM`
1577bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1578732e2eb9SMatthew G Knepley 
157920f4b53cSBarry Smith   Options Database Key:
1580bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1581f27dd7c6SMatthew G. Knepley 
158220f4b53cSBarry Smith   Level: developer
158320f4b53cSBarry Smith 
15841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1585732e2eb9SMatthew G Knepley @*/
1586d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1587d71ae5a4SJacob Faibussowitsch {
1588732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1589732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1590732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1592732e2eb9SMatthew G Knepley }
1593732e2eb9SMatthew G Knepley 
1594b06ff27eSHong Zhang /*@
15950b4b7b1cSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix nonzero structure will be created
1596bb7acecfSBarry Smith   but the array for numerical values will not be allocated.
1597b06ff27eSHong Zhang 
159820f4b53cSBarry Smith   Logically Collective
1599b06ff27eSHong Zhang 
1600d8d19677SJose E. Roman   Input Parameters:
1601bb7acecfSBarry Smith + dm   - the `DM`
16020b4b7b1cSBarry Smith - only - `PETSC_TRUE` if you only want matrix nonzero structure
1603b06ff27eSHong Zhang 
1604b06ff27eSHong Zhang   Level: developer
1605bb7acecfSBarry Smith 
16061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1607b06ff27eSHong Zhang @*/
1608d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1609d71ae5a4SJacob Faibussowitsch {
1610b06ff27eSHong Zhang   PetscFunctionBegin;
1611b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1612b06ff27eSHong Zhang   dm->structure_only = only;
16133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1614b06ff27eSHong Zhang }
1615b06ff27eSHong Zhang 
1616863027abSJed Brown /*@
1617863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1618863027abSJed Brown 
161920f4b53cSBarry Smith   Logically Collective
1620863027abSJed Brown 
1621863027abSJed Brown   Input Parameters:
1622863027abSJed Brown + dm    - the `DM`
1623863027abSJed Brown - btype - block by topological point or field node
1624863027abSJed Brown 
162520f4b53cSBarry Smith   Options Database Key:
16260e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1627863027abSJed Brown 
162820f4b53cSBarry Smith   Level: advanced
162920f4b53cSBarry Smith 
16301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1631863027abSJed Brown @*/
1632863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1633863027abSJed Brown {
1634863027abSJed Brown   PetscFunctionBegin;
1635863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1636863027abSJed Brown   dm->blocking_type = btype;
16373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1638863027abSJed Brown }
1639863027abSJed Brown 
1640863027abSJed Brown /*@
1641863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1642863027abSJed Brown 
1643863027abSJed Brown   Not Collective
1644863027abSJed Brown 
16452fe279fdSBarry Smith   Input Parameter:
1646863027abSJed Brown . dm - the `DM`
1647863027abSJed Brown 
16482fe279fdSBarry Smith   Output Parameter:
1649863027abSJed Brown . btype - block by topological point or field node
1650863027abSJed Brown 
1651863027abSJed Brown   Level: advanced
1652863027abSJed Brown 
16531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1654863027abSJed Brown @*/
1655863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1656863027abSJed Brown {
1657863027abSJed Brown   PetscFunctionBegin;
1658863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16594f572ea9SToby Isaac   PetscAssertPointer(btype, 2);
1660863027abSJed Brown   *btype = dm->blocking_type;
16613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1662863027abSJed Brown }
1663863027abSJed Brown 
1664a89ea682SMatthew G Knepley /*@C
1665bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1666a89ea682SMatthew G Knepley 
1667a89ea682SMatthew G Knepley   Not Collective
1668a89ea682SMatthew G Knepley 
1669a89ea682SMatthew G Knepley   Input Parameters:
1670bb7acecfSBarry Smith + dm    - the `DM` object
1671a5b23f4aSJose E. Roman . count - The minimum size
167220f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1673a89ea682SMatthew G Knepley 
1674a89ea682SMatthew G Knepley   Output Parameter:
167560225df5SJacob Faibussowitsch . mem - the work array
1676a89ea682SMatthew G Knepley 
1677a89ea682SMatthew G Knepley   Level: developer
1678a89ea682SMatthew G Knepley 
167973ff1848SBarry Smith   Notes:
1680da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1681bb7acecfSBarry Smith 
1682bb7acecfSBarry Smith   The array may contain nonzero values
1683bb7acecfSBarry Smith 
16841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1685a89ea682SMatthew G Knepley @*/
1686d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1687d71ae5a4SJacob Faibussowitsch {
1688aa1993deSMatthew G Knepley   DMWorkLink  link;
168969291d52SBarry Smith   PetscMPIInt dsize;
1690a89ea682SMatthew G Knepley 
1691a89ea682SMatthew G Knepley   PetscFunctionBegin;
1692a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16934f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1694442f3b32SStefano Zampini   if (!count) {
1695442f3b32SStefano Zampini     *(void **)mem = NULL;
1696442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1697442f3b32SStefano Zampini   }
1698aa1993deSMatthew G Knepley   if (dm->workin) {
1699aa1993deSMatthew G Knepley     link       = dm->workin;
1700aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1701aa1993deSMatthew G Knepley   } else {
17024dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1703a89ea682SMatthew G Knepley   }
1704b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1705b77fa653SStefano Zampini      Get size directly */
1706b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1707b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1708b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1709b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1710b77fa653SStefano Zampini #endif
1711b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1712b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1713b77fa653SStefano Zampini #endif
1714b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1715b77fa653SStefano Zampini 
17165056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
17179566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
17189566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1719854ce69bSBarry Smith     link->bytes = dsize * count;
1720aa1993deSMatthew G Knepley   }
1721aa1993deSMatthew G Knepley   link->next    = dm->workout;
1722aa1993deSMatthew G Knepley   dm->workout   = link;
1723aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
17243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1725a89ea682SMatthew G Knepley }
1726a89ea682SMatthew G Knepley 
1727aa1993deSMatthew G Knepley /*@C
1728bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1729aa1993deSMatthew G Knepley 
1730aa1993deSMatthew G Knepley   Not Collective
1731aa1993deSMatthew G Knepley 
1732aa1993deSMatthew G Knepley   Input Parameters:
1733bb7acecfSBarry Smith + dm    - the `DM` object
1734a5b23f4aSJose E. Roman . count - The minimum size
173520f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1736aa1993deSMatthew G Knepley 
1737aa1993deSMatthew G Knepley   Output Parameter:
173860225df5SJacob Faibussowitsch . mem - the work array
1739aa1993deSMatthew G Knepley 
1740aa1993deSMatthew G Knepley   Level: developer
1741aa1993deSMatthew G Knepley 
174273ff1848SBarry Smith   Developer Note:
1743bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1744147403d9SBarry Smith 
17451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1746aa1993deSMatthew G Knepley @*/
1747d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1748d71ae5a4SJacob Faibussowitsch {
1749aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1750aa1993deSMatthew G Knepley 
1751aa1993deSMatthew G Knepley   PetscFunctionBegin;
17524f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1753a4e35b19SJacob Faibussowitsch   (void)count;
1754a4e35b19SJacob Faibussowitsch   (void)dtype;
1755442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1756aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1757aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1758aa1993deSMatthew G Knepley       *p            = link->next;
1759aa1993deSMatthew G Knepley       link->next    = dm->workin;
1760aa1993deSMatthew G Knepley       dm->workin    = link;
17610298fd71SBarry Smith       *(void **)mem = NULL;
17623ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1763aa1993deSMatthew G Knepley     }
1764aa1993deSMatthew G Knepley   }
1765aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1766aa1993deSMatthew G Knepley }
1767e7c4fc90SDmitry Karpeev 
17688cda7954SMatthew G. Knepley /*@C
1769bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1770bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17718cda7954SMatthew G. Knepley 
177220f4b53cSBarry Smith   Logically Collective; No Fortran Support
17738cda7954SMatthew G. Knepley 
17748cda7954SMatthew G. Knepley   Input Parameters:
1775bb7acecfSBarry Smith + dm     - The `DM`
17768cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17778cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17788cda7954SMatthew G. Knepley 
177920f4b53cSBarry Smith   Calling sequence of `nullsp`:
1780bb7acecfSBarry Smith + dm        - The present `DM`
1781bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1782147403d9SBarry Smith . field     - The field number in dm
1783147403d9SBarry Smith - nullSpace - The nullspace for the given field
17848cda7954SMatthew G. Knepley 
178549762cbcSSatish Balay   Level: intermediate
178649762cbcSSatish Balay 
17871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1788147403d9SBarry Smith @*/
1789a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1790d71ae5a4SJacob Faibussowitsch {
1791435a35e8SMatthew G Knepley   PetscFunctionBegin;
1792435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17937a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1794435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1796435a35e8SMatthew G Knepley }
1797435a35e8SMatthew G Knepley 
17988cda7954SMatthew G. Knepley /*@C
1799bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
18008cda7954SMatthew G. Knepley 
180120f4b53cSBarry Smith   Not Collective; No Fortran Support
18028cda7954SMatthew G. Knepley 
18038cda7954SMatthew G. Knepley   Input Parameters:
1804bb7acecfSBarry Smith + dm    - The `DM`
18058cda7954SMatthew G. Knepley - field - The field number for the nullspace
18068cda7954SMatthew G. Knepley 
18078cda7954SMatthew G. Knepley   Output Parameter:
18088cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
18098cda7954SMatthew G. Knepley 
181020f4b53cSBarry Smith   Calling sequence of `nullsp`:
1811147403d9SBarry Smith + dm        - The present DM
1812147403d9SBarry Smith . origField - The field number given above, in the original DM
1813147403d9SBarry Smith . field     - The field number in dm
1814147403d9SBarry Smith - nullSpace - The nullspace for the given field
18158cda7954SMatthew G. Knepley 
181649762cbcSSatish Balay   Level: intermediate
181749762cbcSSatish Balay 
18181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1819147403d9SBarry Smith @*/
1820a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1821d71ae5a4SJacob Faibussowitsch {
18220a50eb56SMatthew G. Knepley   PetscFunctionBegin;
18230a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18244f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18257a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
18260a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
18273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18280a50eb56SMatthew G. Knepley }
18290a50eb56SMatthew G. Knepley 
18308cda7954SMatthew G. Knepley /*@C
1831bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18328cda7954SMatthew G. Knepley 
183320f4b53cSBarry Smith   Logically Collective; No Fortran Support
18348cda7954SMatthew G. Knepley 
18358cda7954SMatthew G. Knepley   Input Parameters:
1836bb7acecfSBarry Smith + dm     - The `DM`
18378cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18388cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18398cda7954SMatthew G. Knepley 
184020f4b53cSBarry Smith   Calling sequence of `nullsp`:
1841bb7acecfSBarry Smith + dm        - The present `DM`
1842bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1843147403d9SBarry Smith . field     - The field number in dm
1844147403d9SBarry Smith - nullSpace - The nullspace for the given field
18458cda7954SMatthew G. Knepley 
184649762cbcSSatish Balay   Level: intermediate
184749762cbcSSatish Balay 
18481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1849bb7acecfSBarry Smith           `MatNullSpace`
1850147403d9SBarry Smith @*/
1851a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1852d71ae5a4SJacob Faibussowitsch {
1853f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1854f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18557a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1856f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1858f9d4088aSMatthew G. Knepley }
1859f9d4088aSMatthew G. Knepley 
18608cda7954SMatthew G. Knepley /*@C
1861bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18628cda7954SMatthew G. Knepley 
186320f4b53cSBarry Smith   Not Collective; No Fortran Support
18648cda7954SMatthew G. Knepley 
18658cda7954SMatthew G. Knepley   Input Parameters:
1866bb7acecfSBarry Smith + dm    - The `DM`
18678cda7954SMatthew G. Knepley - field - The field number for the nullspace
18688cda7954SMatthew G. Knepley 
18698cda7954SMatthew G. Knepley   Output Parameter:
18708cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18718cda7954SMatthew G. Knepley 
187220f4b53cSBarry Smith   Calling sequence of `nullsp`:
1873bb7acecfSBarry Smith + dm        - The present `DM`
1874bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1875147403d9SBarry Smith . field     - The field number in dm
1876147403d9SBarry Smith - nullSpace - The nullspace for the given field
18778cda7954SMatthew G. Knepley 
187849762cbcSSatish Balay   Level: intermediate
187949762cbcSSatish Balay 
18801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1881bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1882147403d9SBarry Smith @*/
1883a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1884d71ae5a4SJacob Faibussowitsch {
1885f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1886f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18874f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18887a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1889f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1891f9d4088aSMatthew G. Knepley }
1892f9d4088aSMatthew G. Knepley 
18934f3b5142SJed Brown /*@C
1894bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18954d343eeaSMatthew G Knepley 
189620f4b53cSBarry Smith   Not Collective; No Fortran Support
18974d343eeaSMatthew G Knepley 
18984d343eeaSMatthew G Knepley   Input Parameter:
1899bb7acecfSBarry Smith . dm - the `DM` object
19004d343eeaSMatthew G Knepley 
19014d343eeaSMatthew G Knepley   Output Parameters:
190220f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
1903ce78bad3SBarry Smith . fieldNames - The name of each field (or `NULL` if not requested)
190420f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
19054d343eeaSMatthew G Knepley 
19064d343eeaSMatthew G Knepley   Level: intermediate
19074d343eeaSMatthew G Knepley 
1908bb7acecfSBarry Smith   Note:
190973ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `fieldNames` should be freed with
191073ff1848SBarry Smith   `PetscFree()`, every entry of `fields` should be destroyed with `ISDestroy()`, and both arrays should be freed with
1911bb7acecfSBarry Smith   `PetscFree()`.
191221c9b008SJed Brown 
191373ff1848SBarry Smith   Developer Note:
1914bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1915bb7acecfSBarry Smith   likely be removed.
1916bb7acecfSBarry Smith 
19171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1918bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
19194d343eeaSMatthew G Knepley @*/
1920ce78bad3SBarry Smith PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS *fields[])
1921d71ae5a4SJacob Faibussowitsch {
192237d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
19234d343eeaSMatthew G Knepley 
19244d343eeaSMatthew G Knepley   PetscFunctionBegin;
19254d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
192669ca1f37SDmitry Karpeev   if (numFields) {
19274f572ea9SToby Isaac     PetscAssertPointer(numFields, 2);
192869ca1f37SDmitry Karpeev     *numFields = 0;
192969ca1f37SDmitry Karpeev   }
193037d0c07bSMatthew G Knepley   if (fieldNames) {
19314f572ea9SToby Isaac     PetscAssertPointer(fieldNames, 3);
19320298fd71SBarry Smith     *fieldNames = NULL;
193369ca1f37SDmitry Karpeev   }
193469ca1f37SDmitry Karpeev   if (fields) {
19354f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
19360298fd71SBarry Smith     *fields = NULL;
193769ca1f37SDmitry Karpeev   }
19389566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
193937d0c07bSMatthew G Knepley   if (section) {
19403a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
194137d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
194237d0c07bSMatthew G Knepley 
19439566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19449566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19459566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19469566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
194737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
194837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19499566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
195037d0c07bSMatthew G Knepley     }
195137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
195237d0c07bSMatthew G Knepley       PetscInt gdof;
195337d0c07bSMatthew G Knepley 
19549566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
195537d0c07bSMatthew G Knepley       if (gdof > 0) {
195637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19573a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
195837d0c07bSMatthew G Knepley 
19599566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19609566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19613a544194SStefano Zampini           fpdof = fdof - fcdof;
19623a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19633a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19643a544194SStefano Zampini             fieldNc[f] = 1;
19653a544194SStefano Zampini           }
19663a544194SStefano Zampini           fieldSizes[f] += fpdof;
196737d0c07bSMatthew G Knepley         }
196837d0c07bSMatthew G Knepley       }
196937d0c07bSMatthew G Knepley     }
197037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19719566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
197237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
197337d0c07bSMatthew G Knepley     }
197437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
197537d0c07bSMatthew G Knepley       PetscInt gdof, goff;
197637d0c07bSMatthew G Knepley 
19779566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
197837d0c07bSMatthew G Knepley       if (gdof > 0) {
19799566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
198037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
198137d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
198237d0c07bSMatthew G Knepley 
19839566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19849566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1985ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
198637d0c07bSMatthew G Knepley         }
198737d0c07bSMatthew G Knepley       }
198837d0c07bSMatthew G Knepley     }
19898865f1eaSKarl Rupp     if (numFields) *numFields = nF;
199037d0c07bSMatthew G Knepley     if (fieldNames) {
19919566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
199237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
199337d0c07bSMatthew G Knepley         const char *fieldName;
199437d0c07bSMatthew G Knepley 
19959566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
1996835f2295SStefano Zampini         PetscCall(PetscStrallocpy(fieldName, &(*fieldNames)[f]));
199737d0c07bSMatthew G Knepley       }
199837d0c07bSMatthew G Knepley     }
199937d0c07bSMatthew G Knepley     if (fields) {
20009566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
200137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
20023a544194SStefano Zampini         PetscInt bs, in[2], out[2];
20033a544194SStefano Zampini 
20049566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
20053a544194SStefano Zampini         in[0] = -fieldNc[f];
20063a544194SStefano Zampini         in[1] = fieldNc[f];
2007462c564dSBarry Smith         PetscCallMPI(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
20083a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
20099566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
201037d0c07bSMatthew G Knepley       }
201137d0c07bSMatthew G Knepley     }
20129566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
2013dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
20143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20154d343eeaSMatthew G Knepley }
20164d343eeaSMatthew G Knepley 
201716621825SDmitry Karpeev /*@C
2018bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
2019a4e35b19SJacob Faibussowitsch   corresponding to different fields.
2020e7c4fc90SDmitry Karpeev 
202120f4b53cSBarry Smith   Not Collective; No Fortran Support
2022e7c4fc90SDmitry Karpeev 
2023e7c4fc90SDmitry Karpeev   Input Parameter:
2024bb7acecfSBarry Smith . dm - the `DM` object
2025e7c4fc90SDmitry Karpeev 
2026e7c4fc90SDmitry Karpeev   Output Parameters:
202720f4b53cSBarry Smith + len      - The number of fields (or `NULL` if not requested)
202820f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested)
202920f4b53cSBarry Smith . islist   - The global indices for each field (or `NULL` if not requested)
203020f4b53cSBarry Smith - dmlist   - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
2031e7c4fc90SDmitry Karpeev 
2032e7c4fc90SDmitry Karpeev   Level: intermediate
2033e7c4fc90SDmitry Karpeev 
2034a4e35b19SJacob Faibussowitsch   Notes:
2035a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding field, defined by
2036a4e35b19SJacob Faibussowitsch   `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2037a4e35b19SJacob Faibussowitsch 
2038a4e35b19SJacob Faibussowitsch   The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2039a4e35b19SJacob Faibussowitsch 
204073ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
204173ff1848SBarry Smith   `PetscFree()`, every entry of `islist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2042bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2043e7c4fc90SDmitry Karpeev 
20448d9ecca5SBarry Smith   Fortran Notes:
20458d9ecca5SBarry Smith   Use the declarations
20468d9ecca5SBarry Smith .vb
20478d9ecca5SBarry Smith   character(80), pointer :: namelist(:)
20488d9ecca5SBarry Smith   IS, pointer :: islist(:)
20498d9ecca5SBarry Smith   DM, pointer :: dmlist(:)
20508d9ecca5SBarry Smith .ve
20518d9ecca5SBarry Smith 
20528d9ecca5SBarry Smith   `namelist` must be provided, `islist` may be `PETSC_NULL_IS_POINTER` and `dmlist` may be `PETSC_NULL_DM_POINTER`
20538d9ecca5SBarry Smith 
20548d9ecca5SBarry Smith   Use `DMDestroyFieldDecomposition()` to free the returned objects
20558d9ecca5SBarry Smith 
205660225df5SJacob Faibussowitsch   Developer Notes:
2057bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2058bb7acecfSBarry Smith 
205973ff1848SBarry Smith   Unlike  `DMRefine()`, `DMCoarsen()`, and `DMCreateDomainDecomposition()` this provides no mechanism to provide hooks that are called after the
206073ff1848SBarry Smith   decomposition is computed.
206173ff1848SBarry Smith 
206273ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
2063e7c4fc90SDmitry Karpeev @*/
2064ce78bad3SBarry Smith PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS *islist[], DM *dmlist[])
2065d71ae5a4SJacob Faibussowitsch {
2066e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2067e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20688865f1eaSKarl Rupp   if (len) {
20694f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20708865f1eaSKarl Rupp     *len = 0;
20718865f1eaSKarl Rupp   }
20728865f1eaSKarl Rupp   if (namelist) {
20734f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2074ea78f98cSLisandro Dalcin     *namelist = NULL;
20758865f1eaSKarl Rupp   }
20768865f1eaSKarl Rupp   if (islist) {
20774f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2078ea78f98cSLisandro Dalcin     *islist = NULL;
20798865f1eaSKarl Rupp   }
20808865f1eaSKarl Rupp   if (dmlist) {
20814f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2082ea78f98cSLisandro Dalcin     *dmlist = NULL;
20838865f1eaSKarl Rupp   }
2084f3f0edfdSDmitry Karpeev   /*
2085f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2086f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2087f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2088f3f0edfdSDmitry Karpeev    */
20897a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
209016621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2091435a35e8SMatthew G Knepley     PetscSection section;
2092435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2093435a35e8SMatthew G Knepley 
20949566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20959566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2096435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2097f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20989566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20999566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
21009566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2101435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2102435a35e8SMatthew G Knepley         const char *fieldName;
2103435a35e8SMatthew G Knepley 
21049566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
210503dc3394SMatthew G. Knepley         if (namelist) {
21069566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
2107835f2295SStefano Zampini           PetscCall(PetscStrallocpy(fieldName, &(*namelist)[f]));
2108435a35e8SMatthew G Knepley         }
210903dc3394SMatthew G. Knepley       }
2110435a35e8SMatthew G Knepley     } else {
21119566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2112e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
21130298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2114e7c4fc90SDmitry Karpeev     }
2115dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
21163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
211716621825SDmitry Karpeev }
211816621825SDmitry Karpeev 
21195d83a8b1SBarry Smith /*@
212020f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
212120f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2122435a35e8SMatthew G Knepley 
2123435a35e8SMatthew G Knepley   Not collective
2124435a35e8SMatthew G Knepley 
2125435a35e8SMatthew G Knepley   Input Parameters:
2126bb7acecfSBarry Smith + dm        - The `DM` object
2127bb7acecfSBarry Smith . numFields - The number of fields to select
21282adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2129435a35e8SMatthew G Knepley 
2130435a35e8SMatthew G Knepley   Output Parameters:
2131b6971eaeSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`, use `NULL` if not needed
2132b6971eaeSBarry Smith - subdm - The `DM` for the subproblem, use `NULL` if not needed
2133435a35e8SMatthew G Knepley 
213420f4b53cSBarry Smith   Level: intermediate
213520f4b53cSBarry Smith 
2136bb7acecfSBarry Smith   Note:
2137bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21385d3b26e6SMatthew G. Knepley 
213960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2140435a35e8SMatthew G Knepley @*/
2141d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2142d71ae5a4SJacob Faibussowitsch {
2143435a35e8SMatthew G Knepley   PetscFunctionBegin;
2144435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21454f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
21464f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
21474f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2148dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2150435a35e8SMatthew G Knepley }
2151435a35e8SMatthew G Knepley 
21522adcc780SMatthew G. Knepley /*@C
2153ce78bad3SBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and a single `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21542adcc780SMatthew G. Knepley 
21552adcc780SMatthew G. Knepley   Not collective
21562adcc780SMatthew G. Knepley 
2157d8d19677SJose E. Roman   Input Parameters:
2158bb7acecfSBarry Smith + dms - The `DM` objects
2159bb7acecfSBarry Smith - n   - The number of `DM`s
21602adcc780SMatthew G. Knepley 
21612adcc780SMatthew G. Knepley   Output Parameters:
2162ce78bad3SBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or `NULL`, its length is `n`
2163bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21642adcc780SMatthew G. Knepley 
216520f4b53cSBarry Smith   Level: intermediate
216620f4b53cSBarry Smith 
2167bb7acecfSBarry Smith   Note:
2168bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21695d3b26e6SMatthew G. Knepley 
217073ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`, `DMCreateDomainDecomposition()`
21712adcc780SMatthew G. Knepley @*/
21725d83a8b1SBarry Smith PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS *is[], DM *superdm)
2173d71ae5a4SJacob Faibussowitsch {
21742adcc780SMatthew G. Knepley   PetscInt i;
21752adcc780SMatthew G. Knepley 
21762adcc780SMatthew G. Knepley   PetscFunctionBegin;
21774f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2178ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21794f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
21804f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2181bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2182bb7acecfSBarry Smith   if (n) {
2183b9d85ea2SLisandro Dalcin     DM dm = dms[0];
218400045ab3SPierre Jolivet     PetscCheck(dm->ops->createsuperdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No method createsuperdm for DM of type %s", ((PetscObject)dm)->type_name);
2185dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21862adcc780SMatthew G. Knepley   }
21873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21882adcc780SMatthew G. Knepley }
21892adcc780SMatthew G. Knepley 
219016621825SDmitry Karpeev /*@C
2191a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2192a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
219316621825SDmitry Karpeev 
219420f4b53cSBarry Smith   Not Collective
219516621825SDmitry Karpeev 
219616621825SDmitry Karpeev   Input Parameter:
2197bb7acecfSBarry Smith . dm - the `DM` object
219816621825SDmitry Karpeev 
219916621825SDmitry Karpeev   Output Parameters:
2200ce78bad3SBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested), also the length of the four arrays below
220120f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
220273ff1848SBarry Smith . innerislist - The global indices for each inner subdomain (or `NULL`, if not requested)
220373ff1848SBarry Smith . outerislist - The global indices for each outer subdomain (or `NULL`, if not requested)
220473ff1848SBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
220516621825SDmitry Karpeev 
220616621825SDmitry Karpeev   Level: intermediate
220716621825SDmitry Karpeev 
220873ff1848SBarry Smith   Notes:
2209a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2210a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2211a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2212a4e35b19SJacob Faibussowitsch 
2213a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2214a4e35b19SJacob Faibussowitsch 
221573ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
221673ff1848SBarry Smith   `PetscFree()`, every entry of `innerislist` and `outerislist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2217bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
221816621825SDmitry Karpeev 
2219a4e35b19SJacob Faibussowitsch   Developer Notes:
222020f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2221bb7acecfSBarry Smith 
222273ff1848SBarry Smith   The names are inconsistent, the hooks use `DMSubDomainHook` which is nothing like `DMCreateDomainDecomposition()` while `DMRefineHook` is used for `DMRefine()`.
222373ff1848SBarry Smith 
222473ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`,
222573ff1848SBarry Smith           `DMSubDomainHookAdd()`, `DMSubDomainHookRemove()`,`DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
222616621825SDmitry Karpeev @*/
2227ce78bad3SBarry Smith PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS *innerislist[], IS *outerislist[], DM *dmlist[])
2228d71ae5a4SJacob Faibussowitsch {
2229be081cd6SPeter Brune   DMSubDomainHookLink link;
2230be081cd6SPeter Brune   PetscInt            i, l;
223116621825SDmitry Karpeev 
223216621825SDmitry Karpeev   PetscFunctionBegin;
223316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22349371c9d4SSatish Balay   if (n) {
22354f572ea9SToby Isaac     PetscAssertPointer(n, 2);
22369371c9d4SSatish Balay     *n = 0;
22379371c9d4SSatish Balay   }
22389371c9d4SSatish Balay   if (namelist) {
22394f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
22409371c9d4SSatish Balay     *namelist = NULL;
22419371c9d4SSatish Balay   }
22429371c9d4SSatish Balay   if (innerislist) {
22434f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
22449371c9d4SSatish Balay     *innerislist = NULL;
22459371c9d4SSatish Balay   }
22469371c9d4SSatish Balay   if (outerislist) {
22474f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
22489371c9d4SSatish Balay     *outerislist = NULL;
22499371c9d4SSatish Balay   }
22509371c9d4SSatish Balay   if (dmlist) {
22514f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
22529371c9d4SSatish Balay     *dmlist = NULL;
22539371c9d4SSatish Balay   }
2254f3f0edfdSDmitry Karpeev   /*
2255f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2256f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2257f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2258f3f0edfdSDmitry Karpeev    */
22597a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
226016621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2261dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
226214a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2263f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2264be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2265be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22669566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2267be081cd6SPeter Brune         }
2268648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2269e7c4fc90SDmitry Karpeev       }
227014a18fd3SPeter Brune     }
2271bb7acecfSBarry Smith     if (n) *n = l;
227214a18fd3SPeter Brune   }
22733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2274e30e807fSPeter Brune }
2275e30e807fSPeter Brune 
2276e30e807fSPeter Brune /*@C
227773ff1848SBarry Smith   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector for subdomains created with
227873ff1848SBarry Smith   `DMCreateDomainDecomposition()`
2279e30e807fSPeter Brune 
228020f4b53cSBarry Smith   Not Collective
2281e30e807fSPeter Brune 
2282e30e807fSPeter Brune   Input Parameters:
2283bb7acecfSBarry Smith + dm     - the `DM` object
228473ff1848SBarry Smith . n      - the number of subdomains
2285e30e807fSPeter Brune - subdms - the local subdomains
2286e30e807fSPeter Brune 
2287e30e807fSPeter Brune   Output Parameters:
22886b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2289e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2290e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2291e30e807fSPeter Brune 
229220f4b53cSBarry Smith   Level: developer
229320f4b53cSBarry Smith 
2294bb7acecfSBarry Smith   Note:
2295bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2296e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2297e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2298e30e807fSPeter Brune   solution and residual data.
2299e30e807fSPeter Brune 
230073ff1848SBarry Smith   Developer Note:
2301a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2302a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2303bb7acecfSBarry Smith 
23041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2305e30e807fSPeter Brune @*/
23065d83a8b1SBarry Smith PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter *iscat[], VecScatter *oscat[], VecScatter *gscat[])
2307d71ae5a4SJacob Faibussowitsch {
2308e30e807fSPeter Brune   PetscFunctionBegin;
2309e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23104f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2311dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
23123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2313e7c4fc90SDmitry Karpeev }
2314e7c4fc90SDmitry Karpeev 
231547c6ae99SBarry Smith /*@
2316bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
231747c6ae99SBarry Smith 
231820f4b53cSBarry Smith   Collective
231947c6ae99SBarry Smith 
2320d8d19677SJose E. Roman   Input Parameters:
2321bb7acecfSBarry Smith + dm   - the `DM` object
2322bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
232347c6ae99SBarry Smith 
232447c6ae99SBarry Smith   Output Parameter:
232520f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2326ae0a1c52SMatthew G Knepley 
232720f4b53cSBarry Smith   Options Database Key:
2328412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2329412e9a14SMatthew G. Knepley 
233047c6ae99SBarry Smith   Level: developer
233147c6ae99SBarry Smith 
233220f4b53cSBarry Smith   Note:
233320f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
233420f4b53cSBarry Smith 
233573ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
233673ff1848SBarry Smith           `DMRefineHookAdd()`, `DMRefineHookRemove()`
233747c6ae99SBarry Smith @*/
2338d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2339d71ae5a4SJacob Faibussowitsch {
2340c833c3b5SJed Brown   DMRefineHookLink link;
234147c6ae99SBarry Smith 
234247c6ae99SBarry Smith   PetscFunctionBegin;
2343732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23449566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2345dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
23464057135bSMatthew G Knepley   if (*dmf) {
234743842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
23488865f1eaSKarl Rupp 
23499566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23508865f1eaSKarl Rupp 
2351644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23520598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2353656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23548865f1eaSKarl Rupp 
23559566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2356c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23571baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2358c833c3b5SJed Brown     }
2359c833c3b5SJed Brown   }
23609566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2362c833c3b5SJed Brown }
2363c833c3b5SJed Brown 
2364bb9467b5SJed Brown /*@C
2365c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2366c833c3b5SJed Brown 
236720f4b53cSBarry Smith   Logically Collective; No Fortran Support
2368c833c3b5SJed Brown 
23694165533cSJose E. Roman   Input Parameters:
2370bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2371bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2372f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
237320f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2374c833c3b5SJed Brown 
237520f4b53cSBarry Smith   Calling sequence of `refinehook`:
2376bb7acecfSBarry Smith + coarse - coarse level `DM`
2377bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2378c833c3b5SJed Brown - ctx    - optional user-defined function context
2379c833c3b5SJed Brown 
238020f4b53cSBarry Smith   Calling sequence of `interphook`:
2381bb7acecfSBarry Smith + coarse - coarse level `DM`
2382c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2383bb7acecfSBarry Smith . fine   - fine level `DM` to update
2384c833c3b5SJed Brown - ctx    - optional user-defined function context
2385c833c3b5SJed Brown 
2386c833c3b5SJed Brown   Level: advanced
2387c833c3b5SJed Brown 
2388c833c3b5SJed Brown   Notes:
2389bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2390bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2391bb7acecfSBarry Smith 
2392bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2393c833c3b5SJed Brown 
2394c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2395c833c3b5SJed Brown 
23961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2397c833c3b5SJed Brown @*/
2398a4e35b19SJacob 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)
2399d71ae5a4SJacob Faibussowitsch {
2400c833c3b5SJed Brown   DMRefineHookLink link, *p;
2401c833c3b5SJed Brown 
2402c833c3b5SJed Brown   PetscFunctionBegin;
2403c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24043d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
24053ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
24063d8e3701SJed Brown   }
24079566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2408c833c3b5SJed Brown   link->refinehook = refinehook;
2409c833c3b5SJed Brown   link->interphook = interphook;
2410c833c3b5SJed Brown   link->ctx        = ctx;
24110298fd71SBarry Smith   link->next       = NULL;
2412c833c3b5SJed Brown   *p               = link;
24133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2414c833c3b5SJed Brown }
2415c833c3b5SJed Brown 
24163d8e3701SJed Brown /*@C
2417bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2418bb7acecfSBarry Smith   a nonlinear problem to a finer grid
24193d8e3701SJed Brown 
242020f4b53cSBarry Smith   Logically Collective; No Fortran Support
24213d8e3701SJed Brown 
24224165533cSJose E. Roman   Input Parameters:
2423bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2424bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2425bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
242620f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
24273d8e3701SJed Brown 
24283d8e3701SJed Brown   Level: advanced
24293d8e3701SJed Brown 
2430bb7acecfSBarry Smith   Note:
24313d8e3701SJed Brown   This function does nothing if the hook is not in the list.
24323d8e3701SJed Brown 
24331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
24343d8e3701SJed Brown @*/
2435d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2436d71ae5a4SJacob Faibussowitsch {
24373d8e3701SJed Brown   DMRefineHookLink link, *p;
24383d8e3701SJed Brown 
24393d8e3701SJed Brown   PetscFunctionBegin;
24403d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24413d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
24423d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
24433d8e3701SJed Brown       link = *p;
24443d8e3701SJed Brown       *p   = link->next;
24459566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
24463d8e3701SJed Brown       break;
24473d8e3701SJed Brown     }
24483d8e3701SJed Brown   }
24493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24503d8e3701SJed Brown }
24513d8e3701SJed Brown 
2452c833c3b5SJed Brown /*@
2453bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2454c833c3b5SJed Brown 
2455c833c3b5SJed Brown   Collective if any hooks are
2456c833c3b5SJed Brown 
24574165533cSJose E. Roman   Input Parameters:
2458bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2459bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2460bb7acecfSBarry Smith - fine   - finer `DM` to update
2461c833c3b5SJed Brown 
2462c833c3b5SJed Brown   Level: developer
2463c833c3b5SJed Brown 
246473ff1848SBarry Smith   Developer Note:
2465bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2466bb7acecfSBarry Smith   an API with consistent terminology.
2467bb7acecfSBarry Smith 
24681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2469c833c3b5SJed Brown @*/
2470d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2471d71ae5a4SJacob Faibussowitsch {
2472c833c3b5SJed Brown   DMRefineHookLink link;
2473c833c3b5SJed Brown 
2474c833c3b5SJed Brown   PetscFunctionBegin;
2475c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24761baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24774057135bSMatthew G Knepley   }
24783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
247947c6ae99SBarry Smith }
248047c6ae99SBarry Smith 
2481eb3f98d2SBarry Smith /*@
24821f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24831f3379b2SToby Isaac 
248420f4b53cSBarry Smith   Collective
24851f3379b2SToby Isaac 
24864165533cSJose E. Roman   Input Parameters:
2487bb7acecfSBarry Smith + coarse    - coarse `DM`
2488bb7acecfSBarry Smith . fine      - fine `DM`
2489bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2490bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2491bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24921f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
24931f3379b2SToby Isaac 
24944165533cSJose E. Roman   Output Parameter:
24951f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
24961f3379b2SToby Isaac 
24971f3379b2SToby Isaac   Level: developer
24981f3379b2SToby Isaac 
2499bb7acecfSBarry Smith   Note:
2500bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
25011f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
25021f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
25031f3379b2SToby Isaac   slope-limiting reconstruction.
25041f3379b2SToby Isaac 
250573ff1848SBarry Smith   Developer Note:
2506bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2507bb7acecfSBarry Smith 
25081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
25091f3379b2SToby Isaac @*/
2510d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2511d71ae5a4SJacob Faibussowitsch {
25121f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
25131f3379b2SToby Isaac 
25141f3379b2SToby Isaac   PetscFunctionBegin;
25151f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
25161f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
25171f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
25181f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
25191f3379b2SToby Isaac 
25209566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
25211f3379b2SToby Isaac   if (interpsol) {
25229566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
25231f3379b2SToby Isaac   } else if (interp) {
25249566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
252598921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
25263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
25271f3379b2SToby Isaac }
25281f3379b2SToby Isaac 
25291f3379b2SToby Isaac /*@
2530bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2531eb3f98d2SBarry Smith 
2532eb3f98d2SBarry Smith   Not Collective
2533eb3f98d2SBarry Smith 
2534eb3f98d2SBarry Smith   Input Parameter:
2535bb7acecfSBarry Smith . dm - the `DM` object
2536eb3f98d2SBarry Smith 
2537eb3f98d2SBarry Smith   Output Parameter:
2538eb3f98d2SBarry Smith . level - number of refinements
2539eb3f98d2SBarry Smith 
2540eb3f98d2SBarry Smith   Level: developer
2541eb3f98d2SBarry Smith 
2542bb7acecfSBarry Smith   Note:
2543bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2544bb7acecfSBarry Smith 
25451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2546eb3f98d2SBarry Smith @*/
2547d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2548d71ae5a4SJacob Faibussowitsch {
2549eb3f98d2SBarry Smith   PetscFunctionBegin;
2550eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2551eb3f98d2SBarry Smith   *level = dm->levelup;
25523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2553eb3f98d2SBarry Smith }
2554eb3f98d2SBarry Smith 
2555fef3a512SBarry Smith /*@
2556bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2557fef3a512SBarry Smith 
2558fef3a512SBarry Smith   Not Collective
2559fef3a512SBarry Smith 
2560d8d19677SJose E. Roman   Input Parameters:
2561bb7acecfSBarry Smith + dm    - the `DM` object
2562fef3a512SBarry Smith - level - number of refinements
2563fef3a512SBarry Smith 
2564fef3a512SBarry Smith   Level: advanced
2565fef3a512SBarry Smith 
256695452b02SPatrick Sanan   Notes:
2567bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2568fef3a512SBarry Smith 
2569bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2570bb7acecfSBarry Smith 
25711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2572fef3a512SBarry Smith @*/
2573d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2574d71ae5a4SJacob Faibussowitsch {
2575fef3a512SBarry Smith   PetscFunctionBegin;
2576fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2577fef3a512SBarry Smith   dm->levelup = level;
25783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2579fef3a512SBarry Smith }
2580fef3a512SBarry Smith 
2581d410b0cfSMatthew G. Knepley /*@
2582bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2583d410b0cfSMatthew G. Knepley 
258420f4b53cSBarry Smith   Collective
2585d410b0cfSMatthew G. Knepley 
2586f1a722f8SMatthew G. Knepley   Input Parameters:
2587bb7acecfSBarry Smith + dm     - the `DM` object
2588d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2589d410b0cfSMatthew G. Knepley 
2590d410b0cfSMatthew G. Knepley   Output Parameter:
259120f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2592d410b0cfSMatthew G. Knepley 
2593d410b0cfSMatthew G. Knepley   Level: developer
2594d410b0cfSMatthew G. Knepley 
259520f4b53cSBarry Smith   Note:
259620f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
259720f4b53cSBarry Smith 
25981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2599d410b0cfSMatthew G. Knepley @*/
2600d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2601d71ae5a4SJacob Faibussowitsch {
2602d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2603d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2604dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2605d410b0cfSMatthew G. Knepley   if (*dme) {
2606d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
26079566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2608d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
26099566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2610d410b0cfSMatthew G. Knepley   }
26113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2612d410b0cfSMatthew G. Knepley }
2613d410b0cfSMatthew G. Knepley 
2614d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2615d71ae5a4SJacob Faibussowitsch {
2616ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2617ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26184f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2619ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
26203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2621ca3d3a14SMatthew G. Knepley }
2622ca3d3a14SMatthew G. Knepley 
2623d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2624d71ae5a4SJacob Faibussowitsch {
2625ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2626ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26274f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2628ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
26293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2630ca3d3a14SMatthew G. Knepley }
2631ca3d3a14SMatthew G. Knepley 
2632ca3d3a14SMatthew G. Knepley /*@
2633bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2634ca3d3a14SMatthew G. Knepley 
2635ca3d3a14SMatthew G. Knepley   Input Parameter:
263620f4b53cSBarry Smith . dm - The `DM`
2637ca3d3a14SMatthew G. Knepley 
2638ca3d3a14SMatthew G. Knepley   Output Parameter:
263920f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2640ca3d3a14SMatthew G. Knepley 
2641ca3d3a14SMatthew G. Knepley   Level: developer
2642ca3d3a14SMatthew G. Knepley 
26431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2644ca3d3a14SMatthew G. Knepley @*/
2645d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2646d71ae5a4SJacob Faibussowitsch {
2647ca3d3a14SMatthew G. Knepley   Vec tv;
2648ca3d3a14SMatthew G. Knepley 
2649ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2650ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26514f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
26529566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2653ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2655ca3d3a14SMatthew G. Knepley }
2656ca3d3a14SMatthew G. Knepley 
2657d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2658d71ae5a4SJacob Faibussowitsch {
2659ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2660ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2661ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2662ca3d3a14SMatthew G. Knepley 
2663ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26649566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26659566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26669566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26679566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26689566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26699566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26709566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26719566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2672ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26739566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2674ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2675ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2676ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26779566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2678ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26799566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26809566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2681ca3d3a14SMatthew G. Knepley     }
2682ca3d3a14SMatthew G. Knepley   }
26839566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26849566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26859566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2686ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2687ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26889566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2689ca3d3a14SMatthew G. Knepley       if (dof) {
2690ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2691ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2692ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2693ca3d3a14SMatthew G. Knepley 
2694ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26959566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26969566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26979566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2698ca3d3a14SMatthew G. Knepley       }
2699ca3d3a14SMatthew G. Knepley     }
2700ca3d3a14SMatthew G. Knepley   }
27019566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
27023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2703ca3d3a14SMatthew G. Knepley }
2704ca3d3a14SMatthew G. Knepley 
2705d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2706d71ae5a4SJacob Faibussowitsch {
2707ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2708ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2709ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2710ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2711ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2712ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2713ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
27149566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
27153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2716ca3d3a14SMatthew G. Knepley }
2717ca3d3a14SMatthew G. Knepley 
2718bb9467b5SJed Brown /*@C
2719bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2720baf369e7SPeter Brune 
272120f4b53cSBarry Smith   Logically Collective
2722baf369e7SPeter Brune 
27234165533cSJose E. Roman   Input Parameters:
2724bb7acecfSBarry Smith + dm        - the `DM`
2725bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2726bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
272720f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2728baf369e7SPeter Brune 
272920f4b53cSBarry Smith   Calling sequence of `beginhook`:
2730a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2731baf369e7SPeter Brune . g    - global vector
2732baf369e7SPeter Brune . mode - mode
2733baf369e7SPeter Brune . l    - local vector
2734baf369e7SPeter Brune - ctx  - optional user-defined function context
2735baf369e7SPeter Brune 
273620f4b53cSBarry Smith   Calling sequence of `endhook`:
2737a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2738a4e35b19SJacob Faibussowitsch . g    - global vector
2739a4e35b19SJacob Faibussowitsch . mode - mode
2740a4e35b19SJacob Faibussowitsch . l    - local vector
2741baf369e7SPeter Brune - ctx  - optional user-defined function context
2742baf369e7SPeter Brune 
2743baf369e7SPeter Brune   Level: advanced
2744baf369e7SPeter Brune 
2745bb7acecfSBarry Smith   Note:
2746bb7acecfSBarry 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.
2747bb7acecfSBarry Smith 
27481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2749baf369e7SPeter Brune @*/
2750a4e35b19SJacob 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)
2751d71ae5a4SJacob Faibussowitsch {
2752baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2753baf369e7SPeter Brune 
2754baf369e7SPeter Brune   PetscFunctionBegin;
2755baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2756baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27579566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2758baf369e7SPeter Brune   link->beginhook = beginhook;
2759baf369e7SPeter Brune   link->endhook   = endhook;
2760baf369e7SPeter Brune   link->ctx       = ctx;
27610298fd71SBarry Smith   link->next      = NULL;
2762baf369e7SPeter Brune   *p              = link;
27633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2764baf369e7SPeter Brune }
2765baf369e7SPeter Brune 
2766d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2767d71ae5a4SJacob Faibussowitsch {
27684c274da1SToby Isaac   Mat          cMat;
276979769bd5SJed Brown   Vec          cVec, cBias;
27704c274da1SToby Isaac   PetscSection section, cSec;
27714c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27724c274da1SToby Isaac 
27734c274da1SToby Isaac   PetscFunctionBegin;
2774a4e35b19SJacob Faibussowitsch   (void)g;
2775a4e35b19SJacob Faibussowitsch   (void)ctx;
27764c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27779566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27784c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27795db9a05bSToby Isaac     PetscInt nRows;
27805db9a05bSToby Isaac 
27819566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27823ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27839566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27849566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27859566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27869566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27879566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27884c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27899566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27904c274da1SToby Isaac       if (dof) {
27914c274da1SToby Isaac         PetscScalar *vals;
27929566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27939566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27944c274da1SToby Isaac       }
27954c274da1SToby Isaac     }
27969566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27974c274da1SToby Isaac   }
27983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27994c274da1SToby Isaac }
28004c274da1SToby Isaac 
280147c6ae99SBarry Smith /*@
280201729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
280301729b5cSPatrick Sanan 
280420f4b53cSBarry Smith   Neighbor-wise Collective
280501729b5cSPatrick Sanan 
280601729b5cSPatrick Sanan   Input Parameters:
2807bb7acecfSBarry Smith + dm   - the `DM` object
280801729b5cSPatrick Sanan . g    - the global vector
2809bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
281001729b5cSPatrick Sanan - l    - the local vector
281101729b5cSPatrick Sanan 
281220f4b53cSBarry Smith   Level: beginner
281320f4b53cSBarry Smith 
281401729b5cSPatrick Sanan   Notes:
2815bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2816bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2817bb7acecfSBarry Smith 
2818bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
281901729b5cSPatrick Sanan 
28201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
282160225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2822bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
282301729b5cSPatrick Sanan @*/
2824d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2825d71ae5a4SJacob Faibussowitsch {
282601729b5cSPatrick Sanan   PetscFunctionBegin;
28279566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
28289566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
28293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
283001729b5cSPatrick Sanan }
283101729b5cSPatrick Sanan 
283201729b5cSPatrick Sanan /*@
283347c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
283447c6ae99SBarry Smith 
283520f4b53cSBarry Smith   Neighbor-wise Collective
283647c6ae99SBarry Smith 
283747c6ae99SBarry Smith   Input Parameters:
2838bb7acecfSBarry Smith + dm   - the `DM` object
283947c6ae99SBarry Smith . g    - the global vector
2840bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
284147c6ae99SBarry Smith - l    - the local vector
284247c6ae99SBarry Smith 
284301729b5cSPatrick Sanan   Level: intermediate
284447c6ae99SBarry Smith 
2845bb7acecfSBarry Smith   Notes:
2846bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2847bb7acecfSBarry Smith 
2848bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2849bb7acecfSBarry Smith 
2850bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2851bb7acecfSBarry Smith 
2852bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2853bb7acecfSBarry Smith 
285460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
285547c6ae99SBarry Smith @*/
2856d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2857d71ae5a4SJacob Faibussowitsch {
28587128ae9fSMatthew G Knepley   PetscSF                 sf;
2859baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
286047c6ae99SBarry Smith 
286147c6ae99SBarry Smith   PetscFunctionBegin;
2862171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2863baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28641baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2865baf369e7SPeter Brune   }
28669566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28677128ae9fSMatthew G Knepley   if (sf) {
2868ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2869ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2870d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28717128ae9fSMatthew G Knepley 
28727a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28739566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28749566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28759566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28769566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28779566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28787128ae9fSMatthew G Knepley   } else {
2879fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28807128ae9fSMatthew G Knepley   }
28813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
288247c6ae99SBarry Smith }
288347c6ae99SBarry Smith 
288447c6ae99SBarry Smith /*@
288547c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
288647c6ae99SBarry Smith 
288720f4b53cSBarry Smith   Neighbor-wise Collective
288847c6ae99SBarry Smith 
288947c6ae99SBarry Smith   Input Parameters:
2890bb7acecfSBarry Smith + dm   - the `DM` object
289147c6ae99SBarry Smith . g    - the global vector
2892bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
289347c6ae99SBarry Smith - l    - the local vector
289447c6ae99SBarry Smith 
289501729b5cSPatrick Sanan   Level: intermediate
289647c6ae99SBarry Smith 
2897bb7acecfSBarry Smith   Note:
2898bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2899bb7acecfSBarry Smith 
290060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
290147c6ae99SBarry Smith @*/
2902d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2903d71ae5a4SJacob Faibussowitsch {
29047128ae9fSMatthew G Knepley   PetscSF                 sf;
2905ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2906ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2907ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2908baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2909d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
291047c6ae99SBarry Smith 
291147c6ae99SBarry Smith   PetscFunctionBegin;
2912171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29139566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
29149566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
29157128ae9fSMatthew G Knepley   if (sf) {
29167a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
29177128ae9fSMatthew G Knepley 
29189566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
29199566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
29209566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
29219566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
29229566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
29239566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
29247128ae9fSMatthew G Knepley   } else {
2925fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
29267128ae9fSMatthew G Knepley   }
29279566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2928baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
29299566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2930baf369e7SPeter Brune   }
29313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
293247c6ae99SBarry Smith }
293347c6ae99SBarry Smith 
2934d4d07f1eSToby Isaac /*@C
2935d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2936d4d07f1eSToby Isaac 
293720f4b53cSBarry Smith   Logically Collective
2938d4d07f1eSToby Isaac 
29394165533cSJose E. Roman   Input Parameters:
2940bb7acecfSBarry Smith + dm        - the `DM`
2941bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2942bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
294320f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2944d4d07f1eSToby Isaac 
294520f4b53cSBarry Smith   Calling sequence of `beginhook`:
2946a4e35b19SJacob Faibussowitsch + global - global `DM`
2947d4d07f1eSToby Isaac . l      - local vector
2948d4d07f1eSToby Isaac . mode   - mode
2949d4d07f1eSToby Isaac . g      - global vector
2950d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2951d4d07f1eSToby Isaac 
295220f4b53cSBarry Smith   Calling sequence of `endhook`:
2953bb7acecfSBarry Smith + global - global `DM`
2954d4d07f1eSToby Isaac . l      - local vector
2955d4d07f1eSToby Isaac . mode   - mode
2956d4d07f1eSToby Isaac . g      - global vector
2957d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2958d4d07f1eSToby Isaac 
2959d4d07f1eSToby Isaac   Level: advanced
2960d4d07f1eSToby Isaac 
29611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2962d4d07f1eSToby Isaac @*/
2963a4e35b19SJacob 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)
2964d71ae5a4SJacob Faibussowitsch {
2965d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2966d4d07f1eSToby Isaac 
2967d4d07f1eSToby Isaac   PetscFunctionBegin;
2968d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2969d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29709566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2971d4d07f1eSToby Isaac   link->beginhook = beginhook;
2972d4d07f1eSToby Isaac   link->endhook   = endhook;
2973d4d07f1eSToby Isaac   link->ctx       = ctx;
2974d4d07f1eSToby Isaac   link->next      = NULL;
2975d4d07f1eSToby Isaac   *p              = link;
29763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2977d4d07f1eSToby Isaac }
2978d4d07f1eSToby Isaac 
2979d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2980d71ae5a4SJacob Faibussowitsch {
29814c274da1SToby Isaac   PetscFunctionBegin;
2982a4e35b19SJacob Faibussowitsch   (void)g;
2983a4e35b19SJacob Faibussowitsch   (void)ctx;
29844c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29852b6b7d09SToby Isaac   if (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES) {
29862b6b7d09SToby Isaac     Mat          cMat;
29872b6b7d09SToby Isaac     Vec          cVec;
29885db9a05bSToby Isaac     PetscInt     nRows;
29892b6b7d09SToby Isaac     PetscSection section, cSec;
29902b6b7d09SToby Isaac     PetscInt     pStart, pEnd, p, dof;
29912b6b7d09SToby Isaac 
29922b6b7d09SToby Isaac     PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29932b6b7d09SToby Isaac     if (!cMat) PetscFunctionReturn(PETSC_SUCCESS);
29945db9a05bSToby Isaac 
29959566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29963ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29979566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29989566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29999566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
30004c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
30019566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
30024c274da1SToby Isaac       if (dof) {
30034c274da1SToby Isaac         PetscInt     d;
30044c274da1SToby Isaac         PetscScalar *vals;
30059566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
30069566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
30074c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
30084c274da1SToby Isaac          * we just extracted */
3009ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
30104c274da1SToby Isaac       }
30114c274da1SToby Isaac     }
30129566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
30139566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
30144c274da1SToby Isaac   }
30153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30164c274da1SToby Isaac }
301701729b5cSPatrick Sanan /*@
301801729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
301901729b5cSPatrick Sanan 
302020f4b53cSBarry Smith   Neighbor-wise Collective
302101729b5cSPatrick Sanan 
302201729b5cSPatrick Sanan   Input Parameters:
3023bb7acecfSBarry Smith + dm   - the `DM` object
302401729b5cSPatrick Sanan . l    - the local vector
3025bb7acecfSBarry 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.
302601729b5cSPatrick Sanan - g    - the global vector
302701729b5cSPatrick Sanan 
302820f4b53cSBarry Smith   Level: beginner
302920f4b53cSBarry Smith 
303001729b5cSPatrick Sanan   Notes:
303101729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
3032bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
303301729b5cSPatrick Sanan 
3034bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3035bb7acecfSBarry Smith 
3036bb7acecfSBarry 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.
3037bb7acecfSBarry Smith 
3038bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
303901729b5cSPatrick Sanan 
30401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
304101729b5cSPatrick Sanan @*/
3042d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
3043d71ae5a4SJacob Faibussowitsch {
304401729b5cSPatrick Sanan   PetscFunctionBegin;
30459566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
30469566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
30473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
304801729b5cSPatrick Sanan }
30494c274da1SToby Isaac 
305047c6ae99SBarry Smith /*@
305101729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
30529a42bb27SBarry Smith 
305320f4b53cSBarry Smith   Neighbor-wise Collective
30549a42bb27SBarry Smith 
30559a42bb27SBarry Smith   Input Parameters:
3056bb7acecfSBarry Smith + dm   - the `DM` object
3057f6813fd5SJed Brown . l    - the local vector
3058aa624791SPierre 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.
30591eb28f2eSBarry Smith - g    - the global vector
30609a42bb27SBarry Smith 
306120f4b53cSBarry Smith   Level: intermediate
306220f4b53cSBarry Smith 
306395452b02SPatrick Sanan   Notes:
3064bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3065bb7acecfSBarry Smith 
3066bb7acecfSBarry 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.
3067bb7acecfSBarry Smith 
3068bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3069bb7acecfSBarry Smith 
3070bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3071bb7acecfSBarry Smith 
3072bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30739a42bb27SBarry Smith 
30741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30759a42bb27SBarry Smith @*/
3076d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3077d71ae5a4SJacob Faibussowitsch {
30787128ae9fSMatthew G Knepley   PetscSF                 sf;
307984330215SMatthew G. Knepley   PetscSection            s, gs;
3080d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3081ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3082ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3083ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3084fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3085d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30869a42bb27SBarry Smith 
30879a42bb27SBarry Smith   PetscFunctionBegin;
3088171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3089d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30901baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3091d4d07f1eSToby Isaac   }
30929566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30939566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30949566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30957128ae9fSMatthew G Knepley   switch (mode) {
30967128ae9fSMatthew G Knepley   case INSERT_VALUES:
30977128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3098d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3099d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3100d71ae5a4SJacob Faibussowitsch     break;
31017128ae9fSMatthew G Knepley   case ADD_VALUES:
31027128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3103d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3104d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3105d71ae5a4SJacob Faibussowitsch     break;
3106d71ae5a4SJacob Faibussowitsch   default:
3107d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31087128ae9fSMatthew G Knepley   }
3109ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
31109566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3111ca3d3a14SMatthew G. Knepley     if (transform) {
31129566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31139566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
31149566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
31159566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3116fa88e482SJed Brown     } else if (isInsert) {
31179566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3118fa88e482SJed Brown     } else {
31199566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3120fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3121ca3d3a14SMatthew G. Knepley     }
3122fa88e482SJed Brown     if (s && isInsert) {
31239566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3124fa88e482SJed Brown     } else {
31259566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3126fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3127fa88e482SJed Brown     }
3128ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
31299566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
313084330215SMatthew G. Knepley     } else if (s && isInsert) {
313184330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
313284330215SMatthew G. Knepley 
31339566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
31349566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
31359566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
313684330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3137b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
313884330215SMatthew G. Knepley 
31399566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
31409566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
31419566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
31429566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
31439566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
31449566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3145b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
314603442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
31477a8be351SBarry 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);
3148b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3149b3b16f48SMatthew G. Knepley         if (!gcdof) {
315084330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3151b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3152b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
315384330215SMatthew G. Knepley           const PetscInt *cdofs;
315484330215SMatthew G. Knepley           PetscInt        cind = 0;
315584330215SMatthew G. Knepley 
31569566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3157b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31589371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31599371c9d4SSatish Balay               ++cind;
31609371c9d4SSatish Balay               continue;
31619371c9d4SSatish Balay             }
3162b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
316384330215SMatthew G. Knepley           }
31647a8be351SBarry 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);
316584330215SMatthew G. Knepley       }
3166ca3d3a14SMatthew G. Knepley     }
3167fa88e482SJed Brown     if (g_inplace) {
31689566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3169fa88e482SJed Brown     } else {
31709566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3171fa88e482SJed Brown     }
3172ca3d3a14SMatthew G. Knepley     if (transform) {
31739566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31749566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3175fa88e482SJed Brown     } else if (l_inplace) {
31769566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3177ca3d3a14SMatthew G. Knepley     } else {
31789566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3179ca3d3a14SMatthew G. Knepley     }
31807128ae9fSMatthew G Knepley   } else {
3181fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalbegin, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
31827128ae9fSMatthew G Knepley   }
31833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31849a42bb27SBarry Smith }
31859a42bb27SBarry Smith 
31869a42bb27SBarry Smith /*@
31879a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
318847c6ae99SBarry Smith 
318920f4b53cSBarry Smith   Neighbor-wise Collective
319047c6ae99SBarry Smith 
319147c6ae99SBarry Smith   Input Parameters:
3192bb7acecfSBarry Smith + dm   - the `DM` object
3193f6813fd5SJed Brown . l    - the local vector
3194bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3195f6813fd5SJed Brown - g    - the global vector
319647c6ae99SBarry Smith 
319701729b5cSPatrick Sanan   Level: intermediate
319847c6ae99SBarry Smith 
3199bb7acecfSBarry Smith   Note:
3200bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3201bb7acecfSBarry Smith 
320260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
320347c6ae99SBarry Smith @*/
3204d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3205d71ae5a4SJacob Faibussowitsch {
32067128ae9fSMatthew G Knepley   PetscSF                 sf;
320784330215SMatthew G. Knepley   PetscSection            s;
3208d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3209ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
321047c6ae99SBarry Smith 
321147c6ae99SBarry Smith   PetscFunctionBegin;
3212171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32139566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
32149566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
32157128ae9fSMatthew G Knepley   switch (mode) {
32167128ae9fSMatthew G Knepley   case INSERT_VALUES:
3217d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3218d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3219d71ae5a4SJacob Faibussowitsch     break;
32207128ae9fSMatthew G Knepley   case ADD_VALUES:
3221d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3222d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3223d71ae5a4SJacob Faibussowitsch     break;
3224d71ae5a4SJacob Faibussowitsch   default:
3225d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
32267128ae9fSMatthew G Knepley   }
322784330215SMatthew G. Knepley   if (sf && !isInsert) {
3228ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3229ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3230ca3d3a14SMatthew G. Knepley     Vec                tmpl;
323184330215SMatthew G. Knepley 
32329566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3233ca3d3a14SMatthew G. Knepley     if (transform) {
32349566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
32359566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3236ca3d3a14SMatthew G. Knepley     } else {
32379566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3238ca3d3a14SMatthew G. Knepley     }
32399566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
32409566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3241ca3d3a14SMatthew G. Knepley     if (transform) {
32429566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
32439566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3244ca3d3a14SMatthew G. Knepley     } else {
32459566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3246ca3d3a14SMatthew G. Knepley     }
32479566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
324884330215SMatthew G. Knepley   } else if (s && isInsert) {
32497128ae9fSMatthew G Knepley   } else {
3250fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalend, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
32517128ae9fSMatthew G Knepley   }
3252d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32539566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3254d4d07f1eSToby Isaac   }
32553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
325647c6ae99SBarry Smith }
325747c6ae99SBarry Smith 
3258f089877aSRichard Tran Mills /*@
3259a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3260a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3261a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3262f089877aSRichard Tran Mills 
326320f4b53cSBarry Smith   Neighbor-wise Collective
3264f089877aSRichard Tran Mills 
3265f089877aSRichard Tran Mills   Input Parameters:
3266bb7acecfSBarry Smith + dm   - the `DM` object
3267bc0a1609SRichard Tran Mills . g    - the original local vector
3268bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3269f089877aSRichard Tran Mills 
3270bc0a1609SRichard Tran Mills   Output Parameter:
3271bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3272f089877aSRichard Tran Mills 
3273f089877aSRichard Tran Mills   Level: intermediate
3274f089877aSRichard Tran Mills 
327573ff1848SBarry Smith   Note:
3276a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3277a4e35b19SJacob Faibussowitsch 
327860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3279f089877aSRichard Tran Mills @*/
3280d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3281d71ae5a4SJacob Faibussowitsch {
3282f089877aSRichard Tran Mills   PetscFunctionBegin;
3283f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32849f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32859f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32869f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3288f089877aSRichard Tran Mills }
3289f089877aSRichard Tran Mills 
3290f089877aSRichard Tran Mills /*@
3291bb7acecfSBarry Smith   DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3292bb7acecfSBarry Smith   points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3293f089877aSRichard Tran Mills 
329420f4b53cSBarry Smith   Neighbor-wise Collective
3295f089877aSRichard Tran Mills 
3296f089877aSRichard Tran Mills   Input Parameters:
329760225df5SJacob Faibussowitsch + dm   - the `DM` object
3298bc0a1609SRichard Tran Mills . g    - the original local vector
3299bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3300f089877aSRichard Tran Mills 
3301bc0a1609SRichard Tran Mills   Output Parameter:
3302bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3303f089877aSRichard Tran Mills 
3304f089877aSRichard Tran Mills   Level: intermediate
3305f089877aSRichard Tran Mills 
330660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3307f089877aSRichard Tran Mills @*/
3308d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3309d71ae5a4SJacob Faibussowitsch {
3310f089877aSRichard Tran Mills   PetscFunctionBegin;
3311f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33129f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
33139f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
33149f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
33153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3316f089877aSRichard Tran Mills }
3317f089877aSRichard Tran Mills 
331847c6ae99SBarry Smith /*@
3319bb7acecfSBarry Smith   DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
332047c6ae99SBarry Smith 
332120f4b53cSBarry Smith   Collective
332247c6ae99SBarry Smith 
3323d8d19677SJose E. Roman   Input Parameters:
3324bb7acecfSBarry Smith + dm   - the `DM` object
332520f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
332647c6ae99SBarry Smith 
332747c6ae99SBarry Smith   Output Parameter:
3328bb7acecfSBarry Smith . dmc - the coarsened `DM`
332947c6ae99SBarry Smith 
333047c6ae99SBarry Smith   Level: developer
333147c6ae99SBarry Smith 
333273ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
333373ff1848SBarry Smith           `DMCoarsenHookAdd()`, `DMCoarsenHookRemove()`
333447c6ae99SBarry Smith @*/
3335d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3336d71ae5a4SJacob Faibussowitsch {
3337b17ce1afSJed Brown   DMCoarsenHookLink link;
333847c6ae99SBarry Smith 
333947c6ae99SBarry Smith   PetscFunctionBegin;
3340171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33419566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3342dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3343b9d85ea2SLisandro Dalcin   if (*dmc) {
3344a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
33459566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
334643842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
33479566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3348644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
33490598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3350656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
33519566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3352b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33539566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3354b17ce1afSJed Brown     }
3355b9d85ea2SLisandro Dalcin   }
33569566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33577a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3359b17ce1afSJed Brown }
3360b17ce1afSJed Brown 
3361bb9467b5SJed Brown /*@C
3362b17ce1afSJed Brown   DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3363b17ce1afSJed Brown 
336420f4b53cSBarry Smith   Logically Collective; No Fortran Support
3365b17ce1afSJed Brown 
33664165533cSJose E. Roman   Input Parameters:
3367bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3368b17ce1afSJed Brown . coarsenhook  - function to run when setting up a coarser level
3369bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
337020f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3371b17ce1afSJed Brown 
337220f4b53cSBarry Smith   Calling sequence of `coarsenhook`:
3373bb7acecfSBarry Smith + fine   - fine level `DM`
3374bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3375b17ce1afSJed Brown - ctx    - optional user-defined function context
3376b17ce1afSJed Brown 
337720f4b53cSBarry Smith   Calling sequence of `restricthook`:
3378bb7acecfSBarry Smith + fine      - fine level `DM`
3379bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3380c833c3b5SJed Brown . rscale    - scaling vector for restriction
3381c833c3b5SJed Brown . inject    - matrix restricting by injection
3382b17ce1afSJed Brown . coarse    - coarse level DM to update
3383b17ce1afSJed Brown - ctx       - optional user-defined function context
3384b17ce1afSJed Brown 
3385b17ce1afSJed Brown   Level: advanced
3386b17ce1afSJed Brown 
3387b17ce1afSJed Brown   Notes:
3388bb7acecfSBarry 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`.
3389b17ce1afSJed Brown 
3390b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3391b17ce1afSJed Brown 
3392b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3393bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3394b17ce1afSJed Brown 
3395bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3396bb7acecfSBarry Smith 
33971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3398b17ce1afSJed Brown @*/
3399a4e35b19SJacob 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)
3400d71ae5a4SJacob Faibussowitsch {
3401b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3402b17ce1afSJed Brown 
3403b17ce1afSJed Brown   PetscFunctionBegin;
3404b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
34051e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
34063ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
34071e3d8eccSJed Brown   }
34089566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3409b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3410b17ce1afSJed Brown   link->restricthook = restricthook;
3411b17ce1afSJed Brown   link->ctx          = ctx;
34120298fd71SBarry Smith   link->next         = NULL;
3413b17ce1afSJed Brown   *p                 = link;
34143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3415b17ce1afSJed Brown }
3416b17ce1afSJed Brown 
3417dc822a44SJed Brown /*@C
3418bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3419dc822a44SJed Brown 
342020f4b53cSBarry Smith   Logically Collective; No Fortran Support
3421dc822a44SJed Brown 
34224165533cSJose E. Roman   Input Parameters:
3423bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3424dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3425bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
342620f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3427dc822a44SJed Brown 
3428dc822a44SJed Brown   Level: advanced
3429dc822a44SJed Brown 
343073ff1848SBarry Smith   Notes:
343173ff1848SBarry Smith   This function does nothing if the `coarsenhook` is not in the list.
343273ff1848SBarry Smith 
343373ff1848SBarry Smith   See `DMCoarsenHookAdd()` for the calling sequence of `coarsenhook` and `restricthook`
3434dc822a44SJed Brown 
34351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3436dc822a44SJed Brown @*/
3437d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3438d71ae5a4SJacob Faibussowitsch {
3439dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3440dc822a44SJed Brown 
3441dc822a44SJed Brown   PetscFunctionBegin;
3442dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3443dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3444dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3445dc822a44SJed Brown       link = *p;
3446dc822a44SJed Brown       *p   = link->next;
34479566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3448dc822a44SJed Brown       break;
3449dc822a44SJed Brown     }
3450dc822a44SJed Brown   }
34513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3452dc822a44SJed Brown }
3453dc822a44SJed Brown 
3454b17ce1afSJed Brown /*@
3455bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3456b17ce1afSJed Brown 
3457b17ce1afSJed Brown   Collective if any hooks are
3458b17ce1afSJed Brown 
34594165533cSJose E. Roman   Input Parameters:
3460bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3461bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3462e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3463bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
346420f4b53cSBarry Smith - coarse  - coarser `DM` to update
3465b17ce1afSJed Brown 
3466b17ce1afSJed Brown   Level: developer
3467b17ce1afSJed Brown 
346873ff1848SBarry Smith   Developer Note:
3469bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3470bb7acecfSBarry Smith 
34711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3472b17ce1afSJed Brown @*/
3473d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3474d71ae5a4SJacob Faibussowitsch {
3475b17ce1afSJed Brown   DMCoarsenHookLink link;
3476b17ce1afSJed Brown 
3477b17ce1afSJed Brown   PetscFunctionBegin;
3478b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34791baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3480b17ce1afSJed Brown   }
34813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
348247c6ae99SBarry Smith }
348347c6ae99SBarry Smith 
3484bb9467b5SJed Brown /*@C
348573ff1848SBarry Smith   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
34865dbd56e3SPeter Brune 
348720f4b53cSBarry Smith   Logically Collective; No Fortran Support
34885dbd56e3SPeter Brune 
34894165533cSJose E. Roman   Input Parameters:
3490bb7acecfSBarry Smith + global       - global `DM`
3491bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
34925dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
349320f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34945dbd56e3SPeter Brune 
349520f4b53cSBarry Smith   Calling sequence of `ddhook`:
3496bb7acecfSBarry Smith + global - global `DM`
349773ff1848SBarry Smith . block  - subdomain `DM`
3498ec4806b8SPeter Brune - ctx    - optional user-defined function context
3499ec4806b8SPeter Brune 
350020f4b53cSBarry Smith   Calling sequence of `restricthook`:
3501bb7acecfSBarry Smith + global - global `DM`
350273ff1848SBarry Smith . out    - scatter to the outer (with ghost and overlap points) sub vector
350373ff1848SBarry Smith . in     - scatter to sub vector values only owned locally
350473ff1848SBarry Smith . block  - subdomain `DM`
35055dbd56e3SPeter Brune - ctx    - optional user-defined function context
35065dbd56e3SPeter Brune 
35075dbd56e3SPeter Brune   Level: advanced
35085dbd56e3SPeter Brune 
35095dbd56e3SPeter Brune   Notes:
351073ff1848SBarry Smith   This function can be used if auxiliary data needs to be set up on subdomain `DM`s.
35115dbd56e3SPeter Brune 
35125dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
35135dbd56e3SPeter Brune 
35145dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3515bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
35165dbd56e3SPeter Brune 
351773ff1848SBarry Smith   Developer Note:
351873ff1848SBarry Smith   It is unclear what "block solve" means within the definition of `restricthook`
351973ff1848SBarry Smith 
352073ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`, `DMCreateDomainDecomposition()`
35215dbd56e3SPeter Brune @*/
3522a4e35b19SJacob 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)
3523d71ae5a4SJacob Faibussowitsch {
3524be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
35255dbd56e3SPeter Brune 
35265dbd56e3SPeter Brune   PetscFunctionBegin;
35275dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3528b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
35293ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3530b3a6b972SJed Brown   }
35319566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
35325dbd56e3SPeter Brune   link->restricthook = restricthook;
3533be081cd6SPeter Brune   link->ddhook       = ddhook;
35345dbd56e3SPeter Brune   link->ctx          = ctx;
35350298fd71SBarry Smith   link->next         = NULL;
35365dbd56e3SPeter Brune   *p                 = link;
35373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35385dbd56e3SPeter Brune }
35395dbd56e3SPeter Brune 
3540b3a6b972SJed Brown /*@C
354173ff1848SBarry Smith   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
3542b3a6b972SJed Brown 
354320f4b53cSBarry Smith   Logically Collective; No Fortran Support
3544b3a6b972SJed Brown 
35454165533cSJose E. Roman   Input Parameters:
3546bb7acecfSBarry Smith + global       - global `DM`
3547bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3548b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
354920f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3550b3a6b972SJed Brown 
3551b3a6b972SJed Brown   Level: advanced
3552b3a6b972SJed Brown 
355373ff1848SBarry Smith   Note:
355473ff1848SBarry Smith   See `DMSubDomainHookAdd()` for the calling sequences of `ddhook` and `restricthook`
355573ff1848SBarry Smith 
355673ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`,
355773ff1848SBarry Smith           `DMCreateDomainDecomposition()`
3558b3a6b972SJed Brown @*/
3559d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3560d71ae5a4SJacob Faibussowitsch {
3561b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3562b3a6b972SJed Brown 
3563b3a6b972SJed Brown   PetscFunctionBegin;
3564b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3565b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3566b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3567b3a6b972SJed Brown       link = *p;
3568b3a6b972SJed Brown       *p   = link->next;
35699566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3570b3a6b972SJed Brown       break;
3571b3a6b972SJed Brown     }
3572b3a6b972SJed Brown   }
35733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3574b3a6b972SJed Brown }
3575b3a6b972SJed Brown 
35765dbd56e3SPeter Brune /*@
357773ff1848SBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a subdomain `DM` by running hooks registered by `DMSubDomainHookAdd()`
35785dbd56e3SPeter Brune 
35795dbd56e3SPeter Brune   Collective if any hooks are
35805dbd56e3SPeter Brune 
35814165533cSJose E. Roman   Input Parameters:
3582a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3583a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3584a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3585a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
35865dbd56e3SPeter Brune 
35875dbd56e3SPeter Brune   Level: developer
35885dbd56e3SPeter Brune 
358973ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMCreateDomainDecomposition()`
35905dbd56e3SPeter Brune @*/
3591d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3592d71ae5a4SJacob Faibussowitsch {
3593be081cd6SPeter Brune   DMSubDomainHookLink link;
35945dbd56e3SPeter Brune 
35955dbd56e3SPeter Brune   PetscFunctionBegin;
3596be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35971baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35985dbd56e3SPeter Brune   }
35993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36005dbd56e3SPeter Brune }
36015dbd56e3SPeter Brune 
36025fe1f584SPeter Brune /*@
3603bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
36045fe1f584SPeter Brune 
36055fe1f584SPeter Brune   Not Collective
36065fe1f584SPeter Brune 
36075fe1f584SPeter Brune   Input Parameter:
3608bb7acecfSBarry Smith . dm - the `DM` object
36095fe1f584SPeter Brune 
36105fe1f584SPeter Brune   Output Parameter:
36116a7d9d85SPeter Brune . level - number of coarsenings
36125fe1f584SPeter Brune 
36135fe1f584SPeter Brune   Level: developer
36145fe1f584SPeter Brune 
36151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36165fe1f584SPeter Brune @*/
3617d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3618d71ae5a4SJacob Faibussowitsch {
36195fe1f584SPeter Brune   PetscFunctionBegin;
36205fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36214f572ea9SToby Isaac   PetscAssertPointer(level, 2);
36225fe1f584SPeter Brune   *level = dm->leveldown;
36233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36245fe1f584SPeter Brune }
36255fe1f584SPeter Brune 
36269a64c4a8SMatthew G. Knepley /*@
3627bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
36289a64c4a8SMatthew G. Knepley 
362920f4b53cSBarry Smith   Collective
36309a64c4a8SMatthew G. Knepley 
36319a64c4a8SMatthew G. Knepley   Input Parameters:
3632bb7acecfSBarry Smith + dm    - the `DM` object
36339a64c4a8SMatthew G. Knepley - level - number of coarsenings
36349a64c4a8SMatthew G. Knepley 
36359a64c4a8SMatthew G. Knepley   Level: developer
36369a64c4a8SMatthew G. Knepley 
3637bb7acecfSBarry Smith   Note:
3638bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3639bb7acecfSBarry Smith 
364042747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36419a64c4a8SMatthew G. Knepley @*/
3642d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3643d71ae5a4SJacob Faibussowitsch {
36449a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
36459a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36469a64c4a8SMatthew G. Knepley   dm->leveldown = level;
36473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36489a64c4a8SMatthew G. Knepley }
36499a64c4a8SMatthew G. Knepley 
3650cc4c1da9SBarry Smith /*@
3651bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
365247c6ae99SBarry Smith 
365320f4b53cSBarry Smith   Collective
365447c6ae99SBarry Smith 
3655d8d19677SJose E. Roman   Input Parameters:
3656bb7acecfSBarry Smith + dm      - the `DM` object
365747c6ae99SBarry Smith - nlevels - the number of levels of refinement
365847c6ae99SBarry Smith 
365947c6ae99SBarry Smith   Output Parameter:
3660bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
366147c6ae99SBarry Smith 
366247c6ae99SBarry Smith   Level: developer
366347c6ae99SBarry Smith 
36641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
366547c6ae99SBarry Smith @*/
3666d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3667d71ae5a4SJacob Faibussowitsch {
366847c6ae99SBarry Smith   PetscFunctionBegin;
3669171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36707a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36713ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36724f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
3673213acdd3SPierre Jolivet   if (dm->ops->refine && !dm->ops->refinehierarchy) {
367447c6ae99SBarry Smith     PetscInt i;
367547c6ae99SBarry Smith 
36769566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
367748a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3678213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
36793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
368047c6ae99SBarry Smith }
368147c6ae99SBarry Smith 
3682cc4c1da9SBarry Smith /*@
3683bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
368447c6ae99SBarry Smith 
368520f4b53cSBarry Smith   Collective
368647c6ae99SBarry Smith 
3687d8d19677SJose E. Roman   Input Parameters:
3688bb7acecfSBarry Smith + dm      - the `DM` object
368947c6ae99SBarry Smith - nlevels - the number of levels of coarsening
369047c6ae99SBarry Smith 
369147c6ae99SBarry Smith   Output Parameter:
3692bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
369347c6ae99SBarry Smith 
369447c6ae99SBarry Smith   Level: developer
369547c6ae99SBarry Smith 
36961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
369747c6ae99SBarry Smith @*/
3698d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3699d71ae5a4SJacob Faibussowitsch {
370047c6ae99SBarry Smith   PetscFunctionBegin;
3701171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37027a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
37033ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
37044f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
3705213acdd3SPierre Jolivet   if (dm->ops->coarsen && !dm->ops->coarsenhierarchy) {
370647c6ae99SBarry Smith     PetscInt i;
370747c6ae99SBarry Smith 
37089566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
370948a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3710213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
37113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
371247c6ae99SBarry Smith }
371347c6ae99SBarry Smith 
37141a266240SBarry Smith /*@C
3715bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
37161a266240SBarry Smith 
3717bb7acecfSBarry Smith   Logically Collective if the function is collective
37181a266240SBarry Smith 
37191a266240SBarry Smith   Input Parameters:
3720bb7acecfSBarry Smith + dm      - the `DM` object
372149abdd8aSBarry Smith - destroy - the destroy function, see `PetscCtxDestroyFn` for the calling sequence
37221a266240SBarry Smith 
37231a266240SBarry Smith   Level: intermediate
37241a266240SBarry Smith 
372549abdd8aSBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`,
372649abdd8aSBarry Smith           `DMGetApplicationContext()`, `PetscCtxDestroyFn`
3727f07f9ceaSJed Brown @*/
372849abdd8aSBarry Smith PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscCtxDestroyFn *destroy)
3729d71ae5a4SJacob Faibussowitsch {
37301a266240SBarry Smith   PetscFunctionBegin;
3731171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37321a266240SBarry Smith   dm->ctxdestroy = destroy;
37333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37341a266240SBarry Smith }
37351a266240SBarry Smith 
3736b07ff414SBarry Smith /*@
3737bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
373847c6ae99SBarry Smith 
373947c6ae99SBarry Smith   Not Collective
374047c6ae99SBarry Smith 
374147c6ae99SBarry Smith   Input Parameters:
3742bb7acecfSBarry Smith + dm  - the `DM` object
374347c6ae99SBarry Smith - ctx - the user context
374447c6ae99SBarry Smith 
374547c6ae99SBarry Smith   Level: intermediate
374647c6ae99SBarry Smith 
3747ce78bad3SBarry Smith   Note:
374835cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3749e33b3f0fSStefano Zampini   In a multilevel solver, the user context is shared by all the `DM` in the hierarchy; it is thus not advisable
3750e33b3f0fSStefano Zampini   to store objects that represent discretized quantities inside the context.
3751bb7acecfSBarry Smith 
3752ce78bad3SBarry Smith   Fortran Note:
3753ce78bad3SBarry Smith   This only works when `ctx` is a Fortran derived type (it cannot be a `PetscObject`), we recommend writing a Fortran interface definition for this
3754ce78bad3SBarry Smith   function that tells the Fortran compiler the derived data type that is passed in as the `ctx` argument. See `DMGetApplicationContext()` for
3755ce78bad3SBarry Smith   an example.
3756ce78bad3SBarry Smith 
375760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
375847c6ae99SBarry Smith @*/
3759d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3760d71ae5a4SJacob Faibussowitsch {
376147c6ae99SBarry Smith   PetscFunctionBegin;
3762171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
376347c6ae99SBarry Smith   dm->ctx = ctx;
37643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
376547c6ae99SBarry Smith }
376647c6ae99SBarry Smith 
376747c6ae99SBarry Smith /*@
3768ce78bad3SBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object provided with `DMSetApplicationContext()`
376947c6ae99SBarry Smith 
377047c6ae99SBarry Smith   Not Collective
377147c6ae99SBarry Smith 
377247c6ae99SBarry Smith   Input Parameter:
3773bb7acecfSBarry Smith . dm - the `DM` object
377447c6ae99SBarry Smith 
377547c6ae99SBarry Smith   Output Parameter:
3776ce78bad3SBarry Smith . ctx - a pointer to the user context
377747c6ae99SBarry Smith 
377847c6ae99SBarry Smith   Level: intermediate
377947c6ae99SBarry Smith 
3780bb7acecfSBarry Smith   Note:
378135cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3782bb7acecfSBarry Smith 
3783ce78bad3SBarry Smith   Fortran Notes:
3784ce78bad3SBarry Smith   This only works when the context is a Fortran derived type (it cannot be a `PetscObject`) and you **must** write a Fortran interface definition for this
3785ce78bad3SBarry Smith   function that tells the Fortran compiler the derived data type that is returned as the `ctx` argument. For example,
3786ce78bad3SBarry Smith .vb
3787ce78bad3SBarry Smith   Interface DMGetApplicationContext
3788ce78bad3SBarry Smith     Subroutine DMGetApplicationContext(dm,ctx,ierr)
3789ce78bad3SBarry Smith   #include <petsc/finclude/petscdm.h>
3790ce78bad3SBarry Smith       use petscdm
3791ce78bad3SBarry Smith       DM dm
3792ce78bad3SBarry Smith       type(tUsertype), pointer :: ctx
3793ce78bad3SBarry Smith       PetscErrorCode ierr
3794ce78bad3SBarry Smith     End Subroutine
3795ce78bad3SBarry Smith   End Interface DMGetApplicationContext
3796ce78bad3SBarry Smith .ve
3797ce78bad3SBarry Smith 
3798bfe80ac4SPierre Jolivet   The prototype for `ctx` must be
3799ce78bad3SBarry Smith .vb
3800ce78bad3SBarry Smith   type(tUsertype), pointer :: ctx
3801ce78bad3SBarry Smith .ve
3802ce78bad3SBarry Smith 
380342747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
380447c6ae99SBarry Smith @*/
3805ce78bad3SBarry Smith PetscErrorCode DMGetApplicationContext(DM dm, PeCtx ctx)
3806d71ae5a4SJacob Faibussowitsch {
380747c6ae99SBarry Smith   PetscFunctionBegin;
3808171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38091b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
38103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
381147c6ae99SBarry Smith }
381247c6ae99SBarry Smith 
381308da532bSDmitry Karpeev /*@C
3814bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
381508da532bSDmitry Karpeev 
381620f4b53cSBarry Smith   Logically Collective
381708da532bSDmitry Karpeev 
3818d8d19677SJose E. Roman   Input Parameters:
381908da532bSDmitry Karpeev + dm - the DM object
3820ce78bad3SBarry Smith - f  - the function that computes variable bounds used by `SNESVI` (use `NULL` to cancel a previous function that was set)
382108da532bSDmitry Karpeev 
382208da532bSDmitry Karpeev   Level: intermediate
382308da532bSDmitry Karpeev 
3824ce78bad3SBarry Smith   Developer Note:
3825ce78bad3SBarry Smith   Should be called `DMSetComputeVIBounds()` or something similar
3826ce78bad3SBarry Smith 
38271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3828db781477SPatrick Sanan          `DMSetJacobian()`
382908da532bSDmitry Karpeev @*/
3830d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3831d71ae5a4SJacob Faibussowitsch {
383208da532bSDmitry Karpeev   PetscFunctionBegin;
38335a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
383408da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
38353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
383608da532bSDmitry Karpeev }
383708da532bSDmitry Karpeev 
383808da532bSDmitry Karpeev /*@
3839bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
384008da532bSDmitry Karpeev 
384108da532bSDmitry Karpeev   Not Collective
384208da532bSDmitry Karpeev 
384308da532bSDmitry Karpeev   Input Parameter:
3844bb7acecfSBarry Smith . dm - the `DM` object to destroy
384508da532bSDmitry Karpeev 
384608da532bSDmitry Karpeev   Output Parameter:
3847bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
384808da532bSDmitry Karpeev 
384908da532bSDmitry Karpeev   Level: developer
385008da532bSDmitry Karpeev 
38511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
385208da532bSDmitry Karpeev @*/
3853d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3854d71ae5a4SJacob Faibussowitsch {
385508da532bSDmitry Karpeev   PetscFunctionBegin;
38565a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38574f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
385808da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
38593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
386008da532bSDmitry Karpeev }
386108da532bSDmitry Karpeev 
3862cc4c1da9SBarry Smith /*@
3863bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
386408da532bSDmitry Karpeev 
386520f4b53cSBarry Smith   Logically Collective
386608da532bSDmitry Karpeev 
3867f899ff85SJose E. Roman   Input Parameter:
3868bb7acecfSBarry Smith . dm - the `DM` object
386908da532bSDmitry Karpeev 
387060225df5SJacob Faibussowitsch   Output Parameters:
387108da532bSDmitry Karpeev + xl - lower bound
387208da532bSDmitry Karpeev - xu - upper bound
387308da532bSDmitry Karpeev 
3874907376e6SBarry Smith   Level: advanced
3875907376e6SBarry Smith 
387620f4b53cSBarry Smith   Note:
387795452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
387808da532bSDmitry Karpeev 
38791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
388008da532bSDmitry Karpeev @*/
3881d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3882d71ae5a4SJacob Faibussowitsch {
388308da532bSDmitry Karpeev   PetscFunctionBegin;
38845a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
388508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
38865a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3887dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
388908da532bSDmitry Karpeev }
389008da532bSDmitry Karpeev 
3891b0ae01b7SPeter Brune /*@
3892bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3893b0ae01b7SPeter Brune 
3894b0ae01b7SPeter Brune   Not Collective
3895b0ae01b7SPeter Brune 
3896b0ae01b7SPeter Brune   Input Parameter:
3897b0ae01b7SPeter Brune . dm - the DM object
3898b0ae01b7SPeter Brune 
3899b0ae01b7SPeter Brune   Output Parameter:
3900bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3901b0ae01b7SPeter Brune 
3902b0ae01b7SPeter Brune   Level: developer
3903b0ae01b7SPeter Brune 
39041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3905b0ae01b7SPeter Brune @*/
3906d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3907d71ae5a4SJacob Faibussowitsch {
3908b0ae01b7SPeter Brune   PetscFunctionBegin;
39095a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39104f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3911b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
39123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3913b0ae01b7SPeter Brune }
3914b0ae01b7SPeter Brune 
39153ad4599aSBarry Smith /*@
3916bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
39173ad4599aSBarry Smith 
39183ad4599aSBarry Smith   Not Collective
39193ad4599aSBarry Smith 
39203ad4599aSBarry Smith   Input Parameter:
3921bb7acecfSBarry Smith . dm - the `DM` object
39223ad4599aSBarry Smith 
39233ad4599aSBarry Smith   Output Parameter:
3924bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
39253ad4599aSBarry Smith 
39263ad4599aSBarry Smith   Level: developer
39273ad4599aSBarry Smith 
39281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
39293ad4599aSBarry Smith @*/
3930d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3931d71ae5a4SJacob Faibussowitsch {
39323ad4599aSBarry Smith   PetscFunctionBegin;
39335a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39344f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
39353ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
39363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
39373ad4599aSBarry Smith }
39383ad4599aSBarry Smith 
3939a7058e45SLawrence Mitchell /*@
3940bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3941a7058e45SLawrence Mitchell 
3942a7058e45SLawrence Mitchell   Not Collective
3943a7058e45SLawrence Mitchell 
3944a7058e45SLawrence Mitchell   Input Parameter:
3945bb7acecfSBarry Smith . dm - the `DM` object
3946a7058e45SLawrence Mitchell 
3947a7058e45SLawrence Mitchell   Output Parameter:
3948bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3949a7058e45SLawrence Mitchell 
3950a7058e45SLawrence Mitchell   Level: developer
3951a7058e45SLawrence Mitchell 
39521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3953a7058e45SLawrence Mitchell @*/
3954d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3955d71ae5a4SJacob Faibussowitsch {
3956a7058e45SLawrence Mitchell   PetscFunctionBegin;
39575a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39584f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3959dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3960ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
39613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3962a7058e45SLawrence Mitchell }
3963a7058e45SLawrence Mitchell 
39640298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3965264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3966264ace61SBarry Smith 
3967cc4c1da9SBarry Smith /*@
3968bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3969264ace61SBarry Smith 
397020f4b53cSBarry Smith   Collective
3971264ace61SBarry Smith 
3972264ace61SBarry Smith   Input Parameters:
3973bb7acecfSBarry Smith + dm     - The `DM` object
3974bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3975264ace61SBarry Smith 
3976264ace61SBarry Smith   Options Database Key:
3977bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3978264ace61SBarry Smith 
3979264ace61SBarry Smith   Level: intermediate
3980264ace61SBarry Smith 
3981bb7acecfSBarry Smith   Note:
39820462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3983bb7acecfSBarry Smith 
39841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3985264ace61SBarry Smith @*/
3986d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3987d71ae5a4SJacob Faibussowitsch {
3988264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3989264ace61SBarry Smith   PetscBool match;
3990264ace61SBarry Smith 
3991264ace61SBarry Smith   PetscFunctionBegin;
3992264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39939566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39943ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3995264ace61SBarry Smith 
39969566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39979566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39987a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3999264ace61SBarry Smith 
4000dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
40019566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
40029566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
40039566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
40043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4005264ace61SBarry Smith }
4006264ace61SBarry Smith 
4007cc4c1da9SBarry Smith /*@
4008bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
4009264ace61SBarry Smith 
4010264ace61SBarry Smith   Not Collective
4011264ace61SBarry Smith 
4012264ace61SBarry Smith   Input Parameter:
4013bb7acecfSBarry Smith . dm - The `DM`
4014264ace61SBarry Smith 
4015264ace61SBarry Smith   Output Parameter:
4016bb7acecfSBarry Smith . type - The `DMType` name
4017264ace61SBarry Smith 
4018264ace61SBarry Smith   Level: intermediate
4019264ace61SBarry Smith 
40201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
4021264ace61SBarry Smith @*/
4022d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
4023d71ae5a4SJacob Faibussowitsch {
4024264ace61SBarry Smith   PetscFunctionBegin;
4025264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40264f572ea9SToby Isaac   PetscAssertPointer(type, 2);
40279566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
4028264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
40293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4030264ace61SBarry Smith }
4031264ace61SBarry Smith 
40325d83a8b1SBarry Smith /*@
4033bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
403467a56275SMatthew G Knepley 
403520f4b53cSBarry Smith   Collective
403667a56275SMatthew G Knepley 
403767a56275SMatthew G Knepley   Input Parameters:
4038bb7acecfSBarry Smith + dm      - the `DM`
4039bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
404067a56275SMatthew G Knepley 
404167a56275SMatthew G Knepley   Output Parameter:
4042bb7acecfSBarry Smith . M - pointer to new `DM`
404367a56275SMatthew G Knepley 
404420f4b53cSBarry Smith   Level: intermediate
404520f4b53cSBarry Smith 
404673ff1848SBarry Smith   Note:
4047bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
4048bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
4049bb7acecfSBarry Smith   of the input `DM`.
405067a56275SMatthew G Knepley 
40511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
405267a56275SMatthew G Knepley @*/
4053d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
4054d71ae5a4SJacob Faibussowitsch {
405567a56275SMatthew G Knepley   DM        B;
405667a56275SMatthew G Knepley   char      convname[256];
4057c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
405867a56275SMatthew G Knepley 
405967a56275SMatthew G Knepley   PetscFunctionBegin;
406067a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
406167a56275SMatthew G Knepley   PetscValidType(dm, 1);
40624f572ea9SToby Isaac   PetscAssertPointer(M, 3);
40639566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
40649566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
4065c067b6caSMatthew G. Knepley   if (sametype) {
4066c067b6caSMatthew G. Knepley     *M = dm;
40679566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
40683ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
4069c067b6caSMatthew G. Knepley   } else {
40700298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
407167a56275SMatthew G Knepley 
407267a56275SMatthew G Knepley     /*
407367a56275SMatthew G Knepley        Order of precedence:
407467a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
407567a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
407667a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
407767a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
407867a56275SMatthew G Knepley        5) Use a really basic converter.
407967a56275SMatthew G Knepley     */
408067a56275SMatthew G Knepley 
408167a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
40829566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40839566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40849566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40859566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40869566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40879566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
408867a56275SMatthew G Knepley     if (conv) goto foundconv;
408967a56275SMatthew G Knepley 
409067a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40919566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40929566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40939566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40949566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40959566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40969566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40979566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40989566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
409967a56275SMatthew G Knepley     if (conv) {
41009566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
410167a56275SMatthew G Knepley       goto foundconv;
410267a56275SMatthew G Knepley     }
410367a56275SMatthew G Knepley 
410467a56275SMatthew G Knepley #if 0
410567a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
410667a56275SMatthew G Knepley     conv = B->ops->convertfrom;
41079566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
410867a56275SMatthew G Knepley     if (conv) goto foundconv;
410967a56275SMatthew G Knepley 
411067a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
411167a56275SMatthew G Knepley     if (dm->ops->convert) {
411267a56275SMatthew G Knepley       conv = dm->ops->convert;
411367a56275SMatthew G Knepley     }
411467a56275SMatthew G Knepley     if (conv) goto foundconv;
411567a56275SMatthew G Knepley #endif
411667a56275SMatthew G Knepley 
411767a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
411898921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
411967a56275SMatthew G Knepley 
412067a56275SMatthew G Knepley   foundconv:
41219566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
41229566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
412312fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
412490b157c4SStefano Zampini     {
41254fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
41266858538eSMatthew G. Knepley 
41274fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
41284fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4129c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
41309566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
41319566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
41329566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
41339566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
413412fa691eSMatthew G. Knepley     }
41359566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
413667a56275SMatthew G Knepley   }
41379566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
41383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
413967a56275SMatthew G Knepley }
4140264ace61SBarry Smith 
4141264ace61SBarry Smith /*@C
4142bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
41431c84c290SBarry Smith 
4144cc4c1da9SBarry Smith   Not Collective, No Fortran Support
41451c84c290SBarry Smith 
41461c84c290SBarry Smith   Input Parameters:
414720f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
414820f4b53cSBarry Smith - function - The creation routine itself
414920f4b53cSBarry Smith 
415020f4b53cSBarry Smith   Level: advanced
41511c84c290SBarry Smith 
415273ff1848SBarry Smith   Note:
415320f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
41541c84c290SBarry Smith 
415560225df5SJacob Faibussowitsch   Example Usage:
41561c84c290SBarry Smith .vb
4157bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
41581c84c290SBarry Smith .ve
41591c84c290SBarry Smith 
416020f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
41611c84c290SBarry Smith .vb
41621c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
41631c84c290SBarry Smith     DMSetType(DM,"my_da");
41641c84c290SBarry Smith .ve
41651c84c290SBarry Smith   or at runtime via the option
41661c84c290SBarry Smith .vb
41671c84c290SBarry Smith     -da_type my_da
41681c84c290SBarry Smith .ve
4169264ace61SBarry Smith 
41701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4171264ace61SBarry Smith @*/
4172d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4173d71ae5a4SJacob Faibussowitsch {
4174264ace61SBarry Smith   PetscFunctionBegin;
41759566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
41769566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
41773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4178264ace61SBarry Smith }
4179264ace61SBarry Smith 
4180ffeef943SBarry Smith /*@
4181bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4182b859378eSBarry Smith 
418320f4b53cSBarry Smith   Collective
4184b859378eSBarry Smith 
4185b859378eSBarry Smith   Input Parameters:
4186bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4187bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4188bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4189bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4190b859378eSBarry Smith 
4191b859378eSBarry Smith   Level: intermediate
4192b859378eSBarry Smith 
4193b859378eSBarry Smith   Notes:
419455849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4195b859378eSBarry Smith 
4196bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4197bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4198bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4199cd7e8a5eSksagiyam 
42001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4201b859378eSBarry Smith @*/
4202d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4203d71ae5a4SJacob Faibussowitsch {
42049331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4205b859378eSBarry Smith 
4206b859378eSBarry Smith   PetscFunctionBegin;
4207b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4208b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
42099566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
42109566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
42119566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
42129566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
42139331c7a4SMatthew G. Knepley   if (isbinary) {
42149331c7a4SMatthew G. Knepley     PetscInt classid;
42159331c7a4SMatthew G. Knepley     char     type[256];
4216b859378eSBarry Smith 
42179566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
4218835f2295SStefano Zampini     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %" PetscInt_FMT, classid);
42199566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
42209566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4221dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
42229331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4223dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
42249331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
42259566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
42263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4227b859378eSBarry Smith }
4228b859378eSBarry Smith 
42294ee01570SBarry Smith /* FEM Support */
42307da65231SMatthew G Knepley 
42312ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[])
42322ecf6ec3SMatthew G. Knepley {
42332ecf6ec3SMatthew G. Knepley   PetscInt f;
42342ecf6ec3SMatthew G. Knepley 
42352ecf6ec3SMatthew G. Knepley   PetscFunctionBegin;
42362ecf6ec3SMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42372ecf6ec3SMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %" PetscInt_FMT " |\n", x[f]));
42382ecf6ec3SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
42392ecf6ec3SMatthew G. Knepley }
42402ecf6ec3SMatthew G. Knepley 
4241d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4242d71ae5a4SJacob Faibussowitsch {
42431d47ebbbSSatish Balay   PetscInt f;
42441b30c384SMatthew G Knepley 
42457da65231SMatthew G Knepley   PetscFunctionBegin;
424663a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
424748a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
42483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42497da65231SMatthew G Knepley }
42507da65231SMatthew G Knepley 
42515962854dSMatthew G. Knepley PetscErrorCode DMPrintCellVectorReal(PetscInt c, const char name[], PetscInt len, const PetscReal x[])
42525962854dSMatthew G. Knepley {
42535962854dSMatthew G. Knepley   PetscInt f;
42545962854dSMatthew G. Knepley 
42555962854dSMatthew G. Knepley   PetscFunctionBegin;
42565962854dSMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42575962854dSMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)x[f]));
42585962854dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
42595962854dSMatthew G. Knepley }
42605962854dSMatthew G. Knepley 
4261d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4262d71ae5a4SJacob Faibussowitsch {
42631b30c384SMatthew G Knepley   PetscInt f, g;
42647da65231SMatthew G Knepley 
42657da65231SMatthew G Knepley   PetscFunctionBegin;
426663a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42671d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
42689566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
426948a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
42709566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
42717da65231SMatthew G Knepley   }
42723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42737da65231SMatthew G Knepley }
4274e7c4fc90SDmitry Karpeev 
4275d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4276d71ae5a4SJacob Faibussowitsch {
42770c5b8624SToby Isaac   PetscInt           localSize, bs;
42780c5b8624SToby Isaac   PetscMPIInt        size;
42790c5b8624SToby Isaac   Vec                x, xglob;
42800c5b8624SToby Isaac   const PetscScalar *xarray;
4281e759306cSMatthew G. Knepley 
4282e759306cSMatthew G. Knepley   PetscFunctionBegin;
42839566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
42849566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
42859566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
428693ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
42879566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
42880c5b8624SToby Isaac   if (size > 1) {
42899566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
42909566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
42919566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
42929566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42930c5b8624SToby Isaac   } else {
42940c5b8624SToby Isaac     xglob = x;
42950c5b8624SToby Isaac   }
42969566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42970c5b8624SToby Isaac   if (size > 1) {
42989566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42999566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
43000c5b8624SToby Isaac   }
43019566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
43023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4303e759306cSMatthew G. Knepley }
4304e759306cSMatthew G. Knepley 
430588ed4aceSMatthew G Knepley /*@
4306bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
430788ed4aceSMatthew G Knepley 
430888ed4aceSMatthew G Knepley   Input Parameter:
4309bb7acecfSBarry Smith . dm - The `DM`
431088ed4aceSMatthew G Knepley 
431188ed4aceSMatthew G Knepley   Output Parameter:
4312bb7acecfSBarry Smith . section - The `PetscSection`
431388ed4aceSMatthew G Knepley 
431420f4b53cSBarry Smith   Options Database Key:
4315bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4316e5893cccSMatthew G. Knepley 
431788ed4aceSMatthew G Knepley   Level: intermediate
431888ed4aceSMatthew G Knepley 
4319bb7acecfSBarry Smith   Note:
4320bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
432188ed4aceSMatthew G Knepley 
43221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
432388ed4aceSMatthew G Knepley @*/
4324d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4325d71ae5a4SJacob Faibussowitsch {
432688ed4aceSMatthew G Knepley   PetscFunctionBegin;
432788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43284f572ea9SToby Isaac   PetscAssertPointer(section, 2);
43291bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4330e5e52638SMatthew G. Knepley     PetscInt d;
4331e5e52638SMatthew G. Knepley 
433245480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
433345480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
433445480ffeSMatthew G. Knepley       PetscViewer       viewer;
433545480ffeSMatthew G. Knepley       PetscViewerFormat format;
433645480ffeSMatthew G. Knepley       PetscBool         flg;
433745480ffeSMatthew G. Knepley 
4338648c30bcSBarry Smith       PetscCall(PetscOptionsCreateViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
43399566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
434045480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
43419566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
43429566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
434345480ffeSMatthew G. Knepley       }
434445480ffeSMatthew G. Knepley       if (flg) {
43459566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
43469566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
4347648c30bcSBarry Smith         PetscCall(PetscViewerDestroy(&viewer));
434845480ffeSMatthew G. Knepley       }
434945480ffeSMatthew G. Knepley     }
4350dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
43519566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
43522f0f8703SMatthew G. Knepley   }
43531bb6d2a8SBarry Smith   *section = dm->localSection;
43543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
435588ed4aceSMatthew G Knepley }
435688ed4aceSMatthew G Knepley 
435788ed4aceSMatthew G Knepley /*@
4358bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
435988ed4aceSMatthew G Knepley 
436088ed4aceSMatthew G Knepley   Input Parameters:
4361bb7acecfSBarry Smith + dm      - The `DM`
4362bb7acecfSBarry Smith - section - The `PetscSection`
436388ed4aceSMatthew G Knepley 
436488ed4aceSMatthew G Knepley   Level: intermediate
436588ed4aceSMatthew G Knepley 
4366bb7acecfSBarry Smith   Note:
4367bb7acecfSBarry Smith   Any existing Section will be destroyed
436888ed4aceSMatthew G Knepley 
43691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
437088ed4aceSMatthew G Knepley @*/
4371d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4372d71ae5a4SJacob Faibussowitsch {
4373c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4374af122d2aSMatthew G Knepley   PetscInt f;
437588ed4aceSMatthew G Knepley 
437688ed4aceSMatthew G Knepley   PetscFunctionBegin;
437788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4378b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43799566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43809566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43811bb6d2a8SBarry Smith   dm->localSection = section;
43829566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4383af122d2aSMatthew G Knepley   if (numFields) {
43849566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4385af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43860f21e855SMatthew G. Knepley       PetscObject disc;
4387af122d2aSMatthew G Knepley       const char *name;
4388af122d2aSMatthew G Knepley 
43899566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43909566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43919566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4392af122d2aSMatthew G Knepley     }
4393af122d2aSMatthew G Knepley   }
439450350c8eSStefano Zampini   /* The global section and the SectionSF will be rebuilt
439550350c8eSStefano Zampini      in the next call to DMGetGlobalSection() and DMGetSectionSF(). */
43969566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
439750350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
439852947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
439950350c8eSStefano Zampini 
440050350c8eSStefano Zampini   /* Clear scratch vectors */
440150350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
440250350c8eSStefano Zampini   PetscCall(DMClearLocalVectors(dm));
440350350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
440450350c8eSStefano Zampini   PetscCall(DMClearNamedLocalVectors(dm));
44053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
440688ed4aceSMatthew G Knepley }
440788ed4aceSMatthew G Knepley 
4408adc21957SMatthew G. Knepley /*@C
4409d8b4a066SPierre Jolivet   DMCreateSectionPermutation - Create a permutation of the `PetscSection` chart and optionally a block structure.
4410adc21957SMatthew G. Knepley 
4411adc21957SMatthew G. Knepley   Input Parameter:
4412adc21957SMatthew G. Knepley . dm - The `DM`
4413adc21957SMatthew G. Knepley 
44149cde84edSJose E. Roman   Output Parameters:
4415adc21957SMatthew G. Knepley + perm        - A permutation of the mesh points in the chart
4416b6971eaeSBarry Smith - blockStarts - A high bit is set for the point that begins every block, or `NULL` for default blocking
4417adc21957SMatthew G. Knepley 
4418adc21957SMatthew G. Knepley   Level: developer
4419adc21957SMatthew G. Knepley 
4420adc21957SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMGetGlobalSection()`
4421adc21957SMatthew G. Knepley @*/
4422adc21957SMatthew G. Knepley PetscErrorCode DMCreateSectionPermutation(DM dm, IS *perm, PetscBT *blockStarts)
4423adc21957SMatthew G. Knepley {
4424adc21957SMatthew G. Knepley   PetscFunctionBegin;
4425adc21957SMatthew G. Knepley   *perm        = NULL;
4426adc21957SMatthew G. Knepley   *blockStarts = NULL;
4427adc21957SMatthew G. Knepley   PetscTryTypeMethod(dm, createsectionpermutation, perm, blockStarts);
4428adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
4429adc21957SMatthew G. Knepley }
4430adc21957SMatthew G. Knepley 
44319435951eSToby Isaac /*@
4432bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
44339435951eSToby Isaac 
443420f4b53cSBarry Smith   not Collective
4435e228b242SToby Isaac 
44369435951eSToby Isaac   Input Parameter:
4437bb7acecfSBarry Smith . dm - The `DM`
44389435951eSToby Isaac 
4439d8d19677SJose E. Roman   Output Parameters:
444020f4b53cSBarry 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.
444120f4b53cSBarry 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.
444279769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
44439435951eSToby Isaac 
44449435951eSToby Isaac   Level: advanced
44459435951eSToby Isaac 
4446bb7acecfSBarry Smith   Note:
4447bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
44489435951eSToby Isaac 
44491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
44509435951eSToby Isaac @*/
4451d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4452d71ae5a4SJacob Faibussowitsch {
44539435951eSToby Isaac   PetscFunctionBegin;
44549435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4455dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44563b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44573b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
445879769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44609435951eSToby Isaac }
44619435951eSToby Isaac 
44629435951eSToby Isaac /*@
4463bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44649435951eSToby Isaac 
446520f4b53cSBarry Smith   Collective
4466e228b242SToby Isaac 
44679435951eSToby Isaac   Input Parameters:
4468bb7acecfSBarry Smith + dm      - The `DM`
4469bb7acecfSBarry 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).
447020f4b53cSBarry 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).
447120f4b53cSBarry 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).
44729435951eSToby Isaac 
44739435951eSToby Isaac   Level: advanced
44749435951eSToby Isaac 
447520f4b53cSBarry Smith   Notes:
447620f4b53cSBarry 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()`.
447720f4b53cSBarry Smith 
447820f4b53cSBarry 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.
447920f4b53cSBarry Smith 
4480bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44819435951eSToby Isaac 
44821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
44839435951eSToby Isaac @*/
4484d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4485d71ae5a4SJacob Faibussowitsch {
4486e228b242SToby Isaac   PetscMPIInt result;
44879435951eSToby Isaac 
44889435951eSToby Isaac   PetscFunctionBegin;
44899435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4490e228b242SToby Isaac   if (section) {
4491e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44929566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44937a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4494e228b242SToby Isaac   }
4495e228b242SToby Isaac   if (mat) {
4496e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44979566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44987a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4499e228b242SToby Isaac   }
450079769bd5SJed Brown   if (bias) {
450179769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
45029566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
450379769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
450479769bd5SJed Brown   }
45059566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45069566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
45073b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
45089566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
45099566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
45103b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
45119566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
45129566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
451379769bd5SJed Brown   dm->defaultConstraint.bias = bias;
45143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45159435951eSToby Isaac }
45169435951eSToby Isaac 
4517497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4518507e4973SMatthew G. Knepley /*
4519bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4520507e4973SMatthew G. Knepley 
4521507e4973SMatthew G. Knepley   Input Parameters:
4522bb7acecfSBarry Smith + dm - The `DM`
4523bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4524bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4525507e4973SMatthew G. Knepley 
4526507e4973SMatthew G. Knepley   Level: intermediate
4527507e4973SMatthew G. Knepley 
45281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4529507e4973SMatthew G. Knepley */
4530d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4531d71ae5a4SJacob Faibussowitsch {
4532507e4973SMatthew G. Knepley   MPI_Comm        comm;
4533507e4973SMatthew G. Knepley   PetscLayout     layout;
4534507e4973SMatthew G. Knepley   const PetscInt *ranges;
4535507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4536507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4537507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4538507e4973SMatthew G. Knepley 
4539507e4973SMatthew G. Knepley   PetscFunctionBegin;
45409566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4541507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45429566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
45439566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
45449566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
45459566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
45469566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
45479566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
45489566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
45499566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45509566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4551507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4552f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4553507e4973SMatthew G. Knepley 
45549566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45559566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45569566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45579566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45589566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45599566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4560507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45619371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45629371c9d4SSatish 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));
45639371c9d4SSatish Balay       valid = PETSC_FALSE;
45649371c9d4SSatish Balay     }
45659371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45669371c9d4SSatish 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));
45679371c9d4SSatish Balay       valid = PETSC_FALSE;
45689371c9d4SSatish Balay     }
4569507e4973SMatthew G. Knepley     if (gdof < 0) {
4570507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4571507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4572507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4573507e4973SMatthew G. Knepley 
45749566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4575507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45769371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45779371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45789371c9d4SSatish Balay           valid = PETSC_FALSE;
45799371c9d4SSatish Balay           break;
45809371c9d4SSatish Balay         }
4581507e4973SMatthew G. Knepley       }
4582507e4973SMatthew G. Knepley     }
4583507e4973SMatthew G. Knepley   }
45849566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45859566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
4586462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4587507e4973SMatthew G. Knepley   if (!gvalid) {
45889566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4589507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4590507e4973SMatthew G. Knepley   }
45913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4592507e4973SMatthew G. Knepley }
4593f741bcd2SMatthew G. Knepley #endif
4594507e4973SMatthew G. Knepley 
459590df3356SJames Wright PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45966725e60dSJed Brown {
45976725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45984d86920dSPierre Jolivet 
45996725e60dSJed Brown   PetscFunctionBegin;
46006725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46014f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
46025f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
46036725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
46046725e60dSJed Brown   else *sf = dm->sf;
46053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
46066725e60dSJed Brown }
46076725e60dSJed Brown 
460888ed4aceSMatthew G Knepley /*@
4609bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
461088ed4aceSMatthew G Knepley 
461120f4b53cSBarry Smith   Collective
46128b1ab98fSJed Brown 
461388ed4aceSMatthew G Knepley   Input Parameter:
4614bb7acecfSBarry Smith . dm - The `DM`
461588ed4aceSMatthew G Knepley 
461688ed4aceSMatthew G Knepley   Output Parameter:
4617bb7acecfSBarry Smith . section - The `PetscSection`
461888ed4aceSMatthew G Knepley 
461988ed4aceSMatthew G Knepley   Level: intermediate
462088ed4aceSMatthew G Knepley 
4621bb7acecfSBarry Smith   Note:
4622bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
462388ed4aceSMatthew G Knepley 
46241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
462588ed4aceSMatthew G Knepley @*/
4626d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4627d71ae5a4SJacob Faibussowitsch {
462888ed4aceSMatthew G Knepley   PetscFunctionBegin;
462988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46304f572ea9SToby Isaac   PetscAssertPointer(section, 2);
46311bb6d2a8SBarry Smith   if (!dm->globalSection) {
4632fd59a867SMatthew G. Knepley     PetscSection s;
46336725e60dSJed Brown     PetscSF      sf;
4634fd59a867SMatthew G. Knepley 
46359566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
46367a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
46377a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
46385f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
4639eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
46409566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
46419566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
46429566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
464388ed4aceSMatthew G Knepley   }
46441bb6d2a8SBarry Smith   *section = dm->globalSection;
46453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
464688ed4aceSMatthew G Knepley }
464788ed4aceSMatthew G Knepley 
4648b21d0597SMatthew G Knepley /*@
4649bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4650b21d0597SMatthew G Knepley 
4651b21d0597SMatthew G Knepley   Input Parameters:
4652bb7acecfSBarry Smith + dm      - The `DM`
46532fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4654b21d0597SMatthew G Knepley 
4655b21d0597SMatthew G Knepley   Level: intermediate
4656b21d0597SMatthew G Knepley 
4657bb7acecfSBarry Smith   Note:
4658bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4659b21d0597SMatthew G Knepley 
46601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4661b21d0597SMatthew G Knepley @*/
4662d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4663d71ae5a4SJacob Faibussowitsch {
4664b21d0597SMatthew G Knepley   PetscFunctionBegin;
4665b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46665080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46679566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46689566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46691bb6d2a8SBarry Smith   dm->globalSection = section;
4670497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46719566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4672507e4973SMatthew G. Knepley #endif
467350350c8eSStefano Zampini   /* Clear global scratch vectors and sectionSF */
467450350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
467552947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
467650350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
467750350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
46783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4679b21d0597SMatthew G Knepley }
4680b21d0597SMatthew G Knepley 
468188ed4aceSMatthew G Knepley /*@
4682bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4683bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
468488ed4aceSMatthew G Knepley 
468588ed4aceSMatthew G Knepley   Input Parameter:
4686bb7acecfSBarry Smith . dm - The `DM`
468788ed4aceSMatthew G Knepley 
468888ed4aceSMatthew G Knepley   Output Parameter:
4689bb7acecfSBarry Smith . sf - The `PetscSF`
469088ed4aceSMatthew G Knepley 
469188ed4aceSMatthew G Knepley   Level: intermediate
469288ed4aceSMatthew G Knepley 
4693bb7acecfSBarry Smith   Note:
4694bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
469588ed4aceSMatthew G Knepley 
46961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
469788ed4aceSMatthew G Knepley @*/
4698d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4699d71ae5a4SJacob Faibussowitsch {
470088ed4aceSMatthew G Knepley   PetscInt nroots;
470188ed4aceSMatthew G Knepley 
470288ed4aceSMatthew G Knepley   PetscFunctionBegin;
470388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47044f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
470548a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
47069566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
470788ed4aceSMatthew G Knepley   if (nroots < 0) {
470888ed4aceSMatthew G Knepley     PetscSection section, gSection;
470988ed4aceSMatthew G Knepley 
47109566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
471131ea6d37SMatthew G Knepley     if (section) {
47129566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
47139566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
471431ea6d37SMatthew G Knepley     } else {
47150298fd71SBarry Smith       *sf = NULL;
47163ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
471731ea6d37SMatthew G Knepley     }
471888ed4aceSMatthew G Knepley   }
47191bb6d2a8SBarry Smith   *sf = dm->sectionSF;
47203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
472188ed4aceSMatthew G Knepley }
472288ed4aceSMatthew G Knepley 
472388ed4aceSMatthew G Knepley /*@
4724bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
472588ed4aceSMatthew G Knepley 
472688ed4aceSMatthew G Knepley   Input Parameters:
4727bb7acecfSBarry Smith + dm - The `DM`
4728bb7acecfSBarry Smith - sf - The `PetscSF`
472988ed4aceSMatthew G Knepley 
473088ed4aceSMatthew G Knepley   Level: intermediate
473188ed4aceSMatthew G Knepley 
4732bb7acecfSBarry Smith   Note:
4733bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
473488ed4aceSMatthew G Knepley 
47351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
473688ed4aceSMatthew G Knepley @*/
4737d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4738d71ae5a4SJacob Faibussowitsch {
473988ed4aceSMatthew G Knepley   PetscFunctionBegin;
474088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4741b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47429566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47439566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
47441bb6d2a8SBarry Smith   dm->sectionSF = sf;
47453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
474688ed4aceSMatthew G Knepley }
474788ed4aceSMatthew G Knepley 
4748cc4c1da9SBarry Smith /*@
4749bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
475088ed4aceSMatthew G Knepley   describing the data layout.
475188ed4aceSMatthew G Knepley 
475288ed4aceSMatthew G Knepley   Input Parameters:
4753bb7acecfSBarry Smith + dm            - The `DM`
4754bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4755bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
475688ed4aceSMatthew G Knepley 
47571bb6d2a8SBarry Smith   Level: developer
47581bb6d2a8SBarry Smith 
4759bb7acecfSBarry Smith   Note:
4760bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4761bb7acecfSBarry Smith 
476273ff1848SBarry Smith   Developer Note:
4763bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4764bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4765b6971eaeSBarry Smith   input and should just obtain them from the `DM`? Plus PETSc creation functions return the thing
4766b6971eaeSBarry Smith   they create, this returns nothing
47671bb6d2a8SBarry Smith 
47681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
476988ed4aceSMatthew G Knepley @*/
4770d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4771d71ae5a4SJacob Faibussowitsch {
477288ed4aceSMatthew G Knepley   PetscFunctionBegin;
477388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47749566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
47753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
477688ed4aceSMatthew G Knepley }
4777af122d2aSMatthew G Knepley 
4778b21d0597SMatthew G Knepley /*@
4779bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4780bb7acecfSBarry Smith 
4781bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4782b21d0597SMatthew G Knepley 
4783b21d0597SMatthew G Knepley   Input Parameter:
4784bb7acecfSBarry Smith . dm - The `DM`
4785b21d0597SMatthew G Knepley 
4786b21d0597SMatthew G Knepley   Output Parameter:
4787bb7acecfSBarry Smith . sf - The `PetscSF`
4788b21d0597SMatthew G Knepley 
4789b21d0597SMatthew G Knepley   Level: intermediate
4790b21d0597SMatthew G Knepley 
4791bb7acecfSBarry Smith   Note:
4792bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4793b21d0597SMatthew G Knepley 
47941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4795b21d0597SMatthew G Knepley @*/
4796d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4797d71ae5a4SJacob Faibussowitsch {
4798b21d0597SMatthew G Knepley   PetscFunctionBegin;
4799b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48004f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4801b21d0597SMatthew G Knepley   *sf = dm->sf;
48023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4803b21d0597SMatthew G Knepley }
4804b21d0597SMatthew G Knepley 
4805057b4bcdSMatthew G Knepley /*@
4806bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4807bb7acecfSBarry Smith 
480820f4b53cSBarry Smith   Collective
4809057b4bcdSMatthew G Knepley 
4810057b4bcdSMatthew G Knepley   Input Parameters:
4811bb7acecfSBarry Smith + dm - The `DM`
4812bb7acecfSBarry Smith - sf - The `PetscSF`
4813057b4bcdSMatthew G Knepley 
4814057b4bcdSMatthew G Knepley   Level: intermediate
4815057b4bcdSMatthew G Knepley 
48161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4817057b4bcdSMatthew G Knepley @*/
4818d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4819d71ae5a4SJacob Faibussowitsch {
4820057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4821057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4822b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48239566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48249566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4825057b4bcdSMatthew G Knepley   dm->sf = sf;
48263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4827057b4bcdSMatthew G Knepley }
4828057b4bcdSMatthew G Knepley 
48294f37162bSMatthew G. Knepley /*@
4830bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
48314f37162bSMatthew G. Knepley 
48324f37162bSMatthew G. Knepley   Input Parameter:
4833bb7acecfSBarry Smith . dm - The `DM`
48344f37162bSMatthew G. Knepley 
48354f37162bSMatthew G. Knepley   Output Parameter:
4836bb7acecfSBarry Smith . sf - The `PetscSF`
48374f37162bSMatthew G. Knepley 
48384f37162bSMatthew G. Knepley   Level: intermediate
48394f37162bSMatthew G. Knepley 
4840bb7acecfSBarry Smith   Note:
4841bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
48424f37162bSMatthew G. Knepley 
48431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48444f37162bSMatthew G. Knepley @*/
4845d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4846d71ae5a4SJacob Faibussowitsch {
48474f37162bSMatthew G. Knepley   PetscFunctionBegin;
48484f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48494f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
48504f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
48513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48524f37162bSMatthew G. Knepley }
48534f37162bSMatthew G. Knepley 
48544f37162bSMatthew G. Knepley /*@
48554f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48564f37162bSMatthew G. Knepley 
48574f37162bSMatthew G. Knepley   Input Parameters:
48584f37162bSMatthew G. Knepley + dm - The DM
48594f37162bSMatthew G. Knepley - sf - The PetscSF
48604f37162bSMatthew G. Knepley 
48614f37162bSMatthew G. Knepley   Level: intermediate
48624f37162bSMatthew G. Knepley 
48631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48644f37162bSMatthew G. Knepley @*/
4865d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4866d71ae5a4SJacob Faibussowitsch {
48674f37162bSMatthew G. Knepley   PetscFunctionBegin;
48684f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48694f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48709566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48719566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48724f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48744f37162bSMatthew G. Knepley }
48754f37162bSMatthew G. Knepley 
4876d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4877d71ae5a4SJacob Faibussowitsch {
487834aa8a36SMatthew G. Knepley   PetscClassId id;
487934aa8a36SMatthew G. Knepley 
488034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48819566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
488234aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48839566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
488434aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48859566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
488617c1d62eSMatthew G. Knepley   } else {
48879566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
488834aa8a36SMatthew G. Knepley   }
48893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
489034aa8a36SMatthew G. Knepley }
489134aa8a36SMatthew G. Knepley 
4892d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4893d71ae5a4SJacob Faibussowitsch {
489444a7f3ddSMatthew G. Knepley   RegionField *tmpr;
489544a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
489644a7f3ddSMatthew G. Knepley 
489744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
48983ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
48999566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
490044a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
49019371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
49029371c9d4SSatish Balay     tmpr[f].disc        = NULL;
49039371c9d4SSatish Balay     tmpr[f].label       = NULL;
49049371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
49059371c9d4SSatish Balay   }
49069566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
490744a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
490844a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
49093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
491044a7f3ddSMatthew G. Knepley }
491144a7f3ddSMatthew G. Knepley 
491244a7f3ddSMatthew G. Knepley /*@
491320f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
491444a7f3ddSMatthew G. Knepley 
491520f4b53cSBarry Smith   Logically Collective
491644a7f3ddSMatthew G. Knepley 
491744a7f3ddSMatthew G. Knepley   Input Parameter:
491820f4b53cSBarry Smith . dm - The `DM`
491944a7f3ddSMatthew G. Knepley 
492044a7f3ddSMatthew G. Knepley   Level: intermediate
492144a7f3ddSMatthew G. Knepley 
49221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
492344a7f3ddSMatthew G. Knepley @*/
4924d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4925d71ae5a4SJacob Faibussowitsch {
492644a7f3ddSMatthew G. Knepley   PetscInt f;
492744a7f3ddSMatthew G. Knepley 
492844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
492944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49304c4edb6fSBarry Smith   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS); // DMDA does not use fields field in DM
493144a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
49329566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
49339566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
493444a7f3ddSMatthew G. Knepley   }
49359566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
493644a7f3ddSMatthew G. Knepley   dm->fields = NULL;
493744a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
49383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
493944a7f3ddSMatthew G. Knepley }
494044a7f3ddSMatthew G. Knepley 
4941689b5837SMatthew G. Knepley /*@
494220f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4943689b5837SMatthew G. Knepley 
494420f4b53cSBarry Smith   Not Collective
4945689b5837SMatthew G. Knepley 
4946689b5837SMatthew G. Knepley   Input Parameter:
494720f4b53cSBarry Smith . dm - The `DM`
4948689b5837SMatthew G. Knepley 
4949689b5837SMatthew G. Knepley   Output Parameter:
495060225df5SJacob Faibussowitsch . numFields - The number of fields
4951689b5837SMatthew G. Knepley 
4952689b5837SMatthew G. Knepley   Level: intermediate
4953689b5837SMatthew G. Knepley 
49541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4955689b5837SMatthew G. Knepley @*/
4956d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4957d71ae5a4SJacob Faibussowitsch {
49580f21e855SMatthew G. Knepley   PetscFunctionBegin;
49590f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49604f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
496144a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
49623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4963af122d2aSMatthew G Knepley }
4964af122d2aSMatthew G Knepley 
4965689b5837SMatthew G. Knepley /*@
496620f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4967689b5837SMatthew G. Knepley 
496820f4b53cSBarry Smith   Logically Collective
4969689b5837SMatthew G. Knepley 
4970689b5837SMatthew G. Knepley   Input Parameters:
497120f4b53cSBarry Smith + dm        - The `DM`
497260225df5SJacob Faibussowitsch - numFields - The number of fields
4973689b5837SMatthew G. Knepley 
4974689b5837SMatthew G. Knepley   Level: intermediate
4975689b5837SMatthew G. Knepley 
49761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4977689b5837SMatthew G. Knepley @*/
4978d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4979d71ae5a4SJacob Faibussowitsch {
49800f21e855SMatthew G. Knepley   PetscInt Nf, f;
4981af122d2aSMatthew G Knepley 
4982af122d2aSMatthew G Knepley   PetscFunctionBegin;
4983af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49849566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49850f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49860f21e855SMatthew G. Knepley     PetscContainer obj;
49870f21e855SMatthew G. Knepley 
49889566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49899566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
49909566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4991af122d2aSMatthew G Knepley   }
49923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4993af122d2aSMatthew G Knepley }
4994af122d2aSMatthew G Knepley 
4995c1929be8SMatthew G. Knepley /*@
4996bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4997c1929be8SMatthew G. Knepley 
499820f4b53cSBarry Smith   Not Collective
4999c1929be8SMatthew G. Knepley 
5000c1929be8SMatthew G. Knepley   Input Parameters:
5001bb7acecfSBarry Smith + dm - The `DM`
5002c1929be8SMatthew G. Knepley - f  - The field number
5003c1929be8SMatthew G. Knepley 
500444a7f3ddSMatthew G. Knepley   Output Parameters:
500520f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
500620f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
5007c1929be8SMatthew G. Knepley 
500844a7f3ddSMatthew G. Knepley   Level: intermediate
5009c1929be8SMatthew G. Knepley 
50101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
5011c1929be8SMatthew G. Knepley @*/
5012d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
5013d71ae5a4SJacob Faibussowitsch {
5014af122d2aSMatthew G Knepley   PetscFunctionBegin;
5015af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50164f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
50177a8be351SBarry 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);
50184c4edb6fSBarry Smith   if (!dm->fields) {
50194c4edb6fSBarry Smith     if (label) *label = NULL;
50204c4edb6fSBarry Smith     if (disc) *disc = NULL;
50214c4edb6fSBarry Smith   } else { // some DM such as DMDA do not have dm->fields
502244a7f3ddSMatthew G. Knepley     if (label) *label = dm->fields[f].label;
5023bb7acecfSBarry Smith     if (disc) *disc = dm->fields[f].disc;
50244c4edb6fSBarry Smith   }
50253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5026decb47aaSMatthew G. Knepley }
5027decb47aaSMatthew G. Knepley 
5028083401c6SMatthew G. Knepley /* Does not clear the DS */
5029d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5030d71ae5a4SJacob Faibussowitsch {
5031083401c6SMatthew G. Knepley   PetscFunctionBegin;
50329566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
50339566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
50349566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
5035083401c6SMatthew G. Knepley   dm->fields[f].label = label;
5036bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
50379566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5038835f2295SStefano Zampini   PetscCall(PetscObjectReference(disc));
50393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5040083401c6SMatthew G. Knepley }
5041083401c6SMatthew G. Knepley 
5042ffeef943SBarry Smith /*@
5043bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
5044bb7acecfSBarry Smith   the field numbering.
5045c1929be8SMatthew G. Knepley 
504620f4b53cSBarry Smith   Logically Collective
5047c1929be8SMatthew G. Knepley 
5048c1929be8SMatthew G. Knepley   Input Parameters:
5049bb7acecfSBarry Smith + dm    - The `DM`
5050c1929be8SMatthew G. Knepley . f     - The field number
505120f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5052bb7acecfSBarry Smith - disc  - The discretization object
5053c1929be8SMatthew G. Knepley 
505444a7f3ddSMatthew G. Knepley   Level: intermediate
5055c1929be8SMatthew G. Knepley 
50561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
5057c1929be8SMatthew G. Knepley @*/
5058d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5059d71ae5a4SJacob Faibussowitsch {
5060decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5061decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5062e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
5063bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50647a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5065bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5066bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50679566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
506944a7f3ddSMatthew G. Knepley }
507044a7f3ddSMatthew G. Knepley 
5071ffeef943SBarry Smith /*@
5072bb7acecfSBarry 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)
5073bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
507444a7f3ddSMatthew G. Knepley 
507520f4b53cSBarry Smith   Logically Collective
507644a7f3ddSMatthew G. Knepley 
507744a7f3ddSMatthew G. Knepley   Input Parameters:
5078bb7acecfSBarry Smith + dm    - The `DM`
507920f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5080bb7acecfSBarry Smith - disc  - The discretization object
508144a7f3ddSMatthew G. Knepley 
508244a7f3ddSMatthew G. Knepley   Level: intermediate
508344a7f3ddSMatthew G. Knepley 
5084bb7acecfSBarry Smith   Notes:
5085bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5086bb7acecfSBarry Smith 
5087da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5088bb7acecfSBarry 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
5089bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5090bb7acecfSBarry Smith 
5091ce78bad3SBarry Smith   Fortran Note:
5092ce78bad3SBarry Smith   Use the argument `PetscObjectCast(disc)` as the second argument
5093ce78bad3SBarry Smith 
50941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
509544a7f3ddSMatthew G. Knepley @*/
5096d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5097d71ae5a4SJacob Faibussowitsch {
509844a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
509944a7f3ddSMatthew G. Knepley 
510044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
510144a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5102064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5103bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
51049566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
510544a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5106bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
51079566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5108835f2295SStefano Zampini   PetscCall(PetscObjectReference(disc));
5109bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
51109566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
51113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5112af122d2aSMatthew G Knepley }
51136636e97aSMatthew G Knepley 
5114e5e52638SMatthew G. Knepley /*@
5115e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5116e0b68406SMatthew Knepley 
511720f4b53cSBarry Smith   Logically Collective
5118e0b68406SMatthew Knepley 
5119e0b68406SMatthew Knepley   Input Parameters:
5120bb7acecfSBarry Smith + dm          - The `DM`
5121e0b68406SMatthew Knepley . f           - The field index
5122bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5123e0b68406SMatthew Knepley 
5124e0b68406SMatthew Knepley   Level: intermediate
5125e0b68406SMatthew Knepley 
51261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5127e0b68406SMatthew Knepley @*/
5128d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5129d71ae5a4SJacob Faibussowitsch {
5130e0b68406SMatthew Knepley   PetscFunctionBegin;
513163a3b9bcSJacob 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);
5132e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
51333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5134e0b68406SMatthew Knepley }
5135e0b68406SMatthew Knepley 
5136e0b68406SMatthew Knepley /*@
5137e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5138e0b68406SMatthew Knepley 
513920f4b53cSBarry Smith   Not Collective
5140e0b68406SMatthew Knepley 
5141e0b68406SMatthew Knepley   Input Parameters:
5142bb7acecfSBarry Smith + dm - The `DM`
5143e0b68406SMatthew Knepley - f  - The field index
5144e0b68406SMatthew Knepley 
5145e0b68406SMatthew Knepley   Output Parameter:
5146e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5147e0b68406SMatthew Knepley 
5148e0b68406SMatthew Knepley   Level: intermediate
5149e0b68406SMatthew Knepley 
515060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5151e0b68406SMatthew Knepley @*/
5152d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5153d71ae5a4SJacob Faibussowitsch {
5154e0b68406SMatthew Knepley   PetscFunctionBegin;
515563a3b9bcSJacob 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);
5156e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
51573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5158e0b68406SMatthew Knepley }
5159e0b68406SMatthew Knepley 
5160e0b68406SMatthew Knepley /*@
5161bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5162e5e52638SMatthew G. Knepley 
516320f4b53cSBarry Smith   Collective
5164e5e52638SMatthew G. Knepley 
5165bb4b53efSMatthew G. Knepley   Input Parameters:
5166bb4b53efSMatthew G. Knepley + dm        - The `DM`
5167bb4b53efSMatthew G. Knepley . minDegree - Minimum degree for a discretization, or `PETSC_DETERMINE` for no limit
5168bb4b53efSMatthew G. Knepley - maxDegree - Maximum degree for a discretization, or `PETSC_DETERMINE` for no limit
5169e5e52638SMatthew G. Knepley 
5170e5e52638SMatthew G. Knepley   Output Parameter:
5171bb7acecfSBarry Smith . newdm - The `DM`
5172e5e52638SMatthew G. Knepley 
5173e5e52638SMatthew G. Knepley   Level: advanced
5174e5e52638SMatthew G. Knepley 
51751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5176e5e52638SMatthew G. Knepley @*/
5177bb4b53efSMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, PetscInt minDegree, PetscInt maxDegree, DM newdm)
5178d71ae5a4SJacob Faibussowitsch {
5179e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5180e5e52638SMatthew G. Knepley 
5181e5e52638SMatthew G. Knepley   PetscFunctionBegin;
51823ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
51839566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
51849566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5185e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5186e5e52638SMatthew G. Knepley     DMLabel      label;
5187e5e52638SMatthew G. Knepley     PetscObject  field;
5188bb4b53efSMatthew G. Knepley     PetscClassId id;
518934aa8a36SMatthew G. Knepley     PetscBool    useCone, useClosure;
5190e5e52638SMatthew G. Knepley 
51919566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
5192bb4b53efSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(field, &id));
5193bb4b53efSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
5194bb4b53efSMatthew G. Knepley       PetscFE newfe;
5195bb4b53efSMatthew G. Knepley 
5196bb4b53efSMatthew G. Knepley       PetscCall(PetscFELimitDegree((PetscFE)field, minDegree, maxDegree, &newfe));
5197bb4b53efSMatthew G. Knepley       PetscCall(DMSetField(newdm, f, label, (PetscObject)newfe));
5198bb4b53efSMatthew G. Knepley       PetscCall(PetscFEDestroy(&newfe));
5199bb4b53efSMatthew G. Knepley     } else {
52009566063dSJacob Faibussowitsch       PetscCall(DMSetField(newdm, f, label, field));
5201bb4b53efSMatthew G. Knepley     }
52029566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
52039566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
520434aa8a36SMatthew G. Knepley   }
52053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
520634aa8a36SMatthew G. Knepley }
520734aa8a36SMatthew G. Knepley 
520834aa8a36SMatthew G. Knepley /*@
520934aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
521034aa8a36SMatthew G. Knepley 
521120f4b53cSBarry Smith   Not Collective
521234aa8a36SMatthew G. Knepley 
521334aa8a36SMatthew G. Knepley   Input Parameters:
521420f4b53cSBarry Smith + dm - The `DM` object
521520f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
521634aa8a36SMatthew G. Knepley 
5217d8d19677SJose E. Roman   Output Parameters:
521834aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
521934aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
522034aa8a36SMatthew G. Knepley 
522134aa8a36SMatthew G. Knepley   Level: developer
522234aa8a36SMatthew G. Knepley 
522320f4b53cSBarry Smith   Notes:
522420f4b53cSBarry Smith .vb
522520f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
522620f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
522720f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
522820f4b53cSBarry Smith .ve
522920f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
523020f4b53cSBarry Smith 
52311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
523234aa8a36SMatthew G. Knepley @*/
5233d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5234d71ae5a4SJacob Faibussowitsch {
523534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
523634aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52374f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
52384f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
523934aa8a36SMatthew G. Knepley   if (f < 0) {
524034aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
524134aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
524234aa8a36SMatthew G. Knepley   } else {
524334aa8a36SMatthew G. Knepley     PetscInt Nf;
524434aa8a36SMatthew G. Knepley 
52459566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52467a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
524734aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
524834aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
524934aa8a36SMatthew G. Knepley   }
52503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
525134aa8a36SMatthew G. Knepley }
525234aa8a36SMatthew G. Knepley 
525334aa8a36SMatthew G. Knepley /*@
525434aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
525534aa8a36SMatthew G. Knepley 
525620f4b53cSBarry Smith   Not Collective
525734aa8a36SMatthew G. Knepley 
525834aa8a36SMatthew G. Knepley   Input Parameters:
525920f4b53cSBarry Smith + dm         - The `DM` object
526034aa8a36SMatthew G. Knepley . f          - The field number
526134aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
526234aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
526334aa8a36SMatthew G. Knepley 
526434aa8a36SMatthew G. Knepley   Level: developer
526534aa8a36SMatthew G. Knepley 
526620f4b53cSBarry Smith   Notes:
526720f4b53cSBarry Smith .vb
526820f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
526920f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
527020f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
527120f4b53cSBarry Smith .ve
527220f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
527320f4b53cSBarry Smith 
52741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
527534aa8a36SMatthew G. Knepley @*/
5276d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5277d71ae5a4SJacob Faibussowitsch {
527834aa8a36SMatthew G. Knepley   PetscFunctionBegin;
527934aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
528034aa8a36SMatthew G. Knepley   if (f < 0) {
528134aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
528234aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
528334aa8a36SMatthew G. Knepley   } else {
528434aa8a36SMatthew G. Knepley     PetscInt Nf;
528534aa8a36SMatthew G. Knepley 
52869566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52877a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
528834aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
528934aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5290e5e52638SMatthew G. Knepley   }
52913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5292e5e52638SMatthew G. Knepley }
5293e5e52638SMatthew G. Knepley 
5294b0441da4SMatthew G. Knepley /*@
5295b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5296b0441da4SMatthew G. Knepley 
5297b0441da4SMatthew G. Knepley   Not collective
5298b0441da4SMatthew G. Knepley 
5299f899ff85SJose E. Roman   Input Parameter:
530020f4b53cSBarry Smith . dm - The `DM` object
5301b0441da4SMatthew G. Knepley 
5302d8d19677SJose E. Roman   Output Parameters:
5303b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5304b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5305b0441da4SMatthew G. Knepley 
5306b0441da4SMatthew G. Knepley   Level: developer
5307b0441da4SMatthew G. Knepley 
530820f4b53cSBarry Smith   Notes:
530920f4b53cSBarry Smith .vb
531020f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
531120f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
531220f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
531320f4b53cSBarry Smith .ve
531420f4b53cSBarry Smith 
53151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5316b0441da4SMatthew G. Knepley @*/
5317d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5318d71ae5a4SJacob Faibussowitsch {
5319b0441da4SMatthew G. Knepley   PetscInt Nf;
5320b0441da4SMatthew G. Knepley 
5321b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5322b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53234f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
53244f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
53259566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5326b0441da4SMatthew G. Knepley   if (!Nf) {
53279566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5328b0441da4SMatthew G. Knepley   } else {
53299566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5330b0441da4SMatthew G. Knepley   }
53313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5332b0441da4SMatthew G. Knepley }
5333b0441da4SMatthew G. Knepley 
5334b0441da4SMatthew G. Knepley /*@
5335b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5336b0441da4SMatthew G. Knepley 
533720f4b53cSBarry Smith   Not Collective
5338b0441da4SMatthew G. Knepley 
5339b0441da4SMatthew G. Knepley   Input Parameters:
534020f4b53cSBarry Smith + dm         - The `DM` object
5341b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5342b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5343b0441da4SMatthew G. Knepley 
5344b0441da4SMatthew G. Knepley   Level: developer
5345b0441da4SMatthew G. Knepley 
534620f4b53cSBarry Smith   Notes:
534720f4b53cSBarry Smith .vb
534820f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
534920f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
535020f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
535120f4b53cSBarry Smith .ve
535220f4b53cSBarry Smith 
53531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5354b0441da4SMatthew G. Knepley @*/
5355d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5356d71ae5a4SJacob Faibussowitsch {
5357b0441da4SMatthew G. Knepley   PetscInt Nf;
5358b0441da4SMatthew G. Knepley 
5359b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5360b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53619566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5362b0441da4SMatthew G. Knepley   if (!Nf) {
53639566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5364b0441da4SMatthew G. Knepley   } else {
53659566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5366e5e52638SMatthew G. Knepley   }
53673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5368e5e52638SMatthew G. Knepley }
5369e5e52638SMatthew G. Knepley 
5370d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5371d71ae5a4SJacob Faibussowitsch {
5372799db056SMatthew G. Knepley   DM           plex;
5373799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5374799db056SMatthew G. Knepley   const char **names;
5375799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5376799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5377799db056SMatthew G. Knepley   size_t       len;
5378799db056SMatthew G. Knepley   MPI_Comm     comm;
5379799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5380783e2ec8SMatthew G. Knepley 
5381783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5382799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5383799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5384799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5385799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5386799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5387799db056SMatthew G. Knepley     PetscDS  dsBC;
5388799db056SMatthew G. Knepley     PetscInt numBd;
5389799db056SMatthew G. Knepley 
539007218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5391799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5392799db056SMatthew G. Knepley     maxLabels += numBd;
5393799db056SMatthew G. Knepley   }
5394799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5395799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5396799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5397799db056SMatthew G. Knepley     PetscDS  dsBC;
5398799db056SMatthew G. Knepley     PetscInt numBd, bd;
5399799db056SMatthew G. Knepley 
540007218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5401799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5402799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5403799db056SMatthew G. Knepley       DMLabel      label;
5404799db056SMatthew G. Knepley       PetscInt     field;
5405799db056SMatthew G. Knepley       PetscObject  obj;
5406799db056SMatthew G. Knepley       PetscClassId id;
5407799db056SMatthew G. Knepley 
5408799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
54099566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
54109566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5411fa74d514SMatthew G. Knepley       if (id != PETSCFE_CLASSID || !label) continue;
54129371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
54139371c9d4SSatish Balay         if (labels[l] == label) break;
5414799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5415783e2ec8SMatthew G. Knepley     }
5416799db056SMatthew G. Knepley   }
5417799db056SMatthew G. Knepley   /* Get label names */
5418799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5419799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
54209371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
54219371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
54229371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
54239371c9d4SSatish Balay   }
5424799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5425462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5426799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5427c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5428799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5429799db056SMatthew G. Knepley   /* Put all names on all processes */
5430799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5431799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5432799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5433799db056SMatthew G. Knepley   gNl = displs[size];
54349371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
54359371c9d4SSatish Balay     counts[p] *= gmaxLen;
54369371c9d4SSatish Balay     displs[p] *= gmaxLen;
54379371c9d4SSatish Balay   }
5438799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5439799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5440799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5441799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5442799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5443799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5444799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
54459371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
54465d6b2bfcSJames Wright       if (glabels[m] == glabels[gl]) goto next_label;
54479566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5448799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
54499566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5450799db056SMatthew G. Knepley     ++gl;
54515d6b2bfcSJames Wright   next_label:
54525d6b2bfcSJames Wright     continue;
5453783e2ec8SMatthew G. Knepley   }
5454799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
54553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5456783e2ec8SMatthew G. Knepley }
5457783e2ec8SMatthew G. Knepley 
5458d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5459d71ae5a4SJacob Faibussowitsch {
5460e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5461e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5462e5e52638SMatthew G. Knepley 
5463e5e52638SMatthew G. Knepley   PetscFunctionBegin;
54643ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
54659566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5466e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
54679371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
54689371c9d4SSatish Balay     tmpd[s].ds     = NULL;
54699371c9d4SSatish Balay     tmpd[s].label  = NULL;
54709371c9d4SSatish Balay     tmpd[s].fields = NULL;
54719371c9d4SSatish Balay   }
54729566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5473e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5474e5e52638SMatthew G. Knepley   dm->probs = tmpd;
54753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5476e5e52638SMatthew G. Knepley }
5477e5e52638SMatthew G. Knepley 
5478e5e52638SMatthew G. Knepley /*@
547920f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5480e5e52638SMatthew G. Knepley 
548120f4b53cSBarry Smith   Not Collective
5482e5e52638SMatthew G. Knepley 
5483e5e52638SMatthew G. Knepley   Input Parameter:
548420f4b53cSBarry Smith . dm - The `DM`
5485e5e52638SMatthew G. Knepley 
5486e5e52638SMatthew G. Knepley   Output Parameter:
548720f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5488e5e52638SMatthew G. Knepley 
5489e5e52638SMatthew G. Knepley   Level: intermediate
5490e5e52638SMatthew G. Knepley 
54911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5492e5e52638SMatthew G. Knepley @*/
5493d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5494d71ae5a4SJacob Faibussowitsch {
5495e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5496e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54974f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5498e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
54993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5500e5e52638SMatthew G. Knepley }
5501e5e52638SMatthew G. Knepley 
5502e5e52638SMatthew G. Knepley /*@
550320f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5504e5e52638SMatthew G. Knepley 
550520f4b53cSBarry Smith   Logically Collective
5506e5e52638SMatthew G. Knepley 
5507e5e52638SMatthew G. Knepley   Input Parameter:
550820f4b53cSBarry Smith . dm - The `DM`
5509e5e52638SMatthew G. Knepley 
5510e5e52638SMatthew G. Knepley   Level: intermediate
5511e5e52638SMatthew G. Knepley 
55121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5513e5e52638SMatthew G. Knepley @*/
5514d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5515d71ae5a4SJacob Faibussowitsch {
5516e5e52638SMatthew G. Knepley   PetscInt s;
5517e5e52638SMatthew G. Knepley 
5518e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5519e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5520e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
55219566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
552207218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
55239566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
55249566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5525e5e52638SMatthew G. Knepley   }
55269566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5527e5e52638SMatthew G. Knepley   dm->probs = NULL;
5528e5e52638SMatthew G. Knepley   dm->Nds   = 0;
55293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5530e5e52638SMatthew G. Knepley }
5531e5e52638SMatthew G. Knepley 
5532e5e52638SMatthew G. Knepley /*@
553320f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5534e5e52638SMatthew G. Knepley 
553520f4b53cSBarry Smith   Not Collective
5536e5e52638SMatthew G. Knepley 
5537e5e52638SMatthew G. Knepley   Input Parameter:
553820f4b53cSBarry Smith . dm - The `DM`
5539e5e52638SMatthew G. Knepley 
5540e5e52638SMatthew G. Knepley   Output Parameter:
554107218a29SMatthew G. Knepley . ds - The default `PetscDS`
5542e5e52638SMatthew G. Knepley 
5543e5e52638SMatthew G. Knepley   Level: intermediate
5544e5e52638SMatthew G. Knepley 
5545ce78bad3SBarry Smith   Note:
5546ce78bad3SBarry Smith   The `ds` is owned by the `dm` and should not be destroyed directly.
5547ce78bad3SBarry Smith 
55481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5549e5e52638SMatthew G. Knepley @*/
555007218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5551d71ae5a4SJacob Faibussowitsch {
5552e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5553e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55544f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
555507218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
555607218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
55573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5558e5e52638SMatthew G. Knepley }
5559e5e52638SMatthew G. Knepley 
5560e5e52638SMatthew G. Knepley /*@
556120f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5562e5e52638SMatthew G. Knepley 
556320f4b53cSBarry Smith   Not Collective
5564e5e52638SMatthew G. Knepley 
5565e5e52638SMatthew G. Knepley   Input Parameters:
556620f4b53cSBarry Smith + dm    - The `DM`
556720f4b53cSBarry Smith - point - Cell for the `PetscDS`
5568e5e52638SMatthew G. Knepley 
556907218a29SMatthew G. Knepley   Output Parameters:
557007218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
557107218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5572e5e52638SMatthew G. Knepley 
5573e5e52638SMatthew G. Knepley   Level: developer
5574e5e52638SMatthew G. Knepley 
55751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5576e5e52638SMatthew G. Knepley @*/
557707218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5578d71ae5a4SJacob Faibussowitsch {
557907218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5580e5e52638SMatthew G. Knepley   PetscInt s;
5581e5e52638SMatthew G. Knepley 
5582e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5583e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55844f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
55854f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
558663a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
558707218a29SMatthew G. Knepley   if (ds) *ds = NULL;
558807218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5589e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5590e5e52638SMatthew G. Knepley     PetscInt val;
5591e5e52638SMatthew G. Knepley 
55929371c9d4SSatish Balay     if (!dm->probs[s].label) {
559307218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
55949371c9d4SSatish Balay     } else {
55959566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
55969371c9d4SSatish Balay       if (val >= 0) {
559707218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
559807218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
55999371c9d4SSatish Balay         break;
56009371c9d4SSatish Balay       }
5601e5e52638SMatthew G. Knepley     }
5602e5e52638SMatthew G. Knepley   }
560307218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
56043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5605e5e52638SMatthew G. Knepley }
5606e5e52638SMatthew G. Knepley 
5607e5e52638SMatthew G. Knepley /*@
560820f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5609e5e52638SMatthew G. Knepley 
561020f4b53cSBarry Smith   Not Collective
5611e5e52638SMatthew G. Knepley 
5612e5e52638SMatthew G. Knepley   Input Parameters:
561320f4b53cSBarry Smith + dm    - The `DM`
561420f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5615e5e52638SMatthew G. Knepley 
5616b3cf3223SMatthew G. Knepley   Output Parameters:
561720f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
561807218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
561907218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5620e5e52638SMatthew G. Knepley 
5621e5e52638SMatthew G. Knepley   Level: advanced
5622e5e52638SMatthew G. Knepley 
562320f4b53cSBarry Smith   Note:
562420f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
562520f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
562607218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
562720f4b53cSBarry Smith 
56281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5629e5e52638SMatthew G. Knepley @*/
563007218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5631d71ae5a4SJacob Faibussowitsch {
5632e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5633e5e52638SMatthew G. Knepley 
5634e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5635e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5636e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
56379371c9d4SSatish Balay   if (fields) {
56384f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
56399371c9d4SSatish Balay     *fields = NULL;
56409371c9d4SSatish Balay   }
56419371c9d4SSatish Balay   if (ds) {
56424f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
56439371c9d4SSatish Balay     *ds = NULL;
56449371c9d4SSatish Balay   }
564507218a29SMatthew G. Knepley   if (dsIn) {
56464f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
564707218a29SMatthew G. Knepley     *dsIn = NULL;
564807218a29SMatthew G. Knepley   }
5649e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5650154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5651b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5652b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
565307218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
56543ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5655b3cf3223SMatthew G. Knepley     }
5656e5e52638SMatthew G. Knepley   }
56573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5658e5e52638SMatthew G. Knepley }
5659e5e52638SMatthew G. Knepley 
5660e5e52638SMatthew G. Knepley /*@
5661bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5662083401c6SMatthew G. Knepley 
566320f4b53cSBarry Smith   Collective
5664083401c6SMatthew G. Knepley 
5665083401c6SMatthew G. Knepley   Input Parameters:
5666bb7acecfSBarry Smith + dm     - The `DM`
566720f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
566807218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
566907218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
567007218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5671083401c6SMatthew G. Knepley 
567220f4b53cSBarry Smith   Level: advanced
567320f4b53cSBarry Smith 
5674bb7acecfSBarry Smith   Note:
5675bb7acecfSBarry 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,
5676083401c6SMatthew G. Knepley   the fields argument is ignored.
5677083401c6SMatthew G. Knepley 
56781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5679083401c6SMatthew G. Knepley @*/
568007218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5681d71ae5a4SJacob Faibussowitsch {
5682083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5683083401c6SMatthew G. Knepley 
5684083401c6SMatthew G. Knepley   PetscFunctionBegin;
5685083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5686083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
568707218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5688064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
568907218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5690083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5691083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
56929566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
569307218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5694083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
569507218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
56963ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5697083401c6SMatthew G. Knepley     }
5698083401c6SMatthew G. Knepley   }
56999566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
57009566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57019566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
57029566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
570307218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5704083401c6SMatthew G. Knepley   if (!label) {
5705083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5706083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5707083401c6SMatthew G. Knepley     Nds = 0;
5708083401c6SMatthew G. Knepley   }
5709083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5710083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5711083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
571207218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
57133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5714083401c6SMatthew G. Knepley }
5715083401c6SMatthew G. Knepley 
5716083401c6SMatthew G. Knepley /*@
571720f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5718e5e52638SMatthew G. Knepley 
571920f4b53cSBarry Smith   Not Collective
5720e5e52638SMatthew G. Knepley 
5721e5e52638SMatthew G. Knepley   Input Parameters:
572220f4b53cSBarry Smith + dm  - The `DM`
5723e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5724e5e52638SMatthew G. Knepley 
5725e5e52638SMatthew G. Knepley   Output Parameters:
572620f4b53cSBarry Smith + label  - The region label, or `NULL`
572720f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
572807218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
572907218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5730e5e52638SMatthew G. Knepley 
5731e5e52638SMatthew G. Knepley   Level: advanced
5732e5e52638SMatthew G. Knepley 
57331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5734e5e52638SMatthew G. Knepley @*/
573507218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5736d71ae5a4SJacob Faibussowitsch {
5737e5e52638SMatthew G. Knepley   PetscInt Nds;
5738e5e52638SMatthew G. Knepley 
5739e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5740e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57419566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
574263a3b9bcSJacob 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);
5743e5e52638SMatthew G. Knepley   if (label) {
57444f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5745e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5746e5e52638SMatthew G. Knepley   }
5747b3cf3223SMatthew G. Knepley   if (fields) {
57484f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5749b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5750b3cf3223SMatthew G. Knepley   }
5751e5e52638SMatthew G. Knepley   if (ds) {
57524f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5753e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5754e5e52638SMatthew G. Knepley   }
575507218a29SMatthew G. Knepley   if (dsIn) {
57564f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
575707218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
575807218a29SMatthew G. Knepley   }
57593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5760e5e52638SMatthew G. Knepley }
5761e5e52638SMatthew G. Knepley 
5762e5e52638SMatthew G. Knepley /*@
576320f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5764e5e52638SMatthew G. Knepley 
576520f4b53cSBarry Smith   Not Collective
5766e5e52638SMatthew G. Knepley 
5767e5e52638SMatthew G. Knepley   Input Parameters:
576820f4b53cSBarry Smith + dm     - The `DM`
5769083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
577020f4b53cSBarry Smith . label  - The region label, or `NULL`
577107218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
577207218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
577307218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5774e5e52638SMatthew G. Knepley 
5775e5e52638SMatthew G. Knepley   Level: advanced
5776e5e52638SMatthew G. Knepley 
57771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5778e5e52638SMatthew G. Knepley @*/
577907218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5780d71ae5a4SJacob Faibussowitsch {
5781083401c6SMatthew G. Knepley   PetscInt Nds;
5782e5e52638SMatthew G. Knepley 
5783e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5784e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5785ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
57869566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
578763a3b9bcSJacob 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);
57889566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57899566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5790083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5791083401c6SMatthew G. Knepley   if (fields) {
5792083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
57939566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
57949566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5795083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5796e5e52638SMatthew G. Knepley   }
5797083401c6SMatthew G. Knepley   if (ds) {
5798083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
57999566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
58009566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5801083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5802083401c6SMatthew G. Knepley   }
580307218a29SMatthew G. Knepley   if (dsIn) {
580407218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
580507218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
580607218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
580707218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
580807218a29SMatthew G. Knepley   }
58093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5810e5e52638SMatthew G. Knepley }
5811e5e52638SMatthew G. Knepley 
5812e5e52638SMatthew G. Knepley /*@
581320f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
58141d3af9e0SMatthew G. Knepley 
581520f4b53cSBarry Smith   Not Collective
58161d3af9e0SMatthew G. Knepley 
58171d3af9e0SMatthew G. Knepley   Input Parameters:
581820f4b53cSBarry Smith + dm - The `DM`
581920f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
58201d3af9e0SMatthew G. Knepley 
58211d3af9e0SMatthew G. Knepley   Output Parameter:
58221d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
58231d3af9e0SMatthew G. Knepley 
58241d3af9e0SMatthew G. Knepley   Level: advanced
58251d3af9e0SMatthew G. Knepley 
58261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
58271d3af9e0SMatthew G. Knepley @*/
5828d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5829d71ae5a4SJacob Faibussowitsch {
58301d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
58311d3af9e0SMatthew G. Knepley 
58321d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
58331d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58341d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
58354f572ea9SToby Isaac   PetscAssertPointer(num, 3);
58369566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
58379371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
58389371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
58391d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
58401d3af9e0SMatthew G. Knepley   else *num = n;
58413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58421d3af9e0SMatthew G. Knepley }
58431d3af9e0SMatthew G. Knepley 
5844cc4c1da9SBarry Smith /*@
5845bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
58462df84da0SMatthew G. Knepley 
584720f4b53cSBarry Smith   Not Collective
58482df84da0SMatthew G. Knepley 
5849f1a722f8SMatthew G. Knepley   Input Parameters:
5850bb7acecfSBarry Smith + dm     - The `DM`
58512df84da0SMatthew G. Knepley . Nc     - The number of components for the field
585220f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5853bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
58542df84da0SMatthew G. Knepley 
58552df84da0SMatthew G. Knepley   Output Parameter:
5856bb7acecfSBarry Smith . fem - The `PetscFE`
58572df84da0SMatthew G. Knepley 
585820f4b53cSBarry Smith   Level: intermediate
585920f4b53cSBarry Smith 
5860bb7acecfSBarry Smith   Note:
5861bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
58622df84da0SMatthew G. Knepley 
58631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
58642df84da0SMatthew G. Knepley @*/
5865d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5866d71ae5a4SJacob Faibussowitsch {
58672df84da0SMatthew G. Knepley   DMPolytopeType ct;
58682df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
58692df84da0SMatthew G. Knepley 
58702df84da0SMatthew G. Knepley   PetscFunctionBegin;
58712df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58722df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
58734f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
58742df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
58754f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
58769566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
58779566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
58789566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
58799566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
58803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58812df84da0SMatthew G. Knepley }
58822df84da0SMatthew G. Knepley 
58831d3af9e0SMatthew G. Knepley /*@
5884bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5885e5e52638SMatthew G. Knepley 
588620f4b53cSBarry Smith   Collective
5887e5e52638SMatthew G. Knepley 
5888e5e52638SMatthew G. Knepley   Input Parameter:
5889bb7acecfSBarry Smith . dm - The `DM`
5890e5e52638SMatthew G. Knepley 
589120f4b53cSBarry Smith   Options Database Key:
5892bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
589345480ffeSMatthew G. Knepley 
589420f4b53cSBarry Smith   Level: intermediate
589520f4b53cSBarry Smith 
5896ce78bad3SBarry Smith   Developer Note:
5897ce78bad3SBarry Smith   The name of this function is wrong. Create functions always return the created object as one of the arguments.
5898ce78bad3SBarry Smith 
58991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5900e5e52638SMatthew G. Knepley @*/
5901d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5902d71ae5a4SJacob Faibussowitsch {
5903e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5904083401c6SMatthew G. Knepley   PetscDS   dsDef;
5905083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5906f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5907f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5908e5e52638SMatthew G. Knepley 
5909e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5910e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59113ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
59129566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
59139566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5914083401c6SMatthew G. Knepley   /* Determine how many regions we have */
59159566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5916083401c6SMatthew G. Knepley   Nl   = 0;
5917083401c6SMatthew G. Knepley   Ndef = 0;
5918083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5919083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5920083401c6SMatthew G. Knepley     PetscInt l;
5921083401c6SMatthew G. Knepley 
5922f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5923f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5924f918ec44SMatthew G. Knepley     {
5925f918ec44SMatthew G. Knepley       PetscClassId id;
5926f918ec44SMatthew G. Knepley 
59279566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5928f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5929f918ec44SMatthew G. Knepley         Ceed ceed;
5930f918ec44SMatthew G. Knepley 
59319566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
59329566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5933f918ec44SMatthew G. Knepley       }
5934f918ec44SMatthew G. Knepley     }
5935f918ec44SMatthew G. Knepley #endif
59369371c9d4SSatish Balay     if (!label) {
59379371c9d4SSatish Balay       ++Ndef;
59389371c9d4SSatish Balay       continue;
59399371c9d4SSatish Balay     }
59409371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
59419371c9d4SSatish Balay       if (label == labelSet[l]) break;
5942083401c6SMatthew G. Knepley     if (l < Nl) continue;
5943083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5944083401c6SMatthew G. Knepley   }
5945083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
594607218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5947083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5948b3cf3223SMatthew G. Knepley     IS        fields;
5949b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5950b3cf3223SMatthew G. Knepley 
59519371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59529371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
59537a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
59549566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59559371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59569371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
59579566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59589566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59599566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59609566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
596188f0c812SMatthew G. Knepley 
59629566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
596307218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
59649566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59659566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
59662df9ee95SMatthew G. Knepley   }
596707218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
59689566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5969083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5970083401c6SMatthew G. Knepley   if (Ndef && Nl) {
59710122748bSMatthew G. Knepley     DM              plex;
5972083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5973083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5974083401c6SMatthew G. Knepley     PetscInt       *fields;
5975083401c6SMatthew G. Knepley     const PetscInt *cells;
5976083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
59770122748bSMatthew G. Knepley 
59789566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
59799566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
59809566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
59819566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
59825fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5983083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5984083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5985083401c6SMatthew G. Knepley       IS      pointIS;
5986083401c6SMatthew G. Knepley 
59879566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
59889566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
59899566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
59909566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5991083401c6SMatthew G. Knepley     }
59929566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5993083401c6SMatthew G. Knepley 
59949566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
59959566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
59969566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
59979566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
59989566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
59999566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
60009566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
6001083401c6SMatthew G. Knepley 
60029566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
60039371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
60049371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
60059566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
60069566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
60079566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
60089566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
6009083401c6SMatthew G. Knepley 
60109566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
601107218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
60129566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
60139566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
60149566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
60159566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
60169566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
6017083401c6SMatthew G. Knepley   }
6018083401c6SMatthew G. Knepley   /* Create label DSes
6019083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
6020083401c6SMatthew G. Knepley   */
6021083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
6022083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
6023083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
602407218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
6025083401c6SMatthew G. Knepley     IS        fields;
6026083401c6SMatthew G. Knepley     PetscInt *fld, nf;
6027083401c6SMatthew G. Knepley 
60289566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
60299371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
60309371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
60319566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
60329371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
60339371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
60349566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
60359566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
60369566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
60379566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
60389566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
6039083401c6SMatthew G. Knepley     {
6040083401c6SMatthew G. Knepley       DMPolytopeType ct;
6041083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
60425fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
60430122748bSMatthew G. Knepley 
60449566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
6045665f567fSMatthew G. Knepley       if (lStart >= 0) {
60469566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
6047412e9a14SMatthew G. Knepley         switch (ct) {
6048412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
6049412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
6050412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
6051d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
6052d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
6053d71ae5a4SJacob Faibussowitsch           break;
6054d71ae5a4SJacob Faibussowitsch         default:
6055d71ae5a4SJacob Faibussowitsch           break;
6056412e9a14SMatthew G. Knepley         }
6057665f567fSMatthew G. Knepley       }
6058462c564dSBarry Smith       PetscCallMPI(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
605907218a29SMatthew G. Knepley       if (isCohesive) {
606007218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
606107218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
606207218a29SMatthew G. Knepley       }
60635fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
60645fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
60655fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
60669566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
60679566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
606807218a29SMatthew G. Knepley             if (dsIn) {
606907218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
607007218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
607107218a29SMatthew G. Knepley             }
60725fedec97SMatthew G. Knepley           }
60735fedec97SMatthew G. Knepley           ++nf;
60745fedec97SMatthew G. Knepley         }
60755fedec97SMatthew G. Knepley       }
6076e5e52638SMatthew G. Knepley     }
607707218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
607807218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
60799566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
608007218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
6081e5e52638SMatthew G. Knepley   }
60829566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
6083e5e52638SMatthew G. Knepley   /* Set fields in DSes */
6084083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
6085083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
608607218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
6087083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
6088083401c6SMatthew G. Knepley     const PetscInt *fld;
60895fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
60905fedec97SMatthew G. Knepley     PetscBool       isCohesive;
6091e5e52638SMatthew G. Knepley 
60929566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
60939566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
60949566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
60959566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
6096083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
6097083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
60985fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
6099e5e52638SMatthew G. Knepley       PetscClassId id;
6100e5e52638SMatthew G. Knepley 
61015fedec97SMatthew G. Knepley       /* Handle DS with no fields */
61029566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
61035fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
610407218a29SMatthew G. Knepley       if (isCohesive) {
610507218a29SMatthew G. Knepley         if (!isCohesiveField) {
610607218a29SMatthew G. Knepley           PetscObject bdDisc;
610707218a29SMatthew G. Knepley 
610807218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
610907218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
611007218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
611107218a29SMatthew G. Knepley         } else {
61129566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
611307218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
611407218a29SMatthew G. Knepley         }
611507218a29SMatthew G. Knepley       } else {
611607218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
611707218a29SMatthew G. Knepley       }
6118083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
61199566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
6120e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
6121e5e52638SMatthew G. Knepley     }
61229566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6123e5e52638SMatthew G. Knepley   }
6124f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
61259566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6126f9244615SMatthew G. Knepley   if (flg) {
61273b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
61283b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
612907218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
61303b4aee56SMatthew G. Knepley       PetscInt Nf, f;
61313b4aee56SMatthew G. Knepley 
61329566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
613307218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
613407218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
613507218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
613607218a29SMatthew G. Knepley       }
61373b4aee56SMatthew G. Knepley     }
6138f9244615SMatthew G. Knepley   }
6139e5e52638SMatthew G. Knepley   /* Setup DSes */
6140e5e52638SMatthew G. Knepley   if (doSetup) {
614107218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
6142cf1363e4SMatthew G. Knepley       if (dm->setfromoptionscalled) {
6143cf1363e4SMatthew G. Knepley         PetscCall(PetscDSSetFromOptions(dm->probs[s].ds));
6144cf1363e4SMatthew G. Knepley         if (dm->probs[s].dsIn) PetscCall(PetscDSSetFromOptions(dm->probs[s].dsIn));
6145cf1363e4SMatthew G. Knepley       }
614607218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
614707218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
614807218a29SMatthew G. Knepley     }
6149e5e52638SMatthew G. Knepley   }
61503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6151e5e52638SMatthew G. Knepley }
6152e5e52638SMatthew G. Knepley 
6153e5e52638SMatthew G. Knepley /*@
6154d2b2dc1eSMatthew G. Knepley   DMUseTensorOrder - Use a tensor product closure ordering for the default section
6155d2b2dc1eSMatthew G. Knepley 
6156d2b2dc1eSMatthew G. Knepley   Input Parameters:
6157d2b2dc1eSMatthew G. Knepley + dm     - The DM
6158d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order
6159d2b2dc1eSMatthew G. Knepley 
6160d2b2dc1eSMatthew G. Knepley   Level: developer
6161d2b2dc1eSMatthew G. Knepley 
6162d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()`
6163d2b2dc1eSMatthew G. Knepley @*/
6164d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor)
6165d2b2dc1eSMatthew G. Knepley {
6166d2b2dc1eSMatthew G. Knepley   PetscInt  Nf;
6167d2b2dc1eSMatthew G. Knepley   PetscBool reorder = PETSC_TRUE, isPlex;
6168d2b2dc1eSMatthew G. Knepley 
6169d2b2dc1eSMatthew G. Knepley   PetscFunctionBegin;
6170d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex));
6171d2b2dc1eSMatthew G. Knepley   PetscCall(DMGetNumFields(dm, &Nf));
6172d2b2dc1eSMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
6173d2b2dc1eSMatthew G. Knepley     PetscObject  obj;
6174d2b2dc1eSMatthew G. Knepley     PetscClassId id;
6175d2b2dc1eSMatthew G. Knepley 
6176d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetField(dm, f, NULL, &obj));
6177d2b2dc1eSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
6178d2b2dc1eSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
6179d2b2dc1eSMatthew G. Knepley       PetscSpace sp;
6180d2b2dc1eSMatthew G. Knepley       PetscBool  tensor;
6181d2b2dc1eSMatthew G. Knepley 
6182d2b2dc1eSMatthew G. Knepley       PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp));
6183d2b2dc1eSMatthew G. Knepley       PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor));
6184d2b2dc1eSMatthew G. Knepley       reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE;
6185d2b2dc1eSMatthew G. Knepley     } else reorder = PETSC_FALSE;
6186d2b2dc1eSMatthew G. Knepley   }
6187d2b2dc1eSMatthew G. Knepley   if (tensor) {
6188d2b2dc1eSMatthew G. Knepley     if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
6189d2b2dc1eSMatthew G. Knepley   } else {
6190d2b2dc1eSMatthew G. Knepley     PetscSection s;
6191d2b2dc1eSMatthew G. Knepley 
6192d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetLocalSection(dm, &s));
6193d2b2dc1eSMatthew G. Knepley     if (s) PetscCall(PetscSectionResetClosurePermutation(s));
6194d2b2dc1eSMatthew G. Knepley   }
6195d2b2dc1eSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6196d2b2dc1eSMatthew G. Knepley }
6197d2b2dc1eSMatthew G. Knepley 
6198d2b2dc1eSMatthew G. Knepley /*@
6199bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
62007f96f943SMatthew G. Knepley 
620120f4b53cSBarry Smith   Collective
6202f2cacb80SMatthew G. Knepley 
62037f96f943SMatthew G. Knepley   Input Parameters:
6204bb7acecfSBarry Smith + dm   - The `DM`
62057f96f943SMatthew G. Knepley - time - The time
62067f96f943SMatthew G. Knepley 
62077f96f943SMatthew G. Knepley   Output Parameters:
620820f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
620920f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
621020f4b53cSBarry Smith 
621120f4b53cSBarry Smith   Level: developer
62127f96f943SMatthew G. Knepley 
6213bb7acecfSBarry Smith   Note:
6214bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
62157f96f943SMatthew G. Knepley 
62161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
62177f96f943SMatthew G. Knepley @*/
6218d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6219d71ae5a4SJacob Faibussowitsch {
62207f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
62217f96f943SMatthew G. Knepley   void   **ectxs;
6222f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
62237f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
62247f96f943SMatthew G. Knepley 
62257f96f943SMatthew G. Knepley   PetscFunctionBegin;
6226f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6227f60fa741SMatthew G. Knepley   if (u) {
6228f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6229f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6230f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6231f60fa741SMatthew G. Knepley   }
6232f60fa741SMatthew G. Knepley   if (u_t) {
6233f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6234f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6235f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6236f60fa741SMatthew G. Knepley   }
62379566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
62389566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
62399566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62407f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
62417f96f943SMatthew G. Knepley     PetscDS         ds;
62427f96f943SMatthew G. Knepley     DMLabel         label;
62437f96f943SMatthew G. Knepley     IS              fieldIS;
62447f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
62457f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
62467f96f943SMatthew G. Knepley 
624707218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
62489566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
62499566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
62509566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
62519566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6252f2cacb80SMatthew G. Knepley     if (u) {
6253f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6254f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6255f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
62567f96f943SMatthew G. Knepley     }
6257f2cacb80SMatthew G. Knepley     if (u_t) {
62589566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
62599566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6260f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6261f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6262f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6263f2cacb80SMatthew G. Knepley     }
62649566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6265f2cacb80SMatthew G. Knepley   }
6266f2cacb80SMatthew G. Knepley   if (u) {
62679566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
62689566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6269f2cacb80SMatthew G. Knepley   }
6270f2cacb80SMatthew G. Knepley   if (u_t) {
62719566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
62729566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6273f2cacb80SMatthew G. Knepley   }
62749566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6275f60fa741SMatthew G. Knepley   if (u) {
6276f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6277f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6278f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6279f60fa741SMatthew G. Knepley   }
6280f60fa741SMatthew G. Knepley   if (u_t) {
6281f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6282f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6283f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6284f60fa741SMatthew G. Knepley   }
62853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
62867f96f943SMatthew G. Knepley }
62877f96f943SMatthew G. Knepley 
6288bb4b53efSMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscInt minDegree, PetscInt maxDegree, PetscDS ds, PetscDS dsIn)
6289d71ae5a4SJacob Faibussowitsch {
629007218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
629145480ffeSMatthew G. Knepley 
629245480ffeSMatthew G. Knepley   PetscFunctionBegin;
62939566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
6294bb4b53efSMatthew G. Knepley   PetscCall(PetscDSCopy(ds, minDegree, maxDegree, dm, dsNew));
629507218a29SMatthew G. Knepley   if (dsIn) {
629607218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
6297bb4b53efSMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, minDegree, maxDegree, dm, dsInNew));
629845480ffeSMatthew G. Knepley   }
629907218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
63009566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
630107218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
63023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
630345480ffeSMatthew G. Knepley }
630445480ffeSMatthew G. Knepley 
63057f96f943SMatthew G. Knepley /*@
6306bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6307e5e52638SMatthew G. Knepley 
630820f4b53cSBarry Smith   Collective
6309e5e52638SMatthew G. Knepley 
6310bb4b53efSMatthew G. Knepley   Input Parameters:
6311bb4b53efSMatthew G. Knepley + dm        - The `DM`
6312bb4b53efSMatthew G. Knepley . minDegree - Minimum degree for a discretization, or `PETSC_DETERMINE` for no limit
6313bb4b53efSMatthew G. Knepley - maxDegree - Maximum degree for a discretization, or `PETSC_DETERMINE` for no limit
6314e5e52638SMatthew G. Knepley 
6315e5e52638SMatthew G. Knepley   Output Parameter:
6316bb7acecfSBarry Smith . newdm - The `DM`
6317e5e52638SMatthew G. Knepley 
6318e5e52638SMatthew G. Knepley   Level: advanced
6319e5e52638SMatthew G. Knepley 
63201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6321e5e52638SMatthew G. Knepley @*/
6322bb4b53efSMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, PetscInt minDegree, PetscInt maxDegree, DM newdm)
6323d71ae5a4SJacob Faibussowitsch {
6324e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6325e5e52638SMatthew G. Knepley 
6326e5e52638SMatthew G. Knepley   PetscFunctionBegin;
63273ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
63289566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
63299566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6330e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6331e5e52638SMatthew G. Knepley     DMLabel  label;
6332b3cf3223SMatthew G. Knepley     IS       fields;
633307218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6334783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6335e5e52638SMatthew G. Knepley 
633607218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6337b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
6338bb4b53efSMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, minDegree, maxDegree, ds, dsIn));
6339d5b43468SJose E. Roman     /* Complete new labels in the new DS */
634007218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
63419566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6342783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6343b8025e53SMatthew G. Knepley       PetscWeakForm wf;
634445480ffeSMatthew G. Knepley       DMLabel       label;
6345783e2ec8SMatthew G. Knepley       PetscInt      field;
6346783e2ec8SMatthew G. Knepley 
63479566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
63489566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6349783e2ec8SMatthew G. Knepley     }
6350e5e52638SMatthew G. Knepley   }
6351799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
63523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6353e5e52638SMatthew G. Knepley }
6354e5e52638SMatthew G. Knepley 
6355e5e52638SMatthew G. Knepley /*@
6356bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6357e5e52638SMatthew G. Knepley 
635820f4b53cSBarry Smith   Collective
6359e5e52638SMatthew G. Knepley 
6360e5e52638SMatthew G. Knepley   Input Parameter:
6361bb7acecfSBarry Smith . dm - The `DM`
6362e5e52638SMatthew G. Knepley 
6363e5e52638SMatthew G. Knepley   Output Parameter:
6364bb7acecfSBarry Smith . newdm - The `DM`
6365e5e52638SMatthew G. Knepley 
6366e5e52638SMatthew G. Knepley   Level: advanced
6367e5e52638SMatthew G. Knepley 
636873ff1848SBarry Smith   Developer Note:
6369bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6370bb7acecfSBarry Smith 
63711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6372e5e52638SMatthew G. Knepley @*/
6373d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6374d71ae5a4SJacob Faibussowitsch {
6375e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6376bb4b53efSMatthew G. Knepley   PetscCall(DMCopyFields(dm, PETSC_DETERMINE, PETSC_DETERMINE, newdm));
6377bb4b53efSMatthew G. Knepley   PetscCall(DMCopyDS(dm, PETSC_DETERMINE, PETSC_DETERMINE, newdm));
63783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6379e5e52638SMatthew G. Knepley }
6380e5e52638SMatthew G. Knepley 
6381c73cfb54SMatthew G. Knepley /*@
6382bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6383c73cfb54SMatthew G. Knepley 
638420f4b53cSBarry Smith   Not Collective
6385c73cfb54SMatthew G. Knepley 
6386c73cfb54SMatthew G. Knepley   Input Parameter:
6387bb7acecfSBarry Smith . dm - The `DM`
6388c73cfb54SMatthew G. Knepley 
6389c73cfb54SMatthew G. Knepley   Output Parameter:
6390c73cfb54SMatthew G. Knepley . dim - The topological dimension
6391c73cfb54SMatthew G. Knepley 
6392c73cfb54SMatthew G. Knepley   Level: beginner
6393c73cfb54SMatthew G. Knepley 
63941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6395c73cfb54SMatthew G. Knepley @*/
6396d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6397d71ae5a4SJacob Faibussowitsch {
6398c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6399c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64004f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6401c73cfb54SMatthew G. Knepley   *dim = dm->dim;
64023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6403c73cfb54SMatthew G. Knepley }
6404c73cfb54SMatthew G. Knepley 
6405c73cfb54SMatthew G. Knepley /*@
6406bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6407c73cfb54SMatthew G. Knepley 
640820f4b53cSBarry Smith   Collective
6409c73cfb54SMatthew G. Knepley 
6410c73cfb54SMatthew G. Knepley   Input Parameters:
6411bb7acecfSBarry Smith + dm  - The `DM`
6412c73cfb54SMatthew G. Knepley - dim - The topological dimension
6413c73cfb54SMatthew G. Knepley 
6414c73cfb54SMatthew G. Knepley   Level: beginner
6415c73cfb54SMatthew G. Knepley 
64161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6417c73cfb54SMatthew G. Knepley @*/
6418d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6419d71ae5a4SJacob Faibussowitsch {
6420e5e52638SMatthew G. Knepley   PetscDS  ds;
642145480ffeSMatthew G. Knepley   PetscInt Nds, n;
6422f17e8794SMatthew G. Knepley 
6423c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6424c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6425c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6426c73cfb54SMatthew G. Knepley   dm->dim = dim;
6427d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
64289566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
642945480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
643007218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
64319566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
643245480ffeSMatthew G. Knepley     }
6433d17bd122SMatthew G. Knepley   }
64343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6435c73cfb54SMatthew G. Knepley }
6436c73cfb54SMatthew G. Knepley 
6437793f3fe5SMatthew G. Knepley /*@
6438793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6439793f3fe5SMatthew G. Knepley 
644020f4b53cSBarry Smith   Collective
6441793f3fe5SMatthew G. Knepley 
6442793f3fe5SMatthew G. Knepley   Input Parameters:
6443bb7acecfSBarry Smith + dm  - the `DM`
6444793f3fe5SMatthew G. Knepley - dim - the dimension
6445793f3fe5SMatthew G. Knepley 
6446793f3fe5SMatthew G. Knepley   Output Parameters:
6447793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6448aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6449793f3fe5SMatthew G. Knepley 
645020f4b53cSBarry Smith   Level: intermediate
645120f4b53cSBarry Smith 
6452793f3fe5SMatthew G. Knepley   Note:
6453793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6454a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6455793f3fe5SMatthew G. Knepley   then the interval is empty.
6456793f3fe5SMatthew G. Knepley 
64571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6458793f3fe5SMatthew G. Knepley @*/
6459d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6460d71ae5a4SJacob Faibussowitsch {
6461793f3fe5SMatthew G. Knepley   PetscInt d;
6462793f3fe5SMatthew G. Knepley 
6463793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6464793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64659566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
64667a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6467dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
64683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6469793f3fe5SMatthew G. Knepley }
6470793f3fe5SMatthew G. Knepley 
64716636e97aSMatthew G Knepley /*@
6472bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6473f4d763aaSMatthew G. Knepley 
647420f4b53cSBarry Smith   Collective
64758f700142SStefano Zampini 
6476f4d763aaSMatthew G. Knepley   Input Parameter:
6477bb7acecfSBarry Smith . dm - The original `DM`
6478f4d763aaSMatthew G. Knepley 
6479f4d763aaSMatthew G. Knepley   Output Parameter:
6480bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6481f4d763aaSMatthew G. Knepley 
6482f4d763aaSMatthew G. Knepley   Level: intermediate
6483f4d763aaSMatthew G. Knepley 
6484bb7acecfSBarry Smith   Note:
6485bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6486bb7acecfSBarry 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
6487bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6488bb7acecfSBarry Smith 
64891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6490f4d763aaSMatthew G. Knepley @*/
6491d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6492d71ae5a4SJacob Faibussowitsch {
6493c26acbdeSMatthew G. Knepley   PetscSection section;
6494eb9d3e4dSMatthew G. Knepley   IS           perm;
6495eb9d3e4dSMatthew G. Knepley   PetscBool    hasConstraints, newDM, gnewDM;
649692a26154SJames Wright   PetscInt     num_face_sfs = 0;
649714f150ffSMatthew G. Knepley 
649814f150ffSMatthew G. Knepley   PetscFunctionBegin;
649914f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65004f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
65019566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
65029566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6503eb9d3e4dSMatthew G. Knepley   PetscCall(PetscSectionGetPermutation(section, &perm));
650492a26154SJames Wright   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, NULL));
650592a26154SJames Wright   newDM = hasConstraints || perm || (num_face_sfs > 0) ? PETSC_TRUE : PETSC_FALSE;
6506462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&newDM, &gnewDM, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
6507eb9d3e4dSMatthew G. Knepley   if (!gnewDM) {
6508c26acbdeSMatthew G. Knepley     *odm = dm;
65093ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6510c26acbdeSMatthew G. Knepley   }
651114f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6512c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
651392a26154SJames Wright     PetscSF      sf, sfNatural;
6514eb9d3e4dSMatthew G. Knepley     PetscBool    usePerm = dm->ignorePermOutput ? PETSC_FALSE : PETSC_TRUE;
651514f150ffSMatthew G. Knepley 
65169566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
65179566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
65189566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
65199566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
65209566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
652192a26154SJames Wright     PetscCall(DMGetNaturalSF(dm, &sfNatural));
652292a26154SJames Wright     PetscCall(DMSetNaturalSF(dm->dmBC, sfNatural));
65239566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
6524eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(section, sf, usePerm, PETSC_TRUE, PETSC_FALSE, &gsection));
65259566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
65269566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
652714f150ffSMatthew G. Knepley   }
652814f150ffSMatthew G. Knepley   *odm = dm->dmBC;
65293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
653014f150ffSMatthew G. Knepley }
6531f4d763aaSMatthew G. Knepley 
6532f4d763aaSMatthew G. Knepley /*@
6533cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6534f4d763aaSMatthew G. Knepley 
6535f4d763aaSMatthew G. Knepley   Input Parameter:
6536bb7acecfSBarry Smith . dm - The original `DM`
6537f4d763aaSMatthew G. Knepley 
6538cdb7a50dSMatthew G. Knepley   Output Parameters:
6539cdb7a50dSMatthew G. Knepley + num - The output sequence number
6540cdb7a50dSMatthew G. Knepley - val - The output sequence value
6541f4d763aaSMatthew G. Knepley 
6542f4d763aaSMatthew G. Knepley   Level: intermediate
6543f4d763aaSMatthew G. Knepley 
6544bb7acecfSBarry Smith   Note:
6545bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6546bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6547bb7acecfSBarry Smith 
654873ff1848SBarry Smith   Developer Note:
6549bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6550bb7acecfSBarry Smith   not directly related to the `DM`.
6551f4d763aaSMatthew G. Knepley 
65521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6553f4d763aaSMatthew G. Knepley @*/
6554d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6555d71ae5a4SJacob Faibussowitsch {
6556f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6557f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65589371c9d4SSatish Balay   if (num) {
65594f572ea9SToby Isaac     PetscAssertPointer(num, 2);
65609371c9d4SSatish Balay     *num = dm->outputSequenceNum;
65619371c9d4SSatish Balay   }
65629371c9d4SSatish Balay   if (val) {
65634f572ea9SToby Isaac     PetscAssertPointer(val, 3);
65649371c9d4SSatish Balay     *val = dm->outputSequenceVal;
65659371c9d4SSatish Balay   }
65663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6567f4d763aaSMatthew G. Knepley }
6568f4d763aaSMatthew G. Knepley 
6569f4d763aaSMatthew G. Knepley /*@
6570cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6571f4d763aaSMatthew G. Knepley 
6572f4d763aaSMatthew G. Knepley   Input Parameters:
6573bb7acecfSBarry Smith + dm  - The original `DM`
6574cdb7a50dSMatthew G. Knepley . num - The output sequence number
6575cdb7a50dSMatthew G. Knepley - val - The output sequence value
6576f4d763aaSMatthew G. Knepley 
6577f4d763aaSMatthew G. Knepley   Level: intermediate
6578f4d763aaSMatthew G. Knepley 
6579bb7acecfSBarry Smith   Note:
6580bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6581bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6582f4d763aaSMatthew G. Knepley 
65831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6584f4d763aaSMatthew G. Knepley @*/
6585d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6586d71ae5a4SJacob Faibussowitsch {
6587f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6588f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6589f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6590cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
65913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6592cdb7a50dSMatthew G. Knepley }
6593cdb7a50dSMatthew G. Knepley 
65945d83a8b1SBarry Smith /*@
6595bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6596cdb7a50dSMatthew G. Knepley 
6597cdb7a50dSMatthew G. Knepley   Input Parameters:
6598bb7acecfSBarry Smith + dm     - The original `DM`
6599b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6600cdb7a50dSMatthew G. Knepley . name   - The sequence name
6601cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6602cdb7a50dSMatthew G. Knepley 
6603cdb7a50dSMatthew G. Knepley   Output Parameter:
6604cdb7a50dSMatthew G. Knepley . val - The output sequence value
6605cdb7a50dSMatthew G. Knepley 
6606cdb7a50dSMatthew G. Knepley   Level: intermediate
6607cdb7a50dSMatthew G. Knepley 
6608bb7acecfSBarry Smith   Note:
6609bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6610bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6611bb7acecfSBarry Smith 
661273ff1848SBarry Smith   Developer Note:
6613bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6614cdb7a50dSMatthew G. Knepley 
66151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6616cdb7a50dSMatthew G. Knepley @*/
6617b2033f5dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char name[], PetscInt num, PetscReal *val)
6618d71ae5a4SJacob Faibussowitsch {
6619cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6620cdb7a50dSMatthew G. Knepley 
6621cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6622cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6623cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6624b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
66254f572ea9SToby Isaac   PetscAssertPointer(val, 5);
66269566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6627cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6628cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6629cdb7a50dSMatthew G. Knepley     PetscScalar value;
6630cdb7a50dSMatthew G. Knepley 
66319566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
66324aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6633cdb7a50dSMatthew G. Knepley #endif
6634cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
66353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6636f4d763aaSMatthew G. Knepley }
66378e4ac7eaSMatthew G. Knepley 
66388e4ac7eaSMatthew G. Knepley /*@
6639b2033f5dSMatthew G. Knepley   DMGetOutputSequenceLength - Retrieve the number of sequence values from a `PetscViewer`
6640b2033f5dSMatthew G. Knepley 
6641b2033f5dSMatthew G. Knepley   Input Parameters:
6642b2033f5dSMatthew G. Knepley + dm     - The original `DM`
6643b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6644b2033f5dSMatthew G. Knepley - name   - The sequence name
6645b2033f5dSMatthew G. Knepley 
6646b2033f5dSMatthew G. Knepley   Output Parameter:
6647b2033f5dSMatthew G. Knepley . len - The length of the output sequence
6648b2033f5dSMatthew G. Knepley 
6649b2033f5dSMatthew G. Knepley   Level: intermediate
6650b2033f5dSMatthew G. Knepley 
6651b2033f5dSMatthew G. Knepley   Note:
6652b2033f5dSMatthew G. Knepley   This is intended for output that should appear in sequence, for instance
6653b2033f5dSMatthew G. Knepley   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6654b2033f5dSMatthew G. Knepley 
6655b2033f5dSMatthew G. Knepley   Developer Note:
6656b2033f5dSMatthew G. Knepley   It is unclear at the user API level why a `DM` is needed as input
6657b2033f5dSMatthew G. Knepley 
6658b2033f5dSMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6659b2033f5dSMatthew G. Knepley @*/
6660b2033f5dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceLength(DM dm, PetscViewer viewer, const char name[], PetscInt *len)
6661b2033f5dSMatthew G. Knepley {
6662b2033f5dSMatthew G. Knepley   PetscBool ishdf5;
6663b2033f5dSMatthew G. Knepley 
6664b2033f5dSMatthew G. Knepley   PetscFunctionBegin;
6665b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6666b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6667b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
6668b2033f5dSMatthew G. Knepley   PetscAssertPointer(len, 4);
6669b2033f5dSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6670b2033f5dSMatthew G. Knepley   if (ishdf5) {
6671b2033f5dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6672b2033f5dSMatthew G. Knepley     PetscCall(DMSequenceGetLength_HDF5_Internal(dm, name, len, viewer));
6673b2033f5dSMatthew G. Knepley #endif
6674b2033f5dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6675b2033f5dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6676b2033f5dSMatthew G. Knepley }
6677b2033f5dSMatthew G. Knepley 
6678b2033f5dSMatthew G. Knepley /*@
6679bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66808e4ac7eaSMatthew G. Knepley 
668120f4b53cSBarry Smith   Not Collective
66828e4ac7eaSMatthew G. Knepley 
66838e4ac7eaSMatthew G. Knepley   Input Parameter:
6684bb7acecfSBarry Smith . dm - The `DM`
66858e4ac7eaSMatthew G. Knepley 
66868e4ac7eaSMatthew G. Knepley   Output Parameter:
6687bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66888e4ac7eaSMatthew G. Knepley 
66898e4ac7eaSMatthew G. Knepley   Level: beginner
66908e4ac7eaSMatthew G. Knepley 
66911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
66928e4ac7eaSMatthew G. Knepley @*/
6693d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6694d71ae5a4SJacob Faibussowitsch {
66958e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66968e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66974f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
66988e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
66993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67008e4ac7eaSMatthew G. Knepley }
67018e4ac7eaSMatthew G. Knepley 
67028e4ac7eaSMatthew G. Knepley /*@
6703bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
67048e4ac7eaSMatthew G. Knepley 
670520f4b53cSBarry Smith   Collective
67068e4ac7eaSMatthew G. Knepley 
67078e4ac7eaSMatthew G. Knepley   Input Parameters:
6708bb7acecfSBarry Smith + dm         - The `DM`
6709bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
67108e4ac7eaSMatthew G. Knepley 
671173ff1848SBarry Smith   Level: beginner
671273ff1848SBarry Smith 
6713bb7acecfSBarry Smith   Note:
6714bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
67155d3b26e6SMatthew G. Knepley 
67161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
67178e4ac7eaSMatthew G. Knepley @*/
6718d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6719d71ae5a4SJacob Faibussowitsch {
67208e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
67218e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67228833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
67238e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
67243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67258e4ac7eaSMatthew G. Knepley }
6726c58f1c22SToby Isaac 
6727cc4c1da9SBarry Smith /*@
6728bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6729c58f1c22SToby Isaac 
6730c58f1c22SToby Isaac   Not Collective
6731c58f1c22SToby Isaac 
6732c58f1c22SToby Isaac   Input Parameters:
6733bb7acecfSBarry Smith + dm   - The `DM` object
6734c58f1c22SToby Isaac - name - The label name
6735c58f1c22SToby Isaac 
6736c58f1c22SToby Isaac   Level: intermediate
6737c58f1c22SToby Isaac 
67381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6739c58f1c22SToby Isaac @*/
6740d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6741d71ae5a4SJacob Faibussowitsch {
67425d80c0bfSVaclav Hapla   PetscBool flg;
67435d80c0bfSVaclav Hapla   DMLabel   label;
6744c58f1c22SToby Isaac 
6745c58f1c22SToby Isaac   PetscFunctionBegin;
6746c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67474f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67489566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6749c58f1c22SToby Isaac   if (!flg) {
67509566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67519566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67529566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6753c58f1c22SToby Isaac   }
67543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6755c58f1c22SToby Isaac }
6756c58f1c22SToby Isaac 
6757cc4c1da9SBarry Smith /*@
6758bb7acecfSBarry 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.
67590fdc7489SMatthew Knepley 
67600fdc7489SMatthew Knepley   Not Collective
67610fdc7489SMatthew Knepley 
67620fdc7489SMatthew Knepley   Input Parameters:
6763bb7acecfSBarry Smith + dm   - The `DM` object
67640fdc7489SMatthew Knepley . l    - The index for the label
67650fdc7489SMatthew Knepley - name - The label name
67660fdc7489SMatthew Knepley 
67670fdc7489SMatthew Knepley   Level: intermediate
67680fdc7489SMatthew Knepley 
67691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
67700fdc7489SMatthew Knepley @*/
6771d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6772d71ae5a4SJacob Faibussowitsch {
67730fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
67740fdc7489SMatthew Knepley   DMLabel     label;
67750fdc7489SMatthew Knepley   PetscInt    Nl, m;
67760fdc7489SMatthew Knepley   PetscBool   flg, match;
67770fdc7489SMatthew Knepley   const char *lname;
67780fdc7489SMatthew Knepley 
67790fdc7489SMatthew Knepley   PetscFunctionBegin;
67800fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67814f572ea9SToby Isaac   PetscAssertPointer(name, 3);
67829566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
67830fdc7489SMatthew Knepley   if (!flg) {
67849566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67859566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67869566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
67870fdc7489SMatthew Knepley   }
67889566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
678963a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
67900fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
67919566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
67929566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
67930fdc7489SMatthew Knepley     if (match) break;
67940fdc7489SMatthew Knepley   }
67953ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
67960fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
67970fdc7489SMatthew Knepley   else prev->next = orig->next;
67980fdc7489SMatthew Knepley   if (!l) {
67990fdc7489SMatthew Knepley     orig->next = dm->labels;
68000fdc7489SMatthew Knepley     dm->labels = orig;
68010fdc7489SMatthew Knepley   } else {
6802fbccb6d4SPierre Jolivet     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next);
68030fdc7489SMatthew Knepley     orig->next = prev->next;
68040fdc7489SMatthew Knepley     prev->next = orig;
68050fdc7489SMatthew Knepley   }
68063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
68070fdc7489SMatthew Knepley }
68080fdc7489SMatthew Knepley 
6809cc4c1da9SBarry Smith /*@
6810bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6811c58f1c22SToby Isaac 
6812c58f1c22SToby Isaac   Not Collective
6813c58f1c22SToby Isaac 
6814c58f1c22SToby Isaac   Input Parameters:
6815bb7acecfSBarry Smith + dm    - The `DM` object
6816c58f1c22SToby Isaac . name  - The label name
6817c58f1c22SToby Isaac - point - The mesh point
6818c58f1c22SToby Isaac 
6819c58f1c22SToby Isaac   Output Parameter:
6820c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6821c58f1c22SToby Isaac 
6822c58f1c22SToby Isaac   Level: beginner
6823c58f1c22SToby Isaac 
68241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6825c58f1c22SToby Isaac @*/
6826d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6827d71ae5a4SJacob Faibussowitsch {
6828c58f1c22SToby Isaac   DMLabel label;
6829c58f1c22SToby Isaac 
6830c58f1c22SToby Isaac   PetscFunctionBegin;
6831c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68324f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68339566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68347a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
68359566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
68363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6837c58f1c22SToby Isaac }
6838c58f1c22SToby Isaac 
6839cc4c1da9SBarry Smith /*@
6840bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6841c58f1c22SToby Isaac 
6842c58f1c22SToby Isaac   Not Collective
6843c58f1c22SToby Isaac 
6844c58f1c22SToby Isaac   Input Parameters:
6845bb7acecfSBarry Smith + dm    - The `DM` object
6846c58f1c22SToby Isaac . name  - The label name
6847c58f1c22SToby Isaac . point - The mesh point
6848c58f1c22SToby Isaac - value - The label value for this point
6849c58f1c22SToby Isaac 
6850c58f1c22SToby Isaac   Output Parameter:
6851c58f1c22SToby Isaac 
6852c58f1c22SToby Isaac   Level: beginner
6853c58f1c22SToby Isaac 
68541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6855c58f1c22SToby Isaac @*/
6856d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6857d71ae5a4SJacob Faibussowitsch {
6858c58f1c22SToby Isaac   DMLabel label;
6859c58f1c22SToby Isaac 
6860c58f1c22SToby Isaac   PetscFunctionBegin;
6861c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68624f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68639566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6864c58f1c22SToby Isaac   if (!label) {
68659566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
68669566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6867c58f1c22SToby Isaac   }
68689566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
68693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6870c58f1c22SToby Isaac }
6871c58f1c22SToby Isaac 
6872cc4c1da9SBarry Smith /*@
6873bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6874c58f1c22SToby Isaac 
6875c58f1c22SToby Isaac   Not Collective
6876c58f1c22SToby Isaac 
6877c58f1c22SToby Isaac   Input Parameters:
6878bb7acecfSBarry Smith + dm    - The `DM` object
6879c58f1c22SToby Isaac . name  - The label name
6880c58f1c22SToby Isaac . point - The mesh point
6881c58f1c22SToby Isaac - value - The label value for this point
6882c58f1c22SToby Isaac 
6883c58f1c22SToby Isaac   Level: beginner
6884c58f1c22SToby Isaac 
68851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6886c58f1c22SToby Isaac @*/
6887d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6888d71ae5a4SJacob Faibussowitsch {
6889c58f1c22SToby Isaac   DMLabel label;
6890c58f1c22SToby Isaac 
6891c58f1c22SToby Isaac   PetscFunctionBegin;
6892c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68934f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68949566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68953ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68969566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
68973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6898c58f1c22SToby Isaac }
6899c58f1c22SToby Isaac 
6900cc4c1da9SBarry Smith /*@
6901bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6902c58f1c22SToby Isaac 
6903c58f1c22SToby Isaac   Not Collective
6904c58f1c22SToby Isaac 
6905c58f1c22SToby Isaac   Input Parameters:
6906bb7acecfSBarry Smith + dm   - The `DM` object
6907c58f1c22SToby Isaac - name - The label name
6908c58f1c22SToby Isaac 
6909c58f1c22SToby Isaac   Output Parameter:
6910c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6911c58f1c22SToby Isaac 
6912c58f1c22SToby Isaac   Level: beginner
6913c58f1c22SToby Isaac 
691473ff1848SBarry Smith   Developer Note:
6915bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6916bb7acecfSBarry Smith 
69171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6918c58f1c22SToby Isaac @*/
6919d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6920d71ae5a4SJacob Faibussowitsch {
6921c58f1c22SToby Isaac   DMLabel label;
6922c58f1c22SToby Isaac 
6923c58f1c22SToby Isaac   PetscFunctionBegin;
6924c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69254f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69264f572ea9SToby Isaac   PetscAssertPointer(size, 3);
69279566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6928c58f1c22SToby Isaac   *size = 0;
69293ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69309566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
69313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6932c58f1c22SToby Isaac }
6933c58f1c22SToby Isaac 
6934cc4c1da9SBarry Smith /*@
6935bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6936c58f1c22SToby Isaac 
6937c58f1c22SToby Isaac   Not Collective
6938c58f1c22SToby Isaac 
6939c58f1c22SToby Isaac   Input Parameters:
694060225df5SJacob Faibussowitsch + dm   - The `DM` object
6941c58f1c22SToby Isaac - name - The label name
6942c58f1c22SToby Isaac 
6943c58f1c22SToby Isaac   Output Parameter:
694420f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6945c58f1c22SToby Isaac 
6946c58f1c22SToby Isaac   Level: beginner
6947c58f1c22SToby Isaac 
69481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6949c58f1c22SToby Isaac @*/
6950d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6951d71ae5a4SJacob Faibussowitsch {
6952c58f1c22SToby Isaac   DMLabel label;
6953c58f1c22SToby Isaac 
6954c58f1c22SToby Isaac   PetscFunctionBegin;
6955c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69564f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69574f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
69589566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6959c58f1c22SToby Isaac   *ids = NULL;
6960dab2e251SBlaise Bourdin   if (label) {
69619566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6962dab2e251SBlaise Bourdin   } else {
6963dab2e251SBlaise Bourdin     /* returning an empty IS */
69649566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6965dab2e251SBlaise Bourdin   }
69663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6967c58f1c22SToby Isaac }
6968c58f1c22SToby Isaac 
6969cc4c1da9SBarry Smith /*@
6970c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6971c58f1c22SToby Isaac 
6972c58f1c22SToby Isaac   Not Collective
6973c58f1c22SToby Isaac 
6974c58f1c22SToby Isaac   Input Parameters:
6975bb7acecfSBarry Smith + dm    - The `DM` object
6976b6971eaeSBarry Smith . name  - The label name of the stratum
6977c58f1c22SToby Isaac - value - The stratum value
6978c58f1c22SToby Isaac 
6979c58f1c22SToby Isaac   Output Parameter:
6980bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6981c58f1c22SToby Isaac 
6982c58f1c22SToby Isaac   Level: beginner
6983c58f1c22SToby Isaac 
69841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6985c58f1c22SToby Isaac @*/
6986d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6987d71ae5a4SJacob Faibussowitsch {
6988c58f1c22SToby Isaac   DMLabel label;
6989c58f1c22SToby Isaac 
6990c58f1c22SToby Isaac   PetscFunctionBegin;
6991c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69924f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69934f572ea9SToby Isaac   PetscAssertPointer(size, 4);
69949566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6995c58f1c22SToby Isaac   *size = 0;
69963ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69979566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
69983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6999c58f1c22SToby Isaac }
7000c58f1c22SToby Isaac 
7001cc4c1da9SBarry Smith /*@
7002c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7003c58f1c22SToby Isaac 
7004c58f1c22SToby Isaac   Not Collective
7005c58f1c22SToby Isaac 
7006c58f1c22SToby Isaac   Input Parameters:
7007bb7acecfSBarry Smith + dm    - The `DM` object
7008c58f1c22SToby Isaac . name  - The label name
7009c58f1c22SToby Isaac - value - The stratum value
7010c58f1c22SToby Isaac 
7011c58f1c22SToby Isaac   Output Parameter:
701220f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
7013c58f1c22SToby Isaac 
7014c58f1c22SToby Isaac   Level: beginner
7015c58f1c22SToby Isaac 
70161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
7017c58f1c22SToby Isaac @*/
7018d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7019d71ae5a4SJacob Faibussowitsch {
7020c58f1c22SToby Isaac   DMLabel label;
7021c58f1c22SToby Isaac 
7022c58f1c22SToby Isaac   PetscFunctionBegin;
7023c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70244f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70254f572ea9SToby Isaac   PetscAssertPointer(points, 4);
70269566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7027c58f1c22SToby Isaac   *points = NULL;
70283ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70299566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
70303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7031c58f1c22SToby Isaac }
7032c58f1c22SToby Isaac 
7033cc4c1da9SBarry Smith /*@
70349044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
70354de306b1SToby Isaac 
70364de306b1SToby Isaac   Not Collective
70374de306b1SToby Isaac 
70384de306b1SToby Isaac   Input Parameters:
7039bb7acecfSBarry Smith + dm     - The `DM` object
70404de306b1SToby Isaac . name   - The label name
70414de306b1SToby Isaac . value  - The stratum value
70424de306b1SToby Isaac - points - The stratum points
70434de306b1SToby Isaac 
70444de306b1SToby Isaac   Level: beginner
70454de306b1SToby Isaac 
70461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
70474de306b1SToby Isaac @*/
7048d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
7049d71ae5a4SJacob Faibussowitsch {
70504de306b1SToby Isaac   DMLabel label;
70514de306b1SToby Isaac 
70524de306b1SToby Isaac   PetscFunctionBegin;
70534de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70544f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7055292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
70569566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70573ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70589566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
70593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
70604de306b1SToby Isaac }
70614de306b1SToby Isaac 
7062cc4c1da9SBarry Smith /*@
7063bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
7064c58f1c22SToby Isaac 
7065c58f1c22SToby Isaac   Not Collective
7066c58f1c22SToby Isaac 
7067c58f1c22SToby Isaac   Input Parameters:
7068bb7acecfSBarry Smith + dm    - The `DM` object
7069c58f1c22SToby Isaac . name  - The label name
7070c58f1c22SToby Isaac - value - The label value for this point
7071c58f1c22SToby Isaac 
7072c58f1c22SToby Isaac   Output Parameter:
7073c58f1c22SToby Isaac 
7074c58f1c22SToby Isaac   Level: beginner
7075c58f1c22SToby Isaac 
70761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
7077c58f1c22SToby Isaac @*/
7078d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7079d71ae5a4SJacob Faibussowitsch {
7080c58f1c22SToby Isaac   DMLabel label;
7081c58f1c22SToby Isaac 
7082c58f1c22SToby Isaac   PetscFunctionBegin;
7083c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70844f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70859566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70863ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70879566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
70883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7089c58f1c22SToby Isaac }
7090c58f1c22SToby Isaac 
7091c58f1c22SToby Isaac /*@
7092bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
7093c58f1c22SToby Isaac 
7094c58f1c22SToby Isaac   Not Collective
7095c58f1c22SToby Isaac 
7096c58f1c22SToby Isaac   Input Parameter:
7097bb7acecfSBarry Smith . dm - The `DM` object
7098c58f1c22SToby Isaac 
7099c58f1c22SToby Isaac   Output Parameter:
7100c58f1c22SToby Isaac . numLabels - the number of Labels
7101c58f1c22SToby Isaac 
7102c58f1c22SToby Isaac   Level: intermediate
7103c58f1c22SToby Isaac 
71041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7105c58f1c22SToby Isaac @*/
7106d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7107d71ae5a4SJacob Faibussowitsch {
71085d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7109c58f1c22SToby Isaac   PetscInt    n    = 0;
7110c58f1c22SToby Isaac 
7111c58f1c22SToby Isaac   PetscFunctionBegin;
7112c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71134f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
71149371c9d4SSatish Balay   while (next) {
71159371c9d4SSatish Balay     ++n;
71169371c9d4SSatish Balay     next = next->next;
71179371c9d4SSatish Balay   }
7118c58f1c22SToby Isaac   *numLabels = n;
71193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7120c58f1c22SToby Isaac }
7121c58f1c22SToby Isaac 
7122cc4c1da9SBarry Smith /*@
7123c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7124c58f1c22SToby Isaac 
7125c58f1c22SToby Isaac   Not Collective
7126c58f1c22SToby Isaac 
7127c58f1c22SToby Isaac   Input Parameters:
7128bb7acecfSBarry Smith + dm - The `DM` object
7129c58f1c22SToby Isaac - n  - the label number
7130c58f1c22SToby Isaac 
7131c58f1c22SToby Isaac   Output Parameter:
7132c58f1c22SToby Isaac . name - the label name
7133c58f1c22SToby Isaac 
7134c58f1c22SToby Isaac   Level: intermediate
7135c58f1c22SToby Isaac 
713673ff1848SBarry Smith   Developer Note:
7137bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
7138bb7acecfSBarry Smith 
71391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7140c58f1c22SToby Isaac @*/
7141cc4c1da9SBarry Smith PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char *name[])
7142d71ae5a4SJacob Faibussowitsch {
71435d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7144c58f1c22SToby Isaac   PetscInt    l    = 0;
7145c58f1c22SToby Isaac 
7146c58f1c22SToby Isaac   PetscFunctionBegin;
7147c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71484f572ea9SToby Isaac   PetscAssertPointer(name, 3);
7149c58f1c22SToby Isaac   while (next) {
7150c58f1c22SToby Isaac     if (l == n) {
71519566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
71523ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7153c58f1c22SToby Isaac     }
7154c58f1c22SToby Isaac     ++l;
7155c58f1c22SToby Isaac     next = next->next;
7156c58f1c22SToby Isaac   }
715763a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7158c58f1c22SToby Isaac }
7159c58f1c22SToby Isaac 
7160cc4c1da9SBarry Smith /*@
7161bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
7162c58f1c22SToby Isaac 
7163c58f1c22SToby Isaac   Not Collective
7164c58f1c22SToby Isaac 
7165c58f1c22SToby Isaac   Input Parameters:
7166bb7acecfSBarry Smith + dm   - The `DM` object
7167c58f1c22SToby Isaac - name - The label name
7168c58f1c22SToby Isaac 
7169c58f1c22SToby Isaac   Output Parameter:
7170bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
7171c58f1c22SToby Isaac 
7172c58f1c22SToby Isaac   Level: intermediate
7173c58f1c22SToby Isaac 
71741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7175c58f1c22SToby Isaac @*/
7176d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7177d71ae5a4SJacob Faibussowitsch {
71785d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7179d67d17b1SMatthew G. Knepley   const char *lname;
7180c58f1c22SToby Isaac 
7181c58f1c22SToby Isaac   PetscFunctionBegin;
7182c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71834f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71844f572ea9SToby Isaac   PetscAssertPointer(hasLabel, 3);
7185c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7186c58f1c22SToby Isaac   while (next) {
71879566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71889566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7189c58f1c22SToby Isaac     if (*hasLabel) break;
7190c58f1c22SToby Isaac     next = next->next;
7191c58f1c22SToby Isaac   }
71923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7193c58f1c22SToby Isaac }
7194c58f1c22SToby Isaac 
7195a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7196cc4c1da9SBarry Smith /*@
719720f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
7198c58f1c22SToby Isaac 
7199c58f1c22SToby Isaac   Not Collective
7200c58f1c22SToby Isaac 
7201c58f1c22SToby Isaac   Input Parameters:
7202bb7acecfSBarry Smith + dm   - The `DM` object
7203c58f1c22SToby Isaac - name - The label name
7204c58f1c22SToby Isaac 
7205c58f1c22SToby Isaac   Output Parameter:
720620f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
7207c58f1c22SToby Isaac 
7208bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7209bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7210bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7211bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7212bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7213bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7214bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
72156d7c9049SMatthew G. Knepley 
7216c58f1c22SToby Isaac   Level: intermediate
7217c58f1c22SToby Isaac 
721860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7219c58f1c22SToby Isaac @*/
7220d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7221d71ae5a4SJacob Faibussowitsch {
72225d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7223c58f1c22SToby Isaac   PetscBool   hasLabel;
7224d67d17b1SMatthew G. Knepley   const char *lname;
7225c58f1c22SToby Isaac 
7226c58f1c22SToby Isaac   PetscFunctionBegin;
7227c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72284f572ea9SToby Isaac   PetscAssertPointer(name, 2);
72294f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7230c58f1c22SToby Isaac   *label = NULL;
7231c58f1c22SToby Isaac   while (next) {
72329566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72339566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7234c58f1c22SToby Isaac     if (hasLabel) {
7235c58f1c22SToby Isaac       *label = next->label;
7236c58f1c22SToby Isaac       break;
7237c58f1c22SToby Isaac     }
7238c58f1c22SToby Isaac     next = next->next;
7239c58f1c22SToby Isaac   }
72403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7241c58f1c22SToby Isaac }
7242c58f1c22SToby Isaac 
7243cc4c1da9SBarry Smith /*@
7244bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7245c58f1c22SToby Isaac 
7246c58f1c22SToby Isaac   Not Collective
7247c58f1c22SToby Isaac 
7248c58f1c22SToby Isaac   Input Parameters:
7249bb7acecfSBarry Smith + dm - The `DM` object
7250c58f1c22SToby Isaac - n  - the label number
7251c58f1c22SToby Isaac 
7252c58f1c22SToby Isaac   Output Parameter:
7253c58f1c22SToby Isaac . label - the label
7254c58f1c22SToby Isaac 
7255c58f1c22SToby Isaac   Level: intermediate
7256c58f1c22SToby Isaac 
72571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7258c58f1c22SToby Isaac @*/
7259d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7260d71ae5a4SJacob Faibussowitsch {
72615d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7262c58f1c22SToby Isaac   PetscInt    l    = 0;
7263c58f1c22SToby Isaac 
7264c58f1c22SToby Isaac   PetscFunctionBegin;
7265c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72664f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7267c58f1c22SToby Isaac   while (next) {
7268c58f1c22SToby Isaac     if (l == n) {
7269c58f1c22SToby Isaac       *label = next->label;
72703ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7271c58f1c22SToby Isaac     }
7272c58f1c22SToby Isaac     ++l;
7273c58f1c22SToby Isaac     next = next->next;
7274c58f1c22SToby Isaac   }
727563a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7276c58f1c22SToby Isaac }
7277c58f1c22SToby Isaac 
7278cc4c1da9SBarry Smith /*@
7279bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7280c58f1c22SToby Isaac 
7281c58f1c22SToby Isaac   Not Collective
7282c58f1c22SToby Isaac 
7283c58f1c22SToby Isaac   Input Parameters:
7284bb7acecfSBarry Smith + dm    - The `DM` object
7285bb7acecfSBarry Smith - label - The `DMLabel`
7286c58f1c22SToby Isaac 
7287c58f1c22SToby Isaac   Level: developer
7288c58f1c22SToby Isaac 
728960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7290c58f1c22SToby Isaac @*/
7291d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7292d71ae5a4SJacob Faibussowitsch {
72935d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7294c58f1c22SToby Isaac   PetscBool   hasLabel;
7295d67d17b1SMatthew G. Knepley   const char *lname;
72965d80c0bfSVaclav Hapla   PetscBool   flg;
7297c58f1c22SToby Isaac 
7298c58f1c22SToby Isaac   PetscFunctionBegin;
7299c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73009566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
73019566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
73027a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
73039566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7304c58f1c22SToby Isaac   tmpLabel->label  = label;
7305c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
73065d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
73075d80c0bfSVaclav Hapla   *p = tmpLabel;
73089566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
73099566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
73105d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
73119566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7312ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
73133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7314c58f1c22SToby Isaac }
7315c58f1c22SToby Isaac 
7316a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7317cc4c1da9SBarry Smith /*@
73184a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
73194a7ee7d0SMatthew G. Knepley 
73204a7ee7d0SMatthew G. Knepley   Not Collective
73214a7ee7d0SMatthew G. Knepley 
73224a7ee7d0SMatthew G. Knepley   Input Parameters:
7323bb7acecfSBarry Smith + dm    - The `DM` object
7324bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
73254a7ee7d0SMatthew G. Knepley 
7326bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7327bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7328bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7329bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7330bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7331bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7332bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
73334a7ee7d0SMatthew G. Knepley 
73344a7ee7d0SMatthew G. Knepley   Level: intermediate
73354a7ee7d0SMatthew G. Knepley 
73361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
73374a7ee7d0SMatthew G. Knepley @*/
7338d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7339d71ae5a4SJacob Faibussowitsch {
73404a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
73414a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
73424a7ee7d0SMatthew G. Knepley   const char *name, *lname;
73434a7ee7d0SMatthew G. Knepley 
73444a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
73454a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73464a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
73479566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
73484a7ee7d0SMatthew G. Knepley   while (next) {
73499566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73509566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
73514a7ee7d0SMatthew G. Knepley     if (hasLabel) {
73529566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
73539566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
73544a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
73559566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
73564a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
73579566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
73584a7ee7d0SMatthew G. Knepley       next->label = label;
73594a7ee7d0SMatthew G. Knepley       break;
73604a7ee7d0SMatthew G. Knepley     }
73614a7ee7d0SMatthew G. Knepley     next = next->next;
73624a7ee7d0SMatthew G. Knepley   }
73633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
73644a7ee7d0SMatthew G. Knepley }
73654a7ee7d0SMatthew G. Knepley 
7366cc4c1da9SBarry Smith /*@
7367bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7368c58f1c22SToby Isaac 
7369c58f1c22SToby Isaac   Not Collective
7370c58f1c22SToby Isaac 
7371c58f1c22SToby Isaac   Input Parameters:
7372bb7acecfSBarry Smith + dm   - The `DM` object
7373c58f1c22SToby Isaac - name - The label name
7374c58f1c22SToby Isaac 
7375c58f1c22SToby Isaac   Output Parameter:
737620f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7377bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7378c58f1c22SToby Isaac 
7379c58f1c22SToby Isaac   Level: developer
7380c58f1c22SToby Isaac 
73811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7382c58f1c22SToby Isaac @*/
7383d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7384d71ae5a4SJacob Faibussowitsch {
738595d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7386c58f1c22SToby Isaac   PetscBool   hasLabel;
7387d67d17b1SMatthew G. Knepley   const char *lname;
7388c58f1c22SToby Isaac 
7389c58f1c22SToby Isaac   PetscFunctionBegin;
7390c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73914f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7392e5472504SVaclav Hapla   if (label) {
73934f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7394c58f1c22SToby Isaac     *label = NULL;
7395e5472504SVaclav Hapla   }
73965d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
73979566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
73989566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7399c58f1c22SToby Isaac     if (hasLabel) {
740095d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
74019566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
740295d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
74039566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7404ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
740595d578d6SVaclav Hapla       if (label) *label = link->label;
74069566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
74079566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7408c58f1c22SToby Isaac       break;
7409c58f1c22SToby Isaac     }
7410c58f1c22SToby Isaac   }
74113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7412c58f1c22SToby Isaac }
7413c58f1c22SToby Isaac 
7414306894acSVaclav Hapla /*@
7415bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7416306894acSVaclav Hapla 
7417306894acSVaclav Hapla   Not Collective
7418306894acSVaclav Hapla 
7419306894acSVaclav Hapla   Input Parameters:
7420bb7acecfSBarry Smith + dm           - The `DM` object
7421bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
742220f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7423306894acSVaclav Hapla 
7424306894acSVaclav Hapla   Level: developer
7425306894acSVaclav Hapla 
7426bb7acecfSBarry Smith   Note:
7427306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7428bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7429306894acSVaclav Hapla   *label nullified.
7430306894acSVaclav Hapla 
74311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7432306894acSVaclav Hapla @*/
7433d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7434d71ae5a4SJacob Faibussowitsch {
743543e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7436306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7437306894acSVaclav Hapla 
7438306894acSVaclav Hapla   PetscFunctionBegin;
7439306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74404f572ea9SToby Isaac   PetscAssertPointer(label, 2);
74413ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7442306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7443306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
74445d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
744543e45a93SVaclav Hapla     if (*label == link->label) {
7446306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
744743e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7448306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7449ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
745043e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
74519566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
74529566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7453306894acSVaclav Hapla       break;
7454306894acSVaclav Hapla     }
7455306894acSVaclav Hapla   }
74567a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
74573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7458306894acSVaclav Hapla }
7459306894acSVaclav Hapla 
7460cc4c1da9SBarry Smith /*@
7461c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7462c58f1c22SToby Isaac 
7463c58f1c22SToby Isaac   Not Collective
7464c58f1c22SToby Isaac 
7465c58f1c22SToby Isaac   Input Parameters:
7466bb7acecfSBarry Smith + dm   - The `DM` object
7467c58f1c22SToby Isaac - name - The label name
7468c58f1c22SToby Isaac 
7469c58f1c22SToby Isaac   Output Parameter:
7470c58f1c22SToby Isaac . output - The flag for output
7471c58f1c22SToby Isaac 
7472c58f1c22SToby Isaac   Level: developer
7473c58f1c22SToby Isaac 
74741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7475c58f1c22SToby Isaac @*/
7476d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7477d71ae5a4SJacob Faibussowitsch {
74785d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7479d67d17b1SMatthew G. Knepley   const char *lname;
7480c58f1c22SToby Isaac 
7481c58f1c22SToby Isaac   PetscFunctionBegin;
7482c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74834f572ea9SToby Isaac   PetscAssertPointer(name, 2);
74844f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7485c58f1c22SToby Isaac   while (next) {
7486c58f1c22SToby Isaac     PetscBool flg;
7487c58f1c22SToby Isaac 
74889566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74899566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74909371c9d4SSatish Balay     if (flg) {
74919371c9d4SSatish Balay       *output = next->output;
74923ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74939371c9d4SSatish Balay     }
7494c58f1c22SToby Isaac     next = next->next;
7495c58f1c22SToby Isaac   }
749698921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7497c58f1c22SToby Isaac }
7498c58f1c22SToby Isaac 
7499cc4c1da9SBarry Smith /*@
7500bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7501c58f1c22SToby Isaac 
7502c58f1c22SToby Isaac   Not Collective
7503c58f1c22SToby Isaac 
7504c58f1c22SToby Isaac   Input Parameters:
7505bb7acecfSBarry Smith + dm     - The `DM` object
7506c58f1c22SToby Isaac . name   - The label name
7507bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7508c58f1c22SToby Isaac 
7509c58f1c22SToby Isaac   Level: developer
7510c58f1c22SToby Isaac 
75111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7512c58f1c22SToby Isaac @*/
7513d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7514d71ae5a4SJacob Faibussowitsch {
75155d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7516d67d17b1SMatthew G. Knepley   const char *lname;
7517c58f1c22SToby Isaac 
7518c58f1c22SToby Isaac   PetscFunctionBegin;
7519c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75204f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7521c58f1c22SToby Isaac   while (next) {
7522c58f1c22SToby Isaac     PetscBool flg;
7523c58f1c22SToby Isaac 
75249566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
75259566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
75269371c9d4SSatish Balay     if (flg) {
75279371c9d4SSatish Balay       next->output = output;
75283ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
75299371c9d4SSatish Balay     }
7530c58f1c22SToby Isaac     next = next->next;
7531c58f1c22SToby Isaac   }
753298921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7533c58f1c22SToby Isaac }
7534c58f1c22SToby Isaac 
7535c58f1c22SToby Isaac /*@
7536bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7537c58f1c22SToby Isaac 
753820f4b53cSBarry Smith   Collective
7539c58f1c22SToby Isaac 
7540d8d19677SJose E. Roman   Input Parameters:
7541bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7542bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7543bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7544bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7545bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7546c58f1c22SToby Isaac 
7547c58f1c22SToby Isaac   Level: intermediate
7548c58f1c22SToby Isaac 
7549bb7acecfSBarry Smith   Note:
75502cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7551c58f1c22SToby Isaac 
75521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7553c58f1c22SToby Isaac @*/
7554d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7555d71ae5a4SJacob Faibussowitsch {
75562cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7557c58f1c22SToby Isaac   const char *name;
7558c58f1c22SToby Isaac   PetscBool   flg;
75595d80c0bfSVaclav Hapla   DMLabelLink link;
7560c58f1c22SToby Isaac 
75615d80c0bfSVaclav Hapla   PetscFunctionBegin;
75625d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
75635d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
75645d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
75655d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
75667a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
75673ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
75685d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
75695d80c0bfSVaclav Hapla     label = link->label;
75709566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
75715d80c0bfSVaclav Hapla     if (!all) {
75729566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7573c58f1c22SToby Isaac       if (flg) continue;
75749566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
75757d5acc75SStefano Zampini       if (flg) continue;
75769566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7577ba2698f1SMatthew G. Knepley       if (flg) continue;
75785d80c0bfSVaclav Hapla     }
75799566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
75802cbb9b06SVaclav Hapla     if (labelOld) {
75812cbb9b06SVaclav Hapla       switch (emode) {
7582d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7583d71ae5a4SJacob Faibussowitsch         continue;
7584d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7585d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7586d71ae5a4SJacob Faibussowitsch         break;
7587d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7588d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7589d71ae5a4SJacob Faibussowitsch       default:
7590d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
75912cbb9b06SVaclav Hapla       }
75922cbb9b06SVaclav Hapla     }
75935d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
75949566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
75955d80c0bfSVaclav Hapla     } else {
75965d80c0bfSVaclav Hapla       labelNew = label;
75975d80c0bfSVaclav Hapla     }
75989566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
75999566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7600c58f1c22SToby Isaac   }
76013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7602c58f1c22SToby Isaac }
7603461a15a0SLisandro Dalcin 
7604609dae6eSVaclav Hapla /*@C
7605b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7606609dae6eSVaclav Hapla 
760720f4b53cSBarry Smith   Collective; No Fortran Support
7608609dae6eSVaclav Hapla 
7609609dae6eSVaclav Hapla   Input Parameters:
7610bb7acecfSBarry Smith + dm0 - First `DM` object
7611bb7acecfSBarry Smith - dm1 - Second `DM` object
7612609dae6eSVaclav Hapla 
7613a4e35b19SJacob Faibussowitsch   Output Parameters:
7614ce78bad3SBarry Smith + equal   - (Optional) Flag whether labels of `dm0` and `dm1` are the same
761520f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7616609dae6eSVaclav Hapla 
7617609dae6eSVaclav Hapla   Level: intermediate
7618609dae6eSVaclav Hapla 
7619609dae6eSVaclav Hapla   Notes:
7620bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7621bb7acecfSBarry Smith 
762220f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7623bb7acecfSBarry Smith 
762420f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7625609dae6eSVaclav Hapla 
76265efe38ccSVaclav Hapla   The output message is set independently on each rank.
7627bb7acecfSBarry Smith 
7628bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7629bb7acecfSBarry Smith 
7630ce78bad3SBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to `stderr` in synchronized manner.
7631bb7acecfSBarry Smith 
763220f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7633609dae6eSVaclav Hapla 
7634609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7635bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7636609dae6eSVaclav Hapla 
7637cc4c1da9SBarry Smith   Developer Note:
7638ce78bad3SBarry Smith   Cannot automatically generate the Fortran stub because `message` must be freed with `PetscFree()`
7639cc4c1da9SBarry Smith 
76401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7641609dae6eSVaclav Hapla @*/
7642ce78bad3SBarry Smith PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char *message[]) PeNS
7643d71ae5a4SJacob Faibussowitsch {
76445efe38ccSVaclav Hapla   PetscInt    n, i;
7645609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
76465efe38ccSVaclav Hapla   PetscBool   eq;
7647609dae6eSVaclav Hapla   MPI_Comm    comm;
76485efe38ccSVaclav Hapla   PetscMPIInt rank;
7649609dae6eSVaclav Hapla 
7650609dae6eSVaclav Hapla   PetscFunctionBegin;
7651609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7652609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7653609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
76544f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
76554f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
76569566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
76579566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
76585efe38ccSVaclav Hapla   {
76595efe38ccSVaclav Hapla     PetscInt n1;
76605efe38ccSVaclav Hapla 
76619566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
76629566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
76635efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
766448a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7665462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
76665efe38ccSVaclav Hapla     if (!eq) goto finish;
76675efe38ccSVaclav Hapla   }
76685efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7669609dae6eSVaclav Hapla     DMLabel     l0, l1;
7670609dae6eSVaclav Hapla     const char *name;
7671609dae6eSVaclav Hapla     char       *msgInner;
7672609dae6eSVaclav Hapla 
7673609dae6eSVaclav Hapla     /* Ignore label order */
76749566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
76759566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
76769566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7677609dae6eSVaclav Hapla     if (!l1) {
767863a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
76795efe38ccSVaclav Hapla       eq = PETSC_FALSE;
76805efe38ccSVaclav Hapla       break;
7681609dae6eSVaclav Hapla     }
76829566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
76839566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
76849566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
76855efe38ccSVaclav Hapla     if (!eq) break;
7686609dae6eSVaclav Hapla   }
7687462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7688609dae6eSVaclav Hapla finish:
76895efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7690609dae6eSVaclav Hapla   if (message) {
7691609dae6eSVaclav Hapla     *message = NULL;
769248a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
76935efe38ccSVaclav Hapla   } else {
769448a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
76959566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
76965efe38ccSVaclav Hapla   }
76975efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
76985efe38ccSVaclav Hapla   if (equal) *equal = eq;
76997a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
77003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7701609dae6eSVaclav Hapla }
7702609dae6eSVaclav Hapla 
7703d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7704d71ae5a4SJacob Faibussowitsch {
7705461a15a0SLisandro Dalcin   PetscFunctionBegin;
77064f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7707461a15a0SLisandro Dalcin   if (!*label) {
77089566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
77099566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7710461a15a0SLisandro Dalcin   }
77119566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
77123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7713461a15a0SLisandro Dalcin }
7714461a15a0SLisandro Dalcin 
77150fdc7489SMatthew Knepley /*
77160fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
77170fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
77180fdc7489SMatthew Knepley   (label, id) pair in the DM.
77190fdc7489SMatthew Knepley 
77200fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
77210fdc7489SMatthew Knepley   each label.
77220fdc7489SMatthew Knepley */
7723d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7724d71ae5a4SJacob Faibussowitsch {
77250fdc7489SMatthew Knepley   DMUniversalLabel ul;
77260fdc7489SMatthew Knepley   PetscBool       *active;
77270fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
77280fdc7489SMatthew Knepley 
77290fdc7489SMatthew Knepley   PetscFunctionBegin;
77309566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
77319566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
77329566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
77339566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
77340fdc7489SMatthew Knepley   ul->Nl = 0;
77350fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
77360fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
77370fdc7489SMatthew Knepley     const char *name;
77380fdc7489SMatthew Knepley 
77399566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
77409566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
77419566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
77420fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
77430fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
77440fdc7489SMatthew Knepley   }
77459566063dSJacob 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));
77460fdc7489SMatthew Knepley   ul->Nv = 0;
77470fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
77480fdc7489SMatthew Knepley     DMLabel     label;
77490fdc7489SMatthew Knepley     PetscInt    nv;
77500fdc7489SMatthew Knepley     const char *name;
77510fdc7489SMatthew Knepley 
77520fdc7489SMatthew Knepley     if (!active[l]) continue;
77539566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
77549566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
77559566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
77569566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
77570fdc7489SMatthew Knepley     ul->indices[m] = l;
77580fdc7489SMatthew Knepley     ul->Nv += nv;
77590fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
77600fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
77610fdc7489SMatthew Knepley     ++m;
77620fdc7489SMatthew Knepley   }
77630fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
77640fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
77650fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
77660fdc7489SMatthew Knepley   }
77670fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
77680fdc7489SMatthew Knepley     PetscInt b;
77690fdc7489SMatthew Knepley 
77700fdc7489SMatthew Knepley     ul->masks[l] = 0;
77710fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
77720fdc7489SMatthew Knepley   }
77739566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
77740fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
77750fdc7489SMatthew Knepley     DMLabel         label;
77760fdc7489SMatthew Knepley     IS              valueIS;
77770fdc7489SMatthew Knepley     const PetscInt *varr;
77780fdc7489SMatthew Knepley     PetscInt        nv, v;
77790fdc7489SMatthew Knepley 
77800fdc7489SMatthew Knepley     if (!active[l]) continue;
77819566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
77829566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
77839566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
77849566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7785ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
77869566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
77879566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
77889566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
77890fdc7489SMatthew Knepley     ++m;
77900fdc7489SMatthew Knepley   }
77919566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
77920fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
77930fdc7489SMatthew Knepley     PetscInt  uval   = 0;
77940fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
77950fdc7489SMatthew Knepley 
77960fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
77970fdc7489SMatthew Knepley       DMLabel  label;
77980649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
77990fdc7489SMatthew Knepley 
78000fdc7489SMatthew Knepley       if (!active[l]) continue;
78019566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
78029566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
78039566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
78049371c9d4SSatish Balay       if (val == defval) {
78059371c9d4SSatish Balay         ++m;
78069371c9d4SSatish Balay         continue;
78079371c9d4SSatish Balay       }
78080649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
78090fdc7489SMatthew Knepley       marked = PETSC_TRUE;
78109566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
781163a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
78120fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
78130fdc7489SMatthew Knepley       ++m;
78140fdc7489SMatthew Knepley     }
78159566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
78160fdc7489SMatthew Knepley   }
78179566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
78180fdc7489SMatthew Knepley   *universal = ul;
78193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78200fdc7489SMatthew Knepley }
78210fdc7489SMatthew Knepley 
7822d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7823d71ae5a4SJacob Faibussowitsch {
78240fdc7489SMatthew Knepley   PetscInt l;
78250fdc7489SMatthew Knepley 
78260fdc7489SMatthew Knepley   PetscFunctionBegin;
78279566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
78289566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
78299566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
78309566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
78319566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
78320fdc7489SMatthew Knepley   *universal = NULL;
78333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78340fdc7489SMatthew Knepley }
78350fdc7489SMatthew Knepley 
7836d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7837d71ae5a4SJacob Faibussowitsch {
78380fdc7489SMatthew Knepley   PetscFunctionBegin;
78394f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
78400fdc7489SMatthew Knepley   *ulabel = ul->label;
78413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78420fdc7489SMatthew Knepley }
78430fdc7489SMatthew Knepley 
7844d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7845d71ae5a4SJacob Faibussowitsch {
78460fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
78470fdc7489SMatthew Knepley 
78480fdc7489SMatthew Knepley   PetscFunctionBegin;
7849064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
78500fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
78519566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
78529566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
78530fdc7489SMatthew Knepley   }
78540fdc7489SMatthew Knepley   if (preserveOrder) {
78550fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
78560fdc7489SMatthew Knepley       const char *name;
78570fdc7489SMatthew Knepley       PetscBool   match;
78580fdc7489SMatthew Knepley 
78599566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
78609566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
786163a3b9bcSJacob 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]);
78620fdc7489SMatthew Knepley     }
78630fdc7489SMatthew Knepley   }
78643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78650fdc7489SMatthew Knepley }
78660fdc7489SMatthew Knepley 
7867d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7868d71ae5a4SJacob Faibussowitsch {
78690fdc7489SMatthew Knepley   PetscInt l;
78700fdc7489SMatthew Knepley 
78710fdc7489SMatthew Knepley   PetscFunctionBegin;
78720fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
78730fdc7489SMatthew Knepley     DMLabel  label;
78740fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
78750fdc7489SMatthew Knepley 
78760fdc7489SMatthew Knepley     if (lval) {
78779566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
78789566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
78799566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
78800fdc7489SMatthew Knepley     }
78810fdc7489SMatthew Knepley   }
78823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78830fdc7489SMatthew Knepley }
7884a8fb8f29SToby Isaac 
7885a8fb8f29SToby Isaac /*@
7886bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7887bb7acecfSBarry Smith 
788820f4b53cSBarry Smith   Not Collective
7889a8fb8f29SToby Isaac 
7890a8fb8f29SToby Isaac   Input Parameter:
7891bb7acecfSBarry Smith . dm - The `DM` object
7892a8fb8f29SToby Isaac 
7893a8fb8f29SToby Isaac   Output Parameter:
7894bb7acecfSBarry Smith . cdm - The coarse `DM`
7895a8fb8f29SToby Isaac 
7896a8fb8f29SToby Isaac   Level: intermediate
7897a8fb8f29SToby Isaac 
78981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7899a8fb8f29SToby Isaac @*/
7900d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7901d71ae5a4SJacob Faibussowitsch {
7902a8fb8f29SToby Isaac   PetscFunctionBegin;
7903a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79044f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7905a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
79063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7907a8fb8f29SToby Isaac }
7908a8fb8f29SToby Isaac 
7909a8fb8f29SToby Isaac /*@
7910bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7911a8fb8f29SToby Isaac 
7912a8fb8f29SToby Isaac   Input Parameters:
7913bb7acecfSBarry Smith + dm  - The `DM` object
7914bb7acecfSBarry Smith - cdm - The coarse `DM`
7915a8fb8f29SToby Isaac 
7916a8fb8f29SToby Isaac   Level: intermediate
7917a8fb8f29SToby Isaac 
7918bb7acecfSBarry Smith   Note:
7919bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7920bb7acecfSBarry Smith 
79211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7922a8fb8f29SToby Isaac @*/
7923d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7924d71ae5a4SJacob Faibussowitsch {
7925a8fb8f29SToby Isaac   PetscFunctionBegin;
7926a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7927a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
792889d734beSBarry Smith   if (dm == cdm) cdm = NULL;
79299566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
79309566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7931a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
79323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7933a8fb8f29SToby Isaac }
7934a8fb8f29SToby Isaac 
793588bdff64SToby Isaac /*@
7936bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
793788bdff64SToby Isaac 
793888bdff64SToby Isaac   Input Parameter:
7939bb7acecfSBarry Smith . dm - The `DM` object
794088bdff64SToby Isaac 
794188bdff64SToby Isaac   Output Parameter:
7942bb7acecfSBarry Smith . fdm - The fine `DM`
794388bdff64SToby Isaac 
794488bdff64SToby Isaac   Level: intermediate
794588bdff64SToby Isaac 
79461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
794788bdff64SToby Isaac @*/
7948d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7949d71ae5a4SJacob Faibussowitsch {
795088bdff64SToby Isaac   PetscFunctionBegin;
795188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79524f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
795388bdff64SToby Isaac   *fdm = dm->fineMesh;
79543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
795588bdff64SToby Isaac }
795688bdff64SToby Isaac 
795788bdff64SToby Isaac /*@
7958bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
795988bdff64SToby Isaac 
796088bdff64SToby Isaac   Input Parameters:
7961bb7acecfSBarry Smith + dm  - The `DM` object
7962bb7acecfSBarry Smith - fdm - The fine `DM`
796388bdff64SToby Isaac 
7964bb7acecfSBarry Smith   Level: developer
796588bdff64SToby Isaac 
7966bb7acecfSBarry Smith   Note:
7967bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7968bb7acecfSBarry Smith 
79691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
797088bdff64SToby Isaac @*/
7971d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7972d71ae5a4SJacob Faibussowitsch {
797388bdff64SToby Isaac   PetscFunctionBegin;
797488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
797588bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
797689d734beSBarry Smith   if (dm == fdm) fdm = NULL;
79779566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
79789566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
797988bdff64SToby Isaac   dm->fineMesh = fdm;
79803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
798188bdff64SToby Isaac }
798288bdff64SToby Isaac 
7983a6ba4734SToby Isaac /*@C
7984bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7985a6ba4734SToby Isaac 
798620f4b53cSBarry Smith   Collective
7987783e2ec8SMatthew G. Knepley 
7988a6ba4734SToby Isaac   Input Parameters:
7989bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7990bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7991a6ba4734SToby Isaac . name     - The BC name
799245480ffeSMatthew G. Knepley . label    - The label defining constrained points
7993bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
799445480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7995a6ba4734SToby Isaac . field    - The field to constrain
7996258f4e96SStefano Zampini . Nc       - The number of constrained field components (0 will constrain all components)
7997a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7998a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
799956cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
8000a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
8001a6ba4734SToby Isaac 
800245480ffeSMatthew G. Knepley   Output Parameter:
800345480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
800445480ffeSMatthew G. Knepley 
8005a6ba4734SToby Isaac   Options Database Keys:
8006a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
8007a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8008a6ba4734SToby Isaac 
800920f4b53cSBarry Smith   Level: intermediate
801020f4b53cSBarry Smith 
8011bb7acecfSBarry Smith   Notes:
8012fa74d514SMatthew G. Knepley   If the `DM` is of type `DMPLEX` and the field is of type `PetscFE`, then this function completes the label using `DMPlexLabelComplete()`.
8013fa74d514SMatthew G. Knepley 
8014aeebefc2SPierre Jolivet   Both bcFunc and bcFunc_t will depend on the boundary condition type. If the type if `DM_BC_ESSENTIAL`, then the calling sequence is\:
801573ff1848SBarry Smith .vb
801673ff1848SBarry Smith  void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
801773ff1848SBarry Smith .ve
801856cf3b9cSMatthew G. Knepley 
8019a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
802056cf3b9cSMatthew G. Knepley 
802120f4b53cSBarry Smith .vb
802220f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
802320f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
802420f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
802520f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
802620f4b53cSBarry Smith .ve
802756cf3b9cSMatthew G. Knepley + dim - the spatial dimension
802856cf3b9cSMatthew G. Knepley . Nf - the number of fields
802956cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
803056cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
803156cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
803256cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
803356cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
803456cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
803556cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
803656cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
803756cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
803856cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
803956cf3b9cSMatthew G. Knepley . t - current time
804056cf3b9cSMatthew G. Knepley . x - coordinates of the current point
804156cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
804256cf3b9cSMatthew G. Knepley . constants - constant parameters
804356cf3b9cSMatthew G. Knepley - bcval - output values at the current point
804456cf3b9cSMatthew G. Knepley 
80451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
8046a6ba4734SToby Isaac @*/
8047d71ae5a4SJacob 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)
8048d71ae5a4SJacob Faibussowitsch {
8049e5e52638SMatthew G. Knepley   PetscDS ds;
8050a6ba4734SToby Isaac 
8051a6ba4734SToby Isaac   PetscFunctionBegin;
8052a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8053783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
805445480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
805545480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
805645480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
805745480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
805801a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
80599566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8060799db056SMatthew G. Knepley   /* Complete label */
8061799db056SMatthew G. Knepley   if (label) {
8062799db056SMatthew G. Knepley     PetscObject  obj;
8063799db056SMatthew G. Knepley     PetscClassId id;
8064799db056SMatthew G. Knepley 
8065799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
8066799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
8067799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
8068799db056SMatthew G. Knepley       DM plex;
8069799db056SMatthew G. Knepley 
8070799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
8071799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
8072799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
8073799db056SMatthew G. Knepley     }
8074799db056SMatthew G. Knepley   }
80759566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
80763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8077a6ba4734SToby Isaac }
8078a6ba4734SToby Isaac 
807945480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8080d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
8081d71ae5a4SJacob Faibussowitsch {
8082e5e52638SMatthew G. Knepley   PetscDS     ds;
8083dff059c6SToby Isaac   DMBoundary *lastnext;
8084e6f8dbb6SToby Isaac   DSBoundary  dsbound;
8085e6f8dbb6SToby Isaac 
8086e6f8dbb6SToby Isaac   PetscFunctionBegin;
80879566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8088e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
808947a1f5adSToby Isaac   if (dm->boundary) {
809047a1f5adSToby Isaac     DMBoundary next = dm->boundary;
809147a1f5adSToby Isaac 
809247a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
80933ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
809447a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
809547a1f5adSToby Isaac     while (next) {
809647a1f5adSToby Isaac       DMBoundary b = next;
809747a1f5adSToby Isaac 
809847a1f5adSToby Isaac       next = b->next;
80999566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
8100a6ba4734SToby Isaac     }
810147a1f5adSToby Isaac     dm->boundary = NULL;
8102a6ba4734SToby Isaac   }
810347a1f5adSToby Isaac 
8104f4f49eeaSPierre Jolivet   lastnext = &dm->boundary;
8105e6f8dbb6SToby Isaac   while (dsbound) {
8106e6f8dbb6SToby Isaac     DMBoundary dmbound;
8107e6f8dbb6SToby Isaac 
81089566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
8109e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
811045480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
811147a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8112dff059c6SToby Isaac     *lastnext = dmbound;
8113f4f49eeaSPierre Jolivet     lastnext  = &dmbound->next;
8114dff059c6SToby Isaac     dsbound   = dsbound->next;
8115a6ba4734SToby Isaac   }
81163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8117a6ba4734SToby Isaac }
8118a6ba4734SToby Isaac 
8119bb7acecfSBarry Smith /* TODO: missing manual page */
8120d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8121d71ae5a4SJacob Faibussowitsch {
8122b95f2879SToby Isaac   DMBoundary b;
8123a6ba4734SToby Isaac 
8124a6ba4734SToby Isaac   PetscFunctionBegin;
8125a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81264f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
8127a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
81289566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
8129b95f2879SToby Isaac   b = dm->boundary;
813057508eceSPierre Jolivet   while (b && !*isBd) {
8131e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8132e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8133a6ba4734SToby Isaac     PetscInt   i;
8134a6ba4734SToby Isaac 
813545480ffeSMatthew G. Knepley     if (label) {
813657508eceSPierre Jolivet       for (i = 0; i < dsb->Nv && !*isBd; ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8137a6ba4734SToby Isaac     }
8138a6ba4734SToby Isaac     b = b->next;
8139a6ba4734SToby Isaac   }
81403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8141a6ba4734SToby Isaac }
81424d6f44ffSToby Isaac 
814301468941SMatthew G. Knepley /*@
814401468941SMatthew G. Knepley   DMHasBound - Determine whether a bound condition was specified
814501468941SMatthew G. Knepley 
814601468941SMatthew G. Knepley   Logically collective
814701468941SMatthew G. Knepley 
814801468941SMatthew G. Knepley   Input Parameter:
814901468941SMatthew G. Knepley . dm - The `DM`, with a `PetscDS` that matches the problem being constrained
815001468941SMatthew G. Knepley 
815101468941SMatthew G. Knepley   Output Parameter:
815201468941SMatthew G. Knepley . hasBound - Flag indicating if a bound condition was specified
815301468941SMatthew G. Knepley 
815401468941SMatthew G. Knepley   Level: intermediate
815501468941SMatthew G. Knepley 
815601468941SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DSAddBoundary()`, `PetscDSAddBoundary()`
815701468941SMatthew G. Knepley @*/
815801468941SMatthew G. Knepley PetscErrorCode DMHasBound(DM dm, PetscBool *hasBound)
815901468941SMatthew G. Knepley {
816001468941SMatthew G. Knepley   PetscDS  ds;
816101468941SMatthew G. Knepley   PetscInt Nf, numBd;
816201468941SMatthew G. Knepley 
816301468941SMatthew G. Knepley   PetscFunctionBegin;
816401468941SMatthew G. Knepley   *hasBound = PETSC_FALSE;
816501468941SMatthew G. Knepley   PetscCall(DMGetDS(dm, &ds));
816601468941SMatthew G. Knepley   PetscCall(PetscDSGetNumFields(ds, &Nf));
816701468941SMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
81682192575eSBarry Smith     PetscSimplePointFn *lfunc, *ufunc;
816901468941SMatthew G. Knepley 
817001468941SMatthew G. Knepley     PetscCall(PetscDSGetLowerBound(ds, f, &lfunc, NULL));
817101468941SMatthew G. Knepley     PetscCall(PetscDSGetUpperBound(ds, f, &ufunc, NULL));
817201468941SMatthew G. Knepley     if (lfunc || ufunc) *hasBound = PETSC_TRUE;
817301468941SMatthew G. Knepley   }
817401468941SMatthew G. Knepley 
817501468941SMatthew G. Knepley   PetscCall(PetscDSGetNumBoundary(ds, &numBd));
817601468941SMatthew G. Knepley   PetscCall(PetscDSUpdateBoundaryLabels(ds, dm));
817701468941SMatthew G. Knepley   for (PetscInt b = 0; b < numBd; ++b) {
817801468941SMatthew G. Knepley     PetscWeakForm           wf;
817901468941SMatthew G. Knepley     DMBoundaryConditionType type;
818001468941SMatthew G. Knepley     const char             *name;
818101468941SMatthew G. Knepley     DMLabel                 label;
818201468941SMatthew G. Knepley     PetscInt                numids;
818301468941SMatthew G. Knepley     const PetscInt         *ids;
818401468941SMatthew G. Knepley     PetscInt                field, Nc;
818501468941SMatthew G. Knepley     const PetscInt         *comps;
818601468941SMatthew G. Knepley     void (*bvfunc)(void);
818701468941SMatthew G. Knepley     void *ctx;
818801468941SMatthew G. Knepley 
818901468941SMatthew G. Knepley     PetscCall(PetscDSGetBoundary(ds, b, &wf, &type, &name, &label, &numids, &ids, &field, &Nc, &comps, &bvfunc, NULL, &ctx));
819001468941SMatthew G. Knepley     if (type == DM_BC_LOWER_BOUND || type == DM_BC_UPPER_BOUND) *hasBound = PETSC_TRUE;
819101468941SMatthew G. Knepley   }
819201468941SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
819301468941SMatthew G. Knepley }
819401468941SMatthew G. Knepley 
81954d6f44ffSToby Isaac /*@C
8196bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
8197a6e0b375SMatthew G. Knepley 
819820f4b53cSBarry Smith   Collective
81994d6f44ffSToby Isaac 
82004d6f44ffSToby Isaac   Input Parameters:
8201bb7acecfSBarry Smith + dm    - The `DM`
82020709b2feSToby Isaac . time  - The time
82034d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
82044d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
82054d6f44ffSToby Isaac - mode  - The insertion mode for values
82064d6f44ffSToby Isaac 
82074d6f44ffSToby Isaac   Output Parameter:
82084d6f44ffSToby Isaac . X - vector
82094d6f44ffSToby Isaac 
821020f4b53cSBarry Smith   Calling sequence of `funcs`:
82114d6f44ffSToby Isaac + dim  - The spatial dimension
82128ec8862eSJed Brown . time - The time at which to sample
82134d6f44ffSToby Isaac . x    - The coordinates
821477b739a6SMatthew Knepley . Nc   - The number of components
82154d6f44ffSToby Isaac . u    - The output field values
82164d6f44ffSToby Isaac - ctx  - optional user-defined function context
82174d6f44ffSToby Isaac 
82184d6f44ffSToby Isaac   Level: developer
82194d6f44ffSToby Isaac 
8220bb7acecfSBarry Smith   Developer Notes:
8221bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8222bb7acecfSBarry Smith 
8223bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8224bb7acecfSBarry Smith 
82251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
82264d6f44ffSToby Isaac @*/
8227a4e35b19SJacob 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)
8228d71ae5a4SJacob Faibussowitsch {
82294d6f44ffSToby Isaac   Vec localX;
82304d6f44ffSToby Isaac 
82314d6f44ffSToby Isaac   PetscFunctionBegin;
82324d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8233708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
82349566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8235f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
82369566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
82379566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
82389566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
82399566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
8240708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
82413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82424d6f44ffSToby Isaac }
82434d6f44ffSToby Isaac 
8244a6e0b375SMatthew G. Knepley /*@C
8245bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
8246a6e0b375SMatthew G. Knepley 
824720f4b53cSBarry Smith   Not Collective
8248a6e0b375SMatthew G. Knepley 
8249a6e0b375SMatthew G. Knepley   Input Parameters:
8250bb7acecfSBarry Smith + dm    - The `DM`
8251a6e0b375SMatthew G. Knepley . time  - The time
8252a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
8253a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8254a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
8255a6e0b375SMatthew G. Knepley 
8256a6e0b375SMatthew G. Knepley   Output Parameter:
8257a6e0b375SMatthew G. Knepley . localX - vector
8258a6e0b375SMatthew G. Knepley 
825920f4b53cSBarry Smith   Calling sequence of `funcs`:
8260a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8261a4e35b19SJacob Faibussowitsch . time - The current timestep
8262a6e0b375SMatthew G. Knepley . x    - The coordinates
826377b739a6SMatthew Knepley . Nc   - The number of components
8264a6e0b375SMatthew G. Knepley . u    - The output field values
8265a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8266a6e0b375SMatthew G. Knepley 
8267a6e0b375SMatthew G. Knepley   Level: developer
8268a6e0b375SMatthew G. Knepley 
8269bb7acecfSBarry Smith   Developer Notes:
8270bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8271bb7acecfSBarry Smith 
8272bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8273bb7acecfSBarry Smith 
82741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8275a6e0b375SMatthew G. Knepley @*/
8276a4e35b19SJacob 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)
8277d71ae5a4SJacob Faibussowitsch {
82784d6f44ffSToby Isaac   PetscFunctionBegin;
82794d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8280064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8281fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlocal, time, funcs, ctxs, mode, localX);
82823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82834d6f44ffSToby Isaac }
82844d6f44ffSToby Isaac 
8285a6e0b375SMatthew G. Knepley /*@C
8286bb7acecfSBarry 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.
8287a6e0b375SMatthew G. Knepley 
828820f4b53cSBarry Smith   Collective
8289a6e0b375SMatthew G. Knepley 
8290a6e0b375SMatthew G. Knepley   Input Parameters:
8291bb7acecfSBarry Smith + dm     - The `DM`
8292a6e0b375SMatthew G. Knepley . time   - The time
8293a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8294a4e35b19SJacob Faibussowitsch . ids    - The ids
8295a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8296a4e35b19SJacob Faibussowitsch . comps  - The components
8297bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8298a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8299bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8300a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8301a6e0b375SMatthew G. Knepley 
8302a6e0b375SMatthew G. Knepley   Output Parameter:
8303a6e0b375SMatthew G. Knepley . X - vector
8304a6e0b375SMatthew G. Knepley 
830520f4b53cSBarry Smith   Calling sequence of `funcs`:
8306a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8307a4e35b19SJacob Faibussowitsch . time - The current timestep
8308a6e0b375SMatthew G. Knepley . x    - The coordinates
830977b739a6SMatthew Knepley . Nc   - The number of components
8310a6e0b375SMatthew G. Knepley . u    - The output field values
8311a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8312a6e0b375SMatthew G. Knepley 
8313a6e0b375SMatthew G. Knepley   Level: developer
8314a6e0b375SMatthew G. Knepley 
8315bb7acecfSBarry Smith   Developer Notes:
8316bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8317bb7acecfSBarry Smith 
8318bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8319bb7acecfSBarry Smith 
83201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8321a6e0b375SMatthew G. Knepley @*/
8322a4e35b19SJacob 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)
8323d71ae5a4SJacob Faibussowitsch {
83242c53366bSMatthew G. Knepley   Vec localX;
83252c53366bSMatthew G. Knepley 
83262c53366bSMatthew G. Knepley   PetscFunctionBegin;
83272c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
83289566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8329f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
83309566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
83319566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
83329566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
83339566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
83343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83352c53366bSMatthew G. Knepley }
83362c53366bSMatthew G. Knepley 
8337a6e0b375SMatthew G. Knepley /*@C
8338bb7acecfSBarry 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.
8339a6e0b375SMatthew G. Knepley 
834020f4b53cSBarry Smith   Not Collective
8341a6e0b375SMatthew G. Knepley 
8342a6e0b375SMatthew G. Knepley   Input Parameters:
8343bb7acecfSBarry Smith + dm     - The `DM`
8344a6e0b375SMatthew G. Knepley . time   - The time
8345bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8346a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8347a4e35b19SJacob Faibussowitsch . ids    - The ids
8348a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8349a4e35b19SJacob Faibussowitsch . comps  - The components
8350a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8351a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8352a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8353a6e0b375SMatthew G. Knepley 
8354a6e0b375SMatthew G. Knepley   Output Parameter:
8355a6e0b375SMatthew G. Knepley . localX - vector
8356a6e0b375SMatthew G. Knepley 
835720f4b53cSBarry Smith   Calling sequence of `funcs`:
8358a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8359a4e35b19SJacob Faibussowitsch . time - The current time
8360a6e0b375SMatthew G. Knepley . x    - The coordinates
836177b739a6SMatthew Knepley . Nc   - The number of components
8362a6e0b375SMatthew G. Knepley . u    - The output field values
8363a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8364a6e0b375SMatthew G. Knepley 
8365a6e0b375SMatthew G. Knepley   Level: developer
8366a6e0b375SMatthew G. Knepley 
8367bb7acecfSBarry Smith   Developer Notes:
8368bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8369bb7acecfSBarry Smith 
8370bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8371bb7acecfSBarry Smith 
83721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8373a6e0b375SMatthew G. Knepley @*/
8374a4e35b19SJacob 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)
8375d71ae5a4SJacob Faibussowitsch {
83764d6f44ffSToby Isaac   PetscFunctionBegin;
83774d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8378064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8379fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlabellocal, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);
83803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83814d6f44ffSToby Isaac }
83822716604bSToby Isaac 
8383a6e0b375SMatthew G. Knepley /*@C
8384bb7acecfSBarry 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.
8385a6e0b375SMatthew G. Knepley 
838620f4b53cSBarry Smith   Not Collective
8387a6e0b375SMatthew G. Knepley 
8388a6e0b375SMatthew G. Knepley   Input Parameters:
8389bb7acecfSBarry Smith + dm     - The `DM`
8390a6e0b375SMatthew G. Knepley . time   - The time
839120f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8392a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8393a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8394a6e0b375SMatthew G. Knepley 
8395a6e0b375SMatthew G. Knepley   Output Parameter:
8396a6e0b375SMatthew G. Knepley . localX - The output vector
8397a6e0b375SMatthew G. Knepley 
839820f4b53cSBarry Smith   Calling sequence of `funcs`:
8399a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8400a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8401a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8402a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8403a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8404a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8405a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8406a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8407a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8408a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8409a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8410a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8411a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8412a6e0b375SMatthew G. Knepley . t            - The current time
8413a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8414a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8415a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8416a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8417a6e0b375SMatthew G. Knepley 
841873ff1848SBarry Smith   Level: intermediate
841973ff1848SBarry Smith 
8420bb7acecfSBarry Smith   Note:
8421bb7acecfSBarry 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.
8422bb7acecfSBarry 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
8423bb7acecfSBarry 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
8424a6e0b375SMatthew 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.
8425a6e0b375SMatthew G. Knepley 
8426bb7acecfSBarry Smith   Developer Notes:
8427bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8428bb7acecfSBarry Smith 
8429bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8430bb7acecfSBarry Smith 
8431a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8432a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8433a6e0b375SMatthew G. Knepley @*/
8434a4e35b19SJacob 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)
8435d71ae5a4SJacob Faibussowitsch {
84368c6c5593SMatthew G. Knepley   PetscFunctionBegin;
84378c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8438eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
84398c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8440fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlocal, time, localU, funcs, mode, localX);
84413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84428c6c5593SMatthew G. Knepley }
84438c6c5593SMatthew G. Knepley 
8444a6e0b375SMatthew G. Knepley /*@C
8445a6e0b375SMatthew 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.
8446a6e0b375SMatthew G. Knepley 
844720f4b53cSBarry Smith   Not Collective
8448a6e0b375SMatthew G. Knepley 
8449a6e0b375SMatthew G. Knepley   Input Parameters:
8450bb7acecfSBarry Smith + dm     - The `DM`
8451a6e0b375SMatthew G. Knepley . time   - The time
8452bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8453a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8454a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8455bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
845620f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8457a6e0b375SMatthew G. Knepley . localU - The input field vector
8458a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8459a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8460a6e0b375SMatthew G. Knepley 
8461a6e0b375SMatthew G. Knepley   Output Parameter:
8462a6e0b375SMatthew G. Knepley . localX - The output vector
8463a6e0b375SMatthew G. Knepley 
846420f4b53cSBarry Smith   Calling sequence of `funcs`:
8465a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8466a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8467a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8468a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8469a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8470a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8471a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8472a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8473a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8474a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8475a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8476a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8477a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8478a6e0b375SMatthew G. Knepley . t            - The current time
8479a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8480a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8481a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8482a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8483a6e0b375SMatthew G. Knepley 
848473ff1848SBarry Smith   Level: intermediate
848573ff1848SBarry Smith 
8486bb7acecfSBarry Smith   Note:
8487bb7acecfSBarry 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.
8488bb7acecfSBarry 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
8489bb7acecfSBarry 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
8490a6e0b375SMatthew 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.
8491a6e0b375SMatthew G. Knepley 
8492bb7acecfSBarry Smith   Developer Notes:
8493bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8494bb7acecfSBarry Smith 
8495bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8496bb7acecfSBarry Smith 
84971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8498a6e0b375SMatthew G. Knepley @*/
8499a4e35b19SJacob 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)
8500d71ae5a4SJacob Faibussowitsch {
85018c6c5593SMatthew G. Knepley   PetscFunctionBegin;
85028c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8503064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8504064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8505fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
85063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
85078c6c5593SMatthew G. Knepley }
85088c6c5593SMatthew G. Knepley 
85092716604bSToby Isaac /*@C
8510d29d7c6eSMatthew 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.
8511d29d7c6eSMatthew G. Knepley 
851220f4b53cSBarry Smith   Not Collective
8513d29d7c6eSMatthew G. Knepley 
8514d29d7c6eSMatthew G. Knepley   Input Parameters:
8515bb7acecfSBarry Smith + dm     - The `DM`
8516d29d7c6eSMatthew G. Knepley . time   - The time
8517bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8518d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8519d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8520bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
852120f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8522d29d7c6eSMatthew G. Knepley . U      - The input field vector
8523d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8524d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8525d29d7c6eSMatthew G. Knepley 
8526d29d7c6eSMatthew G. Knepley   Output Parameter:
8527d29d7c6eSMatthew G. Knepley . X - The output vector
8528d29d7c6eSMatthew G. Knepley 
852920f4b53cSBarry Smith   Calling sequence of `funcs`:
8530d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8531d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8532d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8533d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8534d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8535d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
8536d29d7c6eSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8537d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8538d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8539d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8540d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8541d29d7c6eSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8542d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8543d29d7c6eSMatthew G. Knepley . t            - The current time
8544d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8545d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8546d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8547d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8548d29d7c6eSMatthew G. Knepley 
854973ff1848SBarry Smith   Level: intermediate
855073ff1848SBarry Smith 
8551bb7acecfSBarry Smith   Note:
8552bb7acecfSBarry 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.
8553bb7acecfSBarry 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
8554bb7acecfSBarry 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
8555d29d7c6eSMatthew 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.
8556d29d7c6eSMatthew G. Knepley 
8557bb7acecfSBarry Smith   Developer Notes:
8558bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8559bb7acecfSBarry Smith 
8560bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8561bb7acecfSBarry Smith 
85621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8563d29d7c6eSMatthew G. Knepley @*/
8564a4e35b19SJacob 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)
8565d71ae5a4SJacob Faibussowitsch {
8566d29d7c6eSMatthew G. Knepley   DM  dmIn;
8567d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8568d29d7c6eSMatthew G. Knepley 
8569d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8570d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8571d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8572d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8573d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8574f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
857572fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
857672fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8577d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8578d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8579d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8580d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8581d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
85823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8583d29d7c6eSMatthew G. Knepley }
8584d29d7c6eSMatthew G. Knepley 
8585d29d7c6eSMatthew G. Knepley /*@C
8586ece3a9fcSMatthew 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.
8587ece3a9fcSMatthew G. Knepley 
858820f4b53cSBarry Smith   Not Collective
8589ece3a9fcSMatthew G. Knepley 
8590ece3a9fcSMatthew G. Knepley   Input Parameters:
8591bb7acecfSBarry Smith + dm     - The `DM`
8592ece3a9fcSMatthew G. Knepley . time   - The time
8593bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8594ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8595ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8596bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
859720f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8598ece3a9fcSMatthew G. Knepley . localU - The input field vector
8599ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8600ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8601ece3a9fcSMatthew G. Knepley 
8602ece3a9fcSMatthew G. Knepley   Output Parameter:
8603ece3a9fcSMatthew G. Knepley . localX - The output vector
8604ece3a9fcSMatthew G. Knepley 
860520f4b53cSBarry Smith   Calling sequence of `funcs`:
8606ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8607ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8608ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8609ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8610ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8611ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
8612ece3a9fcSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8613ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8614ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8615ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8616ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8617ece3a9fcSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8618ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8619ece3a9fcSMatthew G. Knepley . t            - The current time
8620ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8621ece3a9fcSMatthew G. Knepley . n            - The face normal
8622ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8623ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8624ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8625ece3a9fcSMatthew G. Knepley 
862673ff1848SBarry Smith   Level: intermediate
862773ff1848SBarry Smith 
8628ece3a9fcSMatthew G. Knepley   Note:
8629bb7acecfSBarry 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.
8630bb7acecfSBarry 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
8631bb7acecfSBarry 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
8632ece3a9fcSMatthew 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.
8633ece3a9fcSMatthew G. Knepley 
8634bb7acecfSBarry Smith   Developer Notes:
8635bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8636bb7acecfSBarry Smith 
8637bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8638bb7acecfSBarry Smith 
86391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8640ece3a9fcSMatthew G. Knepley @*/
8641a4e35b19SJacob 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)
8642d71ae5a4SJacob Faibussowitsch {
8643ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8644ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8645064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8646064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8647fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectbdfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
86483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8649ece3a9fcSMatthew G. Knepley }
8650ece3a9fcSMatthew G. Knepley 
8651ece3a9fcSMatthew G. Knepley /*@C
86522716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
86532716604bSToby Isaac 
865420f4b53cSBarry Smith   Collective
8655bb7acecfSBarry Smith 
86562716604bSToby Isaac   Input Parameters:
8657bb7acecfSBarry Smith + dm    - The `DM`
86580709b2feSToby Isaac . time  - The time
86592716604bSToby Isaac . funcs - The functions to evaluate for each field component
86602716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8661574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86622716604bSToby Isaac 
86632716604bSToby Isaac   Output Parameter:
86642716604bSToby Isaac . diff - The diff ||u - u_h||_2
86652716604bSToby Isaac 
86662716604bSToby Isaac   Level: developer
86672716604bSToby Isaac 
8668bb7acecfSBarry Smith   Developer Notes:
8669bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8670bb7acecfSBarry Smith 
8671bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8672bb7acecfSBarry Smith 
86731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
86742716604bSToby Isaac @*/
8675d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8676d71ae5a4SJacob Faibussowitsch {
86772716604bSToby Isaac   PetscFunctionBegin;
86782716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8679b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8680fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2diff, time, funcs, ctxs, X, diff);
86813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86822716604bSToby Isaac }
8683b698f381SToby Isaac 
8684b698f381SToby Isaac /*@C
8685b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8686b698f381SToby Isaac 
868720f4b53cSBarry Smith   Collective
8688d083f849SBarry Smith 
8689b698f381SToby Isaac   Input Parameters:
8690bb7acecfSBarry Smith + dm    - The `DM`
8691a4e35b19SJacob Faibussowitsch . time  - The time
8692b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8693b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8694574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8695b698f381SToby Isaac - n     - The vector to project along
8696b698f381SToby Isaac 
8697b698f381SToby Isaac   Output Parameter:
8698b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8699b698f381SToby Isaac 
8700b698f381SToby Isaac   Level: developer
8701b698f381SToby Isaac 
8702bb7acecfSBarry Smith   Developer Notes:
8703bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8704bb7acecfSBarry Smith 
8705bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8706bb7acecfSBarry Smith 
87071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8708b698f381SToby Isaac @*/
8709d71ae5a4SJacob 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)
8710d71ae5a4SJacob Faibussowitsch {
8711b698f381SToby Isaac   PetscFunctionBegin;
8712b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8713b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8714fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2gradientdiff, time, funcs, ctxs, X, n, diff);
87153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8716b698f381SToby Isaac }
8717b698f381SToby Isaac 
87182a16baeaSToby Isaac /*@C
87192a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
87202a16baeaSToby Isaac 
872120f4b53cSBarry Smith   Collective
8722d083f849SBarry Smith 
87232a16baeaSToby Isaac   Input Parameters:
8724bb7acecfSBarry Smith + dm    - The `DM`
87252a16baeaSToby Isaac . time  - The time
87262a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
87272a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8728574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
87292a16baeaSToby Isaac 
87302a16baeaSToby Isaac   Output Parameter:
87312a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
87322a16baeaSToby Isaac 
87332a16baeaSToby Isaac   Level: developer
87342a16baeaSToby Isaac 
8735bb7acecfSBarry Smith   Developer Notes:
8736bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8737bb7acecfSBarry Smith 
8738bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8739bb7acecfSBarry Smith 
874042747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
87412a16baeaSToby Isaac @*/
8742d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8743d71ae5a4SJacob Faibussowitsch {
87442a16baeaSToby Isaac   PetscFunctionBegin;
87452a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87462a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8747fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2fielddiff, time, funcs, ctxs, X, diff);
87483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
87492a16baeaSToby Isaac }
87502a16baeaSToby Isaac 
8751df0b854cSToby Isaac /*@C
8752bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8753502a2867SDave May 
8754502a2867SDave May   Not Collective
8755502a2867SDave May 
8756502a2867SDave May   Input Parameter:
8757bb7acecfSBarry Smith . dm - The `DM`
8758502a2867SDave May 
87590a19bb7dSprj-   Output Parameters:
87600a19bb7dSprj- + nranks - the number of neighbours
87610a19bb7dSprj- - ranks  - the neighbors ranks
8762502a2867SDave May 
87639bdbcad8SBarry Smith   Level: beginner
87649bdbcad8SBarry Smith 
8765bb7acecfSBarry Smith   Note:
8766bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8767502a2867SDave May 
87681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8769502a2867SDave May @*/
8770d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8771d71ae5a4SJacob Faibussowitsch {
8772502a2867SDave May   PetscFunctionBegin;
8773502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8774fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, getneighbors, nranks, ranks);
87753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8776502a2867SDave May }
8777502a2867SDave May 
8778531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8779531c7667SBarry Smith 
8780531c7667SBarry Smith /*
8781531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
87822b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8783531c7667SBarry Smith */
878466976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8785d71ae5a4SJacob Faibussowitsch {
8786531c7667SBarry Smith   PetscFunctionBegin;
8787531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8788531c7667SBarry Smith     Vec x1local;
8789531c7667SBarry Smith     DM  dm;
87909566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
87917a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
87929566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
87939566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
87949566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8795531c7667SBarry Smith     x1 = x1local;
8796531c7667SBarry Smith   }
87979566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8798531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8799531c7667SBarry Smith     DM dm;
88009566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
88019566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8802531c7667SBarry Smith   }
88033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8804531c7667SBarry Smith }
8805531c7667SBarry Smith 
8806531c7667SBarry Smith /*@
8807bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8808531c7667SBarry Smith 
8809a4e35b19SJacob Faibussowitsch   Input Parameters:
8810a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8811a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8812531c7667SBarry Smith 
88139bdbcad8SBarry Smith   Level: advanced
88149bdbcad8SBarry Smith 
881573ff1848SBarry Smith   Developer Note:
881673ff1848SBarry Smith   This routine exists because the PETSc `Mat` library does not know about the `DM` objects
8817531c7667SBarry Smith 
88181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8819531c7667SBarry Smith @*/
8820d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8821d71ae5a4SJacob Faibussowitsch {
8822531c7667SBarry Smith   PetscFunctionBegin;
8823531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
88243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8825531c7667SBarry Smith }
88268320bc6fSPatrick Sanan 
88278320bc6fSPatrick Sanan /*@
8828bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
88298320bc6fSPatrick Sanan 
88308320bc6fSPatrick Sanan   Collective
88318320bc6fSPatrick Sanan 
88328320bc6fSPatrick Sanan   Input Parameters:
8833bb7acecfSBarry Smith + dm1 - the first `DM`
8834bb7acecfSBarry Smith - dm2 - the second `DM`
88358320bc6fSPatrick Sanan 
88368320bc6fSPatrick Sanan   Output Parameters:
8837bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8838bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
88398320bc6fSPatrick Sanan 
884020f4b53cSBarry Smith   Level: advanced
884120f4b53cSBarry Smith 
88428320bc6fSPatrick Sanan   Notes:
8843bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
88443d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
88458320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8846bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
88473d862458SPatrick Sanan   decomposition, but hold different data.
88488320bc6fSPatrick Sanan 
88498320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8850bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
88518320bc6fSPatrick Sanan 
8852bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
88538320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
88548320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8855bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
88568320bc6fSPatrick Sanan 
8857bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8858bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
88598320bc6fSPatrick Sanan .vb
88608320bc6fSPatrick Sanan   ...
88619566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
88628320bc6fSPatrick Sanan   if (set && compatible)  {
88639566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
88649566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
88659566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
88668320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
88678320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
88688320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
88698320bc6fSPatrick Sanan       }
88708320bc6fSPatrick Sanan     }
88719566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
88729566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
88738320bc6fSPatrick Sanan   } else {
88748320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
88758320bc6fSPatrick Sanan   }
88768320bc6fSPatrick Sanan   ...
88778320bc6fSPatrick Sanan .ve
88788320bc6fSPatrick Sanan 
8879bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
88808320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
88818320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
88828320bc6fSPatrick Sanan   always check the "set" output parameter.
88838320bc6fSPatrick Sanan 
8884bb7acecfSBarry Smith   A `DM` is always compatible with itself.
88858320bc6fSPatrick Sanan 
8886bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
88878320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
88888320bc6fSPatrick Sanan   incompatible.
88898320bc6fSPatrick Sanan 
88908320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8891bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
88928320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
88938320bc6fSPatrick Sanan 
889473ff1848SBarry Smith   Developer Note:
8895bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
88963d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8897a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8898bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
88998320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
89003d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8901bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
89028320bc6fSPatrick Sanan 
89031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
89048320bc6fSPatrick Sanan @*/
8905d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8906d71ae5a4SJacob Faibussowitsch {
89078320bc6fSPatrick Sanan   PetscMPIInt compareResult;
89088320bc6fSPatrick Sanan   DMType      type, type2;
89098320bc6fSPatrick Sanan   PetscBool   sameType;
89108320bc6fSPatrick Sanan 
89118320bc6fSPatrick Sanan   PetscFunctionBegin;
8912a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
89138320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
89148320bc6fSPatrick Sanan 
89158320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8916a5bc1bf3SBarry Smith   if (dm1 == dm2) {
89178320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
89188320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
89193ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
89208320bc6fSPatrick Sanan   }
89218320bc6fSPatrick Sanan 
89228320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
89238320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
89248320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
89258320bc6fSPatrick Sanan      determined by the implementation-specific logic */
89269566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
89278320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
89288320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
89298320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
89303ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
89318320bc6fSPatrick Sanan   }
89328320bc6fSPatrick Sanan 
89338320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8934a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8935dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
89363ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
89378320bc6fSPatrick Sanan   }
89388320bc6fSPatrick Sanan 
8939a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
89408320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
89419566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
89429566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
89439566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
89448320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8945dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
89468320bc6fSPatrick Sanan   } else {
89478320bc6fSPatrick Sanan     *set = PETSC_FALSE;
89488320bc6fSPatrick Sanan   }
89493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89508320bc6fSPatrick Sanan }
8951c0f0dcc3SMatthew G. Knepley 
8952c0f0dcc3SMatthew G. Knepley /*@C
8953bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8954c0f0dcc3SMatthew G. Knepley 
895520f4b53cSBarry Smith   Logically Collective
8956c0f0dcc3SMatthew G. Knepley 
8957c0f0dcc3SMatthew G. Knepley   Input Parameters:
895860225df5SJacob Faibussowitsch + dm             - the `DM`
8959c0f0dcc3SMatthew G. Knepley . f              - the monitor function
896020f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
896149abdd8aSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`), see `PetscCtxDestroyFn` for the calling sequence
8962c0f0dcc3SMatthew G. Knepley 
896320f4b53cSBarry Smith   Options Database Key:
896460225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8965c0f0dcc3SMatthew G. Knepley                        does not cancel those set via the options database.
8966c0f0dcc3SMatthew G. Knepley 
89679bdbcad8SBarry Smith   Level: intermediate
89689bdbcad8SBarry Smith 
8969bb7acecfSBarry Smith   Note:
8970c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8971bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8972c0f0dcc3SMatthew G. Knepley   order in which they were set.
8973c0f0dcc3SMatthew G. Knepley 
897473ff1848SBarry Smith   Fortran Note:
8975bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8976bb7acecfSBarry Smith 
897773ff1848SBarry Smith   Developer Note:
8978bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8979c0f0dcc3SMatthew G. Knepley 
898049abdd8aSBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`, `PetscCtxDestroyFn`
8981c0f0dcc3SMatthew G. Knepley @*/
898249abdd8aSBarry Smith PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscCtxDestroyFn *monitordestroy)
8983d71ae5a4SJacob Faibussowitsch {
8984c0f0dcc3SMatthew G. Knepley   PetscInt m;
8985c0f0dcc3SMatthew G. Knepley 
8986c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8987c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8988c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8989c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8990c0f0dcc3SMatthew G. Knepley 
89919566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))f, mctx, monitordestroy, (PetscErrorCode (*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
89923ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8993c0f0dcc3SMatthew G. Knepley   }
89947a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8995c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8996c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8997835f2295SStefano Zampini   dm->monitorcontext[dm->numbermonitors++] = mctx;
89983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8999c0f0dcc3SMatthew G. Knepley }
9000c0f0dcc3SMatthew G. Knepley 
9001c0f0dcc3SMatthew G. Knepley /*@
9002bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
9003c0f0dcc3SMatthew G. Knepley 
900420f4b53cSBarry Smith   Logically Collective
9005c0f0dcc3SMatthew G. Knepley 
9006c0f0dcc3SMatthew G. Knepley   Input Parameter:
9007c0f0dcc3SMatthew G. Knepley . dm - the DM
9008c0f0dcc3SMatthew G. Knepley 
9009c0f0dcc3SMatthew G. Knepley   Options Database Key:
9010c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9011bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
9012c0f0dcc3SMatthew G. Knepley   set via the options database
9013c0f0dcc3SMatthew G. Knepley 
90149bdbcad8SBarry Smith   Level: intermediate
90159bdbcad8SBarry Smith 
9016bb7acecfSBarry Smith   Note:
9017bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
9018c0f0dcc3SMatthew G. Knepley 
90191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
9020c0f0dcc3SMatthew G. Knepley @*/
9021d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
9022d71ae5a4SJacob Faibussowitsch {
9023c0f0dcc3SMatthew G. Knepley   PetscInt m;
9024c0f0dcc3SMatthew G. Knepley 
9025c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9026c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9027c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
90289566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
9029c0f0dcc3SMatthew G. Knepley   }
9030c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
90313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9032c0f0dcc3SMatthew G. Knepley }
9033c0f0dcc3SMatthew G. Knepley 
9034c0f0dcc3SMatthew G. Knepley /*@C
9035c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9036c0f0dcc3SMatthew G. Knepley 
903720f4b53cSBarry Smith   Collective
9038c0f0dcc3SMatthew G. Knepley 
9039c0f0dcc3SMatthew G. Knepley   Input Parameters:
9040bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
9041c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
9042c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
9043c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
904449abdd8aSBarry Smith . monitor      - the monitor function, this must use a `PetscViewerFormat` as its context
9045bb7acecfSBarry 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
9046c0f0dcc3SMatthew G. Knepley 
9047c0f0dcc3SMatthew G. Knepley   Output Parameter:
9048c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9049c0f0dcc3SMatthew G. Knepley 
9050c0f0dcc3SMatthew G. Knepley   Level: developer
9051c0f0dcc3SMatthew G. Knepley 
9052648c30bcSBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsCreateViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
9053db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
905460225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
9055db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
9056c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
9057db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
9058bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
9059c0f0dcc3SMatthew G. Knepley @*/
9060d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
9061d71ae5a4SJacob Faibussowitsch {
9062c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9063c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9064c0f0dcc3SMatthew G. Knepley 
9065c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9066c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9067648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
9068c0f0dcc3SMatthew G. Knepley   if (*flg) {
9069c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9070c0f0dcc3SMatthew G. Knepley 
90719566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
9072648c30bcSBarry Smith     PetscCall(PetscViewerDestroy(&viewer));
90739566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
9074835f2295SStefano Zampini     PetscCall(DMMonitorSet(dm, monitor, vf, (PetscCtxDestroyFn *)PetscViewerAndFormatDestroy));
9075c0f0dcc3SMatthew G. Knepley   }
90763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9077c0f0dcc3SMatthew G. Knepley }
9078c0f0dcc3SMatthew G. Knepley 
9079c0f0dcc3SMatthew G. Knepley /*@
9080c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
9081c0f0dcc3SMatthew G. Knepley 
908220f4b53cSBarry Smith   Collective
9083c0f0dcc3SMatthew G. Knepley 
90842fe279fdSBarry Smith   Input Parameter:
9085bb7acecfSBarry Smith . dm - The `DM`
9086c0f0dcc3SMatthew G. Knepley 
9087c0f0dcc3SMatthew G. Knepley   Level: developer
9088c0f0dcc3SMatthew G. Knepley 
908973ff1848SBarry Smith   Developer Note:
9090a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
9091a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
9092a4e35b19SJacob Faibussowitsch   concept of discretization.
9093bb7acecfSBarry Smith 
90941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
9095c0f0dcc3SMatthew G. Knepley @*/
9096d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
9097d71ae5a4SJacob Faibussowitsch {
9098c0f0dcc3SMatthew G. Knepley   PetscInt m;
9099c0f0dcc3SMatthew G. Knepley 
9100c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
91013ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
9102c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
910348a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
91043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9105c0f0dcc3SMatthew G. Knepley }
91062e4af2aeSMatthew G. Knepley 
91072e4af2aeSMatthew G. Knepley /*@
9108bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
91092e4af2aeSMatthew G. Knepley 
911020f4b53cSBarry Smith   Collective
91112e4af2aeSMatthew G. Knepley 
91122e4af2aeSMatthew G. Knepley   Input Parameters:
9113bb7acecfSBarry Smith + dm  - The `DM`
91146b867d5aSJose E. Roman - sol - The solution vector
91152e4af2aeSMatthew G. Knepley 
91166b867d5aSJose E. Roman   Input/Output Parameter:
911720f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
91186b867d5aSJose E. Roman            contains the error in each field
91196b867d5aSJose E. Roman 
91206b867d5aSJose E. Roman   Output Parameter:
912120f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
912220f4b53cSBarry Smith 
912320f4b53cSBarry Smith   Level: developer
91242e4af2aeSMatthew G. Knepley 
9125bb7acecfSBarry Smith   Note:
9126bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
91272e4af2aeSMatthew G. Knepley 
91281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
91292e4af2aeSMatthew G. Knepley @*/
9130d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
9131d71ae5a4SJacob Faibussowitsch {
91322e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
91332e4af2aeSMatthew G. Knepley   void    **ctxs;
91342e4af2aeSMatthew G. Knepley   PetscReal time;
91352e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
91362e4af2aeSMatthew G. Knepley 
91372e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
91389566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
91399566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
91409566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
91412e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
91422e4af2aeSMatthew G. Knepley     PetscDS         ds;
91432e4af2aeSMatthew G. Knepley     DMLabel         label;
91442e4af2aeSMatthew G. Knepley     IS              fieldIS;
91452e4af2aeSMatthew G. Knepley     const PetscInt *fields;
91462e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
91472e4af2aeSMatthew G. Knepley 
914807218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
91499566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
91509566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
91512e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
91522e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
91539566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
91542e4af2aeSMatthew G. Knepley     }
91559566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
91562e4af2aeSMatthew G. Knepley   }
9157ad540459SPierre 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);
91589566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
91599566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
91602e4af2aeSMatthew G. Knepley   if (errorVec) {
91612e4af2aeSMatthew G. Knepley     DM             edm;
91622e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
91632e4af2aeSMatthew G. Knepley     PetscBool      simplex;
91642e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
91652e4af2aeSMatthew G. Knepley 
91669566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
91679566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
91689566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
91699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
91702e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
91719566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
91722e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
91732e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
91742e4af2aeSMatthew G. Knepley       PetscQuadrature q;
91752e4af2aeSMatthew G. Knepley       const char     *name;
91762e4af2aeSMatthew G. Knepley 
91779566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
91789566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
91799566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
91809566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
91819566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
91829566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
91839566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
91849566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
91852e4af2aeSMatthew G. Knepley     }
91869566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
91872e4af2aeSMatthew G. Knepley 
91889566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
91899566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
91909566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
91919566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
91922e4af2aeSMatthew G. Knepley   }
91939566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
91943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91952e4af2aeSMatthew G. Knepley }
91969a2a23afSMatthew G. Knepley 
91979a2a23afSMatthew G. Knepley /*@
9198bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
91999a2a23afSMatthew G. Knepley 
920020f4b53cSBarry Smith   Not Collective
92019a2a23afSMatthew G. Knepley 
92029a2a23afSMatthew G. Knepley   Input Parameter:
9203bb7acecfSBarry Smith . dm - The `DM`
92049a2a23afSMatthew G. Knepley 
92059a2a23afSMatthew G. Knepley   Output Parameter:
9206a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
92079a2a23afSMatthew G. Knepley 
92089a2a23afSMatthew G. Knepley   Level: advanced
92099a2a23afSMatthew G. Knepley 
9210e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
92119a2a23afSMatthew G. Knepley @*/
9212d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
9213d71ae5a4SJacob Faibussowitsch {
92149a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92159a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92169566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
92173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92189a2a23afSMatthew G. Knepley }
92199a2a23afSMatthew G. Knepley 
92209a2a23afSMatthew G. Knepley /*@
9221ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
92229a2a23afSMatthew G. Knepley 
922320f4b53cSBarry Smith   Not Collective
92249a2a23afSMatthew G. Knepley 
92259a2a23afSMatthew G. Knepley   Input Parameters:
9226bb7acecfSBarry Smith + dm    - The `DM`
9227bb7acecfSBarry Smith . label - The `DMLabel`
9228ac17215fSMatthew G. Knepley . value - The label value indicating the region
9229ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
92309a2a23afSMatthew G. Knepley 
92319a2a23afSMatthew G. Knepley   Output Parameter:
9232bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
92339a2a23afSMatthew G. Knepley 
92349bdbcad8SBarry Smith   Level: advanced
92359bdbcad8SBarry Smith 
9236bb7acecfSBarry Smith   Note:
9237bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
923804c51a94SMatthew G. Knepley 
9239e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
92409a2a23afSMatthew G. Knepley @*/
9241d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
9242d71ae5a4SJacob Faibussowitsch {
9243ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
924404c51a94SMatthew G. Knepley   PetscBool       has;
92459a2a23afSMatthew G. Knepley 
92469a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92479a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92489a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
92499a2a23afSMatthew G. Knepley   key.label = label;
92509a2a23afSMatthew G. Knepley   key.value = value;
9251ac17215fSMatthew G. Knepley   key.part  = part;
92529566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
92539566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
92549566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
92553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92569a2a23afSMatthew G. Knepley }
92579a2a23afSMatthew G. Knepley 
92589a2a23afSMatthew G. Knepley /*@
9259bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
92609a2a23afSMatthew G. Knepley 
926120f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
92629a2a23afSMatthew G. Knepley 
92639a2a23afSMatthew G. Knepley   Input Parameters:
9264bb7acecfSBarry Smith + dm    - The `DM`
9265bb7acecfSBarry Smith . label - The `DMLabel`
92669a2a23afSMatthew G. Knepley . value - The label value indicating the region
9267ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
9268bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
92699a2a23afSMatthew G. Knepley 
92709a2a23afSMatthew G. Knepley   Level: advanced
92719a2a23afSMatthew G. Knepley 
9272e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
92739a2a23afSMatthew G. Knepley @*/
9274d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9275d71ae5a4SJacob Faibussowitsch {
92769a2a23afSMatthew G. Knepley   Vec             old;
92779a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
92789a2a23afSMatthew G. Knepley 
92799a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92809a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92819a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
92829a2a23afSMatthew G. Knepley   key.label = label;
92839a2a23afSMatthew G. Knepley   key.value = value;
9284ac17215fSMatthew G. Knepley   key.part  = part;
92859566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
92869566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
92879566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
92889566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
9289d705d0eaSStefano Zampini   PetscCall(VecDestroy(&old));
92903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92919a2a23afSMatthew G. Knepley }
92929a2a23afSMatthew G. Knepley 
9293cc4c1da9SBarry Smith /*@
9294bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
92959a2a23afSMatthew G. Knepley 
929620f4b53cSBarry Smith   Not Collective
92979a2a23afSMatthew G. Knepley 
92989a2a23afSMatthew G. Knepley   Input Parameter:
9299bb7acecfSBarry Smith . dm - The `DM`
93009a2a23afSMatthew G. Knepley 
93019a2a23afSMatthew G. Knepley   Output Parameters:
9302bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9303bb7acecfSBarry Smith . values - The label values for each `Vec`
9304bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
93059a2a23afSMatthew G. Knepley 
93069bdbcad8SBarry Smith   Level: advanced
93079bdbcad8SBarry Smith 
9308bb7acecfSBarry Smith   Note:
9309bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
93109a2a23afSMatthew G. Knepley 
9311e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
93129a2a23afSMatthew G. Knepley @*/
9313d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9314d71ae5a4SJacob Faibussowitsch {
93159a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
93169a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
93179a2a23afSMatthew G. Knepley 
93189a2a23afSMatthew G. Knepley   PetscFunctionBegin;
93199a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
93204f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
93214f572ea9SToby Isaac   PetscAssertPointer(values, 3);
93224f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
93239566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
93249566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
93259566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
93269371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
93279371c9d4SSatish Balay     labels[i] = keys[i].label;
93289371c9d4SSatish Balay     values[i] = keys[i].value;
93299371c9d4SSatish Balay     parts[i]  = keys[i].part;
93309371c9d4SSatish Balay   }
93319566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
93323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
93339a2a23afSMatthew G. Knepley }
93349a2a23afSMatthew G. Knepley 
93359a2a23afSMatthew G. Knepley /*@
9336bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
93379a2a23afSMatthew G. Knepley 
933820f4b53cSBarry Smith   Not Collective
93399a2a23afSMatthew G. Knepley 
93409a2a23afSMatthew G. Knepley   Input Parameter:
9341bb7acecfSBarry Smith . dm - The `DM`
93429a2a23afSMatthew G. Knepley 
93439a2a23afSMatthew G. Knepley   Output Parameter:
9344bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
93459a2a23afSMatthew G. Knepley 
93469a2a23afSMatthew G. Knepley   Level: advanced
93479a2a23afSMatthew G. Knepley 
9348bb7acecfSBarry Smith   Note:
9349bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9350bb7acecfSBarry Smith 
9351e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
93529a2a23afSMatthew G. Knepley @*/
9353d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9354d71ae5a4SJacob Faibussowitsch {
93559a2a23afSMatthew G. Knepley   PetscFunctionBegin;
93569a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9357d705d0eaSStefano Zampini   PetscValidHeaderSpecific(dmNew, DM_CLASSID, 2);
9358d705d0eaSStefano Zampini   if (dm == dmNew) PetscFunctionReturn(PETSC_SUCCESS);
9359e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(dmNew));
9360e4d5475eSStefano Zampini 
9361e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
93629566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
9363d705d0eaSStefano Zampini   {
9364d705d0eaSStefano Zampini     Vec     *auxData;
9365d705d0eaSStefano Zampini     PetscInt n, i, off = 0;
9366d705d0eaSStefano Zampini 
9367d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetSize(dmNew->auxData, &n));
9368d705d0eaSStefano Zampini     PetscCall(PetscMalloc1(n, &auxData));
9369d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetVals(dmNew->auxData, &off, auxData));
9370d705d0eaSStefano Zampini     for (i = 0; i < n; ++i) PetscCall(PetscObjectReference((PetscObject)auxData[i]));
9371d705d0eaSStefano Zampini     PetscCall(PetscFree(auxData));
9372e4d5475eSStefano Zampini   }
9373e4d5475eSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
9374e4d5475eSStefano Zampini }
9375e4d5475eSStefano Zampini 
9376e4d5475eSStefano Zampini /*@
9377e4d5475eSStefano Zampini   DMClearAuxiliaryVec - Destroys the auxiliary vector information and creates a new empty one
9378e4d5475eSStefano Zampini 
9379e4d5475eSStefano Zampini   Not Collective
9380e4d5475eSStefano Zampini 
9381e4d5475eSStefano Zampini   Input Parameter:
9382e4d5475eSStefano Zampini . dm - The `DM`
9383e4d5475eSStefano Zampini 
9384e4d5475eSStefano Zampini   Level: advanced
9385e4d5475eSStefano Zampini 
9386e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMCopyAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
9387e4d5475eSStefano Zampini @*/
9388e4d5475eSStefano Zampini PetscErrorCode DMClearAuxiliaryVec(DM dm)
9389e4d5475eSStefano Zampini {
9390e4d5475eSStefano Zampini   Vec     *auxData;
9391e4d5475eSStefano Zampini   PetscInt n, i, off = 0;
9392e4d5475eSStefano Zampini 
9393e4d5475eSStefano Zampini   PetscFunctionBegin;
9394e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetSize(dm->auxData, &n));
9395d705d0eaSStefano Zampini   PetscCall(PetscMalloc1(n, &auxData));
9396e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetVals(dm->auxData, &off, auxData));
9397d705d0eaSStefano Zampini   for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
9398d705d0eaSStefano Zampini   PetscCall(PetscFree(auxData));
9399e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dm->auxData));
9400e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxCreate(&dm->auxData));
94013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
94029a2a23afSMatthew G. Knepley }
9403b5a892a1SMatthew G. Knepley 
9404cc4c1da9SBarry Smith /*@
9405bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9406b5a892a1SMatthew G. Knepley 
940720f4b53cSBarry Smith   Not Collective
9408b5a892a1SMatthew G. Knepley 
9409b5a892a1SMatthew G. Knepley   Input Parameters:
9410bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9411b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9412b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9413b5a892a1SMatthew G. Knepley 
9414b5a892a1SMatthew G. Knepley   Output Parameters:
9415bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9416b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9417b5a892a1SMatthew G. Knepley 
9418b5a892a1SMatthew G. Knepley   Level: advanced
9419b5a892a1SMatthew G. Knepley 
9420bb7acecfSBarry Smith   Note:
9421bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9422bb7acecfSBarry Smith 
942385036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9424bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9425bb7acecfSBarry Smith 
9426bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9427bb7acecfSBarry Smith 
94281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9429b5a892a1SMatthew G. Knepley @*/
9430d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9431d71ae5a4SJacob Faibussowitsch {
9432b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
943385036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9434b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9435b5a892a1SMatthew G. Knepley 
9436b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94379371c9d4SSatish Balay   if (!nO) {
94389371c9d4SSatish Balay     *ornt  = 0;
94399371c9d4SSatish Balay     *found = PETSC_TRUE;
94403ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
94419371c9d4SSatish Balay   }
9442b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
944385036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
9444b5a892a1SMatthew G. Knepley 
94459371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
94469371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
94479371c9d4SSatish Balay     if (c == cS) {
94489371c9d4SSatish Balay       *ornt = o;
94499371c9d4SSatish Balay       break;
94509371c9d4SSatish Balay     }
9451b5a892a1SMatthew G. Knepley   }
9452b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
94533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9454b5a892a1SMatthew G. Knepley }
9455b5a892a1SMatthew G. Knepley 
9456cc4c1da9SBarry Smith /*@
9457bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9458b5a892a1SMatthew G. Knepley 
945920f4b53cSBarry Smith   Not Collective
9460b5a892a1SMatthew G. Knepley 
9461b5a892a1SMatthew G. Knepley   Input Parameters:
9462bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9463b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9464b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9465b5a892a1SMatthew G. Knepley 
94662fe279fdSBarry Smith   Output Parameter:
9467bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9468b5a892a1SMatthew G. Knepley 
9469b5a892a1SMatthew G. Knepley   Level: advanced
9470b5a892a1SMatthew G. Knepley 
9471bb7acecfSBarry Smith   Note:
9472bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9473bb7acecfSBarry Smith 
947473ff1848SBarry Smith   Developer Note:
9475bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9476bb7acecfSBarry Smith 
94771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9478b5a892a1SMatthew G. Knepley @*/
9479d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9480d71ae5a4SJacob Faibussowitsch {
9481b5a892a1SMatthew G. Knepley   PetscBool found;
9482b5a892a1SMatthew G. Knepley 
9483b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94849566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
94857a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
94863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9487b5a892a1SMatthew G. Knepley }
9488b5a892a1SMatthew G. Knepley 
9489cc4c1da9SBarry Smith /*@
9490bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9491b5a892a1SMatthew G. Knepley 
949220f4b53cSBarry Smith   Not Collective
9493b5a892a1SMatthew G. Knepley 
9494b5a892a1SMatthew G. Knepley   Input Parameters:
9495bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9496b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9497b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9498b5a892a1SMatthew G. Knepley 
9499b5a892a1SMatthew G. Knepley   Output Parameters:
9500bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9501b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9502b5a892a1SMatthew G. Knepley 
9503b5a892a1SMatthew G. Knepley   Level: advanced
9504b5a892a1SMatthew G. Knepley 
950573ff1848SBarry Smith   Notes:
9506bb7acecfSBarry Smith   An arrangement is a vertex order
9507bb7acecfSBarry Smith 
950885036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9509bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9510bb7acecfSBarry Smith 
9511bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9512bb7acecfSBarry Smith 
951385036b15SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangement()`
9514b5a892a1SMatthew G. Knepley @*/
9515d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9516d71ae5a4SJacob Faibussowitsch {
9517b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
951885036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9519b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9520b5a892a1SMatthew G. Knepley 
9521b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
95229371c9d4SSatish Balay   if (!nO) {
95239371c9d4SSatish Balay     *ornt  = 0;
95249371c9d4SSatish Balay     *found = PETSC_TRUE;
95253ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
95269371c9d4SSatish Balay   }
9527b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
952885036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangement(ct, o);
9529b5a892a1SMatthew G. Knepley 
95309371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
95319371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
95329371c9d4SSatish Balay     if (c == cS) {
95339371c9d4SSatish Balay       *ornt = o;
95349371c9d4SSatish Balay       break;
95359371c9d4SSatish Balay     }
9536b5a892a1SMatthew G. Knepley   }
9537b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
95383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9539b5a892a1SMatthew G. Knepley }
9540b5a892a1SMatthew G. Knepley 
9541cc4c1da9SBarry Smith /*@
9542bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9543b5a892a1SMatthew G. Knepley 
954420f4b53cSBarry Smith   Not Collective
9545b5a892a1SMatthew G. Knepley 
9546b5a892a1SMatthew G. Knepley   Input Parameters:
9547bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9548b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9549b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9550b5a892a1SMatthew G. Knepley 
95512fe279fdSBarry Smith   Output Parameter:
9552bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9553b5a892a1SMatthew G. Knepley 
9554b5a892a1SMatthew G. Knepley   Level: advanced
9555b5a892a1SMatthew G. Knepley 
9556bb7acecfSBarry Smith   Note:
9557bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9558bb7acecfSBarry Smith 
955973ff1848SBarry Smith   Developer Note:
9560bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9561bb7acecfSBarry Smith 
95621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9563b5a892a1SMatthew G. Knepley @*/
9564d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9565d71ae5a4SJacob Faibussowitsch {
9566b5a892a1SMatthew G. Knepley   PetscBool found;
9567b5a892a1SMatthew G. Knepley 
9568b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
95699566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
95707a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
95713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9572b5a892a1SMatthew G. Knepley }
9573012bc364SMatthew G. Knepley 
9574cc4c1da9SBarry Smith /*@
9575012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9576012bc364SMatthew G. Knepley 
957720f4b53cSBarry Smith   Not Collective
9578012bc364SMatthew G. Knepley 
9579012bc364SMatthew G. Knepley   Input Parameters:
9580bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9581012bc364SMatthew G. Knepley - point - Coordinates of the point
9582012bc364SMatthew G. Knepley 
95832fe279fdSBarry Smith   Output Parameter:
9584012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9585012bc364SMatthew G. Knepley 
9586012bc364SMatthew G. Knepley   Level: advanced
9587012bc364SMatthew G. Knepley 
95881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9589012bc364SMatthew G. Knepley @*/
9590d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9591d71ae5a4SJacob Faibussowitsch {
9592012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9593012bc364SMatthew G. Knepley   PetscInt  d;
9594012bc364SMatthew G. Knepley 
9595012bc364SMatthew G. Knepley   PetscFunctionBegin;
9596012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9597012bc364SMatthew G. Knepley   switch (ct) {
9598012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9599012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9600012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
96019371c9d4SSatish Balay       if (point[d] < -1.0) {
96029371c9d4SSatish Balay         *inside = PETSC_FALSE;
96039371c9d4SSatish Balay         break;
96049371c9d4SSatish Balay       }
9605012bc364SMatthew G. Knepley       sum += point[d];
9606012bc364SMatthew G. Knepley     }
96079371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
96089371c9d4SSatish Balay       *inside = PETSC_FALSE;
96099371c9d4SSatish Balay       break;
96109371c9d4SSatish Balay     }
9611012bc364SMatthew G. Knepley     break;
9612012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9613012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9614012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
96159371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
96169371c9d4SSatish Balay         *inside = PETSC_FALSE;
9617012bc364SMatthew G. Knepley         break;
96189371c9d4SSatish Balay       }
96199371c9d4SSatish Balay     break;
9620d71ae5a4SJacob Faibussowitsch   default:
9621d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9622012bc364SMatthew G. Knepley   }
96233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9624012bc364SMatthew G. Knepley }
9625adc21957SMatthew G. Knepley 
9626adc21957SMatthew G. Knepley /*@
9627adc21957SMatthew G. Knepley   DMReorderSectionSetDefault - Set flag indicating whether the local section should be reordered by default
9628adc21957SMatthew G. Knepley 
9629adc21957SMatthew G. Knepley   Logically collective
9630adc21957SMatthew G. Knepley 
9631adc21957SMatthew G. Knepley   Input Parameters:
9632adc21957SMatthew G. Knepley + dm      - The DM
9633adc21957SMatthew G. Knepley - reorder - Flag for reordering
9634adc21957SMatthew G. Knepley 
9635adc21957SMatthew G. Knepley   Level: intermediate
9636adc21957SMatthew G. Knepley 
9637adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetDefault()`
9638adc21957SMatthew G. Knepley @*/
9639adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetDefault(DM dm, DMReorderDefaultFlag reorder)
9640adc21957SMatthew G. Knepley {
9641adc21957SMatthew G. Knepley   PetscFunctionBegin;
9642adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9643adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetDefault_C", (DM, DMReorderDefaultFlag), (dm, reorder));
9644adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9645adc21957SMatthew G. Knepley }
9646adc21957SMatthew G. Knepley 
9647adc21957SMatthew G. Knepley /*@
9648adc21957SMatthew G. Knepley   DMReorderSectionGetDefault - Get flag indicating whether the local section should be reordered by default
9649adc21957SMatthew G. Knepley 
9650adc21957SMatthew G. Knepley   Not collective
9651adc21957SMatthew G. Knepley 
9652adc21957SMatthew G. Knepley   Input Parameter:
9653adc21957SMatthew G. Knepley . dm - The DM
9654adc21957SMatthew G. Knepley 
9655adc21957SMatthew G. Knepley   Output Parameter:
9656adc21957SMatthew G. Knepley . reorder - Flag for reordering
9657adc21957SMatthew G. Knepley 
9658adc21957SMatthew G. Knepley   Level: intermediate
9659adc21957SMatthew G. Knepley 
9660adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`
9661adc21957SMatthew G. Knepley @*/
9662adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetDefault(DM dm, DMReorderDefaultFlag *reorder)
9663adc21957SMatthew G. Knepley {
9664adc21957SMatthew G. Knepley   PetscFunctionBegin;
9665adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9666adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9667adc21957SMatthew G. Knepley   *reorder = DM_REORDER_DEFAULT_NOTSET;
9668adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetDefault_C", (DM, DMReorderDefaultFlag *), (dm, reorder));
9669adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9670adc21957SMatthew G. Knepley }
9671adc21957SMatthew G. Knepley 
9672cc4c1da9SBarry Smith /*@
9673adc21957SMatthew G. Knepley   DMReorderSectionSetType - Set the type of local section reordering
9674adc21957SMatthew G. Knepley 
9675adc21957SMatthew G. Knepley   Logically collective
9676adc21957SMatthew G. Knepley 
9677adc21957SMatthew G. Knepley   Input Parameters:
9678adc21957SMatthew G. Knepley + dm      - The DM
9679adc21957SMatthew G. Knepley - reorder - The reordering method
9680adc21957SMatthew G. Knepley 
9681adc21957SMatthew G. Knepley   Level: intermediate
9682adc21957SMatthew G. Knepley 
9683adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetType()`, `DMReorderSectionSetDefault()`
9684adc21957SMatthew G. Knepley @*/
9685adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetType(DM dm, MatOrderingType reorder)
9686adc21957SMatthew G. Knepley {
9687adc21957SMatthew G. Knepley   PetscFunctionBegin;
9688adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9689adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetType_C", (DM, MatOrderingType), (dm, reorder));
9690adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9691adc21957SMatthew G. Knepley }
9692adc21957SMatthew G. Knepley 
9693cc4c1da9SBarry Smith /*@
9694adc21957SMatthew G. Knepley   DMReorderSectionGetType - Get the reordering type for the local section
9695adc21957SMatthew G. Knepley 
9696adc21957SMatthew G. Knepley   Not collective
9697adc21957SMatthew G. Knepley 
9698adc21957SMatthew G. Knepley   Input Parameter:
9699adc21957SMatthew G. Knepley . dm - The DM
9700adc21957SMatthew G. Knepley 
9701adc21957SMatthew G. Knepley   Output Parameter:
9702adc21957SMatthew G. Knepley . reorder - The reordering method
9703adc21957SMatthew G. Knepley 
9704adc21957SMatthew G. Knepley   Level: intermediate
9705adc21957SMatthew G. Knepley 
9706adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`, `DMReorderSectionGetDefault()`
9707adc21957SMatthew G. Knepley @*/
9708adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetType(DM dm, MatOrderingType *reorder)
9709adc21957SMatthew G. Knepley {
9710adc21957SMatthew G. Knepley   PetscFunctionBegin;
9711adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9712adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9713adc21957SMatthew G. Knepley   *reorder = NULL;
9714adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetType_C", (DM, MatOrderingType *), (dm, reorder));
9715adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9716adc21957SMatthew G. Knepley }
9717