xref: /petsc/src/dm/interface/dm.c (revision bb7acecff235415e388d731130654f7159883491)
1d0295fc0SJunchao Zhang #include <petscvec.h>
2af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
3c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
4e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
53e922f36SToby Isaac #include <petscdmplex.h>
6f19dbd58SToby Isaac #include <petscdmfield.h>
70c312b8eSJed Brown #include <petscsf.h>
82764a2aaSMatthew G. Knepley #include <petscds.h>
947c6ae99SBarry Smith 
10f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
11f918ec44SMatthew G. Knepley #include <petscfeceed.h>
12f918ec44SMatthew G. Knepley #endif
13f918ec44SMatthew G. Knepley 
14cea3dcb8SSatish Balay #if !defined(PETSC_HAVE_WINDOWS_COMPILERS)
15cea3dcb8SSatish Balay #include <petsc/private/valgrind/memcheck.h>
1600d952a4SJed Brown #endif
1700d952a4SJed Brown 
18732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
19d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
205b8ffe73SMark Adams PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction, DM_CreateInjection, DM_CreateMatrix, DM_CreateMassMatrix, DM_Load, DM_AdaptInterpolator;
2167a56275SMatthew G Knepley 
22ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_", NULL};
23d1b3049bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID","ESSENTIAL","NATURAL","INVALID","INVALID","ESSENTIAL_FIELD","NATURAL_FIELD","INVALID","INVALID","ESSENTIAL_BD_FIELD","NATURAL_RIEMANN","DMBoundaryConditionType","DM_BC_", NULL};
24da9060c4SMatthew G. Knepley const char *const DMPolytopeTypes[] = {"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", "unknown", "invalid", "DMPolytopeType", "DM_POLYTOPE_", NULL};
252cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace","keep","fail","DMCopyLabelsMode","DM_COPY_LABELS_", NULL};
2660c22052SBarry Smith 
27a4121054SBarry Smith /*@
28*bb7acecfSBarry Smith   DMCreate - Creates an empty `DM` object. `DM`s are the abstract objects in PETSc that mediate between meshes and discretizations and the
29*bb7acecfSBarry Smith   algebraic solvers, time integrators, and optimization algorithms.
30a4121054SBarry Smith 
31d083f849SBarry Smith   Collective
32a4121054SBarry Smith 
33a4121054SBarry Smith   Input Parameter:
34*bb7acecfSBarry Smith . comm - The communicator for the `DM` object
35a4121054SBarry Smith 
36a4121054SBarry Smith   Output Parameter:
37*bb7acecfSBarry Smith . dm - The `DM` object
38a4121054SBarry Smith 
39a4121054SBarry Smith   Level: beginner
40a4121054SBarry Smith 
41*bb7acecfSBarry Smith   Notes:
42*bb7acecfSBarry Smith   See `DMType` for a brief summary of available `DM`.
43*bb7acecfSBarry Smith 
44*bb7acecfSBarry Smith   The type must then be set with `DMSetType()`. If you never call `DMSetType()` it will generate an
45*bb7acecfSBarry Smith   error when you try to use the dm.
46*bb7acecfSBarry Smith 
47*bb7acecfSBarry Smith .seealso: `DMSetType()`, `DMType`, `DMDACreate()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK`
48a4121054SBarry Smith @*/
497087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
50a4121054SBarry Smith {
51a4121054SBarry Smith   DM             v;
52e5e52638SMatthew G. Knepley   PetscDS        ds;
53a4121054SBarry Smith 
54a4121054SBarry Smith   PetscFunctionBegin;
551411c6eeSJed Brown   PetscValidPointer(dm,2);
560298fd71SBarry Smith   *dm = NULL;
579566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
58a4121054SBarry Smith 
599566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
60e7c4fc90SDmitry Karpeev 
6149be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
6249be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
630298fd71SBarry Smith   v->ltogmap                  = NULL;
64a4ea9b21SRichard Tran Mills   v->bind_below               = 0;
651411c6eeSJed Brown   v->bs                       = 1;
66171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
679566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
689566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
69c58f1c22SToby Isaac   v->labels                   = NULL;
7034aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
7134aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
72c58f1c22SToby Isaac   v->depthLabel               = NULL;
73ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
741bb6d2a8SBarry Smith   v->localSection             = NULL;
751bb6d2a8SBarry Smith   v->globalSection            = NULL;
763b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
773b8ba7d1SJed Brown   v->defaultConstraint.mat    = NULL;
7879769bd5SJed Brown   v->defaultConstraint.bias   = NULL;
796858538eSMatthew G. Knepley   v->coordinates[0].dim       = PETSC_DEFAULT;
806858538eSMatthew G. Knepley   v->coordinates[1].dim       = PETSC_DEFAULT;
816858538eSMatthew G. Knepley   v->sparseLocalize           = PETSC_TRUE;
8296173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
83435a35e8SMatthew G Knepley   {
84435a35e8SMatthew G Knepley     PetscInt i;
85435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
860298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
87f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
88435a35e8SMatthew G Knepley     }
89435a35e8SMatthew G Knepley   }
909566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
919566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(v, NULL, NULL, ds));
929566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
939566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
9414f150ffSMatthew G. Knepley   v->dmBC = NULL;
95a8fb8f29SToby Isaac   v->coarseMesh = NULL;
96f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
97cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
989566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v,VECSTANDARD));
999566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v,MATAIJ));
1004a7a4c06SLawrence Mitchell 
1011411c6eeSJed Brown   *dm = v;
102a4121054SBarry Smith   PetscFunctionReturn(0);
103a4121054SBarry Smith }
104a4121054SBarry Smith 
10538221697SMatthew G. Knepley /*@
106*bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
10738221697SMatthew G. Knepley 
108d083f849SBarry Smith   Collective
10938221697SMatthew G. Knepley 
11038221697SMatthew G. Knepley   Input Parameter:
111*bb7acecfSBarry Smith . dm - The original `DM` object
11238221697SMatthew G. Knepley 
11338221697SMatthew G. Knepley   Output Parameter:
114*bb7acecfSBarry Smith . newdm  - The new `DM` object
11538221697SMatthew G. Knepley 
11638221697SMatthew G. Knepley   Level: beginner
11738221697SMatthew G. Knepley 
1181cb8cacdSPatrick Sanan   Notes:
119*bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
120*bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
121*bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1221bb6d2a8SBarry Smith 
123*bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
12489706ed2SPatrick Sanan 
125*bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
126*bb7acecfSBarry Smith 
127*bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMSetType()`, `DMConvert()`
1281bb6d2a8SBarry Smith 
12938221697SMatthew G. Knepley @*/
13038221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
13138221697SMatthew G. Knepley {
13238221697SMatthew G. Knepley   PetscSF        sf;
13338221697SMatthew G. Knepley   Vec            coords;
13438221697SMatthew G. Knepley   void          *ctx;
1356858538eSMatthew G. Knepley   PetscInt       dim, cdim, i;
13638221697SMatthew G. Knepley 
13738221697SMatthew G. Knepley   PetscFunctionBegin;
13838221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13938221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
1409566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject) dm), newdm));
1419566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
142ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
143ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
144c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
1459566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1469566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype,(char**)&(*newdm)->vectype));
1479566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1489566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype,(char**)&(*newdm)->mattype));
1499566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1509566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
1511baa6e33SBarry Smith   if (dm->ops->clone) PetscCall((*dm->ops->clone)(dm, newdm));
1523f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1539566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1549566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1559566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1569566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
1576858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1586858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
159be4c1c3eSMatthew G. Knepley       DM           ncdm;
160be4c1c3eSMatthew G. Knepley       PetscSection cs;
1615a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
162be4c1c3eSMatthew G. Knepley 
1636858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1649566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
1659566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm)));
1665a0206caSToby Isaac       if (pEndMax >= 0) {
1676858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1686858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1699566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1706858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1716858538eSMatthew G. Knepley         else   PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1729566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
173be4c1c3eSMatthew G. Knepley       }
174be4c1c3eSMatthew G. Knepley     }
1756858538eSMatthew G. Knepley   }
1769566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1779566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1789566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
17938221697SMatthew G. Knepley   if (coords) {
1809566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
18138221697SMatthew G. Knepley   } else {
1829566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1839566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
18438221697SMatthew G. Knepley   }
1856858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
1866858538eSMatthew G. Knepley   if (coords) {
1876858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
1886858538eSMatthew G. Knepley   } else {
1896858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
1906858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
1916858538eSMatthew G. Knepley   }
19290b157c4SStefano Zampini   {
1934fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
1946858538eSMatthew G. Knepley 
1954fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm,    &maxCell, &Lstart, &L));
1964fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell,  Lstart,  L));
197c6b900c6SMatthew G. Knepley   }
19834aa8a36SMatthew G. Knepley   {
19934aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
20034aa8a36SMatthew G. Knepley 
2019566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2029566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
20334aa8a36SMatthew G. Knepley   }
20438221697SMatthew G. Knepley   PetscFunctionReturn(0);
20538221697SMatthew G. Knepley }
20638221697SMatthew G. Knepley 
2079a42bb27SBarry Smith /*@C
208*bb7acecfSBarry Smith        DMSetVecType - Sets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2099a42bb27SBarry Smith 
210d083f849SBarry Smith    Logically Collective on da
2119a42bb27SBarry Smith 
212147403d9SBarry Smith    Input Parameters:
2139a42bb27SBarry Smith +  da - initial distributed array
214*bb7acecfSBarry Smith -  ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2159a42bb27SBarry Smith 
2169a42bb27SBarry Smith    Options Database:
217147403d9SBarry Smith .   -dm_vec_type ctype - the type of vector to create
2189a42bb27SBarry Smith 
2199a42bb27SBarry Smith    Level: intermediate
2209a42bb27SBarry Smith 
221*bb7acecfSBarry Smith .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
222*bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2239a42bb27SBarry Smith @*/
22419fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2259a42bb27SBarry Smith {
2269a42bb27SBarry Smith   PetscFunctionBegin;
2279a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2289566063dSJacob Faibussowitsch   PetscCall(PetscFree(da->vectype));
2299566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype,(char**)&da->vectype));
2309a42bb27SBarry Smith   PetscFunctionReturn(0);
2319a42bb27SBarry Smith }
2329a42bb27SBarry Smith 
233c0dedaeaSBarry Smith /*@C
234*bb7acecfSBarry Smith        DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
235c0dedaeaSBarry Smith 
236d083f849SBarry Smith    Logically Collective on da
237c0dedaeaSBarry Smith 
238c0dedaeaSBarry Smith    Input Parameter:
239c0dedaeaSBarry Smith .  da - initial distributed array
240c0dedaeaSBarry Smith 
241c0dedaeaSBarry Smith    Output Parameter:
242c0dedaeaSBarry Smith .  ctype - the vector type
243c0dedaeaSBarry Smith 
244c0dedaeaSBarry Smith    Level: intermediate
245c0dedaeaSBarry Smith 
246db781477SPatrick Sanan .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
247c0dedaeaSBarry Smith @*/
248c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
249c0dedaeaSBarry Smith {
250c0dedaeaSBarry Smith   PetscFunctionBegin;
251c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
252c0dedaeaSBarry Smith   *ctype = da->vectype;
253c0dedaeaSBarry Smith   PetscFunctionReturn(0);
254c0dedaeaSBarry Smith }
255c0dedaeaSBarry Smith 
2565f1ad066SMatthew G Knepley /*@
257*bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2585f1ad066SMatthew G Knepley 
2595f1ad066SMatthew G Knepley   Not collective
2605f1ad066SMatthew G Knepley 
2615f1ad066SMatthew G Knepley   Input Parameter:
262*bb7acecfSBarry Smith . v - The `Vec`
2635f1ad066SMatthew G Knepley 
2645f1ad066SMatthew G Knepley   Output Parameter:
265*bb7acecfSBarry Smith . dm - The `DM`
2665f1ad066SMatthew G Knepley 
2675f1ad066SMatthew G Knepley   Level: intermediate
2685f1ad066SMatthew G Knepley 
269*bb7acecfSBarry Smith   Note:
270*bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
271*bb7acecfSBarry Smith 
272*bb7acecfSBarry Smith .seealso: `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2735f1ad066SMatthew G Knepley @*/
2745f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2755f1ad066SMatthew G Knepley {
2765f1ad066SMatthew G Knepley   PetscFunctionBegin;
2775f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2785f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2799566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm));
2805f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2815f1ad066SMatthew G Knepley }
2825f1ad066SMatthew G Knepley 
2835f1ad066SMatthew G Knepley /*@
284*bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
2855f1ad066SMatthew G Knepley 
2865f1ad066SMatthew G Knepley   Not collective
2875f1ad066SMatthew G Knepley 
2885f1ad066SMatthew G Knepley   Input Parameters:
289*bb7acecfSBarry Smith + v - The `Vec`
290*bb7acecfSBarry Smith - dm - The `DM`
2915f1ad066SMatthew G Knepley 
292*bb7acecfSBarry Smith   Note:
293*bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
294d9805387SMatthew G. Knepley 
295*bb7acecfSBarry 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.
296*bb7acecfSBarry Smith 
297*bb7acecfSBarry Smith   Level: developer
2985f1ad066SMatthew G Knepley 
299db781477SPatrick Sanan .seealso: `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3005f1ad066SMatthew G Knepley @*/
3015f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
3025f1ad066SMatthew G Knepley {
3035f1ad066SMatthew G Knepley   PetscFunctionBegin;
3045f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
305d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
3069566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm));
3075f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
3085f1ad066SMatthew G Knepley }
3095f1ad066SMatthew G Knepley 
310521d9a4cSLisandro Dalcin /*@C
311*bb7acecfSBarry Smith        DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3128f1509bcSBarry Smith 
313d083f849SBarry Smith    Logically Collective on dm
3148f1509bcSBarry Smith 
3158f1509bcSBarry Smith    Input Parameters:
316*bb7acecfSBarry Smith +  dm - the `DM` context
3178f1509bcSBarry Smith -  ctype - the matrix type
3188f1509bcSBarry Smith 
3198f1509bcSBarry Smith    Options Database:
3208f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3218f1509bcSBarry Smith 
3228f1509bcSBarry Smith    Level: intermediate
3238f1509bcSBarry Smith 
324db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
325*bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3268f1509bcSBarry Smith @*/
3278f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3288f1509bcSBarry Smith {
3298f1509bcSBarry Smith   PetscFunctionBegin;
3308f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3318f1509bcSBarry Smith   dm->coloringtype = ctype;
3328f1509bcSBarry Smith   PetscFunctionReturn(0);
3338f1509bcSBarry Smith }
3348f1509bcSBarry Smith 
3358f1509bcSBarry Smith /*@C
336*bb7acecfSBarry Smith        DMGetISColoringType - Gets the type of coloring,  `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
337521d9a4cSLisandro Dalcin 
338d083f849SBarry Smith    Logically Collective on dm
339521d9a4cSLisandro Dalcin 
340521d9a4cSLisandro Dalcin    Input Parameter:
341*bb7acecfSBarry Smith .  dm - the `DM` context
3428f1509bcSBarry Smith 
3438f1509bcSBarry Smith    Output Parameter:
3448f1509bcSBarry Smith .  ctype - the matrix type
3458f1509bcSBarry Smith 
3468f1509bcSBarry Smith    Options Database:
3478f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3488f1509bcSBarry Smith 
3498f1509bcSBarry Smith    Level: intermediate
3508f1509bcSBarry Smith 
351db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
352*bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3538f1509bcSBarry Smith @*/
3548f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3558f1509bcSBarry Smith {
3568f1509bcSBarry Smith   PetscFunctionBegin;
3578f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3588f1509bcSBarry Smith   *ctype = dm->coloringtype;
3598f1509bcSBarry Smith   PetscFunctionReturn(0);
3608f1509bcSBarry Smith }
3618f1509bcSBarry Smith 
3628f1509bcSBarry Smith /*@C
363*bb7acecfSBarry Smith        DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3648f1509bcSBarry Smith 
365d083f849SBarry Smith    Logically Collective on dm
3668f1509bcSBarry Smith 
3678f1509bcSBarry Smith    Input Parameters:
368*bb7acecfSBarry Smith +  dm - the `DM` context
369*bb7acecfSBarry Smith -  ctype - the matrix type, for example `MATMPIAIJ`
370521d9a4cSLisandro Dalcin 
371521d9a4cSLisandro Dalcin    Options Database:
372*bb7acecfSBarry Smith .   -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
373521d9a4cSLisandro Dalcin 
374521d9a4cSLisandro Dalcin    Level: intermediate
375521d9a4cSLisandro Dalcin 
376*bb7acecfSBarry Smith .seealso: `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`, `DMSetMatType()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
377521d9a4cSLisandro Dalcin @*/
37819fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
379521d9a4cSLisandro Dalcin {
380521d9a4cSLisandro Dalcin   PetscFunctionBegin;
381521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3829566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->mattype));
3839566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype,(char**)&dm->mattype));
384521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
385521d9a4cSLisandro Dalcin }
386521d9a4cSLisandro Dalcin 
387c0dedaeaSBarry Smith /*@C
388*bb7acecfSBarry Smith        DMGetMatType - Gets the type of matrix created with `DMCreateMatrix()`
389c0dedaeaSBarry Smith 
390d083f849SBarry Smith    Logically Collective on dm
391c0dedaeaSBarry Smith 
392c0dedaeaSBarry Smith    Input Parameter:
393*bb7acecfSBarry Smith .  dm - the `DM` context
394c0dedaeaSBarry Smith 
395c0dedaeaSBarry Smith    Output Parameter:
396c0dedaeaSBarry Smith .  ctype - the matrix type
397c0dedaeaSBarry Smith 
398c0dedaeaSBarry Smith    Level: intermediate
399c0dedaeaSBarry Smith 
400db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`, `DMSetMatType()`, `DMGetMatType()`
401c0dedaeaSBarry Smith @*/
402c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
403c0dedaeaSBarry Smith {
404c0dedaeaSBarry Smith   PetscFunctionBegin;
405c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
406c0dedaeaSBarry Smith   *ctype = dm->mattype;
407c0dedaeaSBarry Smith   PetscFunctionReturn(0);
408c0dedaeaSBarry Smith }
409c0dedaeaSBarry Smith 
410c688c046SMatthew G Knepley /*@
411*bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
412c688c046SMatthew G Knepley 
413c688c046SMatthew G Knepley   Not collective
414c688c046SMatthew G Knepley 
415c688c046SMatthew G Knepley   Input Parameter:
416*bb7acecfSBarry Smith . A - The `Mat`
417c688c046SMatthew G Knepley 
418c688c046SMatthew G Knepley   Output Parameter:
419*bb7acecfSBarry Smith . dm - The `DM`
420c688c046SMatthew G Knepley 
421c688c046SMatthew G Knepley   Level: intermediate
422c688c046SMatthew G Knepley 
423*bb7acecfSBarry Smith   Note:
424*bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
425*bb7acecfSBarry Smith 
426*bb7acecfSBarry Smith   Developer Note:
427*bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4288f1509bcSBarry Smith 
429db781477SPatrick Sanan .seealso: `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
430c688c046SMatthew G Knepley @*/
431c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
432c688c046SMatthew G Knepley {
433c688c046SMatthew G Knepley   PetscFunctionBegin;
434c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
435c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
4369566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm));
437c688c046SMatthew G Knepley   PetscFunctionReturn(0);
438c688c046SMatthew G Knepley }
439c688c046SMatthew G Knepley 
440c688c046SMatthew G Knepley /*@
441*bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
442c688c046SMatthew G Knepley 
443c688c046SMatthew G Knepley   Not collective
444c688c046SMatthew G Knepley 
445c688c046SMatthew G Knepley   Input Parameters:
446c688c046SMatthew G Knepley + A - The Mat
447c688c046SMatthew G Knepley - dm - The DM
448c688c046SMatthew G Knepley 
449*bb7acecfSBarry Smith   Level: developer
450c688c046SMatthew G Knepley 
451*bb7acecfSBarry Smith   Note:
452*bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
453*bb7acecfSBarry Smith 
454*bb7acecfSBarry Smith   Developer Note:
455*bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
456*bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4578f1509bcSBarry Smith 
458db781477SPatrick Sanan .seealso: `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
459c688c046SMatthew G Knepley @*/
460c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
461c688c046SMatthew G Knepley {
462c688c046SMatthew G Knepley   PetscFunctionBegin;
463c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4648865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
4659566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm));
466c688c046SMatthew G Knepley   PetscFunctionReturn(0);
467c688c046SMatthew G Knepley }
468c688c046SMatthew G Knepley 
4699a42bb27SBarry Smith /*@C
470*bb7acecfSBarry Smith    DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4719a42bb27SBarry Smith 
472d083f849SBarry Smith    Logically Collective on dm
4739a42bb27SBarry Smith 
474d8d19677SJose E. Roman    Input Parameters:
475*bb7acecfSBarry Smith +  da - the `DM` context
476*bb7acecfSBarry Smith -  prefix - the prefix to prepend
4779a42bb27SBarry Smith 
4789a42bb27SBarry Smith    Notes:
4799a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4809a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4819a42bb27SBarry Smith 
4829a42bb27SBarry Smith    Level: advanced
4839a42bb27SBarry Smith 
484*bb7acecfSBarry Smith .seealso: `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
4859a42bb27SBarry Smith @*/
4867087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4879a42bb27SBarry Smith {
4889a42bb27SBarry Smith   PetscFunctionBegin;
4899a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4909566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm,prefix));
4911baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix));
4921baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix));
4939a42bb27SBarry Smith   PetscFunctionReturn(0);
4949a42bb27SBarry Smith }
4959a42bb27SBarry Smith 
49631697293SDave May /*@C
497*bb7acecfSBarry Smith    DMAppendOptionsPrefix - Appends an additional string to an already exising prefix used for searching for
498*bb7acecfSBarry Smith    `DM` options in the options database.
49931697293SDave May 
500d083f849SBarry Smith    Logically Collective on dm
50131697293SDave May 
50231697293SDave May    Input Parameters:
503*bb7acecfSBarry Smith +  dm - the `DM` context
504*bb7acecfSBarry Smith -  prefix - the string to append to the current prefix
50531697293SDave May 
50631697293SDave May    Notes:
507*bb7acecfSBarry 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.
50831697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
50931697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
51031697293SDave May 
51131697293SDave May    Level: advanced
51231697293SDave May 
513*bb7acecfSBarry Smith .seealso: `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
51431697293SDave May @*/
51531697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
51631697293SDave May {
51731697293SDave May   PetscFunctionBegin;
51831697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5199566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix));
52031697293SDave May   PetscFunctionReturn(0);
52131697293SDave May }
52231697293SDave May 
52331697293SDave May /*@C
52431697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
525*bb7acecfSBarry Smith    DM options in the options database.
52631697293SDave May 
52731697293SDave May    Not Collective
52831697293SDave May 
52931697293SDave May    Input Parameters:
530*bb7acecfSBarry Smith .  dm - the `DM` context
53131697293SDave May 
53231697293SDave May    Output Parameters:
53331697293SDave May .  prefix - pointer to the prefix string used is returned
53431697293SDave May 
535*bb7acecfSBarry Smith    Fortran Note:
53695452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
53731697293SDave May    sufficient length to hold the prefix.
53831697293SDave May 
53931697293SDave May    Level: advanced
54031697293SDave May 
541*bb7acecfSBarry Smith .seealso: `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
54231697293SDave May @*/
54331697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
54431697293SDave May {
54531697293SDave May   PetscFunctionBegin;
54631697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5479566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm,prefix));
54831697293SDave May   PetscFunctionReturn(0);
54931697293SDave May }
55031697293SDave May 
55188bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
55288bdff64SToby Isaac {
5536eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
55488bdff64SToby Isaac 
55588bdff64SToby Isaac   PetscFunctionBegin;
556aab5bcd8SJed Brown   *ncrefct = 0;
55788bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55888bdff64SToby Isaac     refct--;
55988bdff64SToby Isaac     if (recurseCoarse) {
56088bdff64SToby Isaac       PetscInt coarseCount;
56188bdff64SToby Isaac 
5629566063dSJacob Faibussowitsch       PetscCall(DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount));
56388bdff64SToby Isaac       refct += coarseCount;
56488bdff64SToby Isaac     }
56588bdff64SToby Isaac   }
56688bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56788bdff64SToby Isaac     refct--;
56888bdff64SToby Isaac     if (recurseFine) {
56988bdff64SToby Isaac       PetscInt fineCount;
57088bdff64SToby Isaac 
5719566063dSJacob Faibussowitsch       PetscCall(DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount));
57288bdff64SToby Isaac       refct += fineCount;
57388bdff64SToby Isaac     }
57488bdff64SToby Isaac   }
57588bdff64SToby Isaac   *ncrefct = refct;
57688bdff64SToby Isaac   PetscFunctionReturn(0);
57788bdff64SToby Isaac }
57888bdff64SToby Isaac 
579f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
580354557abSToby Isaac {
5815d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
582354557abSToby Isaac 
583354557abSToby Isaac   PetscFunctionBegin;
584354557abSToby Isaac   /* destroy the labels */
585354557abSToby Isaac   while (next) {
586354557abSToby Isaac     DMLabelLink tmp = next->next;
587354557abSToby Isaac 
5885d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
589ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
5909566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
5919566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
592354557abSToby Isaac     next = tmp;
593354557abSToby Isaac   }
5945d80c0bfSVaclav Hapla   dm->labels = NULL;
595354557abSToby Isaac   PetscFunctionReturn(0);
596354557abSToby Isaac }
597354557abSToby Isaac 
5986858538eSMatthew G. Knepley PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
5996858538eSMatthew G. Knepley {
6006858538eSMatthew G. Knepley   PetscFunctionBegin;
6016858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6026858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6036858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6046858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6056858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6066858538eSMatthew G. Knepley   PetscFunctionReturn(0);
6076858538eSMatthew G. Knepley }
6086858538eSMatthew G. Knepley 
6091fb7b255SJunchao Zhang /*@C
610*bb7acecfSBarry Smith     DMDestroy - Destroys a `DM`.
61147c6ae99SBarry Smith 
612d083f849SBarry Smith     Collective on dm
61347c6ae99SBarry Smith 
61447c6ae99SBarry Smith     Input Parameter:
615*bb7acecfSBarry Smith .   dm - the `DM` object to destroy
61647c6ae99SBarry Smith 
61747c6ae99SBarry Smith     Level: developer
61847c6ae99SBarry Smith 
619*bb7acecfSBarry Smith .seealso: `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
62047c6ae99SBarry Smith 
62147c6ae99SBarry Smith @*/
622fcfd50ebSBarry Smith PetscErrorCode DMDestroy(DM *dm)
62347c6ae99SBarry Smith {
6246eb26441SStefano Zampini   PetscInt       cnt;
625dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
62647c6ae99SBarry Smith 
62747c6ae99SBarry Smith   PetscFunctionBegin;
6286bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6296bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
63087e657c6SBarry Smith 
63188bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
6329566063dSJacob Faibussowitsch   PetscCall(DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt));
63388bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
634ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
6356bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6366bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6376eb26441SStefano Zampini 
6389566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6399566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
6406eb26441SStefano Zampini 
641f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6420298fd71SBarry Smith   (*dm)->namedglobal = NULL;
643f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6442348bcf4SPeter Brune     nnext = nlink->next;
6457a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN,((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
6469566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6479566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6489566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
6492348bcf4SPeter Brune   }
650f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6510298fd71SBarry Smith   (*dm)->namedlocal = NULL;
652f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
653f490541aSPeter Brune     nnext = nlink->next;
6547a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN,((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
6559566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6569566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6579566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
658f490541aSPeter Brune   }
6592348bcf4SPeter Brune 
660b17ce1afSJed Brown   /* Destroy the list of hooks */
661c833c3b5SJed Brown   {
662c833c3b5SJed Brown     DMCoarsenHookLink link,next;
663b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
664b17ce1afSJed Brown       next = link->next;
6659566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
666b17ce1afSJed Brown     }
6670298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
668c833c3b5SJed Brown   }
669c833c3b5SJed Brown   {
670c833c3b5SJed Brown     DMRefineHookLink link,next;
671c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
672c833c3b5SJed Brown       next = link->next;
6739566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
674c833c3b5SJed Brown     }
6750298fd71SBarry Smith     (*dm)->refinehook = NULL;
676c833c3b5SJed Brown   }
677be081cd6SPeter Brune   {
678be081cd6SPeter Brune     DMSubDomainHookLink link,next;
679be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
680be081cd6SPeter Brune       next = link->next;
6819566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
682be081cd6SPeter Brune     }
6830298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
684be081cd6SPeter Brune   }
685baf369e7SPeter Brune   {
686baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
687baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
688baf369e7SPeter Brune       next = link->next;
6899566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
690baf369e7SPeter Brune     }
6910298fd71SBarry Smith     (*dm)->gtolhook = NULL;
692baf369e7SPeter Brune   }
693d4d07f1eSToby Isaac   {
694d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
695d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
696d4d07f1eSToby Isaac       next = link->next;
6979566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
698d4d07f1eSToby Isaac     }
699d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
700d4d07f1eSToby Isaac   }
701aa1993deSMatthew G Knepley   /* Destroy the work arrays */
702aa1993deSMatthew G Knepley   {
703aa1993deSMatthew G Knepley     DMWorkLink link,next;
7047a8be351SBarry Smith     PetscCheck(!(*dm)->workout,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
705aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
706aa1993deSMatthew G Knepley       next = link->next;
7079566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
7089566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
709aa1993deSMatthew G Knepley     }
7100298fd71SBarry Smith     (*dm)->workin = NULL;
711aa1993deSMatthew G Knepley   }
712c58f1c22SToby Isaac   /* destroy the labels */
7139566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
714f4cdcedcSVaclav Hapla   /* destroy the fields */
7159566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
716f4cdcedcSVaclav Hapla   /* destroy the boundaries */
717e6f8dbb6SToby Isaac   {
718e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
719e6f8dbb6SToby Isaac     while (next) {
720e6f8dbb6SToby Isaac       DMBoundary b = next;
721e6f8dbb6SToby Isaac 
722e6f8dbb6SToby Isaac       next = b->next;
7239566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
724e6f8dbb6SToby Isaac     }
725e6f8dbb6SToby Isaac   }
726b17ce1afSJed Brown 
7279566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7289566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7299566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
73052536dc3SBarry Smith 
7311a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7329566063dSJacob Faibussowitsch     PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7331a266240SBarry Smith   }
7349566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7359566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7369566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7379566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
73888ed4aceSMatthew G Knepley 
7399566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7409566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
7419566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7429566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7439566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7449566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7459566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
746736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
747736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7489566063dSJacob Faibussowitsch       PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
749736995cdSBlaise Bourdin     }
7509566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject) (*dm)->sfMigration));
751736995cdSBlaise Bourdin   }
7529a2a23afSMatthew G. Knepley   {
7539a2a23afSMatthew G. Knepley     Vec     *auxData;
7549a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7559a2a23afSMatthew G. Knepley 
7569566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetSize((*dm)->auxData, &n));
7579566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &auxData));
7589566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetVals((*dm)->auxData, &off, auxData));
7599566063dSJacob Faibussowitsch     for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
7609566063dSJacob Faibussowitsch     PetscCall(PetscFree(auxData));
7619566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
7629a2a23afSMatthew G. Knepley   }
76388bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
7649566063dSJacob Faibussowitsch     PetscCall(DMSetFineDM((*dm)->coarseMesh,NULL));
76588bdff64SToby Isaac   }
7666eb26441SStefano Zampini 
7679566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
76888bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
7699566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM((*dm)->fineMesh,NULL));
77088bdff64SToby Isaac   }
7719566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7724fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7739566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7749566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7756858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7766858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7779566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7789566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7799566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7806636e97aSMatthew G Knepley 
7819566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7829566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
783e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7849566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
785732e2eb9SMatthew G Knepley 
786ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7879566063dSJacob Faibussowitsch     PetscCall((*(*dm)->ops->destroy)(*dm));
788ed3c66a1SDave May   }
7899566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
790f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7919566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7929566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
793f918ec44SMatthew G. Knepley #endif
794435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7959566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
79647c6ae99SBarry Smith   PetscFunctionReturn(0);
79747c6ae99SBarry Smith }
79847c6ae99SBarry Smith 
799d7bf68aeSBarry Smith /*@
800*bb7acecfSBarry Smith     DMSetUp - sets up the data structures inside a `DM` object
801d7bf68aeSBarry Smith 
802d083f849SBarry Smith     Collective on dm
803d7bf68aeSBarry Smith 
804d7bf68aeSBarry Smith     Input Parameter:
805*bb7acecfSBarry Smith .   dm - the `DM` object to setup
806d7bf68aeSBarry Smith 
807*bb7acecfSBarry Smith     Level: intermediate
808d7bf68aeSBarry Smith 
809*bb7acecfSBarry Smith     Note:
810*bb7acecfSBarry Smith     This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
811*bb7acecfSBarry Smith 
812*bb7acecfSBarry Smith .seealso: `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
813d7bf68aeSBarry Smith 
814d7bf68aeSBarry Smith @*/
8157087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
816d7bf68aeSBarry Smith {
817d7bf68aeSBarry Smith   PetscFunctionBegin;
818171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8198387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
8201baa6e33SBarry Smith   if (dm->ops->setup) PetscCall((*dm->ops->setup)(dm));
8218387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
822d7bf68aeSBarry Smith   PetscFunctionReturn(0);
823d7bf68aeSBarry Smith }
824d7bf68aeSBarry Smith 
825d7bf68aeSBarry Smith /*@
826*bb7acecfSBarry Smith     DMSetFromOptions - sets parameters in a `DM` from the options database
827d7bf68aeSBarry Smith 
828d083f849SBarry Smith     Collective on dm
829d7bf68aeSBarry Smith 
830d7bf68aeSBarry Smith     Input Parameter:
831*bb7acecfSBarry Smith .   dm - the `DM` object to set options for
832d7bf68aeSBarry Smith 
833732e2eb9SMatthew G Knepley     Options Database:
834*bb7acecfSBarry Smith +   -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
835*bb7acecfSBarry Smith .   -dm_vec_type <type>  - type of vector to create inside `DM`
836*bb7acecfSBarry Smith .   -dm_mat_type <type>  - type of matrix to create inside `DM`
837a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
838*bb7acecfSBarry 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`
839732e2eb9SMatthew G Knepley 
8409318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8419318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8429318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
843cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
844*bb7acecfSBarry Smith . -dm_plex_shape <shape>            - The domain shape, such as `DM_SHAPE_BOX`, `DM_SHAPE_SPHERE`, etc.
8459318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8469318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8479318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
848*bb7acecfSBarry Smith . -dm_plex_simplex <bool>           - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
849*bb7acecfSBarry Smith . -dm_plex_interpolate <bool>       - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8509318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8519318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8529318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8539318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
854*bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>        - Specify the `DMBoundaryType `for each direction
8559318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8569318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8579318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8589318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
859bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8609318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8619318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8629318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8639318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
864bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
865d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
866d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
867d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
868d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
869d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
870909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
871909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8729318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8739318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8749318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8759318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8769318fe57SMatthew G. Knepley 
877384a6580SVaclav Hapla     DMPLEX Specific Checks
878*bb7acecfSBarry Smith +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
879*bb7acecfSBarry Smith .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
880*bb7acecfSBarry 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()`
881*bb7acecfSBarry Smith .   -dm_plex_check_geometry        - Check that cells have positive volume - `DMPlexCheckGeometry()`
882*bb7acecfSBarry Smith .   -dm_plex_check_pointsf         - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
883*bb7acecfSBarry Smith .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
884384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
885d7bf68aeSBarry Smith 
88695eb5ee5SVaclav Hapla     Level: intermediate
88795eb5ee5SVaclav Hapla 
888*bb7acecfSBarry Smith .seealso: `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
889*bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
890*bb7acecfSBarry Smith          `DMSetOptionsPrefix()`, `DM`, `DMType`, `DMPLEX`, `DMDA`
891d7bf68aeSBarry Smith 
892d7bf68aeSBarry Smith @*/
8937087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
894d7bf68aeSBarry Smith {
8957781c08eSBarry Smith   char           typeName[256];
896ca266f36SBarry Smith   PetscBool      flg;
897d7bf68aeSBarry Smith 
898d7bf68aeSBarry Smith   PetscFunctionBegin;
899171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
90049be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
9019566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
9029566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
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));
9099566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL));
9109566063dSJacob 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));
9111baa6e33SBarry Smith   if (dm->ops->setfromoptions) PetscCall((*dm->ops->setfromoptions)(PetscOptionsObject,dm));
912f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
9139566063dSJacob Faibussowitsch   PetscCall(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm));
914d0609cedSBarry Smith   PetscOptionsEnd();
915d7bf68aeSBarry Smith   PetscFunctionReturn(0);
916d7bf68aeSBarry Smith }
917d7bf68aeSBarry Smith 
918fc9bc008SSatish Balay /*@C
919*bb7acecfSBarry Smith    DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
920fe2efc57SMark 
921*bb7acecfSBarry Smith    Collective on dm
922fe2efc57SMark 
923fe2efc57SMark    Input Parameters:
924*bb7acecfSBarry Smith +  dm - the `DM` object
925*bb7acecfSBarry Smith .  obj - optional object that provides the prefix for the options database (if NULL then the prefix in obj is used)
926*bb7acecfSBarry Smith -  optionname - option string that is used to activate viewing
927fe2efc57SMark 
928fe2efc57SMark    Level: intermediate
929*bb7acecfSBarry Smith 
930*bb7acecfSBarry Smith    Note:
931*bb7acecfSBarry Smith    See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
932*bb7acecfSBarry Smith 
933*bb7acecfSBarry Smith .seealso: `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`, `PetscObjectViewFromOptions()`
934fe2efc57SMark @*/
935fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
936fe2efc57SMark {
937fe2efc57SMark   PetscFunctionBegin;
938fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9399566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm,obj,name));
940fe2efc57SMark   PetscFunctionReturn(0);
941fe2efc57SMark }
942fe2efc57SMark 
943fe2efc57SMark /*@C
944*bb7acecfSBarry 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
945*bb7acecfSBarry Smith     save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
94647c6ae99SBarry Smith 
947d083f849SBarry Smith     Collective on dm
94847c6ae99SBarry Smith 
949d8d19677SJose E. Roman     Input Parameters:
950*bb7acecfSBarry Smith +   dm - the `DM` object to view
95147c6ae99SBarry Smith -   v - the viewer
95247c6ae99SBarry Smith 
953cd7e8a5eSksagiyam     Notes:
954*bb7acecfSBarry Smith     Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
955*bb7acecfSBarry Smith     meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
956*bb7acecfSBarry Smith     before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
957cd7e8a5eSksagiyam 
958224748a4SBarry Smith     Level: beginner
95947c6ae99SBarry Smith 
960*bb7acecfSBarry Smith .seealso: `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat`(), `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
96147c6ae99SBarry Smith 
96247c6ae99SBarry Smith @*/
9637087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
96447c6ae99SBarry Smith {
96532c0f0efSBarry Smith   PetscBool         isbinary;
96676a8abe0SBarry Smith   PetscMPIInt       size;
96776a8abe0SBarry Smith   PetscViewerFormat format;
96847c6ae99SBarry Smith 
96947c6ae99SBarry Smith   PetscFunctionBegin;
970171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9713014e516SBarry Smith   if (!v) {
9729566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v));
9733014e516SBarry Smith   }
974b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
97574903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
97674903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
97774903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
97874903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
97974903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
98074903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
98174903a4fSStefano Zampini      in an error here */
98274903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9839566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
984b1b135c8SBarry Smith 
9859566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v,&format));
9869566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size));
98776a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
9889566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm,v));
9899566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary));
99032c0f0efSBarry Smith   if (isbinary) {
99155849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
99232c0f0efSBarry Smith     char     type[256];
99332c0f0efSBarry Smith 
9949566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v,&classid,1,PETSC_INT));
9959566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(type,((PetscObject)dm)->type_name,256));
9969566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v,type,256,PETSC_CHAR));
99732c0f0efSBarry Smith   }
9981baa6e33SBarry Smith   if (dm->ops->view) PetscCall((*dm->ops->view)(dm,v));
99947c6ae99SBarry Smith   PetscFunctionReturn(0);
100047c6ae99SBarry Smith }
100147c6ae99SBarry Smith 
100247c6ae99SBarry Smith /*@
1003*bb7acecfSBarry 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,
1004*bb7acecfSBarry Smith     that is it has no ghost locations.
100547c6ae99SBarry Smith 
1006d083f849SBarry Smith     Collective on dm
100747c6ae99SBarry Smith 
100847c6ae99SBarry Smith     Input Parameter:
1009*bb7acecfSBarry Smith .   dm - the `DM` object
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith     Output Parameter:
101247c6ae99SBarry Smith .   vec - the global vector
101347c6ae99SBarry Smith 
1014073dac72SJed Brown     Level: beginner
101547c6ae99SBarry Smith 
1016*bb7acecfSBarry Smith .seealso: `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1017*bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
101847c6ae99SBarry Smith 
101947c6ae99SBarry Smith @*/
10207087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
102147c6ae99SBarry Smith {
102247c6ae99SBarry Smith   PetscFunctionBegin;
1023171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1024b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
10257a8be351SBarry Smith   PetscCheck(dm->ops->createglobalvector,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
10269566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createglobalvector)(dm,vec));
102776bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1028c6b011d8SStefano Zampini     DM vdm;
1029c6b011d8SStefano Zampini 
10309566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec,&vdm));
10317a8be351SBarry Smith     PetscCheck(vdm,PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the vector",((PetscObject)dm)->type_name);
1032c6b011d8SStefano Zampini   }
103347c6ae99SBarry Smith   PetscFunctionReturn(0);
103447c6ae99SBarry Smith }
103547c6ae99SBarry Smith 
103647c6ae99SBarry Smith /*@
1037*bb7acecfSBarry Smith     DMCreateLocalVector - Creates a local vector from a `DM` object.
103847c6ae99SBarry Smith 
103947c6ae99SBarry Smith     Not Collective
104047c6ae99SBarry Smith 
104147c6ae99SBarry Smith     Input Parameter:
1042*bb7acecfSBarry Smith .   dm - the `DM` object
104347c6ae99SBarry Smith 
104447c6ae99SBarry Smith     Output Parameter:
104547c6ae99SBarry Smith .   vec - the local vector
104647c6ae99SBarry Smith 
1047073dac72SJed Brown     Level: beginner
104847c6ae99SBarry Smith 
1049*bb7acecfSBarry Smith     Notes:
1050*bb7acecfSBarry 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.
1051*bb7acecfSBarry Smith 
1052*bb7acecfSBarry Smith  .seealso: `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1053*bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
105447c6ae99SBarry Smith 
105547c6ae99SBarry Smith @*/
10567087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
105747c6ae99SBarry Smith {
105847c6ae99SBarry Smith   PetscFunctionBegin;
1059171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1060b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
10617a8be351SBarry Smith   PetscCheck(dm->ops->createlocalvector,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
10629566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createlocalvector)(dm,vec));
106376bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1064c6b011d8SStefano Zampini     DM vdm;
1065c6b011d8SStefano Zampini 
10669566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec,&vdm));
10677a8be351SBarry Smith     PetscCheck(vdm,PETSC_COMM_SELF,PETSC_ERR_LIB,"DM type '%s' did not attach the DM to the vector",((PetscObject)dm)->type_name);
1068c6b011d8SStefano Zampini   }
106947c6ae99SBarry Smith   PetscFunctionReturn(0);
107047c6ae99SBarry Smith }
107147c6ae99SBarry Smith 
10721411c6eeSJed Brown /*@
1073*bb7acecfSBarry Smith    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10741411c6eeSJed Brown 
1075d083f849SBarry Smith    Collective on dm
10761411c6eeSJed Brown 
10771411c6eeSJed Brown    Input Parameter:
1078*bb7acecfSBarry Smith .  dm - the `DM` that provides the mapping
10791411c6eeSJed Brown 
10801411c6eeSJed Brown    Output Parameter:
10811411c6eeSJed Brown .  ltog - the mapping
10821411c6eeSJed Brown 
1083*bb7acecfSBarry Smith    Level: advanced
10841411c6eeSJed Brown 
10851411c6eeSJed Brown    Notes:
1086*bb7acecfSBarry Smith    The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10871411c6eeSJed Brown 
1088*bb7acecfSBarry Smith    Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1089*bb7acecfSBarry Smith    need to use this function with those objects.
1090*bb7acecfSBarry Smith 
1091*bb7acecfSBarry Smith    This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1092*bb7acecfSBarry Smith 
1093*bb7acecfSBarry Smith .seealso: `DMCreateLocalVector()`,  `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1094*bb7acecfSBarry Smith           `DMCreateMatrix()`
10951411c6eeSJed Brown @*/
10967087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10971411c6eeSJed Brown {
10980be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10991411c6eeSJed Brown 
11001411c6eeSJed Brown   PetscFunctionBegin;
11011411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11021411c6eeSJed Brown   PetscValidPointer(ltog,2);
11031411c6eeSJed Brown   if (!dm->ltogmap) {
110437d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
110537d0c07bSMatthew G Knepley 
11069566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
110737d0c07bSMatthew G Knepley     if (section) {
1108a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
110937d0c07bSMatthew G Knepley       PetscInt       *ltog;
1110ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
111137d0c07bSMatthew G Knepley 
11129566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11139566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11149566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11159566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
111637d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1117e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
111837d0c07bSMatthew G Knepley 
111937d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11209566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11219566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11229566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11239566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11241a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11251a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
11261a7dc684SMatthew G. Knepley         if (dof) {
11275b8243e1SJed Brown           bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11281a7dc684SMatthew G. Knepley         }
11295227eafbSStefano Zampini 
1130e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11315227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1132e6befd46SJed Brown             ltog[l] = off < 0 ? off-c : -(off+c+1);
1133e6befd46SJed Brown             cind++;
1134e6befd46SJed Brown           } else {
11355227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off+1) : off) + c - cind;
1136e6befd46SJed Brown           }
113737d0c07bSMatthew G Knepley         }
113837d0c07bSMatthew G Knepley       }
1139bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11400be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11419566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax));
11420be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11430be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
11447591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11457591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1146ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1147ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1148ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1149ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l]/bs : -(-(ltog[l]+1)/bs + 1);
1150ca469d19SJed Brown         }
1151ccf3bd66SMatthew G. Knepley         n /= bs;
1152ccf3bd66SMatthew G. Knepley       }
11539566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
11549566063dSJacob Faibussowitsch       PetscCall(PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap));
115537d0c07bSMatthew G Knepley     } else {
11567a8be351SBarry Smith       PetscCheck(dm->ops->getlocaltoglobalmapping,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
11579566063dSJacob Faibussowitsch       PetscCall((*dm->ops->getlocaltoglobalmapping)(dm));
11581411c6eeSJed Brown     }
115937d0c07bSMatthew G Knepley   }
11601411c6eeSJed Brown   *ltog = dm->ltogmap;
11611411c6eeSJed Brown   PetscFunctionReturn(0);
11621411c6eeSJed Brown }
11631411c6eeSJed Brown 
11641411c6eeSJed Brown /*@
1165*bb7acecfSBarry Smith    DMGetBlockSize - Gets the inherent block size associated with a `DM`
11661411c6eeSJed Brown 
11671411c6eeSJed Brown    Not Collective
11681411c6eeSJed Brown 
11691411c6eeSJed Brown    Input Parameter:
1170*bb7acecfSBarry Smith .  dm - the `DM` with block structure
11711411c6eeSJed Brown 
11721411c6eeSJed Brown    Output Parameter:
11731411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11741411c6eeSJed Brown 
11751411c6eeSJed Brown    Level: intermediate
11761411c6eeSJed Brown 
1177*bb7acecfSBarry Smith    Note:
1178*bb7acecfSBarry Smith    This might be the number of degrees of freedom at each grid point for a structured grid.
1179*bb7acecfSBarry Smith 
1180*bb7acecfSBarry Smith    Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1181*bb7acecfSBarry Smith    rather different locations in the vectors may have a different block size.
1182*bb7acecfSBarry Smith 
1183db781477SPatrick Sanan .seealso: `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11841411c6eeSJed Brown @*/
11857087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
11861411c6eeSJed Brown {
11871411c6eeSJed Brown   PetscFunctionBegin;
11881411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1189534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11907a8be351SBarry Smith   PetscCheck(dm->bs >= 1,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
11911411c6eeSJed Brown   *bs = dm->bs;
11921411c6eeSJed Brown   PetscFunctionReturn(0);
11931411c6eeSJed Brown }
11941411c6eeSJed Brown 
119548eeb7c8SBarry Smith /*@C
1196*bb7acecfSBarry Smith     DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1197*bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
119847c6ae99SBarry Smith 
1199a5bc1bf3SBarry Smith     Collective on dmc
120047c6ae99SBarry Smith 
1201d8d19677SJose E. Roman     Input Parameters:
1202*bb7acecfSBarry Smith +   dmc - the `DM` object
1203*bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
120447c6ae99SBarry Smith 
1205d8d19677SJose E. Roman     Output Parameters:
120647c6ae99SBarry Smith +  mat - the interpolation
1207*bb7acecfSBarry Smith -  vec - the scaling (optional), see `DMCreateInterpolationScale()`
120847c6ae99SBarry Smith 
120947c6ae99SBarry Smith     Level: developer
121047c6ae99SBarry Smith 
121195452b02SPatrick Sanan     Notes:
1212*bb7acecfSBarry 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
1213*bb7acecfSBarry Smith     DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1214d52bd9f3SBarry Smith 
1215*bb7acecfSBarry Smith     For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1216*bb7acecfSBarry Smith     vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
121785afcc9aSBarry Smith 
1218*bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
121947c6ae99SBarry Smith 
122047c6ae99SBarry Smith @*/
1221a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
122247c6ae99SBarry Smith {
122347c6ae99SBarry Smith   PetscFunctionBegin;
1224a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1225a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1226c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
12277a8be351SBarry Smith   PetscCheck(dmc->ops->createinterpolation,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
12289566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0));
12299566063dSJacob Faibussowitsch   PetscCall((*dmc->ops->createinterpolation)(dmc,dmf,mat,vec));
12309566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0));
123147c6ae99SBarry Smith   PetscFunctionReturn(0);
123247c6ae99SBarry Smith }
123347c6ae99SBarry Smith 
12343ad4599aSBarry Smith /*@
1235*bb7acecfSBarry Smith     DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is the transpose of the interpolation between the `DM`.
1236*bb7acecfSBarry Smith     xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual) restriction. In other words xcoarse is the coarse
1237*bb7acecfSBarry Smith     representation of xfine.
12382ed6491fSPatrick Sanan 
12392ed6491fSPatrick Sanan   Input Parameters:
1240*bb7acecfSBarry Smith +      dac - `DM` that defines a coarse mesh
1241*bb7acecfSBarry Smith .      daf - `DM` that defines a fine mesh
12422ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12432ed6491fSPatrick Sanan 
12442ed6491fSPatrick Sanan   Output Parameter:
12452ed6491fSPatrick Sanan .    scale - the scaled vector
12462ed6491fSPatrick Sanan 
1247*bb7acecfSBarry Smith   Level: advanced
12482ed6491fSPatrick Sanan 
1249e9c74fd6SRichard Tran Mills   Developer Notes:
1250*bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1251e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1252e9c74fd6SRichard Tran Mills 
1253*bb7acecfSBarry Smith .seealso: `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, DMCreateRestriction()`, `DMCreateGlobalVector()`
12542ed6491fSPatrick Sanan 
12552ed6491fSPatrick Sanan @*/
12562ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
12572ed6491fSPatrick Sanan {
12582ed6491fSPatrick Sanan   Vec            fine;
12592ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
12609704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1261e9c74fd6SRichard Tran Mills   PetscBool      bindingpropagates,isbound;
12629704db99SRichard Tran Mills #endif
12632ed6491fSPatrick Sanan 
12642ed6491fSPatrick Sanan   PetscFunctionBegin;
12659566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf,&fine));
12669566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac,scale));
12679566063dSJacob Faibussowitsch   PetscCall(VecSet(fine,one));
12689704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12699704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12709704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12719704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12729566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine,&bindingpropagates));
1273e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12749566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat,PETSC_TRUE));
12759566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine,&isbound));
12769566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat,isbound));
127783aa49f4SRichard Tran Mills   }
12789704db99SRichard Tran Mills #endif
12799566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat,fine,*scale));
12809566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12819566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12822ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12832ed6491fSPatrick Sanan }
12842ed6491fSPatrick Sanan 
12852ed6491fSPatrick Sanan /*@
1286*bb7acecfSBarry Smith     DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1287*bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12883ad4599aSBarry Smith 
1289a5bc1bf3SBarry Smith     Collective on dmc
12903ad4599aSBarry Smith 
1291d8d19677SJose E. Roman     Input Parameters:
1292*bb7acecfSBarry Smith +   dmc - the `DM` object
1293*bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
12943ad4599aSBarry Smith 
12953ad4599aSBarry Smith     Output Parameter:
12963ad4599aSBarry Smith .  mat - the restriction
12973ad4599aSBarry Smith 
12983ad4599aSBarry Smith     Level: developer
12993ad4599aSBarry Smith 
1300*bb7acecfSBarry Smith     Note:
1301*bb7acecfSBarry Smith     This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1302*bb7acecfSBarry Smith     matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
13033ad4599aSBarry Smith 
1304*bb7acecfSBarry Smith .seealso: `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
13053ad4599aSBarry Smith 
13063ad4599aSBarry Smith @*/
1307a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
13083ad4599aSBarry Smith {
13093ad4599aSBarry Smith   PetscFunctionBegin;
1310a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1311a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
13125a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
13137a8be351SBarry Smith   PetscCheck(dmc->ops->createrestriction,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
13149566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0));
13159566063dSJacob Faibussowitsch   PetscCall((*dmc->ops->createrestriction)(dmc,dmf,mat));
13169566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0));
13173ad4599aSBarry Smith   PetscFunctionReturn(0);
13183ad4599aSBarry Smith }
13193ad4599aSBarry Smith 
132047c6ae99SBarry Smith /*@
1321*bb7acecfSBarry Smith     DMCreateInjection - Gets injection matrix between two `DM` objects. This is an operator that applied to a vector obtained with
1322*bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting the values
1323*bb7acecfSBarry Smith     on the coarse grid points. This compares to the operator obtained by `DMCreateRestriction()` or the transpose of the operator obtained
1324*bb7acecfSBarry Smith     by `DMCreateInterpolation()` that uses a "local weighted average" of the values around the coarse grid point as the coarse grid value.
132547c6ae99SBarry Smith 
1326a5bc1bf3SBarry Smith     Collective on dac
132747c6ae99SBarry Smith 
1328d8d19677SJose E. Roman     Input Parameters:
1329*bb7acecfSBarry Smith +   dac - the `DM` object
1330*bb7acecfSBarry Smith -   daf - the second, finer `DM` object
133147c6ae99SBarry Smith 
133247c6ae99SBarry Smith     Output Parameter:
13336dbf9973SLawrence Mitchell .   mat - the injection
133447c6ae99SBarry Smith 
133547c6ae99SBarry Smith     Level: developer
133647c6ae99SBarry Smith 
1337*bb7acecfSBarry Smith    Note:
1338*bb7acecfSBarry 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
1339*bb7acecfSBarry Smith         `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
134085afcc9aSBarry Smith 
1341*bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1342*bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
134347c6ae99SBarry Smith 
134447c6ae99SBarry Smith @*/
1345a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
134647c6ae99SBarry Smith {
134747c6ae99SBarry Smith   PetscFunctionBegin;
1348a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1349a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
13505a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
13517a8be351SBarry Smith   PetscCheck(dac->ops->createinjection,PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
13529566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0));
13539566063dSJacob Faibussowitsch   PetscCall((*dac->ops->createinjection)(dac,daf,mat));
13549566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0));
135547c6ae99SBarry Smith   PetscFunctionReturn(0);
135647c6ae99SBarry Smith }
135747c6ae99SBarry Smith 
1358b412c318SBarry Smith /*@
1359*bb7acecfSBarry 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
1360*bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1361bd041c0cSMatthew G. Knepley 
1362a5bc1bf3SBarry Smith   Collective on dac
1363bd041c0cSMatthew G. Knepley 
1364d8d19677SJose E. Roman   Input Parameters:
1365*bb7acecfSBarry Smith + dmc - the target `DM` object
1366*bb7acecfSBarry Smith - dmf - the source `DM` object
1367bd041c0cSMatthew G. Knepley 
1368bd041c0cSMatthew G. Knepley   Output Parameter:
1369b4937a87SMatthew G. Knepley . mat - the mass matrix
1370bd041c0cSMatthew G. Knepley 
1371bd041c0cSMatthew G. Knepley   Level: developer
1372bd041c0cSMatthew G. Knepley 
1373*bb7acecfSBarry Smith   Notes:
1374*bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1375*bb7acecfSBarry Smith 
1376*bb7acecfSBarry Smith   if dmc is dmf then x^t M x is an approximation to the L2 norm of the vector x which is obtained by `DMCreateGlobalVector()`
1377*bb7acecfSBarry Smith 
1378*bb7acecfSBarry Smith .seealso: `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1379bd041c0cSMatthew G. Knepley @*/
1380b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1381bd041c0cSMatthew G. Knepley {
1382bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1383b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1384b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13855a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
13862c71b3e2SJacob Faibussowitsch   PetscCheck(dmc->ops->createmassmatrix,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dmc)->type_name);
13875b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix,0,0,0,0));
13889566063dSJacob Faibussowitsch   PetscCall((*dmc->ops->createmassmatrix)(dmc, dmf, mat));
13895b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix,0,0,0,0));
1390b4937a87SMatthew G. Knepley   PetscFunctionReturn(0);
1391b4937a87SMatthew G. Knepley }
1392b4937a87SMatthew G. Knepley 
1393b4937a87SMatthew G. Knepley /*@
1394*bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1395b4937a87SMatthew G. Knepley 
1396b4937a87SMatthew G. Knepley   Collective on dm
1397b4937a87SMatthew G. Knepley 
1398b4937a87SMatthew G. Knepley   Input Parameter:
1399*bb7acecfSBarry Smith . dm - the `DM` object
1400b4937a87SMatthew G. Knepley 
1401b4937a87SMatthew G. Knepley   Output Parameter:
1402*bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1403b4937a87SMatthew G. Knepley 
1404b4937a87SMatthew G. Knepley   Level: developer
1405b4937a87SMatthew G. Knepley 
1406*bb7acecfSBarry Smith   Note:
1407*bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1408*bb7acecfSBarry Smith 
1409*bb7acecfSBarry Smith .seealso: `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1410b4937a87SMatthew G. Knepley @*/
1411b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1412b4937a87SMatthew G. Knepley {
1413b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1414b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1415b4937a87SMatthew G. Knepley   PetscValidPointer(lm,2);
14162c71b3e2SJacob Faibussowitsch   PetscCheck(dm->ops->createmassmatrixlumped,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrixLumped",((PetscObject)dm)->type_name);
14179566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createmassmatrixlumped)(dm, lm));
1418bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1419bd041c0cSMatthew G. Knepley }
1420bd041c0cSMatthew G. Knepley 
1421bd041c0cSMatthew G. Knepley /*@
1422*bb7acecfSBarry Smith     DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1423*bb7acecfSBarry Smith     of a PDE on the `DM`.
142447c6ae99SBarry Smith 
1425d083f849SBarry Smith     Collective on dm
142647c6ae99SBarry Smith 
1427d8d19677SJose E. Roman     Input Parameters:
1428*bb7acecfSBarry Smith +   dm - the `DM` object
1429*bb7acecfSBarry Smith -   ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
143047c6ae99SBarry Smith 
143147c6ae99SBarry Smith     Output Parameter:
143247c6ae99SBarry Smith .   coloring - the coloring
143347c6ae99SBarry Smith 
1434ec5066bdSBarry Smith     Notes:
1435*bb7acecfSBarry 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
1436*bb7acecfSBarry Smith     matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1437ec5066bdSBarry Smith 
1438*bb7acecfSBarry Smith     This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
1439ec5066bdSBarry Smith 
144047c6ae99SBarry Smith     Level: developer
144147c6ae99SBarry Smith 
1442*bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
144347c6ae99SBarry Smith 
1444aab9d709SJed Brown @*/
1445b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
144647c6ae99SBarry Smith {
144747c6ae99SBarry Smith   PetscFunctionBegin;
1448171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14495a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
14507a8be351SBarry Smith   PetscCheck(dm->ops->getcoloring,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
14519566063dSJacob Faibussowitsch   PetscCall((*dm->ops->getcoloring)(dm,ctype,coloring));
145247c6ae99SBarry Smith   PetscFunctionReturn(0);
145347c6ae99SBarry Smith }
145447c6ae99SBarry Smith 
1455b412c318SBarry Smith /*@
1456*bb7acecfSBarry Smith     DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
145747c6ae99SBarry Smith 
1458d083f849SBarry Smith     Collective on dm
145947c6ae99SBarry Smith 
146047c6ae99SBarry Smith     Input Parameter:
1461*bb7acecfSBarry Smith .   dm - the `DM` object
146247c6ae99SBarry Smith 
146347c6ae99SBarry Smith     Output Parameter:
146447c6ae99SBarry Smith .   mat - the empty Jacobian
146547c6ae99SBarry Smith 
1466073dac72SJed Brown     Level: beginner
146747c6ae99SBarry Smith 
1468f27dd7c6SMatthew G. Knepley     Options Database Keys:
1469*bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1470f27dd7c6SMatthew G. Knepley 
147195452b02SPatrick Sanan     Notes:
147295452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
147394013140SBarry Smith     do not need to do it yourself.
147494013140SBarry Smith 
147594013140SBarry Smith     By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1476*bb7acecfSBarry Smith     the nonzero pattern call `DMSetMatrixPreallocateOnly()`
147794013140SBarry Smith 
1478*bb7acecfSBarry Smith     For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
147994013140SBarry Smith     internally by PETSc.
148094013140SBarry Smith 
1481*bb7acecfSBarry Smith     For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1482*bb7acecfSBarry Smith     `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
148394013140SBarry Smith 
1484*bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
148547c6ae99SBarry Smith 
1486aab9d709SJed Brown @*/
1487b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
148847c6ae99SBarry Smith {
148947c6ae99SBarry Smith   PetscFunctionBegin;
1490171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1491064a246eSJacob Faibussowitsch   PetscValidPointer(mat,2);
14927a8be351SBarry Smith   PetscCheck(dm->ops->creatematrix,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
14939566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14949566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix,0,0,0,0));
14959566063dSJacob Faibussowitsch   PetscCall((*dm->ops->creatematrix)(dm,mat));
149676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1497c6b011d8SStefano Zampini     DM mdm;
1498c6b011d8SStefano Zampini 
14999566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat,&mdm));
15007a8be351SBarry Smith     PetscCheck(mdm,PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the matrix",((PetscObject)dm)->type_name);
1501c6b011d8SStefano Zampini   }
1502e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1503e5e52638SMatthew G. Knepley   if (dm->Nf) {
1504e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1505649ef022SMatthew Knepley     PetscInt     Nf, f;
1506e571a35bSMatthew G. Knepley 
15079566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1508649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1509649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
15109566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
15119566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
15129566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1513649ef022SMatthew Knepley         break;
1514e571a35bSMatthew G. Knepley       }
1515649ef022SMatthew Knepley     }
1516649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1517649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15189566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15199566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15209566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1521e571a35bSMatthew G. Knepley       }
1522e571a35bSMatthew G. Knepley     }
1523e571a35bSMatthew G. Knepley   }
15249566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix,0,0,0,0));
152547c6ae99SBarry Smith   PetscFunctionReturn(0);
152647c6ae99SBarry Smith }
152747c6ae99SBarry Smith 
1528732e2eb9SMatthew G Knepley /*@
1529*bb7acecfSBarry Smith   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and `ISLocalToGlobalMapping` will be
1530*bb7acecfSBarry Smith   properly set, but the data structures to store values in the matrices will not be preallocated. This is most useful to reduce initialization costs when
1531*bb7acecfSBarry Smith   `MatSetPreallocationCOO()` and `MatSetValuesCOO()` will be used.
1532aa0f6e3cSJed Brown 
1533aa0f6e3cSJed Brown   Logically Collective on dm
1534aa0f6e3cSJed Brown 
1535aa0f6e3cSJed Brown   Input Parameters:
1536*bb7acecfSBarry Smith + dm - the `DM`
1537*bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1538aa0f6e3cSJed Brown 
1539aa0f6e3cSJed Brown   Level: developer
1540aa0f6e3cSJed Brown 
1541*bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1542aa0f6e3cSJed Brown @*/
1543aa0f6e3cSJed Brown PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1544aa0f6e3cSJed Brown {
1545aa0f6e3cSJed Brown   PetscFunctionBegin;
1546aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1547aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
1548aa0f6e3cSJed Brown   PetscFunctionReturn(0);
1549aa0f6e3cSJed Brown }
1550aa0f6e3cSJed Brown 
1551aa0f6e3cSJed Brown /*@
1552*bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1553732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1554732e2eb9SMatthew G Knepley 
1555d083f849SBarry Smith   Logically Collective on dm
1556732e2eb9SMatthew G Knepley 
1557d8d19677SJose E. Roman   Input Parameters:
1558*bb7acecfSBarry Smith + dm - the `DM`
1559*bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1560732e2eb9SMatthew G Knepley 
1561732e2eb9SMatthew G Knepley   Level: developer
1562f27dd7c6SMatthew G. Knepley 
1563f27dd7c6SMatthew G. Knepley   Options Database Keys:
1564*bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1565f27dd7c6SMatthew G. Knepley 
1566*bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1567732e2eb9SMatthew G Knepley @*/
1568732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1569732e2eb9SMatthew G Knepley {
1570732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1571732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1572732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1573732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1574732e2eb9SMatthew G Knepley }
1575732e2eb9SMatthew G Knepley 
1576b06ff27eSHong Zhang /*@
1577*bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1578*bb7acecfSBarry Smith     but the array for numerical values will not be allocated.
1579b06ff27eSHong Zhang 
1580d083f849SBarry Smith   Logically Collective on dm
1581b06ff27eSHong Zhang 
1582d8d19677SJose E. Roman   Input Parameters:
1583*bb7acecfSBarry Smith + dm - the `DM`
1584*bb7acecfSBarry Smith - only - `PETSC_TRUE` if you only want matrix stucture
1585b06ff27eSHong Zhang 
1586b06ff27eSHong Zhang   Level: developer
1587*bb7acecfSBarry Smith 
1588*bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1589b06ff27eSHong Zhang @*/
1590b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1591b06ff27eSHong Zhang {
1592b06ff27eSHong Zhang   PetscFunctionBegin;
1593b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1594b06ff27eSHong Zhang   dm->structure_only = only;
1595b06ff27eSHong Zhang   PetscFunctionReturn(0);
1596b06ff27eSHong Zhang }
1597b06ff27eSHong Zhang 
1598a89ea682SMatthew G Knepley /*@C
1599*bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1600a89ea682SMatthew G Knepley 
1601a89ea682SMatthew G Knepley   Not Collective
1602a89ea682SMatthew G Knepley 
1603a89ea682SMatthew G Knepley   Input Parameters:
1604*bb7acecfSBarry Smith + dm - the `DM` object
1605a5b23f4aSJose E. Roman . count - The minimum size
1606*bb7acecfSBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, or MPIU_INT)
1607a89ea682SMatthew G Knepley 
1608a89ea682SMatthew G Knepley   Output Parameter:
1609a89ea682SMatthew G Knepley . array - the work array
1610a89ea682SMatthew G Knepley 
1611a89ea682SMatthew G Knepley   Level: developer
1612a89ea682SMatthew G Knepley 
1613*bb7acecfSBarry Smith   Note:
1614*bb7acecfSBarry Smith   A `DM` may stash the array between instantations so using this routine may be more efficient than calling `PetscMalloc()`
1615*bb7acecfSBarry Smith 
1616*bb7acecfSBarry Smith   The array may contain nonzero values
1617*bb7acecfSBarry Smith 
1618*bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1619a89ea682SMatthew G Knepley @*/
162069291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1621a89ea682SMatthew G Knepley {
1622aa1993deSMatthew G Knepley   DMWorkLink     link;
162369291d52SBarry Smith   PetscMPIInt    dsize;
1624a89ea682SMatthew G Knepley 
1625a89ea682SMatthew G Knepley   PetscFunctionBegin;
1626a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1627aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1628aa1993deSMatthew G Knepley   if (dm->workin) {
1629aa1993deSMatthew G Knepley     link       = dm->workin;
1630aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1631aa1993deSMatthew G Knepley   } else {
16329566063dSJacob Faibussowitsch     PetscCall(PetscNewLog(dm,&link));
1633a89ea682SMatthew G Knepley   }
16349566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dtype,&dsize));
16355056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
16369566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16379566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize*count,&link->mem));
1638854ce69bSBarry Smith     link->bytes = dsize*count;
1639aa1993deSMatthew G Knepley   }
1640aa1993deSMatthew G Knepley   link->next   = dm->workout;
1641aa1993deSMatthew G Knepley   dm->workout  = link;
1642cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
164300d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
164400d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
164500d952a4SJed Brown #endif
1646aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1647a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1648a89ea682SMatthew G Knepley }
1649a89ea682SMatthew G Knepley 
1650aa1993deSMatthew G Knepley /*@C
1651*bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1652aa1993deSMatthew G Knepley 
1653aa1993deSMatthew G Knepley   Not Collective
1654aa1993deSMatthew G Knepley 
1655aa1993deSMatthew G Knepley   Input Parameters:
1656*bb7acecfSBarry Smith + dm - the `DM` object
1657a5b23f4aSJose E. Roman . count - The minimum size
165869291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1659aa1993deSMatthew G Knepley 
1660aa1993deSMatthew G Knepley   Output Parameter:
1661aa1993deSMatthew G Knepley . array - the work array
1662aa1993deSMatthew G Knepley 
1663aa1993deSMatthew G Knepley   Level: developer
1664aa1993deSMatthew G Knepley 
166595452b02SPatrick Sanan   Developer Notes:
1666*bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1667147403d9SBarry Smith 
1668*bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1669aa1993deSMatthew G Knepley @*/
167069291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1671aa1993deSMatthew G Knepley {
1672aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1673aa1993deSMatthew G Knepley 
1674aa1993deSMatthew G Knepley   PetscFunctionBegin;
1675aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1676aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1677aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1678aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1679aa1993deSMatthew G Knepley       *p           = link->next;
1680aa1993deSMatthew G Knepley       link->next   = dm->workin;
1681aa1993deSMatthew G Knepley       dm->workin   = link;
16820298fd71SBarry Smith       *(void**)mem = NULL;
1683aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1684aa1993deSMatthew G Knepley     }
1685aa1993deSMatthew G Knepley   }
1686aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1687aa1993deSMatthew G Knepley }
1688e7c4fc90SDmitry Karpeev 
16898cda7954SMatthew G. Knepley /*@C
1690*bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1691*bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
16928cda7954SMatthew G. Knepley 
1693*bb7acecfSBarry Smith   Logically collective on dm
16948cda7954SMatthew G. Knepley 
16958cda7954SMatthew G. Knepley   Input Parameters:
1696*bb7acecfSBarry Smith + dm     - The `DM`
16978cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16988cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
16998cda7954SMatthew G. Knepley 
1700147403d9SBarry Smith   Calling sequence of nullsp:
1701147403d9SBarry Smith .vb
1702147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1703147403d9SBarry Smith .ve
1704*bb7acecfSBarry Smith +  dm        - The present `DM`
1705*bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1706147403d9SBarry Smith .  field     - The field number in dm
1707147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17088cda7954SMatthew G. Knepley 
170949762cbcSSatish Balay   Level: intermediate
171049762cbcSSatish Balay 
1711*bb7acecfSBarry Smith   Fortran Notes:
1712*bb7acecfSBarry Smith   This function is not available from Fortran.
1713*bb7acecfSBarry Smith 
1714*bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1715147403d9SBarry Smith @*/
1716147403d9SBarry Smith PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace*))
1717435a35e8SMatthew G Knepley {
1718435a35e8SMatthew G Knepley   PetscFunctionBegin;
1719435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17207a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1721435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1722435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1723435a35e8SMatthew G Knepley }
1724435a35e8SMatthew G Knepley 
17258cda7954SMatthew G. Knepley /*@C
1726*bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17278cda7954SMatthew G. Knepley 
17288cda7954SMatthew G. Knepley   Not collective
17298cda7954SMatthew G. Knepley 
17308cda7954SMatthew G. Knepley   Input Parameters:
1731*bb7acecfSBarry Smith + dm     - The `DM`
17328cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17338cda7954SMatthew G. Knepley 
17348cda7954SMatthew G. Knepley   Output Parameter:
17358cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17368cda7954SMatthew G. Knepley 
1737147403d9SBarry Smith   Calling sequence of nullsp:
1738147403d9SBarry Smith .vb
1739147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1740147403d9SBarry Smith .ve
1741147403d9SBarry Smith +  dm        - The present DM
1742147403d9SBarry Smith .  origField - The field number given above, in the original DM
1743147403d9SBarry Smith .  field     - The field number in dm
1744147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17458cda7954SMatthew G. Knepley 
1746*bb7acecfSBarry Smith   Fortran Note:
1747*bb7acecfSBarry Smith   This function is not available from Fortran.
17488cda7954SMatthew G. Knepley 
174949762cbcSSatish Balay    Level: intermediate
175049762cbcSSatish Balay 
1751*bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1752147403d9SBarry Smith @*/
1753147403d9SBarry Smith PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
17540a50eb56SMatthew G. Knepley {
17550a50eb56SMatthew G. Knepley   PetscFunctionBegin;
17560a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1757f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
17587a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
17590a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
17600a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
17610a50eb56SMatthew G. Knepley }
17620a50eb56SMatthew G. Knepley 
17638cda7954SMatthew G. Knepley /*@C
1764*bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
17658cda7954SMatthew G. Knepley 
1766*bb7acecfSBarry Smith   Logically collective on dm
17678cda7954SMatthew G. Knepley 
17688cda7954SMatthew G. Knepley   Input Parameters:
1769*bb7acecfSBarry Smith + dm     - The `DM`
17708cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17718cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
17728cda7954SMatthew G. Knepley 
1773147403d9SBarry Smith   Calling sequence of nullsp:
1774147403d9SBarry Smith .vb
1775147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1776147403d9SBarry Smith .ve
1777*bb7acecfSBarry Smith +  dm        - The present `DM`
1778*bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1779147403d9SBarry Smith .  field     - The field number in dm
1780147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17818cda7954SMatthew G. Knepley 
1782*bb7acecfSBarry Smith   Fortran Note:
1783*bb7acecfSBarry Smith   This function is not available from Fortran.
17848cda7954SMatthew G. Knepley 
178549762cbcSSatish Balay    Level: intermediate
178649762cbcSSatish Balay 
1787*bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1788*bb7acecfSBarry Smith           `MatNullSpace`
1789147403d9SBarry Smith @*/
1790147403d9SBarry Smith PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1791f9d4088aSMatthew G. Knepley {
1792f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1793f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17947a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1795f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1796f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1797f9d4088aSMatthew G. Knepley }
1798f9d4088aSMatthew G. Knepley 
17998cda7954SMatthew G. Knepley /*@C
1800*bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18018cda7954SMatthew G. Knepley 
18028cda7954SMatthew G. Knepley   Not collective
18038cda7954SMatthew G. Knepley 
18048cda7954SMatthew G. Knepley   Input Parameters:
1805*bb7acecfSBarry Smith + dm     - The `DM`
18068cda7954SMatthew G. Knepley - field  - The field number for the nullspace
18078cda7954SMatthew G. Knepley 
18088cda7954SMatthew G. Knepley   Output Parameter:
18098cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18108cda7954SMatthew G. Knepley 
1811147403d9SBarry Smith   Calling sequence of nullsp:
1812147403d9SBarry Smith .vb
1813147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1814147403d9SBarry Smith .ve
1815*bb7acecfSBarry Smith +  dm        - The present `DM`
1816*bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1817147403d9SBarry Smith .  field     - The field number in dm
1818147403d9SBarry Smith -  nullSpace - The nullspace for the given field
18198cda7954SMatthew G. Knepley 
1820*bb7acecfSBarry Smith   Fortran Note:
1821*bb7acecfSBarry Smith   This function is not available from Fortran.
18228cda7954SMatthew G. Knepley 
182349762cbcSSatish Balay    Level: intermediate
182449762cbcSSatish Balay 
1825*bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1826*bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1827147403d9SBarry Smith @*/
1828147403d9SBarry Smith PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1829f9d4088aSMatthew G. Knepley {
1830f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1831f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1832f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
18337a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1834f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1835f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1836f9d4088aSMatthew G. Knepley }
1837f9d4088aSMatthew G. Knepley 
18384f3b5142SJed Brown /*@C
1839*bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18404d343eeaSMatthew G Knepley 
18414d343eeaSMatthew G Knepley   Not collective
18424d343eeaSMatthew G Knepley 
18434d343eeaSMatthew G Knepley   Input Parameter:
1844*bb7acecfSBarry Smith . dm - the `DM` object
18454d343eeaSMatthew G Knepley 
18464d343eeaSMatthew G Knepley   Output Parameters:
18470298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
1848*bb7acecfSBarry Smith . fieldNames - The number of each field (or NULL if not requested)
18490298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
18504d343eeaSMatthew G Knepley 
18514d343eeaSMatthew G Knepley   Level: intermediate
18524d343eeaSMatthew G Knepley 
1853*bb7acecfSBarry Smith   Note:
185421c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1855*bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1856*bb7acecfSBarry Smith   `PetscFree()`.
185721c9b008SJed Brown 
1858*bb7acecfSBarry Smith   Fortran Note:
1859*bb7acecfSBarry Smith   Not available in Fortran.
1860*bb7acecfSBarry Smith 
1861*bb7acecfSBarry Smith   Developer Note:
1862*bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1863*bb7acecfSBarry Smith   likely be removed.
1864*bb7acecfSBarry Smith 
1865*bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1866*bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18674d343eeaSMatthew G Knepley @*/
186837d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
18694d343eeaSMatthew G Knepley {
187037d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
18714d343eeaSMatthew G Knepley 
18724d343eeaSMatthew G Knepley   PetscFunctionBegin;
18734d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
187469ca1f37SDmitry Karpeev   if (numFields) {
1875534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
187669ca1f37SDmitry Karpeev     *numFields = 0;
187769ca1f37SDmitry Karpeev   }
187837d0c07bSMatthew G Knepley   if (fieldNames) {
187937d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
18800298fd71SBarry Smith     *fieldNames = NULL;
188169ca1f37SDmitry Karpeev   }
188269ca1f37SDmitry Karpeev   if (fields) {
188369ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
18840298fd71SBarry Smith     *fields = NULL;
188569ca1f37SDmitry Karpeev   }
18869566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
188737d0c07bSMatthew G Knepley   if (section) {
18883a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
188937d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
189037d0c07bSMatthew G Knepley 
18919566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
18929566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
18939566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices));
18949566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
189537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
189637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
18979566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
189837d0c07bSMatthew G Knepley     }
189937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
190037d0c07bSMatthew G Knepley       PetscInt gdof;
190137d0c07bSMatthew G Knepley 
19029566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
190337d0c07bSMatthew G Knepley       if (gdof > 0) {
190437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19053a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
190637d0c07bSMatthew G Knepley 
19079566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19089566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19093a544194SStefano Zampini           fpdof = fdof-fcdof;
19103a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19113a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19123a544194SStefano Zampini             fieldNc[f] = 1;
19133a544194SStefano Zampini           }
19143a544194SStefano Zampini           fieldSizes[f] += fpdof;
191537d0c07bSMatthew G Knepley         }
191637d0c07bSMatthew G Knepley       }
191737d0c07bSMatthew G Knepley     }
191837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19199566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
192037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
192137d0c07bSMatthew G Knepley     }
192237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
192337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
192437d0c07bSMatthew G Knepley 
19259566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
192637d0c07bSMatthew G Knepley       if (gdof > 0) {
19279566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
192837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
192937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
193037d0c07bSMatthew G Knepley 
19319566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19329566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
193337d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
193437d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
193537d0c07bSMatthew G Knepley           }
193637d0c07bSMatthew G Knepley         }
193737d0c07bSMatthew G Knepley       }
193837d0c07bSMatthew G Knepley     }
19398865f1eaSKarl Rupp     if (numFields) *numFields = nF;
194037d0c07bSMatthew G Knepley     if (fieldNames) {
19419566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
194237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
194337d0c07bSMatthew G Knepley         const char *fieldName;
194437d0c07bSMatthew G Knepley 
19459566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19469566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]));
194737d0c07bSMatthew G Knepley       }
194837d0c07bSMatthew G Knepley     }
194937d0c07bSMatthew G Knepley     if (fields) {
19509566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
195137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19523a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19533a544194SStefano Zampini 
19549566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19553a544194SStefano Zampini         in[0] = -fieldNc[f];
19563a544194SStefano Zampini         in[1] = fieldNc[f];
19571c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19583a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
19599566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
196037d0c07bSMatthew G Knepley       }
196137d0c07bSMatthew G Knepley     }
19629566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes,fieldNc,fieldIndices));
19631baa6e33SBarry Smith   } else if (dm->ops->createfieldis) PetscCall((*dm->ops->createfieldis)(dm, numFields, fieldNames, fields));
19644d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
19654d343eeaSMatthew G Knepley }
19664d343eeaSMatthew G Knepley 
196716621825SDmitry Karpeev /*@C
1968*bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1969*bb7acecfSBarry Smith                           corresponding to different fields: each `IS` contains the global indices of the dofs of the
1970*bb7acecfSBarry Smith                           corresponding field, defined by `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
1971*bb7acecfSBarry Smith                           The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
1972e7c4fc90SDmitry Karpeev 
1973e7c4fc90SDmitry Karpeev   Not collective
1974e7c4fc90SDmitry Karpeev 
1975e7c4fc90SDmitry Karpeev   Input Parameter:
1976*bb7acecfSBarry Smith . dm - the `DM` object
1977e7c4fc90SDmitry Karpeev 
1978e7c4fc90SDmitry Karpeev   Output Parameters:
1979*bb7acecfSBarry Smith + len       - The number of fields (or NULL if not requested)
19800298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
19810298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
1982*bb7acecfSBarry Smith - dmlist    - The `DM`s for each field subproblem (or NULL, if not requested; if NULL is returned, no `DM`s are defined)
1983e7c4fc90SDmitry Karpeev 
1984e7c4fc90SDmitry Karpeev   Level: intermediate
1985e7c4fc90SDmitry Karpeev 
1986*bb7acecfSBarry Smith   Note:
1987e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1988*bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
1989*bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
1990e7c4fc90SDmitry Karpeev 
1991*bb7acecfSBarry Smith   Fortran Note:
1992*bb7acecfSBarry Smith   Not available in Fortran.
1993*bb7acecfSBarry Smith 
1994*bb7acecfSBarry Smith   Developer Note:
1995*bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
1996*bb7acecfSBarry Smith 
1997*bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
1998e7c4fc90SDmitry Karpeev @*/
199916621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2000e7c4fc90SDmitry Karpeev {
2001e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2002e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20038865f1eaSKarl Rupp   if (len) {
2004534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
20058865f1eaSKarl Rupp     *len = 0;
20068865f1eaSKarl Rupp   }
20078865f1eaSKarl Rupp   if (namelist) {
20088865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
2009ea78f98cSLisandro Dalcin     *namelist = NULL;
20108865f1eaSKarl Rupp   }
20118865f1eaSKarl Rupp   if (islist) {
20128865f1eaSKarl Rupp     PetscValidPointer(islist,4);
2013ea78f98cSLisandro Dalcin     *islist = NULL;
20148865f1eaSKarl Rupp   }
20158865f1eaSKarl Rupp   if (dmlist) {
20168865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
2017ea78f98cSLisandro Dalcin     *dmlist = NULL;
20188865f1eaSKarl Rupp   }
2019f3f0edfdSDmitry Karpeev   /*
2020f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2021f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2022f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2023f3f0edfdSDmitry Karpeev    */
20247a8be351SBarry Smith   PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
202516621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2026435a35e8SMatthew G Knepley     PetscSection section;
2027435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2028435a35e8SMatthew G Knepley 
20299566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20309566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2031435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2032f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20339566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields,namelist));
20349566063dSJacob Faibussowitsch       if (islist)   PetscCall(PetscMalloc1(numFields,islist));
20359566063dSJacob Faibussowitsch       if (dmlist)   PetscCall(PetscMalloc1(numFields,dmlist));
2036435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2037435a35e8SMatthew G Knepley         const char *fieldName;
2038435a35e8SMatthew G Knepley 
20399566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
204003dc3394SMatthew G. Knepley         if (namelist) {
20419566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20429566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char**) &(*namelist)[f]));
2043435a35e8SMatthew G Knepley         }
204403dc3394SMatthew G. Knepley       }
2045435a35e8SMatthew G Knepley     } else {
20469566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2047e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20480298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2049e7c4fc90SDmitry Karpeev     }
20508865f1eaSKarl Rupp   } else {
20519566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist));
205216621825SDmitry Karpeev   }
205316621825SDmitry Karpeev   PetscFunctionReturn(0);
205416621825SDmitry Karpeev }
205516621825SDmitry Karpeev 
2056564cec59SMatthew G. Knepley /*@
2057*bb7acecfSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by a selected number of fields, that were defined by `DMAddField()`
2058435a35e8SMatthew G Knepley 
2059435a35e8SMatthew G Knepley   Not collective
2060435a35e8SMatthew G Knepley 
2061435a35e8SMatthew G Knepley   Input Parameters:
2062*bb7acecfSBarry Smith + dm        - The `DM `object
2063*bb7acecfSBarry Smith . numFields - The number of fields to select
20642adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2065435a35e8SMatthew G Knepley 
2066435a35e8SMatthew G Knepley   Output Parameters:
2067*bb7acecfSBarry Smith + is - The global indices for all the degrees of freedom in the new sub `DM`
2068*bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2069435a35e8SMatthew G Knepley 
2070*bb7acecfSBarry Smith   Note:
2071*bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20725d3b26e6SMatthew G. Knepley 
2073435a35e8SMatthew G Knepley   Level: intermediate
2074435a35e8SMatthew G Knepley 
2075*bb7acecfSBarry Smith .seealso: `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `DM`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2076435a35e8SMatthew G Knepley @*/
207737bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2078435a35e8SMatthew G Knepley {
2079435a35e8SMatthew G Knepley   PetscFunctionBegin;
2080435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2081dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields,3);
20828865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
20838865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
20847a8be351SBarry Smith   PetscCheck(dm->ops->createsubdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
20859566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createsubdm)(dm, numFields, fields, is, subdm));
2086435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
2087435a35e8SMatthew G Knepley }
2088435a35e8SMatthew G Knepley 
20892adcc780SMatthew G. Knepley /*@C
2090*bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
20912adcc780SMatthew G. Knepley 
20922adcc780SMatthew G. Knepley   Not collective
20932adcc780SMatthew G. Knepley 
2094d8d19677SJose E. Roman   Input Parameters:
2095*bb7acecfSBarry Smith + dms - The `DM` objects
2096*bb7acecfSBarry Smith - n - The number of `DM`s
20972adcc780SMatthew G. Knepley 
20982adcc780SMatthew G. Knepley   Output Parameters:
2099*bb7acecfSBarry Smith + is - The global indices for each of subproblem within the super `DM`, or NULL
2100*bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21012adcc780SMatthew G. Knepley 
2102*bb7acecfSBarry Smith   Note:
2103*bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21045d3b26e6SMatthew G. Knepley 
21052adcc780SMatthew G. Knepley   Level: intermediate
21062adcc780SMatthew G. Knepley 
2107*bb7acecfSBarry Smith .seealso: `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
21082adcc780SMatthew G. Knepley @*/
2109*bb7acecfSBarry Smith PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
21102adcc780SMatthew G. Knepley {
21112adcc780SMatthew G. Knepley   PetscInt       i;
21122adcc780SMatthew G. Knepley 
21132adcc780SMatthew G. Knepley   PetscFunctionBegin;
21142adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
2115*bb7acecfSBarry Smith   for (i = 0; i < n; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
21162adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
2117a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
2118*bb7acecfSBarry Smith   PetscCheck(n >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2119*bb7acecfSBarry Smith   if (n) {
2120b9d85ea2SLisandro Dalcin     DM dm = dms[0];
21217a8be351SBarry Smith     PetscCheck(dm->ops->createsuperdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
2122*bb7acecfSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21232adcc780SMatthew G. Knepley   }
21242adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
21252adcc780SMatthew G. Knepley }
21262adcc780SMatthew G. Knepley 
212716621825SDmitry Karpeev /*@C
2128*bb7acecfSBarry Smith   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a problem into subproblems
2129*bb7acecfSBarry Smith                           corresponding to restrictions to pairs of nested subdomains: each `IS` contains the global
2130*bb7acecfSBarry Smith                           indices of the dofs of the corresponding subdomains with in the dofs of the original `DM`.
2131*bb7acecfSBarry Smith                           The inner subdomains conceptually define a nonoverlapping covering, while outer subdomains can overlap.
2132*bb7acecfSBarry Smith                           The optional list of `DM`s define a `DM` for each subproblem.
213316621825SDmitry Karpeev 
213416621825SDmitry Karpeev   Not collective
213516621825SDmitry Karpeev 
213616621825SDmitry Karpeev   Input Parameter:
2137*bb7acecfSBarry Smith . dm - the `DM` object
213816621825SDmitry Karpeev 
213916621825SDmitry Karpeev   Output Parameters:
2140*bb7acecfSBarry Smith + n            - The number of subproblems in the domain decomposition (or NULL if not requested)
21410298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
21420298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
21430298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
2144*bb7acecfSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no `DM`s are defined)
214516621825SDmitry Karpeev 
214616621825SDmitry Karpeev   Level: intermediate
214716621825SDmitry Karpeev 
2148*bb7acecfSBarry Smith   Note:
214916621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2150*bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2151*bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
215216621825SDmitry Karpeev 
2153*bb7acecfSBarry Smith   Questions:
2154*bb7acecfSBarry Smith   The dmlist is for the inner subdomains or the outer subdomains or all subdomains?
2155*bb7acecfSBarry Smith 
2156*bb7acecfSBarry Smith .seealso: `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldDecomposition()`
215716621825SDmitry Karpeev @*/
2158*bb7acecfSBarry Smith PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
215916621825SDmitry Karpeev {
2160be081cd6SPeter Brune   DMSubDomainHookLink link;
2161be081cd6SPeter Brune   PetscInt            i,l;
216216621825SDmitry Karpeev 
216316621825SDmitry Karpeev   PetscFunctionBegin;
216416621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2165*bb7acecfSBarry Smith   if (n)             {PetscValidIntPointer(n,2);           *n           = 0;}
21660298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
21670298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
21680298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
21690298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
2170f3f0edfdSDmitry Karpeev   /*
2171f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2172f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2173f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2174f3f0edfdSDmitry Karpeev    */
21757a8be351SBarry Smith   PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
217616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
21779566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist));
217814a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2179f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2180be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2181be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
21829566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm,(*dmlist)[i],link->ctx));
2183be081cd6SPeter Brune         }
2184648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2185e7c4fc90SDmitry Karpeev       }
218614a18fd3SPeter Brune     }
2187*bb7acecfSBarry Smith     if (n) *n = l;
218814a18fd3SPeter Brune   }
2189e30e807fSPeter Brune   PetscFunctionReturn(0);
2190e30e807fSPeter Brune }
2191e30e807fSPeter Brune 
2192e30e807fSPeter Brune /*@C
2193e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2194e30e807fSPeter Brune 
2195e30e807fSPeter Brune   Not collective
2196e30e807fSPeter Brune 
2197e30e807fSPeter Brune   Input Parameters:
2198*bb7acecfSBarry Smith + dm - the `DM` object
2199e30e807fSPeter Brune . n  - the number of subdomain scatters
2200e30e807fSPeter Brune - subdms - the local subdomains
2201e30e807fSPeter Brune 
2202e30e807fSPeter Brune   Output Parameters:
22036b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2204e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2205e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2206e30e807fSPeter Brune 
2207*bb7acecfSBarry Smith   Note:
2208*bb7acecfSBarry Smith     This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2209e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2210e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2211e30e807fSPeter Brune   solution and residual data.
2212e30e807fSPeter Brune 
2213*bb7acecfSBarry Smith   Questions:
2214*bb7acecfSBarry Smith   Can the subdms input be anything or are they exactly the `DM` obtained from `DMCreateDomainDecomposition()`?
2215*bb7acecfSBarry Smith 
2216e30e807fSPeter Brune   Level: developer
2217e30e807fSPeter Brune 
2218*bb7acecfSBarry Smith .seealso: `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2219e30e807fSPeter Brune @*/
2220e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2221e30e807fSPeter Brune {
2222e30e807fSPeter Brune   PetscFunctionBegin;
2223e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2224e30e807fSPeter Brune   PetscValidPointer(subdms,3);
22257a8be351SBarry Smith   PetscCheck(dm->ops->createddscatters,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
22269566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat));
2227e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2228e7c4fc90SDmitry Karpeev }
2229e7c4fc90SDmitry Karpeev 
223047c6ae99SBarry Smith /*@
2231*bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
223247c6ae99SBarry Smith 
2233d083f849SBarry Smith   Collective on dm
223447c6ae99SBarry Smith 
2235d8d19677SJose E. Roman   Input Parameters:
2236*bb7acecfSBarry Smith + dm   - the `DM` object
2237*bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
223847c6ae99SBarry Smith 
223947c6ae99SBarry Smith   Output Parameter:
2240*bb7acecfSBarry Smith . dmf - the refined `D`M, or NULL
2241ae0a1c52SMatthew G Knepley 
2242f27dd7c6SMatthew G. Knepley   Options Database Keys:
2243412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2244412e9a14SMatthew G. Knepley 
2245*bb7acecfSBarry Smith   Note:
2246*bb7acecfSBarry Smith   If no refinement was done, the return value is NULL
224747c6ae99SBarry Smith 
224847c6ae99SBarry Smith   Level: developer
224947c6ae99SBarry Smith 
2250*bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
225147c6ae99SBarry Smith @*/
22527087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
225347c6ae99SBarry Smith {
2254c833c3b5SJed Brown   DMRefineHookLink link;
225547c6ae99SBarry Smith 
225647c6ae99SBarry Smith   PetscFunctionBegin;
2257732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22587a8be351SBarry Smith   PetscCheck(dm->ops->refine,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
22599566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine,dm,0,0,0));
22609566063dSJacob Faibussowitsch   PetscCall((*dm->ops->refine)(dm,comm,dmf));
22614057135bSMatthew G Knepley   if (*dmf) {
226243842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
22638865f1eaSKarl Rupp 
22649566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf));
22658865f1eaSKarl Rupp 
2266644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
22670598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2268656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
22698865f1eaSKarl Rupp 
22709566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf,dm->mattype));
2271c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
22721baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm,*dmf,link->ctx));
2273c833c3b5SJed Brown     }
2274c833c3b5SJed Brown   }
22759566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine,dm,0,0,0));
2276c833c3b5SJed Brown   PetscFunctionReturn(0);
2277c833c3b5SJed Brown }
2278c833c3b5SJed Brown 
2279bb9467b5SJed Brown /*@C
2280c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2281c833c3b5SJed Brown 
2282*bb7acecfSBarry Smith    Logically Collective on coarse
2283c833c3b5SJed Brown 
22844165533cSJose E. Roman    Input Parameters:
2285*bb7acecfSBarry Smith +  coarse - `DM` on which to run a hook when interpolating to a finer level
2286*bb7acecfSBarry Smith .  refinehook - function to run when setting up the finer level
2287*bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels (once per `SNESSolve`())
22880298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2289c833c3b5SJed Brown 
2290c833c3b5SJed Brown    Calling sequence of refinehook:
2291c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2292c833c3b5SJed Brown 
2293*bb7acecfSBarry Smith +  coarse - coarse level `DM`
2294*bb7acecfSBarry Smith .  fine - fine level `DM` to interpolate problem to
2295c833c3b5SJed Brown -  ctx - optional user-defined function context
2296c833c3b5SJed Brown 
2297c833c3b5SJed Brown    Calling sequence for interphook:
2298c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2299c833c3b5SJed Brown 
2300*bb7acecfSBarry Smith +  coarse - coarse level `DM`
2301c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2302*bb7acecfSBarry Smith .  fine - fine level `DM` to update
2303c833c3b5SJed Brown -  ctx - optional user-defined function context
2304c833c3b5SJed Brown 
2305c833c3b5SJed Brown    Level: advanced
2306c833c3b5SJed Brown 
2307c833c3b5SJed Brown    Notes:
2308*bb7acecfSBarry Smith    This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2309*bb7acecfSBarry Smith    passed to fine grids while grid sequencing.
2310*bb7acecfSBarry Smith 
2311*bb7acecfSBarry Smith    The actual interpolation is done when `DMInterpolate()` is called.
2312c833c3b5SJed Brown 
2313c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2314c833c3b5SJed Brown 
2315*bb7acecfSBarry Smith    Fortran Note:
2316*bb7acecfSBarry Smith    This function is not available from Fortran.
2317bb9467b5SJed Brown 
2318*bb7acecfSBarry Smith .seealso: `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2319c833c3b5SJed Brown @*/
2320c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2321c833c3b5SJed Brown {
2322c833c3b5SJed Brown   DMRefineHookLink link,*p;
2323c833c3b5SJed Brown 
2324c833c3b5SJed Brown   PetscFunctionBegin;
2325c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
23263d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
23273d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
23283d8e3701SJed Brown   }
23299566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2330c833c3b5SJed Brown   link->refinehook = refinehook;
2331c833c3b5SJed Brown   link->interphook = interphook;
2332c833c3b5SJed Brown   link->ctx        = ctx;
23330298fd71SBarry Smith   link->next       = NULL;
2334c833c3b5SJed Brown   *p               = link;
2335c833c3b5SJed Brown   PetscFunctionReturn(0);
2336c833c3b5SJed Brown }
2337c833c3b5SJed Brown 
23383d8e3701SJed Brown /*@C
2339*bb7acecfSBarry Smith    DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2340*bb7acecfSBarry Smith     a nonlinear problem to a finer grid
23413d8e3701SJed Brown 
2342*bb7acecfSBarry Smith    Logically Collective on coarse
23433d8e3701SJed Brown 
23444165533cSJose E. Roman    Input Parameters:
2345*bb7acecfSBarry Smith +  coarse - the `DM` on which to run a hook when restricting to a coarser level
2346*bb7acecfSBarry Smith .  refinehook - function to run when setting up a finer level
2347*bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels
23483d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
23493d8e3701SJed Brown 
23503d8e3701SJed Brown    Level: advanced
23513d8e3701SJed Brown 
2352*bb7acecfSBarry Smith    Note:
23533d8e3701SJed Brown    This function does nothing if the hook is not in the list.
23543d8e3701SJed Brown 
2355*bb7acecfSBarry Smith    Fortran Note:
2356*bb7acecfSBarry Smith    This function is not available from Fortran.
23573d8e3701SJed Brown 
2358*bb7acecfSBarry Smith .seealso: `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
23593d8e3701SJed Brown @*/
23603d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
23613d8e3701SJed Brown {
23623d8e3701SJed Brown   DMRefineHookLink link,*p;
23633d8e3701SJed Brown 
23643d8e3701SJed Brown   PetscFunctionBegin;
23653d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
23663d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
23673d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
23683d8e3701SJed Brown       link = *p;
23693d8e3701SJed Brown       *p = link->next;
23709566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
23713d8e3701SJed Brown       break;
23723d8e3701SJed Brown     }
23733d8e3701SJed Brown   }
23743d8e3701SJed Brown   PetscFunctionReturn(0);
23753d8e3701SJed Brown }
23763d8e3701SJed Brown 
2377c833c3b5SJed Brown /*@
2378*bb7acecfSBarry Smith    DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2379c833c3b5SJed Brown 
2380c833c3b5SJed Brown    Collective if any hooks are
2381c833c3b5SJed Brown 
23824165533cSJose E. Roman    Input Parameters:
2383*bb7acecfSBarry Smith +  coarse - coarser `DM` to use as a base
2384*bb7acecfSBarry Smith .  interp - interpolation matrix, apply using `MatInterpolate()`
2385*bb7acecfSBarry Smith -  fine - finer `DM` to update
2386c833c3b5SJed Brown 
2387c833c3b5SJed Brown    Level: developer
2388c833c3b5SJed Brown 
2389*bb7acecfSBarry Smith    Developer Note:
2390*bb7acecfSBarry Smith    This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2391*bb7acecfSBarry Smith    an API with consistent terminology.
2392*bb7acecfSBarry Smith 
2393*bb7acecfSBarry Smith .seealso: `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2394c833c3b5SJed Brown @*/
2395c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2396c833c3b5SJed Brown {
2397c833c3b5SJed Brown   DMRefineHookLink link;
2398c833c3b5SJed Brown 
2399c833c3b5SJed Brown   PetscFunctionBegin;
2400c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
24011baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse,interp,fine,link->ctx));
24024057135bSMatthew G Knepley   }
240347c6ae99SBarry Smith   PetscFunctionReturn(0);
240447c6ae99SBarry Smith }
240547c6ae99SBarry Smith 
2406eb3f98d2SBarry Smith /*@
24071f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24081f3379b2SToby Isaac 
2409*bb7acecfSBarry Smith    Collective on dm
24101f3379b2SToby Isaac 
24114165533cSJose E. Roman    Input Parameters:
2412*bb7acecfSBarry Smith +  coarse - coarse `DM`
2413*bb7acecfSBarry Smith .  fine   - fine `DM`
2414*bb7acecfSBarry Smith .  interp - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2415*bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2416*bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24171f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
24181f3379b2SToby Isaac 
24194165533cSJose E. Roman    Output Parameter:
24201f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
24211f3379b2SToby Isaac 
24221f3379b2SToby Isaac    Level: developer
24231f3379b2SToby Isaac 
2424*bb7acecfSBarry Smith    Note:
2425*bb7acecfSBarry Smith    This function exists because the interpolation of a solution vector between meshes is not always a linear
24261f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24271f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24281f3379b2SToby Isaac    slope-limiting reconstruction.
24291f3379b2SToby Isaac 
2430*bb7acecfSBarry Smith    Developer Note:
2431*bb7acecfSBarry Smith    This doesn't just interpolate "solutions" so its API name is questionable.
2432*bb7acecfSBarry Smith 
2433*bb7acecfSBarry Smith .seealso: `DMInterpolate()`, `DMCreateInterpolation()`
24341f3379b2SToby Isaac @*/
24351f3379b2SToby Isaac PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
24361f3379b2SToby Isaac {
24371f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM,DM,Mat,Vec,Vec) = NULL;
24381f3379b2SToby Isaac 
24391f3379b2SToby Isaac   PetscFunctionBegin;
24401f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
24411f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp,MAT_CLASSID,3);
24421f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol,VEC_CLASSID,4);
24431f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol,VEC_CLASSID,5);
24441f3379b2SToby Isaac 
24459566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse,"DMInterpolateSolution_C", &interpsol));
24461f3379b2SToby Isaac   if (interpsol) {
24479566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24481f3379b2SToby Isaac   } else if (interp) {
24499566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
245098921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24511f3379b2SToby Isaac   PetscFunctionReturn(0);
24521f3379b2SToby Isaac }
24531f3379b2SToby Isaac 
24541f3379b2SToby Isaac /*@
2455*bb7acecfSBarry Smith     DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2456eb3f98d2SBarry Smith 
2457eb3f98d2SBarry Smith     Not Collective
2458eb3f98d2SBarry Smith 
2459eb3f98d2SBarry Smith     Input Parameter:
2460*bb7acecfSBarry Smith .   dm - the `DM` object
2461eb3f98d2SBarry Smith 
2462eb3f98d2SBarry Smith     Output Parameter:
2463eb3f98d2SBarry Smith .   level - number of refinements
2464eb3f98d2SBarry Smith 
2465eb3f98d2SBarry Smith     Level: developer
2466eb3f98d2SBarry Smith 
2467*bb7acecfSBarry Smith     Note:
2468*bb7acecfSBarry Smith     This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2469*bb7acecfSBarry Smith 
2470*bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2471eb3f98d2SBarry Smith 
2472eb3f98d2SBarry Smith @*/
2473eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2474eb3f98d2SBarry Smith {
2475eb3f98d2SBarry Smith   PetscFunctionBegin;
2476eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2477eb3f98d2SBarry Smith   *level = dm->levelup;
2478eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2479eb3f98d2SBarry Smith }
2480eb3f98d2SBarry Smith 
2481fef3a512SBarry Smith /*@
2482*bb7acecfSBarry Smith     DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2483fef3a512SBarry Smith 
2484fef3a512SBarry Smith     Not Collective
2485fef3a512SBarry Smith 
2486d8d19677SJose E. Roman     Input Parameters:
2487*bb7acecfSBarry Smith +   dm - the `DM` object
2488fef3a512SBarry Smith -   level - number of refinements
2489fef3a512SBarry Smith 
2490fef3a512SBarry Smith     Level: advanced
2491fef3a512SBarry Smith 
249295452b02SPatrick Sanan     Notes:
2493*bb7acecfSBarry Smith     This value is used by `PCMG` to determine how many multigrid levels to use
2494fef3a512SBarry Smith 
2495*bb7acecfSBarry Smith     The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2496*bb7acecfSBarry Smith 
2497*bb7acecfSBarry Smith .seealso: `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2498fef3a512SBarry Smith 
2499fef3a512SBarry Smith @*/
2500fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2501fef3a512SBarry Smith {
2502fef3a512SBarry Smith   PetscFunctionBegin;
2503fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2504fef3a512SBarry Smith   dm->levelup = level;
2505fef3a512SBarry Smith   PetscFunctionReturn(0);
2506fef3a512SBarry Smith }
2507fef3a512SBarry Smith 
2508d410b0cfSMatthew G. Knepley /*@
2509*bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2510d410b0cfSMatthew G. Knepley 
2511d410b0cfSMatthew G. Knepley   Collective on dm
2512d410b0cfSMatthew G. Knepley 
2513f1a722f8SMatthew G. Knepley   Input Parameters:
2514*bb7acecfSBarry Smith + dm     - the `DM` object
2515d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2516d410b0cfSMatthew G. Knepley 
2517d410b0cfSMatthew G. Knepley   Output Parameter:
2518*bb7acecfSBarry Smith . dme - the extruded `DM`, or NULL
2519d410b0cfSMatthew G. Knepley 
2520*bb7acecfSBarry Smith   Note:
2521*bb7acecfSBarry Smith   If no extrusion was done, the return value is NULL
2522d410b0cfSMatthew G. Knepley 
2523d410b0cfSMatthew G. Knepley   Level: developer
2524d410b0cfSMatthew G. Knepley 
2525*bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2526d410b0cfSMatthew G. Knepley @*/
2527d410b0cfSMatthew G. Knepley PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2528d410b0cfSMatthew G. Knepley {
2529d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2530d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25317a8be351SBarry Smith   PetscCheck(dm->ops->extrude,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "DM type %s does not implement DMExtrude", ((PetscObject) dm)->type_name);
25329566063dSJacob Faibussowitsch   PetscCall((*dm->ops->extrude)(dm, layers, dme));
2533d410b0cfSMatthew G. Knepley   if (*dme) {
2534d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25359566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject) dm, (PetscObject) *dme));
2536d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25379566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2538d410b0cfSMatthew G. Knepley   }
2539d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2540d410b0cfSMatthew G. Knepley }
2541d410b0cfSMatthew G. Knepley 
2542ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2543ca3d3a14SMatthew G. Knepley {
2544ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2545ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2546ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2547ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2548ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2549ca3d3a14SMatthew G. Knepley }
2550ca3d3a14SMatthew G. Knepley 
2551ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2552ca3d3a14SMatthew G. Knepley {
2553ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2554ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2555ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2556ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2557ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2558ca3d3a14SMatthew G. Knepley }
2559ca3d3a14SMatthew G. Knepley 
2560ca3d3a14SMatthew G. Knepley /*@
2561*bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2562ca3d3a14SMatthew G. Knepley 
2563ca3d3a14SMatthew G. Knepley   Input Parameter:
2564ca3d3a14SMatthew G. Knepley . dm - The DM
2565ca3d3a14SMatthew G. Knepley 
2566ca3d3a14SMatthew G. Knepley   Output Parameter:
2567ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2568ca3d3a14SMatthew G. Knepley 
2569ca3d3a14SMatthew G. Knepley   Level: developer
2570ca3d3a14SMatthew G. Knepley 
2571*bb7acecfSBarry Smith .seealso: `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2572ca3d3a14SMatthew G. Knepley @*/
2573ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2574ca3d3a14SMatthew G. Knepley {
2575ca3d3a14SMatthew G. Knepley   Vec            tv;
2576ca3d3a14SMatthew G. Knepley 
2577ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2578ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2579534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
25809566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2581ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2582ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2583ca3d3a14SMatthew G. Knepley }
2584ca3d3a14SMatthew G. Knepley 
2585ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2586ca3d3a14SMatthew G. Knepley {
2587ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2588ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2589ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2590ca3d3a14SMatthew G. Knepley 
2591ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
25929566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
25939566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
25949566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
25959566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
25969566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
25979566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
25989566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
25999566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2600ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26019566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2602ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2603ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2604ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26059566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2606ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26079566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26089566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2609ca3d3a14SMatthew G. Knepley     }
2610ca3d3a14SMatthew G. Knepley   }
26119566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26129566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26139566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2614ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2615ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26169566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2617ca3d3a14SMatthew G. Knepley       if (dof) {
2618ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2619ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2620ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2621ca3d3a14SMatthew G. Knepley 
2622ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26239566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26249566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva));
26259566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2626ca3d3a14SMatthew G. Knepley       }
2627ca3d3a14SMatthew G. Knepley     }
2628ca3d3a14SMatthew G. Knepley   }
26299566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
2630ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2631ca3d3a14SMatthew G. Knepley }
2632ca3d3a14SMatthew G. Knepley 
2633ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2634ca3d3a14SMatthew G. Knepley {
2635ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2636ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2637ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2638ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2639ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2640ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2641ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26429566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
2643ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2644ca3d3a14SMatthew G. Knepley }
2645ca3d3a14SMatthew G. Knepley 
2646bb9467b5SJed Brown /*@C
2647*bb7acecfSBarry Smith    DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2648baf369e7SPeter Brune 
2649*bb7acecfSBarry Smith    Logically Collective on dm
2650baf369e7SPeter Brune 
26514165533cSJose E. Roman    Input Parameters:
2652*bb7acecfSBarry Smith +  dm - the `DM`
2653*bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2654*bb7acecfSBarry Smith .  endhook - function to run after `DMGlobalToLocalEnd()` has completed
26550298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2656baf369e7SPeter Brune 
2657baf369e7SPeter Brune    Calling sequence for beginhook:
2658baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2659baf369e7SPeter Brune 
2660baf369e7SPeter Brune +  dm - global DM
2661baf369e7SPeter Brune .  g - global vector
2662baf369e7SPeter Brune .  mode - mode
2663baf369e7SPeter Brune .  l - local vector
2664baf369e7SPeter Brune -  ctx - optional user-defined function context
2665baf369e7SPeter Brune 
2666baf369e7SPeter Brune    Calling sequence for endhook:
2667ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2668baf369e7SPeter Brune 
2669baf369e7SPeter Brune +  global - global DM
2670baf369e7SPeter Brune -  ctx - optional user-defined function context
2671baf369e7SPeter Brune 
2672baf369e7SPeter Brune    Level: advanced
2673baf369e7SPeter Brune 
2674*bb7acecfSBarry Smith    Note:
2675*bb7acecfSBarry 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.
2676*bb7acecfSBarry Smith 
2677*bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2678baf369e7SPeter Brune @*/
2679baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2680baf369e7SPeter Brune {
2681baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2682baf369e7SPeter Brune 
2683baf369e7SPeter Brune   PetscFunctionBegin;
2684baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2685baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
26869566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2687baf369e7SPeter Brune   link->beginhook = beginhook;
2688baf369e7SPeter Brune   link->endhook   = endhook;
2689baf369e7SPeter Brune   link->ctx       = ctx;
26900298fd71SBarry Smith   link->next      = NULL;
2691baf369e7SPeter Brune   *p              = link;
2692baf369e7SPeter Brune   PetscFunctionReturn(0);
2693baf369e7SPeter Brune }
2694baf369e7SPeter Brune 
26954c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
26964c274da1SToby Isaac {
26974c274da1SToby Isaac   Mat cMat;
269879769bd5SJed Brown   Vec cVec, cBias;
26994c274da1SToby Isaac   PetscSection section, cSec;
27004c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
27014c274da1SToby Isaac 
27024c274da1SToby Isaac   PetscFunctionBegin;
27034c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27049566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm,&cSec,&cMat,&cBias));
27054c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27065db9a05bSToby Isaac     PetscInt nRows;
27075db9a05bSToby Isaac 
27089566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat,&nRows,NULL));
27095db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
27109566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm,&section));
27119566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat,NULL,&cVec));
27129566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat,l,cVec));
27139566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec,1.,cBias));
27149566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec,&pStart,&pEnd));
27154c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27169566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec,p,&dof));
27174c274da1SToby Isaac       if (dof) {
27184c274da1SToby Isaac         PetscScalar *vals;
27199566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec,cSec,p,&vals));
27209566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES));
27214c274da1SToby Isaac       }
27224c274da1SToby Isaac     }
27239566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27244c274da1SToby Isaac   }
27254c274da1SToby Isaac   PetscFunctionReturn(0);
27264c274da1SToby Isaac }
27274c274da1SToby Isaac 
272847c6ae99SBarry Smith /*@
272901729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
273001729b5cSPatrick Sanan 
2731d083f849SBarry Smith     Neighbor-wise Collective on dm
273201729b5cSPatrick Sanan 
273301729b5cSPatrick Sanan     Input Parameters:
2734*bb7acecfSBarry Smith +   dm - the `DM` object
273501729b5cSPatrick Sanan .   g - the global vector
2736*bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
273701729b5cSPatrick Sanan -   l - the local vector
273801729b5cSPatrick Sanan 
273901729b5cSPatrick Sanan     Notes:
2740*bb7acecfSBarry Smith     The communication involved in this update can be overlapped with computation by instead using
2741*bb7acecfSBarry Smith     `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2742*bb7acecfSBarry Smith 
2743*bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
274401729b5cSPatrick Sanan 
274501729b5cSPatrick Sanan     Level: beginner
274601729b5cSPatrick Sanan 
2747*bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
2748*bb7acecfSBarry Smith           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`,
2749*bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
275001729b5cSPatrick Sanan 
275101729b5cSPatrick Sanan @*/
275201729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
275301729b5cSPatrick Sanan {
275401729b5cSPatrick Sanan   PetscFunctionBegin;
27559566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm,g,mode,l));
27569566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm,g,mode,l));
275701729b5cSPatrick Sanan   PetscFunctionReturn(0);
275801729b5cSPatrick Sanan }
275901729b5cSPatrick Sanan 
276001729b5cSPatrick Sanan /*@
276147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
276247c6ae99SBarry Smith 
2763d083f849SBarry Smith     Neighbor-wise Collective on dm
276447c6ae99SBarry Smith 
276547c6ae99SBarry Smith     Input Parameters:
2766*bb7acecfSBarry Smith +   dm - the `DM` object
276747c6ae99SBarry Smith .   g - the global vector
2768*bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
276947c6ae99SBarry Smith -   l - the local vector
277047c6ae99SBarry Smith 
277101729b5cSPatrick Sanan     Level: intermediate
277247c6ae99SBarry Smith 
2773*bb7acecfSBarry Smith     Notes:
2774*bb7acecfSBarry Smith     The operation is completed with `DMGlobalToLocalEnd()`
2775*bb7acecfSBarry Smith 
2776*bb7acecfSBarry Smith     One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2777*bb7acecfSBarry Smith 
2778*bb7acecfSBarry Smith     `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2779*bb7acecfSBarry Smith 
2780*bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2781*bb7acecfSBarry Smith 
2782*bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
278347c6ae99SBarry Smith 
278447c6ae99SBarry Smith @*/
27857087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
278647c6ae99SBarry Smith {
27877128ae9fSMatthew G Knepley   PetscSF                 sf;
2788baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
278947c6ae99SBarry Smith 
279047c6ae99SBarry Smith   PetscFunctionBegin;
2791171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2792baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
27931baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm,g,mode,l,link->ctx));
2794baf369e7SPeter Brune   }
27959566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
27967128ae9fSMatthew G Knepley   if (sf) {
2797ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2798ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2799d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
28007128ae9fSMatthew G Knepley 
28017a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28029566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28039566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28049566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28059566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28069566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28077128ae9fSMatthew G Knepley   } else {
28087a8be351SBarry Smith     PetscCheck(dm->ops->globaltolocalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
28099566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
28107128ae9fSMatthew G Knepley   }
281147c6ae99SBarry Smith   PetscFunctionReturn(0);
281247c6ae99SBarry Smith }
281347c6ae99SBarry Smith 
281447c6ae99SBarry Smith /*@
281547c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
281647c6ae99SBarry Smith 
2817d083f849SBarry Smith     Neighbor-wise Collective on dm
281847c6ae99SBarry Smith 
281947c6ae99SBarry Smith     Input Parameters:
2820*bb7acecfSBarry Smith +   dm - the `DM` object
282147c6ae99SBarry Smith .   g - the global vector
2822*bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
282347c6ae99SBarry Smith -   l - the local vector
282447c6ae99SBarry Smith 
282501729b5cSPatrick Sanan     Level: intermediate
282647c6ae99SBarry Smith 
2827*bb7acecfSBarry Smith     Note:
2828*bb7acecfSBarry Smith     See `DMGlobalToLocalBegin()` for details.
2829*bb7acecfSBarry Smith 
2830*bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
283147c6ae99SBarry Smith 
283247c6ae99SBarry Smith @*/
28337087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
283447c6ae99SBarry Smith {
28357128ae9fSMatthew G Knepley   PetscSF                 sf;
2836ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2837ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2838ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2839baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2840d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
284147c6ae99SBarry Smith 
284247c6ae99SBarry Smith   PetscFunctionBegin;
2843171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28449566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28459566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28467128ae9fSMatthew G Knepley   if (sf) {
28477a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28487128ae9fSMatthew G Knepley 
28499566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28509566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28519566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray,MPI_REPLACE));
28529566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28539566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28549566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28557128ae9fSMatthew G Knepley   } else {
28567a8be351SBarry Smith     PetscCheck(dm->ops->globaltolocalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
28579566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
28587128ae9fSMatthew G Knepley   }
28599566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL));
2860baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
28619566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm,g,mode,l,link->ctx));
2862baf369e7SPeter Brune   }
286347c6ae99SBarry Smith   PetscFunctionReturn(0);
286447c6ae99SBarry Smith }
286547c6ae99SBarry Smith 
2866d4d07f1eSToby Isaac /*@C
2867d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2868d4d07f1eSToby Isaac 
2869*bb7acecfSBarry Smith    Logically Collective on dm
2870d4d07f1eSToby Isaac 
28714165533cSJose E. Roman    Input Parameters:
2872*bb7acecfSBarry Smith +  dm - the `DM`
2873*bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2874*bb7acecfSBarry Smith .  endhook - function to run after `DMLocalToGlobalEnd()` has completed
2875d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2876d4d07f1eSToby Isaac 
2877d4d07f1eSToby Isaac    Calling sequence for beginhook:
2878d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2879d4d07f1eSToby Isaac 
2880*bb7acecfSBarry Smith +  dm - global `DM`
2881d4d07f1eSToby Isaac .  l - local vector
2882d4d07f1eSToby Isaac .  mode - mode
2883d4d07f1eSToby Isaac .  g - global vector
2884d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2885d4d07f1eSToby Isaac 
2886d4d07f1eSToby Isaac    Calling sequence for endhook:
2887d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2888d4d07f1eSToby Isaac 
2889*bb7acecfSBarry Smith +  global - global `DM`
2890d4d07f1eSToby Isaac .  l - local vector
2891d4d07f1eSToby Isaac .  mode - mode
2892d4d07f1eSToby Isaac .  g - global vector
2893d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2894d4d07f1eSToby Isaac 
2895d4d07f1eSToby Isaac    Level: advanced
2896d4d07f1eSToby Isaac 
2897*bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2898d4d07f1eSToby Isaac @*/
2899d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2900d4d07f1eSToby Isaac {
2901d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2902d4d07f1eSToby Isaac 
2903d4d07f1eSToby Isaac   PetscFunctionBegin;
2904d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2905d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
29069566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2907d4d07f1eSToby Isaac   link->beginhook = beginhook;
2908d4d07f1eSToby Isaac   link->endhook   = endhook;
2909d4d07f1eSToby Isaac   link->ctx       = ctx;
2910d4d07f1eSToby Isaac   link->next      = NULL;
2911d4d07f1eSToby Isaac   *p              = link;
2912d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2913d4d07f1eSToby Isaac }
2914d4d07f1eSToby Isaac 
29154c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
29164c274da1SToby Isaac {
29174c274da1SToby Isaac   Mat cMat;
29184c274da1SToby Isaac   Vec cVec;
29194c274da1SToby Isaac   PetscSection section, cSec;
29204c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
29214c274da1SToby Isaac 
29224c274da1SToby Isaac   PetscFunctionBegin;
29234c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29249566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm,&cSec,&cMat,NULL));
29254c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
29265db9a05bSToby Isaac     PetscInt nRows;
29275db9a05bSToby Isaac 
29289566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat,&nRows,NULL));
29295db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
29309566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm,&section));
29319566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat,NULL,&cVec));
29329566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec,&pStart,&pEnd));
29334c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29349566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec,p,&dof));
29354c274da1SToby Isaac       if (dof) {
29364c274da1SToby Isaac         PetscInt d;
29374c274da1SToby Isaac         PetscScalar *vals;
29389566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l,section,p,&vals));
29399566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec,cSec,p,vals,mode));
29404c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29414c274da1SToby Isaac          * we just extracted */
29424c274da1SToby Isaac         for (d = 0; d < dof; d++) {
29434c274da1SToby Isaac           vals[d] = 0.;
29444c274da1SToby Isaac         }
29454c274da1SToby Isaac       }
29464c274da1SToby Isaac     }
29479566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat,cVec,l,l));
29489566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29494c274da1SToby Isaac   }
29504c274da1SToby Isaac   PetscFunctionReturn(0);
29514c274da1SToby Isaac }
295201729b5cSPatrick Sanan /*@
295301729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
295401729b5cSPatrick Sanan 
2955d083f849SBarry Smith     Neighbor-wise Collective on dm
295601729b5cSPatrick Sanan 
295701729b5cSPatrick Sanan     Input Parameters:
2958*bb7acecfSBarry Smith +   dm - the `DM` object
295901729b5cSPatrick Sanan .   l - the local vector
2960*bb7acecfSBarry 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.
296101729b5cSPatrick Sanan -   g - the global vector
296201729b5cSPatrick Sanan 
296301729b5cSPatrick Sanan     Notes:
296401729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
2965*bb7acecfSBarry Smith     `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
296601729b5cSPatrick Sanan 
2967*bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2968*bb7acecfSBarry Smith 
2969*bb7acecfSBarry 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.
2970*bb7acecfSBarry Smith 
2971*bb7acecfSBarry Smith     Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
297201729b5cSPatrick Sanan 
297301729b5cSPatrick Sanan     Level: beginner
297401729b5cSPatrick Sanan 
2975*bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`,  `DMLocalToGlobalHookAdd()`,  `DMGlobaToLocallHookAdd()`
297601729b5cSPatrick Sanan 
297701729b5cSPatrick Sanan @*/
297801729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
297901729b5cSPatrick Sanan {
298001729b5cSPatrick Sanan   PetscFunctionBegin;
29819566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm,l,mode,g));
29829566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm,l,mode,g));
298301729b5cSPatrick Sanan   PetscFunctionReturn(0);
298401729b5cSPatrick Sanan }
29854c274da1SToby Isaac 
298647c6ae99SBarry Smith /*@
298701729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
29889a42bb27SBarry Smith 
2989d083f849SBarry Smith     Neighbor-wise Collective on dm
29909a42bb27SBarry Smith 
29919a42bb27SBarry Smith     Input Parameters:
2992*bb7acecfSBarry Smith +   dm - the `DM` object
2993f6813fd5SJed Brown .   l - the local vector
2994*bb7acecfSBarry 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.
29951eb28f2eSBarry Smith -   g - the global vector
29969a42bb27SBarry Smith 
299795452b02SPatrick Sanan     Notes:
2998*bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2999*bb7acecfSBarry Smith 
3000*bb7acecfSBarry 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.
3001*bb7acecfSBarry Smith 
3002*bb7acecfSBarry Smith     Use `DMLocalToGlobalEnd()` to complete the communication process.
3003*bb7acecfSBarry Smith 
3004*bb7acecfSBarry Smith     `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3005*bb7acecfSBarry Smith 
3006*bb7acecfSBarry Smith     `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30079a42bb27SBarry Smith 
300801729b5cSPatrick Sanan     Level: intermediate
30099a42bb27SBarry Smith 
3010*bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30119a42bb27SBarry Smith 
30129a42bb27SBarry Smith @*/
30137087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
30149a42bb27SBarry Smith {
30157128ae9fSMatthew G Knepley   PetscSF                 sf;
301684330215SMatthew G. Knepley   PetscSection            s, gs;
3017d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3018ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3019ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3020ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3021fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3022d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
30239a42bb27SBarry Smith 
30249a42bb27SBarry Smith   PetscFunctionBegin;
3025171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3026d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
30271baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm,l,mode,g,link->ctx));
3028d4d07f1eSToby Isaac   }
30299566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL));
30309566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30319566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30327128ae9fSMatthew G Knepley   switch (mode) {
30337128ae9fSMatthew G Knepley   case INSERT_VALUES:
30347128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3035304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
303684330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
30377128ae9fSMatthew G Knepley   case ADD_VALUES:
30387128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3039304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
304084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
30417128ae9fSMatthew G Knepley   default:
304263a3b9bcSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30437128ae9fSMatthew G Knepley   }
3044ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30459566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3046ca3d3a14SMatthew G. Knepley     if (transform) {
30479566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30489566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30499566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30509566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3051fa88e482SJed Brown     } else if (isInsert) {
30529566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3053fa88e482SJed Brown     } else {
30549566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3055fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3056ca3d3a14SMatthew G. Knepley     }
3057fa88e482SJed Brown     if (s && isInsert) {
30589566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3059fa88e482SJed Brown     } else {
30609566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3061fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3062fa88e482SJed Brown     }
3063ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30649566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
306584330215SMatthew G. Knepley     } else if (s && isInsert) {
306684330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
306784330215SMatthew G. Knepley 
30689566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
30699566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
30709566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
307184330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3072b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
307384330215SMatthew G. Knepley 
30749566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
30759566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
30769566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
30779566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
30789566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
30799566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3080b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
308103442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
30827a8be351SBarry 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);
3083b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3084b3b16f48SMatthew G. Knepley         if (!gcdof) {
308584330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
3086b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3087b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
308884330215SMatthew G. Knepley           const PetscInt *cdofs;
308984330215SMatthew G. Knepley           PetscInt        cind = 0;
309084330215SMatthew G. Knepley 
30919566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3092b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
309384330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
3094b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
309584330215SMatthew G. Knepley           }
30967a8be351SBarry 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);
309784330215SMatthew G. Knepley       }
3098ca3d3a14SMatthew G. Knepley     }
3099fa88e482SJed Brown     if (g_inplace) {
31009566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3101fa88e482SJed Brown     } else {
31029566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3103fa88e482SJed Brown     }
3104ca3d3a14SMatthew G. Knepley     if (transform) {
31059566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31069566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3107fa88e482SJed Brown     } else if (l_inplace) {
31089566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3109ca3d3a14SMatthew G. Knepley     } else {
31109566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3111ca3d3a14SMatthew G. Knepley     }
31127128ae9fSMatthew G Knepley   } else {
31137a8be351SBarry Smith     PetscCheck(dm->ops->localtoglobalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
31149566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g));
31157128ae9fSMatthew G Knepley   }
31169a42bb27SBarry Smith   PetscFunctionReturn(0);
31179a42bb27SBarry Smith }
31189a42bb27SBarry Smith 
31199a42bb27SBarry Smith /*@
31209a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
312147c6ae99SBarry Smith 
3122d083f849SBarry Smith     Neighbor-wise Collective on dm
312347c6ae99SBarry Smith 
312447c6ae99SBarry Smith     Input Parameters:
3125*bb7acecfSBarry Smith +   dm - the `DM` object
3126f6813fd5SJed Brown .   l - the local vector
3127*bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
3128f6813fd5SJed Brown -   g - the global vector
312947c6ae99SBarry Smith 
313001729b5cSPatrick Sanan     Level: intermediate
313147c6ae99SBarry Smith 
3132*bb7acecfSBarry Smith     Note:
3133*bb7acecfSBarry Smith     See `DMLocalToGlobalBegin()` for full details
3134*bb7acecfSBarry Smith 
3135*bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalEnd()`
313647c6ae99SBarry Smith 
313747c6ae99SBarry Smith @*/
31387087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
313947c6ae99SBarry Smith {
31407128ae9fSMatthew G Knepley   PetscSF                 sf;
314184330215SMatthew G. Knepley   PetscSection            s;
3142d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3143ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
314447c6ae99SBarry Smith 
314547c6ae99SBarry Smith   PetscFunctionBegin;
3146171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31479566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31489566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31497128ae9fSMatthew G Knepley   switch (mode) {
31507128ae9fSMatthew G Knepley   case INSERT_VALUES:
31517128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
315284330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
31537128ae9fSMatthew G Knepley   case ADD_VALUES:
31547128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
315584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
31567128ae9fSMatthew G Knepley   default:
315763a3b9bcSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31587128ae9fSMatthew G Knepley   }
315984330215SMatthew G. Knepley   if (sf && !isInsert) {
3160ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3161ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3162ca3d3a14SMatthew G. Knepley     Vec                tmpl;
316384330215SMatthew G. Knepley 
31649566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3165ca3d3a14SMatthew G. Knepley     if (transform) {
31669566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31679566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3168ca3d3a14SMatthew G. Knepley     } else {
31699566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3170ca3d3a14SMatthew G. Knepley     }
31719566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
31729566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3173ca3d3a14SMatthew G. Knepley     if (transform) {
31749566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31759566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3176ca3d3a14SMatthew G. Knepley     } else {
31779566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3178ca3d3a14SMatthew G. Knepley     }
31799566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
318084330215SMatthew G. Knepley   } else if (s && isInsert) {
31817128ae9fSMatthew G Knepley   } else {
31827a8be351SBarry Smith     PetscCheck(dm->ops->localtoglobalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
31839566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g));
31847128ae9fSMatthew G Knepley   }
3185d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
31869566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm,g,mode,l,link->ctx));
3187d4d07f1eSToby Isaac   }
318847c6ae99SBarry Smith   PetscFunctionReturn(0);
318947c6ae99SBarry Smith }
319047c6ae99SBarry Smith 
3191f089877aSRichard Tran Mills /*@
3192*bb7acecfSBarry Smith    DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include ghost points
3193bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3194*bb7acecfSBarry Smith    points in the second are set correctly from values on other MPI ranks. Must be followed by `DMLocalToLocalEnd()`.
3195f089877aSRichard Tran Mills 
3196d083f849SBarry Smith    Neighbor-wise Collective on dm
3197f089877aSRichard Tran Mills 
3198f089877aSRichard Tran Mills    Input Parameters:
3199*bb7acecfSBarry Smith +  dm - the `DM` object
3200bc0a1609SRichard Tran Mills .  g - the original local vector
3201*bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3202f089877aSRichard Tran Mills 
3203bc0a1609SRichard Tran Mills    Output Parameter:
3204bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3205f089877aSRichard Tran Mills 
3206f089877aSRichard Tran Mills    Level: intermediate
3207f089877aSRichard Tran Mills 
3208*bb7acecfSBarry Smith .seealso: `DMLocalToLocalEnd(), `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalEnd()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3209f089877aSRichard Tran Mills 
3210f089877aSRichard Tran Mills @*/
3211f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
3212f089877aSRichard Tran Mills {
3213f089877aSRichard Tran Mills   PetscFunctionBegin;
3214f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32157a8be351SBarry Smith   PetscCheck(dm->ops->localtolocalbegin,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
32169566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
3217f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3218f089877aSRichard Tran Mills }
3219f089877aSRichard Tran Mills 
3220f089877aSRichard Tran Mills /*@
3221*bb7acecfSBarry Smith    DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3222*bb7acecfSBarry Smith    points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3223f089877aSRichard Tran Mills 
3224d083f849SBarry Smith    Neighbor-wise Collective on dm
3225f089877aSRichard Tran Mills 
3226f089877aSRichard Tran Mills    Input Parameters:
3227*bb7acecfSBarry Smith +  da - the `DM` object
3228bc0a1609SRichard Tran Mills .  g - the original local vector
3229*bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3230f089877aSRichard Tran Mills 
3231bc0a1609SRichard Tran Mills    Output Parameter:
3232bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3233f089877aSRichard Tran Mills 
3234f089877aSRichard Tran Mills    Level: intermediate
3235f089877aSRichard Tran Mills 
3236*bb7acecfSBarry Smith .seealso: `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalBegin()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3237f089877aSRichard Tran Mills 
3238f089877aSRichard Tran Mills @*/
3239f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
3240f089877aSRichard Tran Mills {
3241f089877aSRichard Tran Mills   PetscFunctionBegin;
3242f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32437a8be351SBarry Smith   PetscCheck(dm->ops->localtolocalend,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
32449566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
3245f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3246f089877aSRichard Tran Mills }
3247f089877aSRichard Tran Mills 
324847c6ae99SBarry Smith /*@
3249*bb7acecfSBarry Smith     DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
325047c6ae99SBarry Smith 
3251d083f849SBarry Smith     Collective on dm
325247c6ae99SBarry Smith 
3253d8d19677SJose E. Roman     Input Parameters:
3254*bb7acecfSBarry Smith +   dm - the `DM` object
3255*bb7acecfSBarry Smith -   comm - the communicator to contain the new `DM` object (or MPI_COMM_NULL)
325647c6ae99SBarry Smith 
325747c6ae99SBarry Smith     Output Parameter:
3258*bb7acecfSBarry Smith .   dmc - the coarsened `DM`
325947c6ae99SBarry Smith 
326047c6ae99SBarry Smith     Level: developer
326147c6ae99SBarry Smith 
3262*bb7acecfSBarry Smith .seealso: `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
326347c6ae99SBarry Smith 
326447c6ae99SBarry Smith @*/
32657087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
326647c6ae99SBarry Smith {
3267b17ce1afSJed Brown   DMCoarsenHookLink link;
326847c6ae99SBarry Smith 
326947c6ae99SBarry Smith   PetscFunctionBegin;
3270171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32717a8be351SBarry Smith   PetscCheck(dm->ops->coarsen,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
32729566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen,dm,0,0,0));
32739566063dSJacob Faibussowitsch   PetscCall((*dm->ops->coarsen)(dm, comm, dmc));
3274b9d85ea2SLisandro Dalcin   if (*dmc) {
3275a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
32769566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm,*dmc));
327743842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
32789566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc));
3279644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
32800598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3281656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
32829566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc,dm->mattype));
3283b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
32849566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm,*dmc,link->ctx));
3285b17ce1afSJed Brown     }
3286b9d85ea2SLisandro Dalcin   }
32879566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen,dm,0,0,0));
32887a8be351SBarry Smith   PetscCheck(*dmc,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3289b17ce1afSJed Brown   PetscFunctionReturn(0);
3290b17ce1afSJed Brown }
3291b17ce1afSJed Brown 
3292bb9467b5SJed Brown /*@C
3293b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3294b17ce1afSJed Brown 
3295*bb7acecfSBarry Smith    Logically Collective on fine
3296b17ce1afSJed Brown 
32974165533cSJose E. Roman    Input Parameters:
3298*bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3299b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3300*bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
33010298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3302b17ce1afSJed Brown 
3303b17ce1afSJed Brown    Calling sequence of coarsenhook:
3304b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3305b17ce1afSJed Brown 
3306*bb7acecfSBarry Smith +  fine - fine level `DM`
3307*bb7acecfSBarry Smith .  coarse - coarse level `DM` to restrict problem to
3308b17ce1afSJed Brown -  ctx - optional user-defined function context
3309b17ce1afSJed Brown 
3310b17ce1afSJed Brown    Calling sequence for restricthook:
3311c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3312*bb7acecfSBarry Smith $
3313*bb7acecfSBarry Smith +  fine - fine level `DM`
3314*bb7acecfSBarry Smith .  mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3315c833c3b5SJed Brown .  rscale - scaling vector for restriction
3316c833c3b5SJed Brown .  inject - matrix restricting by injection
3317b17ce1afSJed Brown .  coarse - coarse level DM to update
3318b17ce1afSJed Brown -  ctx - optional user-defined function context
3319b17ce1afSJed Brown 
3320b17ce1afSJed Brown    Level: advanced
3321b17ce1afSJed Brown 
3322b17ce1afSJed Brown    Notes:
3323*bb7acecfSBarry 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`.
3324b17ce1afSJed Brown 
3325b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3326b17ce1afSJed Brown 
3327b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3328*bb7acecfSBarry Smith    extract the finest level information from its context (instead of from the `SNES`).
3329b17ce1afSJed Brown 
3330*bb7acecfSBarry Smith    The hooks are automatically called by `DMRestrict()`
3331*bb7acecfSBarry Smith 
3332*bb7acecfSBarry Smith    Fortran Note:
3333*bb7acecfSBarry Smith    This function is not available from Fortran.
3334bb9467b5SJed Brown 
3335db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3336b17ce1afSJed Brown @*/
3337b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3338b17ce1afSJed Brown {
3339b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3340b17ce1afSJed Brown 
3341b17ce1afSJed Brown   PetscFunctionBegin;
3342b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
33431e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
33441e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
33451e3d8eccSJed Brown   }
33469566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3347b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3348b17ce1afSJed Brown   link->restricthook = restricthook;
3349b17ce1afSJed Brown   link->ctx          = ctx;
33500298fd71SBarry Smith   link->next         = NULL;
3351b17ce1afSJed Brown   *p                 = link;
3352b17ce1afSJed Brown   PetscFunctionReturn(0);
3353b17ce1afSJed Brown }
3354b17ce1afSJed Brown 
3355dc822a44SJed Brown /*@C
3356*bb7acecfSBarry Smith    DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3357dc822a44SJed Brown 
3358*bb7acecfSBarry Smith    Logically Collective on fine
3359dc822a44SJed Brown 
33604165533cSJose E. Roman    Input Parameters:
3361*bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3362dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3363*bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels
3364dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3365dc822a44SJed Brown 
3366dc822a44SJed Brown    Level: advanced
3367dc822a44SJed Brown 
3368*bb7acecfSBarry Smith    Note:
3369dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3370dc822a44SJed Brown 
3371*bb7acecfSBarry Smith    Fortran Note:
3372*bb7acecfSBarry Smith    This function is not available from Fortran.
3373dc822a44SJed Brown 
3374db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3375dc822a44SJed Brown @*/
3376dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3377dc822a44SJed Brown {
3378dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3379dc822a44SJed Brown 
3380dc822a44SJed Brown   PetscFunctionBegin;
3381dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3382dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3383dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3384dc822a44SJed Brown       link = *p;
3385dc822a44SJed Brown       *p = link->next;
33869566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3387dc822a44SJed Brown       break;
3388dc822a44SJed Brown     }
3389dc822a44SJed Brown   }
3390dc822a44SJed Brown   PetscFunctionReturn(0);
3391dc822a44SJed Brown }
3392dc822a44SJed Brown 
3393b17ce1afSJed Brown /*@
3394*bb7acecfSBarry Smith    DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3395b17ce1afSJed Brown 
3396b17ce1afSJed Brown    Collective if any hooks are
3397b17ce1afSJed Brown 
33984165533cSJose E. Roman    Input Parameters:
3399*bb7acecfSBarry Smith +  fine - finer `DM` from which the data is obtained
3400*bb7acecfSBarry Smith .  restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3401e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3402*bb7acecfSBarry Smith .  inject - injection matrix, also use `MatRestrict()`
3403e91eccc2SStefano Zampini -  coarse - coarser DM to update
3404b17ce1afSJed Brown 
3405b17ce1afSJed Brown    Level: developer
3406b17ce1afSJed Brown 
3407*bb7acecfSBarry Smith    Developer Note:
3408*bb7acecfSBarry Smith    Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3409*bb7acecfSBarry Smith 
3410*bb7acecfSBarry Smith .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3411b17ce1afSJed Brown @*/
3412b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3413b17ce1afSJed Brown {
3414b17ce1afSJed Brown   DMCoarsenHookLink link;
3415b17ce1afSJed Brown 
3416b17ce1afSJed Brown   PetscFunctionBegin;
3417b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
34181baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx));
3419b17ce1afSJed Brown   }
342047c6ae99SBarry Smith   PetscFunctionReturn(0);
342147c6ae99SBarry Smith }
342247c6ae99SBarry Smith 
3423bb9467b5SJed Brown /*@C
3424be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
34255dbd56e3SPeter Brune 
3426d083f849SBarry Smith    Logically Collective on global
34275dbd56e3SPeter Brune 
34284165533cSJose E. Roman    Input Parameters:
3429*bb7acecfSBarry Smith +  global - global `DM`
3430*bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
34315dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
34320298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
34335dbd56e3SPeter Brune 
3434ec4806b8SPeter Brune    Calling sequence for ddhook:
3435ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3436ec4806b8SPeter Brune 
3437*bb7acecfSBarry Smith +  global - global `DM`
3438*bb7acecfSBarry Smith .  block  - block `DM`
3439ec4806b8SPeter Brune -  ctx - optional user-defined function context
3440ec4806b8SPeter Brune 
34415dbd56e3SPeter Brune    Calling sequence for restricthook:
3442ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
34435dbd56e3SPeter Brune 
3444*bb7acecfSBarry Smith +  global - global `DM`
34455dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
34465dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3447*bb7acecfSBarry Smith .  block  - block `DM`
34485dbd56e3SPeter Brune -  ctx - optional user-defined function context
34495dbd56e3SPeter Brune 
34505dbd56e3SPeter Brune    Level: advanced
34515dbd56e3SPeter Brune 
34525dbd56e3SPeter Brune    Notes:
3453*bb7acecfSBarry Smith    This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
34545dbd56e3SPeter Brune 
34555dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
34565dbd56e3SPeter Brune 
34575dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3458*bb7acecfSBarry Smith    extract the global information from its context (instead of from the `SNES`).
34595dbd56e3SPeter Brune 
3460*bb7acecfSBarry Smith    Fortran Note:
3461*bb7acecfSBarry Smith    This function is not available from Fortran.
3462bb9467b5SJed Brown 
3463*bb7acecfSBarry Smith .seealso: `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
34645dbd56e3SPeter Brune @*/
3465be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
34665dbd56e3SPeter Brune {
3467be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
34685dbd56e3SPeter Brune 
34695dbd56e3SPeter Brune   PetscFunctionBegin;
34705dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3471b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3472b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3473b3a6b972SJed Brown   }
34749566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
34755dbd56e3SPeter Brune   link->restricthook = restricthook;
3476be081cd6SPeter Brune   link->ddhook       = ddhook;
34775dbd56e3SPeter Brune   link->ctx          = ctx;
34780298fd71SBarry Smith   link->next         = NULL;
34795dbd56e3SPeter Brune   *p                 = link;
34805dbd56e3SPeter Brune   PetscFunctionReturn(0);
34815dbd56e3SPeter Brune }
34825dbd56e3SPeter Brune 
3483b3a6b972SJed Brown /*@C
3484b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3485b3a6b972SJed Brown 
3486*bb7acecfSBarry Smith    Logically Collective on global
3487b3a6b972SJed Brown 
34884165533cSJose E. Roman    Input Parameters:
3489*bb7acecfSBarry Smith +  global - global `DM`
3490*bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
3491b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3492b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3493b3a6b972SJed Brown 
3494b3a6b972SJed Brown    Level: advanced
3495b3a6b972SJed Brown 
3496*bb7acecfSBarry Smith    Fortran Note:
3497*bb7acecfSBarry Smith    This function is not available from Fortran.
3498b3a6b972SJed Brown 
3499db781477SPatrick Sanan .seealso: `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3500b3a6b972SJed Brown @*/
3501b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3502b3a6b972SJed Brown {
3503b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3504b3a6b972SJed Brown 
3505b3a6b972SJed Brown   PetscFunctionBegin;
3506b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3507b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3508b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3509b3a6b972SJed Brown       link = *p;
3510b3a6b972SJed Brown       *p = link->next;
35119566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3512b3a6b972SJed Brown       break;
3513b3a6b972SJed Brown     }
3514b3a6b972SJed Brown   }
3515b3a6b972SJed Brown   PetscFunctionReturn(0);
3516b3a6b972SJed Brown }
3517b3a6b972SJed Brown 
35185dbd56e3SPeter Brune /*@
3519*bb7acecfSBarry Smith    DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
35205dbd56e3SPeter Brune 
35215dbd56e3SPeter Brune    Collective if any hooks are
35225dbd56e3SPeter Brune 
35234165533cSJose E. Roman    Input Parameters:
3524*bb7acecfSBarry Smith +  fine - finer `DM` to use as a base
3525be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3526be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
3527*bb7acecfSBarry Smith -  coarse - coarser `DM` to update
35285dbd56e3SPeter Brune 
35295dbd56e3SPeter Brune    Level: developer
35305dbd56e3SPeter Brune 
3531db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
35325dbd56e3SPeter Brune @*/
3533be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
35345dbd56e3SPeter Brune {
3535be081cd6SPeter Brune   DMSubDomainHookLink link;
35365dbd56e3SPeter Brune 
35375dbd56e3SPeter Brune   PetscFunctionBegin;
3538be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
35391baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx));
35405dbd56e3SPeter Brune   }
35415dbd56e3SPeter Brune   PetscFunctionReturn(0);
35425dbd56e3SPeter Brune }
35435dbd56e3SPeter Brune 
35445fe1f584SPeter Brune /*@
3545*bb7acecfSBarry Smith     DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35465fe1f584SPeter Brune 
35475fe1f584SPeter Brune     Not Collective
35485fe1f584SPeter Brune 
35495fe1f584SPeter Brune     Input Parameter:
3550*bb7acecfSBarry Smith .   dm - the `DM` object
35515fe1f584SPeter Brune 
35525fe1f584SPeter Brune     Output Parameter:
35536a7d9d85SPeter Brune .   level - number of coarsenings
35545fe1f584SPeter Brune 
35555fe1f584SPeter Brune     Level: developer
35565fe1f584SPeter Brune 
3557*bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35585fe1f584SPeter Brune 
35595fe1f584SPeter Brune @*/
35605fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
35615fe1f584SPeter Brune {
35625fe1f584SPeter Brune   PetscFunctionBegin;
35635fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3564b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
35655fe1f584SPeter Brune   *level = dm->leveldown;
35665fe1f584SPeter Brune   PetscFunctionReturn(0);
35675fe1f584SPeter Brune }
35685fe1f584SPeter Brune 
35699a64c4a8SMatthew G. Knepley /*@
3570*bb7acecfSBarry Smith     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
35719a64c4a8SMatthew G. Knepley 
3572*bb7acecfSBarry Smith     Collective on dm
35739a64c4a8SMatthew G. Knepley 
35749a64c4a8SMatthew G. Knepley     Input Parameters:
3575*bb7acecfSBarry Smith +   dm - the `DM` object
35769a64c4a8SMatthew G. Knepley -   level - number of coarsenings
35779a64c4a8SMatthew G. Knepley 
35789a64c4a8SMatthew G. Knepley     Level: developer
35799a64c4a8SMatthew G. Knepley 
3580*bb7acecfSBarry Smith     Note:
3581*bb7acecfSBarry Smith     This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3582*bb7acecfSBarry Smith 
3583*bb7acecfSBarry Smith .seealso: `DMSetCoarsenLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35849a64c4a8SMatthew G. Knepley @*/
35859a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
35869a64c4a8SMatthew G. Knepley {
35879a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
35889a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35899a64c4a8SMatthew G. Knepley   dm->leveldown = level;
35909a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
35919a64c4a8SMatthew G. Knepley }
35929a64c4a8SMatthew G. Knepley 
359347c6ae99SBarry Smith /*@C
3594*bb7acecfSBarry Smith     DMRefineHierarchy - Refines a `DM` object, all levels at once
359547c6ae99SBarry Smith 
3596d083f849SBarry Smith     Collective on dm
359747c6ae99SBarry Smith 
3598d8d19677SJose E. Roman     Input Parameters:
3599*bb7acecfSBarry Smith +   dm - the `DM` object
360047c6ae99SBarry Smith -   nlevels - the number of levels of refinement
360147c6ae99SBarry Smith 
360247c6ae99SBarry Smith     Output Parameter:
3603*bb7acecfSBarry Smith .   dmf - the refined `DM` hierarchy
360447c6ae99SBarry Smith 
360547c6ae99SBarry Smith     Level: developer
360647c6ae99SBarry Smith 
3607*bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
360847c6ae99SBarry Smith 
360947c6ae99SBarry Smith @*/
36107087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
361147c6ae99SBarry Smith {
361247c6ae99SBarry Smith   PetscFunctionBegin;
3613171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36147a8be351SBarry Smith   PetscCheck(nlevels >= 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
361547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3616b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
361747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
36189566063dSJacob Faibussowitsch     PetscCall((*dm->ops->refinehierarchy)(dm,nlevels,dmf));
361947c6ae99SBarry Smith   } else if (dm->ops->refine) {
362047c6ae99SBarry Smith     PetscInt i;
362147c6ae99SBarry Smith 
36229566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]));
362347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
36249566063dSJacob Faibussowitsch       PetscCall(DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]));
362547c6ae99SBarry Smith     }
3626ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
362747c6ae99SBarry Smith   PetscFunctionReturn(0);
362847c6ae99SBarry Smith }
362947c6ae99SBarry Smith 
363047c6ae99SBarry Smith /*@C
3631*bb7acecfSBarry Smith     DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
363247c6ae99SBarry Smith 
3633d083f849SBarry Smith     Collective on dm
363447c6ae99SBarry Smith 
3635d8d19677SJose E. Roman     Input Parameters:
3636*bb7acecfSBarry Smith +   dm - the `DM` object
363747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
363847c6ae99SBarry Smith 
363947c6ae99SBarry Smith     Output Parameter:
3640*bb7acecfSBarry Smith .   dmc - the coarsened `DM` hierarchy
364147c6ae99SBarry Smith 
364247c6ae99SBarry Smith     Level: developer
364347c6ae99SBarry Smith 
3644*bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
364547c6ae99SBarry Smith 
364647c6ae99SBarry Smith @*/
36477087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
364847c6ae99SBarry Smith {
364947c6ae99SBarry Smith   PetscFunctionBegin;
3650171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36517a8be351SBarry Smith   PetscCheck(nlevels >= 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
365247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
365347c6ae99SBarry Smith   PetscValidPointer(dmc,3);
365447c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
36559566063dSJacob Faibussowitsch     PetscCall((*dm->ops->coarsenhierarchy)(dm, nlevels, dmc));
365647c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
365747c6ae99SBarry Smith     PetscInt i;
365847c6ae99SBarry Smith 
36599566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]));
366047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
36619566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]));
366247c6ae99SBarry Smith     }
3663ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
366447c6ae99SBarry Smith   PetscFunctionReturn(0);
366547c6ae99SBarry Smith }
366647c6ae99SBarry Smith 
36671a266240SBarry Smith /*@C
3668*bb7acecfSBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36691a266240SBarry Smith 
3670*bb7acecfSBarry Smith     Logically Collective if the function is collective
36711a266240SBarry Smith 
36721a266240SBarry Smith     Input Parameters:
3673*bb7acecfSBarry Smith +   dm - the `DM` object
36741a266240SBarry Smith -   destroy - the destroy function
36751a266240SBarry Smith 
36761a266240SBarry Smith     Level: intermediate
36771a266240SBarry Smith 
3678*bb7acecfSBarry Smith .seealso: `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
36791a266240SBarry Smith 
3680f07f9ceaSJed Brown @*/
36811a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
36821a266240SBarry Smith {
36831a266240SBarry Smith   PetscFunctionBegin;
3684171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36851a266240SBarry Smith   dm->ctxdestroy = destroy;
36861a266240SBarry Smith   PetscFunctionReturn(0);
36871a266240SBarry Smith }
36881a266240SBarry Smith 
3689b07ff414SBarry Smith /*@
3690*bb7acecfSBarry Smith     DMSetApplicationContext - Set a user context into a `DM` object
369147c6ae99SBarry Smith 
369247c6ae99SBarry Smith     Not Collective
369347c6ae99SBarry Smith 
369447c6ae99SBarry Smith     Input Parameters:
3695*bb7acecfSBarry Smith +   dm - the `DM` object
369647c6ae99SBarry Smith -   ctx - the user context
369747c6ae99SBarry Smith 
369847c6ae99SBarry Smith     Level: intermediate
369947c6ae99SBarry Smith 
3700*bb7acecfSBarry Smith     Note:
3701*bb7acecfSBarry Smith     A user context is a way to pass problem specific information that is accessable whenever the `DM` is available
3702*bb7acecfSBarry Smith 
3703*bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
370447c6ae99SBarry Smith 
370547c6ae99SBarry Smith @*/
37061b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
370747c6ae99SBarry Smith {
370847c6ae99SBarry Smith   PetscFunctionBegin;
3709171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
371047c6ae99SBarry Smith   dm->ctx = ctx;
371147c6ae99SBarry Smith   PetscFunctionReturn(0);
371247c6ae99SBarry Smith }
371347c6ae99SBarry Smith 
371447c6ae99SBarry Smith /*@
3715*bb7acecfSBarry Smith     DMGetApplicationContext - Gets a user context from a `DM` object
371647c6ae99SBarry Smith 
371747c6ae99SBarry Smith     Not Collective
371847c6ae99SBarry Smith 
371947c6ae99SBarry Smith     Input Parameter:
3720*bb7acecfSBarry Smith .   dm - the `DM` object
372147c6ae99SBarry Smith 
372247c6ae99SBarry Smith     Output Parameter:
372347c6ae99SBarry Smith .   ctx - the user context
372447c6ae99SBarry Smith 
372547c6ae99SBarry Smith     Level: intermediate
372647c6ae99SBarry Smith 
3727*bb7acecfSBarry Smith     Note:
3728*bb7acecfSBarry Smith     A user context is a way to pass problem specific information that is accessable whenever the `DM` is available
3729*bb7acecfSBarry Smith 
3730*bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
373147c6ae99SBarry Smith 
373247c6ae99SBarry Smith @*/
37331b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
373447c6ae99SBarry Smith {
373547c6ae99SBarry Smith   PetscFunctionBegin;
3736171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
37371b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
373847c6ae99SBarry Smith   PetscFunctionReturn(0);
373947c6ae99SBarry Smith }
374047c6ae99SBarry Smith 
374108da532bSDmitry Karpeev /*@C
3742*bb7acecfSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
374308da532bSDmitry Karpeev 
3744d083f849SBarry Smith     Logically Collective on dm
374508da532bSDmitry Karpeev 
3746d8d19677SJose E. Roman     Input Parameters:
374708da532bSDmitry Karpeev +   dm - the DM object
37480298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
374908da532bSDmitry Karpeev 
375008da532bSDmitry Karpeev     Level: intermediate
375108da532bSDmitry Karpeev 
3752*bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3753db781477SPatrick Sanan          `DMSetJacobian()`
375408da532bSDmitry Karpeev 
375508da532bSDmitry Karpeev @*/
375608da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
375708da532bSDmitry Karpeev {
375808da532bSDmitry Karpeev   PetscFunctionBegin;
37595a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
376008da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
376108da532bSDmitry Karpeev   PetscFunctionReturn(0);
376208da532bSDmitry Karpeev }
376308da532bSDmitry Karpeev 
376408da532bSDmitry Karpeev /*@
3765*bb7acecfSBarry Smith     DMHasVariableBounds - does the `DM` object have a variable bounds function?
376608da532bSDmitry Karpeev 
376708da532bSDmitry Karpeev     Not Collective
376808da532bSDmitry Karpeev 
376908da532bSDmitry Karpeev     Input Parameter:
3770*bb7acecfSBarry Smith .   dm - the `DM` object to destroy
377108da532bSDmitry Karpeev 
377208da532bSDmitry Karpeev     Output Parameter:
3773*bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the variable bounds function exists
377408da532bSDmitry Karpeev 
377508da532bSDmitry Karpeev     Level: developer
377608da532bSDmitry Karpeev 
3777*bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
377808da532bSDmitry Karpeev 
377908da532bSDmitry Karpeev @*/
378008da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
378108da532bSDmitry Karpeev {
378208da532bSDmitry Karpeev   PetscFunctionBegin;
37835a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3784534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
378508da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
378608da532bSDmitry Karpeev   PetscFunctionReturn(0);
378708da532bSDmitry Karpeev }
378808da532bSDmitry Karpeev 
378908da532bSDmitry Karpeev /*@C
3790*bb7acecfSBarry Smith     DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
379108da532bSDmitry Karpeev 
3792d083f849SBarry Smith     Logically Collective on dm
379308da532bSDmitry Karpeev 
3794f899ff85SJose E. Roman     Input Parameter:
3795*bb7acecfSBarry Smith .   dm - the `DM` object
379608da532bSDmitry Karpeev 
379708da532bSDmitry Karpeev     Output parameters:
379808da532bSDmitry Karpeev +   xl - lower bound
379908da532bSDmitry Karpeev -   xu - upper bound
380008da532bSDmitry Karpeev 
3801907376e6SBarry Smith     Level: advanced
3802907376e6SBarry Smith 
380395452b02SPatrick Sanan     Notes:
380495452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
380508da532bSDmitry Karpeev 
3806*bb7acecfSBarry Smith .seealso: `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
380708da532bSDmitry Karpeev 
380808da532bSDmitry Karpeev @*/
380908da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
381008da532bSDmitry Karpeev {
381108da532bSDmitry Karpeev   PetscFunctionBegin;
38125a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
381308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
38145a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
38157a8be351SBarry Smith   PetscCheck(dm->ops->computevariablebounds,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
38169566063dSJacob Faibussowitsch   PetscCall((*dm->ops->computevariablebounds)(dm, xl,xu));
381708da532bSDmitry Karpeev   PetscFunctionReturn(0);
381808da532bSDmitry Karpeev }
381908da532bSDmitry Karpeev 
3820b0ae01b7SPeter Brune /*@
3821*bb7acecfSBarry Smith     DMHasColoring - does the `DM` object have a method of providing a coloring?
3822b0ae01b7SPeter Brune 
3823b0ae01b7SPeter Brune     Not Collective
3824b0ae01b7SPeter Brune 
3825b0ae01b7SPeter Brune     Input Parameter:
3826b0ae01b7SPeter Brune .   dm - the DM object
3827b0ae01b7SPeter Brune 
3828b0ae01b7SPeter Brune     Output Parameter:
3829*bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3830b0ae01b7SPeter Brune 
3831b0ae01b7SPeter Brune     Level: developer
3832b0ae01b7SPeter Brune 
3833*bb7acecfSBarry Smith .seealso: `DMCreateColoring()`
3834b0ae01b7SPeter Brune 
3835b0ae01b7SPeter Brune @*/
3836b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3837b0ae01b7SPeter Brune {
3838b0ae01b7SPeter Brune   PetscFunctionBegin;
38395a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3840534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3841b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3842b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3843b0ae01b7SPeter Brune }
3844b0ae01b7SPeter Brune 
38453ad4599aSBarry Smith /*@
3846*bb7acecfSBarry Smith     DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38473ad4599aSBarry Smith 
38483ad4599aSBarry Smith     Not Collective
38493ad4599aSBarry Smith 
38503ad4599aSBarry Smith     Input Parameter:
3851*bb7acecfSBarry Smith .   dm - the `DM` object
38523ad4599aSBarry Smith 
38533ad4599aSBarry Smith     Output Parameter:
3854*bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38553ad4599aSBarry Smith 
38563ad4599aSBarry Smith     Level: developer
38573ad4599aSBarry Smith 
3858*bb7acecfSBarry Smith .seealso: `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38593ad4599aSBarry Smith 
38603ad4599aSBarry Smith @*/
38613ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
38623ad4599aSBarry Smith {
38633ad4599aSBarry Smith   PetscFunctionBegin;
38645a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3865534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
38663ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38673ad4599aSBarry Smith   PetscFunctionReturn(0);
38683ad4599aSBarry Smith }
38693ad4599aSBarry Smith 
3870a7058e45SLawrence Mitchell /*@
3871*bb7acecfSBarry Smith     DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3872a7058e45SLawrence Mitchell 
3873a7058e45SLawrence Mitchell     Not Collective
3874a7058e45SLawrence Mitchell 
3875a7058e45SLawrence Mitchell     Input Parameter:
3876*bb7acecfSBarry Smith .   dm - the `DM` object
3877a7058e45SLawrence Mitchell 
3878a7058e45SLawrence Mitchell     Output Parameter:
3879*bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3880a7058e45SLawrence Mitchell 
3881a7058e45SLawrence Mitchell     Level: developer
3882a7058e45SLawrence Mitchell 
3883*bb7acecfSBarry Smith .seealso: `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3884a7058e45SLawrence Mitchell 
3885a7058e45SLawrence Mitchell @*/
3886a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3887a7058e45SLawrence Mitchell {
3888a7058e45SLawrence Mitchell   PetscFunctionBegin;
38895a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3890534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
38915a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
38929566063dSJacob Faibussowitsch     PetscCall((*dm->ops->hascreateinjection)(dm,flg));
38935a84ad33SLisandro Dalcin   } else {
38945a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
38955a84ad33SLisandro Dalcin   }
3896a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3897a7058e45SLawrence Mitchell }
3898a7058e45SLawrence Mitchell 
38990298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3900264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3901264ace61SBarry Smith 
3902264ace61SBarry Smith /*@C
3903*bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3904264ace61SBarry Smith 
3905d083f849SBarry Smith   Collective on dm
3906264ace61SBarry Smith 
3907264ace61SBarry Smith   Input Parameters:
3908*bb7acecfSBarry Smith + dm     - The `DM` object
3909*bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3910264ace61SBarry Smith 
3911264ace61SBarry Smith   Options Database Key:
3912*bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3913264ace61SBarry Smith 
3914264ace61SBarry Smith   Level: intermediate
3915264ace61SBarry Smith 
3916*bb7acecfSBarry Smith   Note:
3917*bb7acecfSBarry Smith   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPLEXCreateBoxMesh()`
3918*bb7acecfSBarry Smith 
3919*bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3920264ace61SBarry Smith @*/
392119fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3922264ace61SBarry Smith {
3923264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3924264ace61SBarry Smith   PetscBool      match;
3925264ace61SBarry Smith 
3926264ace61SBarry Smith   PetscFunctionBegin;
3927264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
39289566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) dm, method, &match));
3929264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3930264ace61SBarry Smith 
39319566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39329566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList,method,&r));
39337a8be351SBarry Smith   PetscCheck(r,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3934264ace61SBarry Smith 
39351baa6e33SBarry Smith   if (dm->ops->destroy) PetscCall((*dm->ops->destroy)(dm));
39369566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops,sizeof(*dm->ops)));
39379566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm,method));
39389566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
3939264ace61SBarry Smith   PetscFunctionReturn(0);
3940264ace61SBarry Smith }
3941264ace61SBarry Smith 
3942264ace61SBarry Smith /*@C
3943*bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3944264ace61SBarry Smith 
3945264ace61SBarry Smith   Not Collective
3946264ace61SBarry Smith 
3947264ace61SBarry Smith   Input Parameter:
3948*bb7acecfSBarry Smith . dm  - The `DM`
3949264ace61SBarry Smith 
3950264ace61SBarry Smith   Output Parameter:
3951*bb7acecfSBarry Smith . type - The `DMType` name
3952264ace61SBarry Smith 
3953264ace61SBarry Smith   Level: intermediate
3954264ace61SBarry Smith 
3955*bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3956264ace61SBarry Smith @*/
395719fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3958264ace61SBarry Smith {
3959264ace61SBarry Smith   PetscFunctionBegin;
3960264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3961c959eef4SJed Brown   PetscValidPointer(type,2);
39629566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3963264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3964264ace61SBarry Smith   PetscFunctionReturn(0);
3965264ace61SBarry Smith }
3966264ace61SBarry Smith 
396767a56275SMatthew G Knepley /*@C
3968*bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
396967a56275SMatthew G Knepley 
3970d083f849SBarry Smith   Collective on dm
397167a56275SMatthew G Knepley 
397267a56275SMatthew G Knepley   Input Parameters:
3973*bb7acecfSBarry Smith + dm - the `DM`
3974*bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
397567a56275SMatthew G Knepley 
397667a56275SMatthew G Knepley   Output Parameter:
3977*bb7acecfSBarry Smith . M - pointer to new `DM`
397867a56275SMatthew G Knepley 
397967a56275SMatthew G Knepley   Notes:
3980*bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3981*bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3982*bb7acecfSBarry Smith   of the input `DM`.
398367a56275SMatthew G Knepley 
398467a56275SMatthew G Knepley   Level: intermediate
398567a56275SMatthew G Knepley 
3986*bb7acecfSBarry Smith .seealso: `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
398767a56275SMatthew G Knepley @*/
398819fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
398967a56275SMatthew G Knepley {
399067a56275SMatthew G Knepley   DM             B;
399167a56275SMatthew G Knepley   char           convname[256];
3992c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
399367a56275SMatthew G Knepley 
399467a56275SMatthew G Knepley   PetscFunctionBegin;
399567a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
399667a56275SMatthew G Knepley   PetscValidType(dm,1);
399767a56275SMatthew G Knepley   PetscValidPointer(M,3);
39989566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype));
39999566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
4000c067b6caSMatthew G. Knepley   if (sametype) {
4001c067b6caSMatthew G. Knepley     *M   = dm;
40029566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) dm));
4003c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
4004c067b6caSMatthew G. Knepley   } else {
40050298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
400667a56275SMatthew G Knepley 
400767a56275SMatthew G Knepley     /*
400867a56275SMatthew G Knepley        Order of precedence:
400967a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
401067a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
401167a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
401267a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
401367a56275SMatthew G Knepley        5) Use a really basic converter.
401467a56275SMatthew G Knepley     */
401567a56275SMatthew G Knepley 
401667a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
40179566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname,"DMConvert_",sizeof(convname)));
40189566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname)));
40199566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_",sizeof(convname)));
40209566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,newtype,sizeof(convname)));
40219566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_C",sizeof(convname)));
40229566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm,convname,&conv));
402367a56275SMatthew G Knepley     if (conv) goto foundconv;
402467a56275SMatthew G Knepley 
402567a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40269566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40279566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40289566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname,"DMConvert_",sizeof(convname)));
40299566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname)));
40309566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_",sizeof(convname)));
40319566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,newtype,sizeof(convname)));
40329566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_C",sizeof(convname)));
40339566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B,convname,&conv));
403467a56275SMatthew G Knepley     if (conv) {
40359566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
403667a56275SMatthew G Knepley       goto foundconv;
403767a56275SMatthew G Knepley     }
403867a56275SMatthew G Knepley 
403967a56275SMatthew G Knepley #if 0
404067a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
404167a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40429566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
404367a56275SMatthew G Knepley     if (conv) goto foundconv;
404467a56275SMatthew G Knepley 
404567a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
404667a56275SMatthew G Knepley     if (dm->ops->convert) {
404767a56275SMatthew G Knepley       conv = dm->ops->convert;
404867a56275SMatthew G Knepley     }
404967a56275SMatthew G Knepley     if (conv) goto foundconv;
405067a56275SMatthew G Knepley #endif
405167a56275SMatthew G Knepley 
405267a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
405398921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
405467a56275SMatthew G Knepley 
405567a56275SMatthew G Knepley foundconv:
40569566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert,dm,0,0,0));
40579566063dSJacob Faibussowitsch     PetscCall((*conv)(dm,newtype,M));
405812fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
405990b157c4SStefano Zampini     {
40604fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40616858538eSMatthew G. Knepley 
40624fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40634fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M,  maxCell,  Lstart,  L));
4064c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40659566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40669566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype,(char**)&(*M)->vectype));
40679566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40689566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype,(char**)&(*M)->mattype));
406912fa691eSMatthew G. Knepley     }
40709566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert,dm,0,0,0));
407167a56275SMatthew G Knepley   }
40729566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject) *M));
407367a56275SMatthew G Knepley   PetscFunctionReturn(0);
407467a56275SMatthew G Knepley }
4075264ace61SBarry Smith 
4076264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4077264ace61SBarry Smith 
4078264ace61SBarry Smith /*@C
4079*bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40801c84c290SBarry Smith 
40811c84c290SBarry Smith   Not Collective
40821c84c290SBarry Smith 
40831c84c290SBarry Smith   Input Parameters:
40841c84c290SBarry Smith + name        - The name of a new user-defined creation routine
40851c84c290SBarry Smith - create_func - The creation routine itself
40861c84c290SBarry Smith 
40871c84c290SBarry Smith   Notes:
4088*bb7acecfSBarry Smith   DMRegister() may be called multiple times to add several user-defined `DM`s
40891c84c290SBarry Smith 
40901c84c290SBarry Smith   Sample usage:
40911c84c290SBarry Smith .vb
4092bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40931c84c290SBarry Smith .ve
40941c84c290SBarry Smith 
40951c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
40961c84c290SBarry Smith .vb
40971c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
40981c84c290SBarry Smith     DMSetType(DM,"my_da");
40991c84c290SBarry Smith .ve
41001c84c290SBarry Smith    or at runtime via the option
41011c84c290SBarry Smith .vb
41021c84c290SBarry Smith     -da_type my_da
41031c84c290SBarry Smith .ve
4104264ace61SBarry Smith 
4105264ace61SBarry Smith   Level: advanced
41061c84c290SBarry Smith 
4107*bb7acecfSBarry Smith .seealso: `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
41081c84c290SBarry Smith 
4109264ace61SBarry Smith @*/
4110bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
4111264ace61SBarry Smith {
4112264ace61SBarry Smith   PetscFunctionBegin;
41139566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
41149566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList,sname,function));
4115264ace61SBarry Smith   PetscFunctionReturn(0);
4116264ace61SBarry Smith }
4117264ace61SBarry Smith 
4118b859378eSBarry Smith /*@C
4119*bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4120b859378eSBarry Smith 
4121d083f849SBarry Smith   Collective on viewer
4122b859378eSBarry Smith 
4123b859378eSBarry Smith   Input Parameters:
4124*bb7acecfSBarry Smith + newdm - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4125*bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4126*bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4127*bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4128b859378eSBarry Smith 
4129b859378eSBarry Smith    Level: intermediate
4130b859378eSBarry Smith 
4131b859378eSBarry Smith   Notes:
413255849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4133b859378eSBarry Smith 
4134*bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4135*bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4136*bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4137cd7e8a5eSksagiyam 
4138b859378eSBarry Smith   Notes for advanced users:
4139b859378eSBarry Smith   Most users should not need to know the details of the binary storage
4140*bb7acecfSBarry Smith   format, since `DMLoad()` and `DMView()` completely hide these details.
4141b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
4142b859378eSBarry Smith   format is
4143b859378eSBarry Smith .vb
4144b859378eSBarry Smith      has not yet been determined
4145b859378eSBarry Smith .ve
4146b859378eSBarry Smith 
4147db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4148b859378eSBarry Smith @*/
4149b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
4150b859378eSBarry Smith {
41519331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
4152b859378eSBarry Smith 
4153b859378eSBarry Smith   PetscFunctionBegin;
4154b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
4155b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
41569566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41579566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary));
41589566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5));
41599566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load,viewer,0,0,0));
41609331c7a4SMatthew G. Knepley   if (isbinary) {
41619331c7a4SMatthew G. Knepley     PetscInt classid;
41629331c7a4SMatthew G. Knepley     char     type[256];
4163b859378eSBarry Smith 
41649566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT));
41657a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID,PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
41669566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR));
41679566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
41689566063dSJacob Faibussowitsch     if (newdm->ops->load) PetscCall((*newdm->ops->load)(newdm,viewer));
41699331c7a4SMatthew G. Knepley   } else if (ishdf5) {
41709566063dSJacob Faibussowitsch     if (newdm->ops->load) PetscCall((*newdm->ops->load)(newdm,viewer));
41719331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41729566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load,viewer,0,0,0));
4173b859378eSBarry Smith   PetscFunctionReturn(0);
4174b859378eSBarry Smith }
4175b859378eSBarry Smith 
41767da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41777da65231SMatthew G Knepley 
4178a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4179a6dfd86eSKarl Rupp {
41801d47ebbbSSatish Balay   PetscInt       f;
41811b30c384SMatthew G Knepley 
41827da65231SMatthew G Knepley   PetscFunctionBegin;
418363a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41841d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
41859566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41867da65231SMatthew G Knepley   }
41877da65231SMatthew G Knepley   PetscFunctionReturn(0);
41887da65231SMatthew G Knepley }
41897da65231SMatthew G Knepley 
4190a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4191a6dfd86eSKarl Rupp {
41921b30c384SMatthew G Knepley   PetscInt       f, g;
41937da65231SMatthew G Knepley 
41947da65231SMatthew G Knepley   PetscFunctionBegin;
419563a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41961d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
41979566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
41981d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
419963a3b9bcSJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f*cols+g])));
42007da65231SMatthew G Knepley     }
42019566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
42027da65231SMatthew G Knepley   }
42037da65231SMatthew G Knepley   PetscFunctionReturn(0);
42047da65231SMatthew G Knepley }
4205e7c4fc90SDmitry Karpeev 
42066113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4207e759306cSMatthew G. Knepley {
42080c5b8624SToby Isaac   PetscInt          localSize, bs;
42090c5b8624SToby Isaac   PetscMPIInt       size;
42100c5b8624SToby Isaac   Vec               x, xglob;
42110c5b8624SToby Isaac   const PetscScalar *xarray;
4212e759306cSMatthew G. Knepley 
4213e759306cSMatthew G. Knepley   PetscFunctionBegin;
42149566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size));
42159566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
42169566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
42179566063dSJacob Faibussowitsch   PetscCall(VecChop(x, tol));
42189566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name));
42190c5b8624SToby Isaac   if (size > 1) {
42209566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x,&localSize));
42219566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x,&xarray));
42229566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x,&bs));
42239566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob));
42240c5b8624SToby Isaac   } else {
42250c5b8624SToby Isaac     xglob = x;
42260c5b8624SToby Isaac   }
42279566063dSJacob Faibussowitsch   PetscCall(VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm))));
42280c5b8624SToby Isaac   if (size > 1) {
42299566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42309566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x,&xarray));
42310c5b8624SToby Isaac   }
42329566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
4233e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4234e759306cSMatthew G. Knepley }
4235e759306cSMatthew G. Knepley 
423688ed4aceSMatthew G Knepley /*@
4237*bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4238061576a5SJed Brown 
4239061576a5SJed Brown   Input Parameter:
4240*bb7acecfSBarry Smith . dm - The `DM`
4241061576a5SJed Brown 
4242061576a5SJed Brown   Output Parameter:
4243*bb7acecfSBarry Smith . section - The `PetscSection`
4244061576a5SJed Brown 
4245061576a5SJed Brown   Options Database Keys:
4246*bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4247061576a5SJed Brown 
4248061576a5SJed Brown   Level: advanced
4249061576a5SJed Brown 
4250061576a5SJed Brown   Notes:
4251*bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4252061576a5SJed Brown 
4253*bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4254061576a5SJed Brown 
4255db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4256061576a5SJed Brown @*/
4257061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4258061576a5SJed Brown {
4259061576a5SJed Brown   PetscFunctionBegin;
42609566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm,section));
4261061576a5SJed Brown   PetscFunctionReturn(0);
4262061576a5SJed Brown }
4263061576a5SJed Brown 
4264061576a5SJed Brown /*@
4265*bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
426688ed4aceSMatthew G Knepley 
426788ed4aceSMatthew G Knepley   Input Parameter:
4268*bb7acecfSBarry Smith . dm - The `DM`
426988ed4aceSMatthew G Knepley 
427088ed4aceSMatthew G Knepley   Output Parameter:
4271*bb7acecfSBarry Smith . section - The `PetscSection`
427288ed4aceSMatthew G Knepley 
4273e5893cccSMatthew G. Knepley   Options Database Keys:
4274*bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4275e5893cccSMatthew G. Knepley 
427688ed4aceSMatthew G Knepley   Level: intermediate
427788ed4aceSMatthew G Knepley 
4278*bb7acecfSBarry Smith   Note:
4279*bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
428088ed4aceSMatthew G Knepley 
4281db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetGlobalSection()`
428288ed4aceSMatthew G Knepley @*/
4283061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
42840adebc6cSBarry Smith {
428588ed4aceSMatthew G Knepley   PetscFunctionBegin;
428688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
428788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42881bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4289e5e52638SMatthew G. Knepley     PetscInt d;
4290e5e52638SMatthew G. Knepley 
429145480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
429245480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject) dm;
429345480ffeSMatthew G. Knepley       PetscViewer       viewer;
429445480ffeSMatthew G. Knepley       PetscViewerFormat format;
429545480ffeSMatthew G. Knepley       PetscBool         flg;
429645480ffeSMatthew G. Knepley 
42979566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
42989566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
429945480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
43009566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
43019566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
430245480ffeSMatthew G. Knepley       }
430345480ffeSMatthew G. Knepley       if (flg) {
43049566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
43059566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
43069566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
430745480ffeSMatthew G. Knepley       }
430845480ffeSMatthew G. Knepley     }
43099566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createlocalsection)(dm));
43109566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view"));
43112f0f8703SMatthew G. Knepley   }
43121bb6d2a8SBarry Smith   *section = dm->localSection;
431388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
431488ed4aceSMatthew G Knepley }
431588ed4aceSMatthew G Knepley 
431688ed4aceSMatthew G Knepley /*@
4317*bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4318061576a5SJed Brown 
4319061576a5SJed Brown   Input Parameters:
4320*bb7acecfSBarry Smith + dm - The `DM`
4321*bb7acecfSBarry Smith - section - The `PetscSection`
4322061576a5SJed Brown 
4323061576a5SJed Brown   Level: advanced
4324061576a5SJed Brown 
4325061576a5SJed Brown   Notes:
4326*bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4327061576a5SJed Brown 
4328*bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4329061576a5SJed Brown 
4330db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4331061576a5SJed Brown @*/
4332061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4333061576a5SJed Brown {
4334061576a5SJed Brown   PetscFunctionBegin;
43359566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm,section));
4336061576a5SJed Brown   PetscFunctionReturn(0);
4337061576a5SJed Brown }
4338061576a5SJed Brown 
4339061576a5SJed Brown /*@
4340*bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
434188ed4aceSMatthew G Knepley 
434288ed4aceSMatthew G Knepley   Input Parameters:
4343*bb7acecfSBarry Smith + dm - The `DM`
4344*bb7acecfSBarry Smith - section - The `PetscSection`
434588ed4aceSMatthew G Knepley 
434688ed4aceSMatthew G Knepley   Level: intermediate
434788ed4aceSMatthew G Knepley 
4348*bb7acecfSBarry Smith   Note:
4349*bb7acecfSBarry Smith   Any existing Section will be destroyed
435088ed4aceSMatthew G Knepley 
4351*bb7acecfSBarry Smith .seealso: `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
435288ed4aceSMatthew G Knepley @*/
4353061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
43540adebc6cSBarry Smith {
4355c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4356af122d2aSMatthew G Knepley   PetscInt       f;
435788ed4aceSMatthew G Knepley 
435888ed4aceSMatthew G Knepley   PetscFunctionBegin;
435988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4360b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
43619566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43629566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43631bb6d2a8SBarry Smith   dm->localSection = section;
43649566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4365af122d2aSMatthew G Knepley   if (numFields) {
43669566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4367af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43680f21e855SMatthew G. Knepley       PetscObject disc;
4369af122d2aSMatthew G Knepley       const char *name;
4370af122d2aSMatthew G Knepley 
43719566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43729566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43739566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4374af122d2aSMatthew G Knepley     }
4375af122d2aSMatthew G Knepley   }
4376e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43779566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
437888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
437988ed4aceSMatthew G Knepley }
438088ed4aceSMatthew G Knepley 
43819435951eSToby Isaac /*@
4382*bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
43839435951eSToby Isaac 
4384e228b242SToby Isaac   not collective
4385e228b242SToby Isaac 
43869435951eSToby Isaac   Input Parameter:
4387*bb7acecfSBarry Smith . dm - The `DM`
43889435951eSToby Isaac 
4389d8d19677SJose E. Roman   Output Parameters:
4390*bb7acecfSBarry 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.
4391*bb7acecfSBarry 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.
439279769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
43939435951eSToby Isaac 
43949435951eSToby Isaac   Level: advanced
43959435951eSToby Isaac 
4396*bb7acecfSBarry Smith   Note:
4397*bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
43989435951eSToby Isaac 
4399db781477SPatrick Sanan .seealso: `DMSetDefaultConstraints()`
44009435951eSToby Isaac @*/
440179769bd5SJed Brown PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
44029435951eSToby Isaac {
44039435951eSToby Isaac   PetscFunctionBegin;
44049435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44059566063dSJacob Faibussowitsch   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscCall((*dm->ops->createdefaultconstraints)(dm));
44063b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44073b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
440879769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44099435951eSToby Isaac   PetscFunctionReturn(0);
44109435951eSToby Isaac }
44119435951eSToby Isaac 
44129435951eSToby Isaac /*@
4413*bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44149435951eSToby Isaac 
4415*bb7acecfSBarry 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()`.
44169435951eSToby Isaac 
4417*bb7acecfSBarry 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.
44189435951eSToby Isaac 
4419e228b242SToby Isaac   collective on dm
4420e228b242SToby Isaac 
44219435951eSToby Isaac   Input Parameters:
4422*bb7acecfSBarry Smith + dm - The `DM`
4423*bb7acecfSBarry 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).
4424*bb7acecfSBarry 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).
4425*bb7acecfSBarry 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).
44269435951eSToby Isaac 
44279435951eSToby Isaac   Level: advanced
44289435951eSToby Isaac 
4429*bb7acecfSBarry Smith   Note:
4430*bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44319435951eSToby Isaac 
4432db781477SPatrick Sanan .seealso: `DMGetDefaultConstraints()`
44339435951eSToby Isaac @*/
443479769bd5SJed Brown PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
44359435951eSToby Isaac {
4436e228b242SToby Isaac   PetscMPIInt result;
44379435951eSToby Isaac 
44389435951eSToby Isaac   PetscFunctionBegin;
44399435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4440e228b242SToby Isaac   if (section) {
4441e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
44429566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result));
44437a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4444e228b242SToby Isaac   }
4445e228b242SToby Isaac   if (mat) {
4446e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
44479566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result));
44487a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4449e228b242SToby Isaac   }
445079769bd5SJed Brown   if (bias) {
445179769bd5SJed Brown     PetscValidHeaderSpecific(bias,VEC_CLASSID,4);
44529566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)bias),&result));
445379769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint bias must have local communicator");
445479769bd5SJed Brown   }
44559566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44569566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44573b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44589566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44599566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44603b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44619566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44629566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
446379769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44649435951eSToby Isaac   PetscFunctionReturn(0);
44659435951eSToby Isaac }
44669435951eSToby Isaac 
4467497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4468507e4973SMatthew G. Knepley /*
4469*bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4470507e4973SMatthew G. Knepley 
4471507e4973SMatthew G. Knepley   Input Parameters:
4472*bb7acecfSBarry Smith + dm - The `DM`
4473*bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4474*bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4475507e4973SMatthew G. Knepley 
4476507e4973SMatthew G. Knepley   Level: intermediate
4477507e4973SMatthew G. Knepley 
4478db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`
4479507e4973SMatthew G. Knepley */
4480f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4481507e4973SMatthew G. Knepley {
4482507e4973SMatthew G. Knepley   MPI_Comm        comm;
4483507e4973SMatthew G. Knepley   PetscLayout     layout;
4484507e4973SMatthew G. Knepley   const PetscInt *ranges;
4485507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4486507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4487507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4488507e4973SMatthew G. Knepley 
4489507e4973SMatthew G. Knepley   PetscFunctionBegin;
44909566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
4491507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44929566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
44939566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44949566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
44959566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
44969566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
44979566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
44989566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
44999566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45009566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4501507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4502f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4503507e4973SMatthew G. Knepley 
45049566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45059566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45069566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45079566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45089566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45099566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4510507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
451163a3b9bcSJacob Faibussowitsch     if ((gdof < 0 ? -(gdof+1) : gdof) != dof) {PetscCall(PetscSynchronizedPrintf(comm, "[%d]Global dof %" PetscInt_FMT " for point %" PetscInt_FMT " not equal to local dof %" PetscInt_FMT "\n", rank, gdof, p, dof)); valid = PETSC_FALSE;}
451263a3b9bcSJacob Faibussowitsch     if (gcdof && (gcdof != cdof)) {PetscCall(PetscSynchronizedPrintf(comm, "[%d]Global constraints %" PetscInt_FMT " for point %" PetscInt_FMT " not equal to local constraints %" PetscInt_FMT "\n", rank, gcdof, p, cdof)); valid = PETSC_FALSE;}
4513507e4973SMatthew G. Knepley     if (gdof < 0) {
4514507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4515507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4516507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4517507e4973SMatthew G. Knepley 
45189566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset,size+1,ranges,&r));
4519507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
452063a3b9bcSJacob Faibussowitsch         if ((r < 0) || (r >= size)) {PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff)); valid = PETSC_FALSE;break;}
4521507e4973SMatthew G. Knepley       }
4522507e4973SMatthew G. Knepley     }
4523507e4973SMatthew G. Knepley   }
45249566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45259566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
45261c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4527507e4973SMatthew G. Knepley   if (!gvalid) {
45289566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4529507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4530507e4973SMatthew G. Knepley   }
4531507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4532507e4973SMatthew G. Knepley }
4533f741bcd2SMatthew G. Knepley #endif
4534507e4973SMatthew G. Knepley 
453588ed4aceSMatthew G Knepley /*@
4536*bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
453788ed4aceSMatthew G Knepley 
4538d083f849SBarry Smith   Collective on dm
45398b1ab98fSJed Brown 
454088ed4aceSMatthew G Knepley   Input Parameter:
4541*bb7acecfSBarry Smith . dm - The `DM`
454288ed4aceSMatthew G Knepley 
454388ed4aceSMatthew G Knepley   Output Parameter:
4544*bb7acecfSBarry Smith . section - The `PetscSection`
454588ed4aceSMatthew G Knepley 
454688ed4aceSMatthew G Knepley   Level: intermediate
454788ed4aceSMatthew G Knepley 
4548*bb7acecfSBarry Smith   Note:
4549*bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
455088ed4aceSMatthew G Knepley 
4551db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`
455288ed4aceSMatthew G Knepley @*/
4553e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
45540adebc6cSBarry Smith {
455588ed4aceSMatthew G Knepley   PetscFunctionBegin;
455688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
455788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
45581bb6d2a8SBarry Smith   if (!dm->globalSection) {
4559fd59a867SMatthew G. Knepley     PetscSection s;
4560fd59a867SMatthew G. Knepley 
45619566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
45627a8be351SBarry Smith     PetscCheck(s,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
45637a8be351SBarry Smith     PetscCheck(dm->sf,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45649566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
45659566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
45669566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
45679566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
456888ed4aceSMatthew G Knepley   }
45691bb6d2a8SBarry Smith   *section = dm->globalSection;
457088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
457188ed4aceSMatthew G Knepley }
457288ed4aceSMatthew G Knepley 
4573b21d0597SMatthew G Knepley /*@
4574*bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4575b21d0597SMatthew G Knepley 
4576b21d0597SMatthew G Knepley   Input Parameters:
4577*bb7acecfSBarry Smith + dm - The `DM`
45785080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4579b21d0597SMatthew G Knepley 
4580b21d0597SMatthew G Knepley   Level: intermediate
4581b21d0597SMatthew G Knepley 
4582*bb7acecfSBarry Smith   Note:
4583*bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4584b21d0597SMatthew G Knepley 
4585db781477SPatrick Sanan .seealso: `DMGetGlobalSection()`, `DMSetLocalSection()`
4586b21d0597SMatthew G Knepley @*/
4587e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
45880adebc6cSBarry Smith {
4589b21d0597SMatthew G Knepley   PetscFunctionBegin;
4590b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45915080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
45929566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45939566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
45941bb6d2a8SBarry Smith   dm->globalSection = section;
4595497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
45969566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4597507e4973SMatthew G. Knepley #endif
4598b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4599b21d0597SMatthew G Knepley }
4600b21d0597SMatthew G Knepley 
460188ed4aceSMatthew G Knepley /*@
4602*bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4603*bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
460488ed4aceSMatthew G Knepley 
460588ed4aceSMatthew G Knepley   Input Parameter:
4606*bb7acecfSBarry Smith . dm - The `DM`
460788ed4aceSMatthew G Knepley 
460888ed4aceSMatthew G Knepley   Output Parameter:
4609*bb7acecfSBarry Smith . sf - The `PetscSF`
461088ed4aceSMatthew G Knepley 
461188ed4aceSMatthew G Knepley   Level: intermediate
461288ed4aceSMatthew G Knepley 
4613*bb7acecfSBarry Smith   Note:
4614*bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
461588ed4aceSMatthew G Knepley 
4616db781477SPatrick Sanan .seealso: `DMSetSectionSF()`, `DMCreateSectionSF()`
461788ed4aceSMatthew G Knepley @*/
46181bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
46190adebc6cSBarry Smith {
462088ed4aceSMatthew G Knepley   PetscInt       nroots;
462188ed4aceSMatthew G Knepley 
462288ed4aceSMatthew G Knepley   PetscFunctionBegin;
462388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
462488ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
46251bb6d2a8SBarry Smith   if (!dm->sectionSF) {
46269566063dSJacob Faibussowitsch     PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF));
462733907cc2SStefano Zampini   }
46289566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
462988ed4aceSMatthew G Knepley   if (nroots < 0) {
463088ed4aceSMatthew G Knepley     PetscSection section, gSection;
463188ed4aceSMatthew G Knepley 
46329566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
463331ea6d37SMatthew G Knepley     if (section) {
46349566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
46359566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
463631ea6d37SMatthew G Knepley     } else {
46370298fd71SBarry Smith       *sf = NULL;
463831ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
463931ea6d37SMatthew G Knepley     }
464088ed4aceSMatthew G Knepley   }
46411bb6d2a8SBarry Smith   *sf = dm->sectionSF;
464288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
464388ed4aceSMatthew G Knepley }
464488ed4aceSMatthew G Knepley 
464588ed4aceSMatthew G Knepley /*@
4646*bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
464788ed4aceSMatthew G Knepley 
464888ed4aceSMatthew G Knepley   Input Parameters:
4649*bb7acecfSBarry Smith + dm - The `DM`
4650*bb7acecfSBarry Smith - sf - The `PetscSF`
465188ed4aceSMatthew G Knepley 
465288ed4aceSMatthew G Knepley   Level: intermediate
465388ed4aceSMatthew G Knepley 
4654*bb7acecfSBarry Smith   Note:
4655*bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
465688ed4aceSMatthew G Knepley 
4657db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMCreateSectionSF()`
465888ed4aceSMatthew G Knepley @*/
46591bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
46600adebc6cSBarry Smith {
466188ed4aceSMatthew G Knepley   PetscFunctionBegin;
466288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4663b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46649566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
46659566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
46661bb6d2a8SBarry Smith   dm->sectionSF = sf;
466788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
466888ed4aceSMatthew G Knepley }
466988ed4aceSMatthew G Knepley 
467088ed4aceSMatthew G Knepley /*@C
4671*bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
467288ed4aceSMatthew G Knepley   describing the data layout.
467388ed4aceSMatthew G Knepley 
467488ed4aceSMatthew G Knepley   Input Parameters:
4675*bb7acecfSBarry Smith + dm - The `DM`
4676*bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4677*bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
467888ed4aceSMatthew G Knepley 
46791bb6d2a8SBarry Smith   Level: developer
46801bb6d2a8SBarry Smith 
4681*bb7acecfSBarry Smith   Note:
4682*bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4683*bb7acecfSBarry Smith 
4684*bb7acecfSBarry Smith   Developer Note:
4685*bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4686*bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4687*bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
46881bb6d2a8SBarry Smith 
4689db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
469088ed4aceSMatthew G Knepley @*/
46911bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
469288ed4aceSMatthew G Knepley {
469388ed4aceSMatthew G Knepley   PetscFunctionBegin;
469488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46959566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
469688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
469788ed4aceSMatthew G Knepley }
4698af122d2aSMatthew G Knepley 
4699b21d0597SMatthew G Knepley /*@
4700*bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4701*bb7acecfSBarry Smith 
4702*bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4703b21d0597SMatthew G Knepley 
4704b21d0597SMatthew G Knepley   Input Parameter:
4705*bb7acecfSBarry Smith . dm - The `DM`
4706b21d0597SMatthew G Knepley 
4707b21d0597SMatthew G Knepley   Output Parameter:
4708*bb7acecfSBarry Smith . sf - The `PetscSF`
4709b21d0597SMatthew G Knepley 
4710b21d0597SMatthew G Knepley   Level: intermediate
4711b21d0597SMatthew G Knepley 
4712*bb7acecfSBarry Smith   Note:
4713*bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4714b21d0597SMatthew G Knepley 
4715db781477SPatrick Sanan .seealso: `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4716b21d0597SMatthew G Knepley @*/
47170adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
47180adebc6cSBarry Smith {
4719b21d0597SMatthew G Knepley   PetscFunctionBegin;
4720b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4721b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4722b21d0597SMatthew G Knepley   *sf = dm->sf;
4723b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4724b21d0597SMatthew G Knepley }
4725b21d0597SMatthew G Knepley 
4726057b4bcdSMatthew G Knepley /*@
4727*bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4728*bb7acecfSBarry Smith 
4729*bb7acecfSBarry Smith   Collective on dm
4730057b4bcdSMatthew G Knepley 
4731057b4bcdSMatthew G Knepley   Input Parameters:
4732*bb7acecfSBarry Smith + dm - The `DM`
4733*bb7acecfSBarry Smith - sf - The` PetscSF`
4734057b4bcdSMatthew G Knepley 
4735057b4bcdSMatthew G Knepley   Level: intermediate
4736057b4bcdSMatthew G Knepley 
4737db781477SPatrick Sanan .seealso: `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4738057b4bcdSMatthew G Knepley @*/
47390adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
47400adebc6cSBarry Smith {
4741057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4742057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4743b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47449566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
47459566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4746057b4bcdSMatthew G Knepley   dm->sf = sf;
4747057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4748057b4bcdSMatthew G Knepley }
4749057b4bcdSMatthew G Knepley 
47504f37162bSMatthew G. Knepley /*@
4751*bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
47524f37162bSMatthew G. Knepley 
47534f37162bSMatthew G. Knepley   Input Parameter:
4754*bb7acecfSBarry Smith . dm - The `DM`
47554f37162bSMatthew G. Knepley 
47564f37162bSMatthew G. Knepley   Output Parameter:
4757*bb7acecfSBarry Smith . sf - The `PetscSF`
47584f37162bSMatthew G. Knepley 
47594f37162bSMatthew G. Knepley   Level: intermediate
47604f37162bSMatthew G. Knepley 
4761*bb7acecfSBarry Smith   Note:
4762*bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
47634f37162bSMatthew G. Knepley 
4764db781477SPatrick Sanan .seealso: `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47654f37162bSMatthew G. Knepley @*/
47664f37162bSMatthew G. Knepley PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
47674f37162bSMatthew G. Knepley {
47684f37162bSMatthew G. Knepley   PetscFunctionBegin;
47694f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47704f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
47714f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
47724f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
47734f37162bSMatthew G. Knepley }
47744f37162bSMatthew G. Knepley 
47754f37162bSMatthew G. Knepley /*@
47764f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
47774f37162bSMatthew G. Knepley 
47784f37162bSMatthew G. Knepley   Input Parameters:
47794f37162bSMatthew G. Knepley + dm - The DM
47804f37162bSMatthew G. Knepley - sf - The PetscSF
47814f37162bSMatthew G. Knepley 
47824f37162bSMatthew G. Knepley   Level: intermediate
47834f37162bSMatthew G. Knepley 
4784db781477SPatrick Sanan .seealso: `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47854f37162bSMatthew G. Knepley @*/
47864f37162bSMatthew G. Knepley PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
47874f37162bSMatthew G. Knepley {
47884f37162bSMatthew G. Knepley   PetscFunctionBegin;
47894f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47904f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47919566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
47929566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
47934f37162bSMatthew G. Knepley   dm->sfNatural = sf;
47944f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
47954f37162bSMatthew G. Knepley }
47964f37162bSMatthew G. Knepley 
479734aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
479834aa8a36SMatthew G. Knepley {
479934aa8a36SMatthew G. Knepley   PetscClassId   id;
480034aa8a36SMatthew G. Knepley 
480134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48029566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
480334aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48049566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
480534aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48069566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
480717c1d62eSMatthew G. Knepley   } else {
48089566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
480934aa8a36SMatthew G. Knepley   }
481034aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
481134aa8a36SMatthew G. Knepley }
481234aa8a36SMatthew G. Knepley 
481344a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
481444a7f3ddSMatthew G. Knepley {
481544a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
481644a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
481744a7f3ddSMatthew G. Knepley 
481844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
481944a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
48209566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
482144a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4822e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
48239566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
482444a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
482544a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
482644a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
482744a7f3ddSMatthew G. Knepley }
482844a7f3ddSMatthew G. Knepley 
482944a7f3ddSMatthew G. Knepley /*@
483044a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
483144a7f3ddSMatthew G. Knepley 
4832d083f849SBarry Smith   Logically collective on dm
483344a7f3ddSMatthew G. Knepley 
483444a7f3ddSMatthew G. Knepley   Input Parameter:
483544a7f3ddSMatthew G. Knepley . dm - The DM
483644a7f3ddSMatthew G. Knepley 
483744a7f3ddSMatthew G. Knepley   Level: intermediate
483844a7f3ddSMatthew G. Knepley 
4839db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
484044a7f3ddSMatthew G. Knepley @*/
484144a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
484244a7f3ddSMatthew G. Knepley {
484344a7f3ddSMatthew G. Knepley   PetscInt       f;
484444a7f3ddSMatthew G. Knepley 
484544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
484644a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
484744a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
48489566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
48499566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
485044a7f3ddSMatthew G. Knepley   }
48519566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
485244a7f3ddSMatthew G. Knepley   dm->fields = NULL;
485344a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
485444a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
485544a7f3ddSMatthew G. Knepley }
485644a7f3ddSMatthew G. Knepley 
4857689b5837SMatthew G. Knepley /*@
4858689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4859689b5837SMatthew G. Knepley 
4860689b5837SMatthew G. Knepley   Not collective
4861689b5837SMatthew G. Knepley 
4862689b5837SMatthew G. Knepley   Input Parameter:
4863689b5837SMatthew G. Knepley . dm - The DM
4864689b5837SMatthew G. Knepley 
4865689b5837SMatthew G. Knepley   Output Parameter:
4866689b5837SMatthew G. Knepley . Nf - The number of fields
4867689b5837SMatthew G. Knepley 
4868689b5837SMatthew G. Knepley   Level: intermediate
4869689b5837SMatthew G. Knepley 
4870db781477SPatrick Sanan .seealso: `DMSetNumFields()`, `DMSetField()`
4871689b5837SMatthew G. Knepley @*/
48720f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
48730f21e855SMatthew G. Knepley {
48740f21e855SMatthew G. Knepley   PetscFunctionBegin;
48750f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4876534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
487744a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4878af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4879af122d2aSMatthew G Knepley }
4880af122d2aSMatthew G Knepley 
4881689b5837SMatthew G. Knepley /*@
4882689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4883689b5837SMatthew G. Knepley 
4884d083f849SBarry Smith   Logically collective on dm
4885689b5837SMatthew G. Knepley 
4886689b5837SMatthew G. Knepley   Input Parameters:
4887689b5837SMatthew G. Knepley + dm - The DM
4888689b5837SMatthew G. Knepley - Nf - The number of fields
4889689b5837SMatthew G. Knepley 
4890689b5837SMatthew G. Knepley   Level: intermediate
4891689b5837SMatthew G. Knepley 
4892db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetField()`
4893689b5837SMatthew G. Knepley @*/
4894af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4895af122d2aSMatthew G Knepley {
48960f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4897af122d2aSMatthew G Knepley 
4898af122d2aSMatthew G Knepley   PetscFunctionBegin;
4899af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49009566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49010f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49020f21e855SMatthew G. Knepley     PetscContainer obj;
49030f21e855SMatthew G. Knepley 
49049566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj));
49059566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject) obj));
49069566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4907af122d2aSMatthew G Knepley   }
4908af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4909af122d2aSMatthew G Knepley }
4910af122d2aSMatthew G Knepley 
4911c1929be8SMatthew G. Knepley /*@
4912*bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4913c1929be8SMatthew G. Knepley 
4914c1929be8SMatthew G. Knepley   Not collective
4915c1929be8SMatthew G. Knepley 
4916c1929be8SMatthew G. Knepley   Input Parameters:
4917*bb7acecfSBarry Smith + dm - The `DM`
4918c1929be8SMatthew G. Knepley - f  - The field number
4919c1929be8SMatthew G. Knepley 
492044a7f3ddSMatthew G. Knepley   Output Parameters:
4921*bb7acecfSBarry Smith + label - The label indicating the support of the field, or NULL for the entire mesh (pass in NULL if not needed)
4922*bb7acecfSBarry Smith - disc - The discretization object (pass in NULL if not needed)
4923c1929be8SMatthew G. Knepley 
492444a7f3ddSMatthew G. Knepley   Level: intermediate
4925c1929be8SMatthew G. Knepley 
4926db781477SPatrick Sanan .seealso: `DMAddField()`, `DMSetField()`
4927c1929be8SMatthew G. Knepley @*/
4928*bb7acecfSBarry Smith PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4929af122d2aSMatthew G Knepley {
4930af122d2aSMatthew G Knepley   PetscFunctionBegin;
4931af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4932*bb7acecfSBarry Smith   PetscValidPointer(disc, 4);
49337a8be351SBarry 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);
493444a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4935*bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
4936decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4937decb47aaSMatthew G. Knepley }
4938decb47aaSMatthew G. Knepley 
4939083401c6SMatthew G. Knepley /* Does not clear the DS */
4940*bb7acecfSBarry Smith PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4941083401c6SMatthew G. Knepley {
4942083401c6SMatthew G. Knepley   PetscFunctionBegin;
49439566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f+1));
49449566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
49459566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4946083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4947*bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
49489566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
4949*bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject) disc));
4950083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4951083401c6SMatthew G. Knepley }
4952083401c6SMatthew G. Knepley 
4953c1929be8SMatthew G. Knepley /*@
4954*bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
4955*bb7acecfSBarry Smith   the field numbering.
4956c1929be8SMatthew G. Knepley 
4957d083f849SBarry Smith   Logically collective on dm
4958c1929be8SMatthew G. Knepley 
4959c1929be8SMatthew G. Knepley   Input Parameters:
4960*bb7acecfSBarry Smith + dm    - The `DM`
4961c1929be8SMatthew G. Knepley . f     - The field number
496244a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4963*bb7acecfSBarry Smith - disc - The discretization object
4964c1929be8SMatthew G. Knepley 
496544a7f3ddSMatthew G. Knepley   Level: intermediate
4966c1929be8SMatthew G. Knepley 
4967db781477SPatrick Sanan .seealso: `DMAddField()`, `DMGetField()`
4968c1929be8SMatthew G. Knepley @*/
4969*bb7acecfSBarry Smith PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4970decb47aaSMatthew G. Knepley {
4971decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4972decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4973e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
4974*bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
49757a8be351SBarry Smith   PetscCheck(f >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
4976*bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
4977*bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
49789566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
497944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
498044a7f3ddSMatthew G. Knepley }
498144a7f3ddSMatthew G. Knepley 
498244a7f3ddSMatthew G. Knepley /*@
4983*bb7acecfSBarry 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)
4984*bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
498544a7f3ddSMatthew G. Knepley 
4986d083f849SBarry Smith   Logically collective on dm
498744a7f3ddSMatthew G. Knepley 
498844a7f3ddSMatthew G. Knepley   Input Parameters:
4989*bb7acecfSBarry Smith + dm    - The `DM`
499044a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4991*bb7acecfSBarry Smith - disc - The discretization object
499244a7f3ddSMatthew G. Knepley 
499344a7f3ddSMatthew G. Knepley   Level: intermediate
499444a7f3ddSMatthew G. Knepley 
4995*bb7acecfSBarry Smith   Notes:
4996*bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
4997*bb7acecfSBarry Smith 
4998*bb7acecfSBarry Smith   For example, a piecewise continous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
4999*bb7acecfSBarry 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
5000*bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5001*bb7acecfSBarry Smith 
5002*bb7acecfSBarry Smith .seealso: `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
500344a7f3ddSMatthew G. Knepley @*/
5004*bb7acecfSBarry Smith PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
500544a7f3ddSMatthew G. Knepley {
500644a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
500744a7f3ddSMatthew G. Knepley 
500844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
500944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5010064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5011*bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50129566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf+1));
501344a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5014*bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50159566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
5016*bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject) disc));
5017*bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50189566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
5019af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
5020af122d2aSMatthew G Knepley }
50216636e97aSMatthew G Knepley 
5022e5e52638SMatthew G. Knepley /*@
5023e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5024e0b68406SMatthew Knepley 
5025e0b68406SMatthew Knepley   Logically collective on dm
5026e0b68406SMatthew Knepley 
5027e0b68406SMatthew Knepley   Input Parameters:
5028*bb7acecfSBarry Smith + dm          - The `DM`
5029e0b68406SMatthew Knepley . f           - The field index
5030*bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5031e0b68406SMatthew Knepley 
5032e0b68406SMatthew Knepley   Level: intermediate
5033e0b68406SMatthew Knepley 
5034db781477SPatrick Sanan .seealso: `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5035e0b68406SMatthew Knepley @*/
5036e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5037e0b68406SMatthew Knepley {
5038e0b68406SMatthew Knepley   PetscFunctionBegin;
503963a3b9bcSJacob 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);
5040e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
5041e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5042e0b68406SMatthew Knepley }
5043e0b68406SMatthew Knepley 
5044e0b68406SMatthew Knepley /*@
5045e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5046e0b68406SMatthew Knepley 
5047*bb7acecfSBarry Smith   Not collective
5048e0b68406SMatthew Knepley 
5049e0b68406SMatthew Knepley   Input Parameters:
5050*bb7acecfSBarry Smith + dm          - The `DM`
5051e0b68406SMatthew Knepley - f           - The field index
5052e0b68406SMatthew Knepley 
5053e0b68406SMatthew Knepley   Output Parameter:
5054e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5055e0b68406SMatthew Knepley 
5056e0b68406SMatthew Knepley   Level: intermediate
5057e0b68406SMatthew Knepley 
5058*bb7acecfSBarry Smith  .seealso: `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5059e0b68406SMatthew Knepley @*/
5060e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5061e0b68406SMatthew Knepley {
5062e0b68406SMatthew Knepley   PetscFunctionBegin;
506363a3b9bcSJacob 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);
5064e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
5065e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5066e0b68406SMatthew Knepley }
5067e0b68406SMatthew Knepley 
5068e0b68406SMatthew Knepley /*@
5069*bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5070e5e52638SMatthew G. Knepley 
5071d083f849SBarry Smith   Collective on dm
5072e5e52638SMatthew G. Knepley 
5073e5e52638SMatthew G. Knepley   Input Parameter:
5074*bb7acecfSBarry Smith . dm - The `DM`
5075e5e52638SMatthew G. Knepley 
5076e5e52638SMatthew G. Knepley   Output Parameter:
5077*bb7acecfSBarry Smith . newdm - The `DM`
5078e5e52638SMatthew G. Knepley 
5079e5e52638SMatthew G. Knepley   Level: advanced
5080e5e52638SMatthew G. Knepley 
5081db781477SPatrick Sanan .seealso: `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5082e5e52638SMatthew G. Knepley @*/
5083e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
5084e5e52638SMatthew G. Knepley {
5085e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
5086e5e52638SMatthew G. Knepley 
5087e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5088e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
50899566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
50909566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5091e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5092e5e52638SMatthew G. Knepley     DMLabel     label;
5093e5e52638SMatthew G. Knepley     PetscObject field;
509434aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5095e5e52638SMatthew G. Knepley 
50969566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
50979566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
50989566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
50999566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
510034aa8a36SMatthew G. Knepley   }
510134aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
510234aa8a36SMatthew G. Knepley }
510334aa8a36SMatthew G. Knepley 
510434aa8a36SMatthew G. Knepley /*@
510534aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
510634aa8a36SMatthew G. Knepley 
510734aa8a36SMatthew G. Knepley   Not collective
510834aa8a36SMatthew G. Knepley 
510934aa8a36SMatthew G. Knepley   Input Parameters:
511034aa8a36SMatthew G. Knepley + dm - The DM object
511134aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
511234aa8a36SMatthew G. Knepley 
5113d8d19677SJose E. Roman   Output Parameters:
511434aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
511534aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
511634aa8a36SMatthew G. Knepley 
511734aa8a36SMatthew G. Knepley   Notes:
511834aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
511934aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
512034aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5121979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
512234aa8a36SMatthew G. Knepley 
512334aa8a36SMatthew G. Knepley   Level: developer
512434aa8a36SMatthew G. Knepley 
5125db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
512634aa8a36SMatthew G. Knepley @*/
512734aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
512834aa8a36SMatthew G. Knepley {
512934aa8a36SMatthew G. Knepley   PetscFunctionBegin;
513034aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5131534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
5132534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
513334aa8a36SMatthew G. Knepley   if (f < 0) {
513434aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
513534aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
513634aa8a36SMatthew G. Knepley   } else {
513734aa8a36SMatthew G. Knepley     PetscInt       Nf;
513834aa8a36SMatthew G. Knepley 
51399566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51407a8be351SBarry Smith     PetscCheck(f < Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
514134aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
514234aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
514334aa8a36SMatthew G. Knepley   }
514434aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
514534aa8a36SMatthew G. Knepley }
514634aa8a36SMatthew G. Knepley 
514734aa8a36SMatthew G. Knepley /*@
514834aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
514934aa8a36SMatthew G. Knepley 
515034aa8a36SMatthew G. Knepley   Not collective
515134aa8a36SMatthew G. Knepley 
515234aa8a36SMatthew G. Knepley   Input Parameters:
515334aa8a36SMatthew G. Knepley + dm         - The DM object
515434aa8a36SMatthew G. Knepley . f          - The field number
515534aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
515634aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
515734aa8a36SMatthew G. Knepley 
515834aa8a36SMatthew G. Knepley   Notes:
515934aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
516034aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
516134aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5162979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
516334aa8a36SMatthew G. Knepley 
516434aa8a36SMatthew G. Knepley   Level: developer
516534aa8a36SMatthew G. Knepley 
5166db781477SPatrick Sanan .seealso: `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
516734aa8a36SMatthew G. Knepley @*/
516834aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
516934aa8a36SMatthew G. Knepley {
517034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
517134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
517234aa8a36SMatthew G. Knepley   if (f < 0) {
517334aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
517434aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
517534aa8a36SMatthew G. Knepley   } else {
517634aa8a36SMatthew G. Knepley     PetscInt       Nf;
517734aa8a36SMatthew G. Knepley 
51789566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51797a8be351SBarry Smith     PetscCheck(f < Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
518034aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
518134aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5182e5e52638SMatthew G. Knepley   }
5183e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5184e5e52638SMatthew G. Knepley }
5185e5e52638SMatthew G. Knepley 
5186b0441da4SMatthew G. Knepley /*@
5187b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5188b0441da4SMatthew G. Knepley 
5189b0441da4SMatthew G. Knepley   Not collective
5190b0441da4SMatthew G. Knepley 
5191f899ff85SJose E. Roman   Input Parameter:
5192b0441da4SMatthew G. Knepley . dm - The DM object
5193b0441da4SMatthew G. Knepley 
5194d8d19677SJose E. Roman   Output Parameters:
5195b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5196b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5197b0441da4SMatthew G. Knepley 
5198b0441da4SMatthew G. Knepley   Notes:
5199b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5200b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5201b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5202b0441da4SMatthew G. Knepley 
5203b0441da4SMatthew G. Knepley   Level: developer
5204b0441da4SMatthew G. Knepley 
5205db781477SPatrick Sanan .seealso: `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5206b0441da4SMatthew G. Knepley @*/
5207b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5208b0441da4SMatthew G. Knepley {
5209b0441da4SMatthew G. Knepley   PetscInt       Nf;
5210b0441da4SMatthew G. Knepley 
5211b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5212b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5213064a246eSJacob Faibussowitsch   if (useCone)    PetscValidBoolPointer(useCone, 2);
5214064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
52159566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5216b0441da4SMatthew G. Knepley   if (!Nf) {
52179566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5218b0441da4SMatthew G. Knepley   } else {
52199566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5220b0441da4SMatthew G. Knepley   }
5221b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5222b0441da4SMatthew G. Knepley }
5223b0441da4SMatthew G. Knepley 
5224b0441da4SMatthew G. Knepley /*@
5225b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5226b0441da4SMatthew G. Knepley 
5227b0441da4SMatthew G. Knepley   Not collective
5228b0441da4SMatthew G. Knepley 
5229b0441da4SMatthew G. Knepley   Input Parameters:
5230b0441da4SMatthew G. Knepley + dm         - The DM object
5231b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5232b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5233b0441da4SMatthew G. Knepley 
5234b0441da4SMatthew G. Knepley   Notes:
5235b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5236b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5237b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5238b0441da4SMatthew G. Knepley 
5239b0441da4SMatthew G. Knepley   Level: developer
5240b0441da4SMatthew G. Knepley 
5241db781477SPatrick Sanan .seealso: `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5242b0441da4SMatthew G. Knepley @*/
5243b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5244b0441da4SMatthew G. Knepley {
5245b0441da4SMatthew G. Knepley   PetscInt       Nf;
5246b0441da4SMatthew G. Knepley 
5247b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5248b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52499566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5250b0441da4SMatthew G. Knepley   if (!Nf) {
52519566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5252b0441da4SMatthew G. Knepley   } else {
52539566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5254e5e52638SMatthew G. Knepley   }
5255e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5256e5e52638SMatthew G. Knepley }
5257e5e52638SMatthew G. Knepley 
5258799db056SMatthew G. Knepley PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5259783e2ec8SMatthew G. Knepley {
5260799db056SMatthew G. Knepley   DM           plex;
5261799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5262799db056SMatthew G. Knepley   const char **names;
5263799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5264799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5265799db056SMatthew G. Knepley   size_t       len;
5266799db056SMatthew G. Knepley   MPI_Comm     comm;
5267799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5268783e2ec8SMatthew G. Knepley 
5269783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5270799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject) dm, &comm));
5271799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5272799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5273799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5274799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5275799db056SMatthew G. Knepley     PetscDS  dsBC;
5276799db056SMatthew G. Knepley     PetscInt numBd;
5277799db056SMatthew G. Knepley 
5278799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5279799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5280799db056SMatthew G. Knepley     maxLabels += numBd;
5281799db056SMatthew G. Knepley   }
5282799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5283799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5284799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5285799db056SMatthew G. Knepley     PetscDS  dsBC;
5286799db056SMatthew G. Knepley     PetscInt numBd, bd;
5287799db056SMatthew G. Knepley 
5288799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5289799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5290799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5291799db056SMatthew G. Knepley       DMLabel      label;
5292799db056SMatthew G. Knepley       PetscInt     field;
5293799db056SMatthew G. Knepley       PetscObject  obj;
5294799db056SMatthew G. Knepley       PetscClassId id;
5295799db056SMatthew G. Knepley 
5296799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
52979566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
52989566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5299799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
5300799db056SMatthew G. Knepley       for (l = 0; l < Nl; ++l) if (labels[l] == label) break;
5301799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5302783e2ec8SMatthew G. Knepley     }
5303799db056SMatthew G. Knepley   }
5304799db056SMatthew G. Knepley   /* Get label names */
5305799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5306799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject) labels[l], &names[l]));
5307799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {PetscCall(PetscStrlen(names[l], &len)); maxLen = PetscMax(maxLen, (PetscInt) len+2);}
5308799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5309799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5310799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5311799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscStrcpy(&sendNames[gmaxLen*l], names[l]));
5312799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5313799db056SMatthew G. Knepley   /* Put all names on all processes */
5314799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size+1, &displs));
5315799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5316799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p+1] = displs[p] + counts[p];
5317799db056SMatthew G. Knepley   gNl = displs[size];
5318799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) {counts[p] *= gmaxLen; displs[p] *= gmaxLen;}
5319799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5320799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5321799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5322799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5323799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5324799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l*gmaxLen], &glabels[gl]));
5325799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l*gmaxLen], rank);
5326799db056SMatthew G. Knepley     for (m = 0; m < gl; ++m) if (glabels[m] == glabels[gl]) continue;
53279566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5328799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
53299566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5330799db056SMatthew G. Knepley     ++gl;
5331783e2ec8SMatthew G. Knepley   }
5332799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
5333783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5334783e2ec8SMatthew G. Knepley }
5335783e2ec8SMatthew G. Knepley 
5336e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5337e5e52638SMatthew G. Knepley {
5338e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5339e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5340e5e52638SMatthew G. Knepley 
5341e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5342e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
53439566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5344e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5345b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
53469566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5347e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5348e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5349e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5350e5e52638SMatthew G. Knepley }
5351e5e52638SMatthew G. Knepley 
5352e5e52638SMatthew G. Knepley /*@
5353e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5354e5e52638SMatthew G. Knepley 
5355e5e52638SMatthew G. Knepley   Not collective
5356e5e52638SMatthew G. Knepley 
5357e5e52638SMatthew G. Knepley   Input Parameter:
5358e5e52638SMatthew G. Knepley . dm - The DM
5359e5e52638SMatthew G. Knepley 
5360e5e52638SMatthew G. Knepley   Output Parameter:
5361e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5362e5e52638SMatthew G. Knepley 
5363e5e52638SMatthew G. Knepley   Level: intermediate
5364e5e52638SMatthew G. Knepley 
5365db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMGetCellDS()`
5366e5e52638SMatthew G. Knepley @*/
5367e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5368e5e52638SMatthew G. Knepley {
5369e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5370e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5371534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5372e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5373e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5374e5e52638SMatthew G. Knepley }
5375e5e52638SMatthew G. Knepley 
5376e5e52638SMatthew G. Knepley /*@
5377e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5378e5e52638SMatthew G. Knepley 
5379d083f849SBarry Smith   Logically collective on dm
5380e5e52638SMatthew G. Knepley 
5381e5e52638SMatthew G. Knepley   Input Parameter:
5382e5e52638SMatthew G. Knepley . dm - The DM
5383e5e52638SMatthew G. Knepley 
5384e5e52638SMatthew G. Knepley   Level: intermediate
5385e5e52638SMatthew G. Knepley 
5386db781477SPatrick Sanan .seealso: `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5387e5e52638SMatthew G. Knepley @*/
5388e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5389e5e52638SMatthew G. Knepley {
5390e5e52638SMatthew G. Knepley   PetscInt       s;
5391e5e52638SMatthew G. Knepley 
5392e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5393e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5394e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
53959566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
53969566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
53979566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5398e5e52638SMatthew G. Knepley   }
53999566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5400e5e52638SMatthew G. Knepley   dm->probs = NULL;
5401e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5402e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5403e5e52638SMatthew G. Knepley }
5404e5e52638SMatthew G. Knepley 
5405e5e52638SMatthew G. Knepley /*@
5406e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5407e5e52638SMatthew G. Knepley 
5408e5e52638SMatthew G. Knepley   Not collective
5409e5e52638SMatthew G. Knepley 
5410e5e52638SMatthew G. Knepley   Input Parameter:
5411e5e52638SMatthew G. Knepley . dm    - The DM
5412e5e52638SMatthew G. Knepley 
5413e5e52638SMatthew G. Knepley   Output Parameter:
5414e5e52638SMatthew G. Knepley . prob - The default PetscDS
5415e5e52638SMatthew G. Knepley 
5416e5e52638SMatthew G. Knepley   Level: intermediate
5417e5e52638SMatthew G. Knepley 
5418db781477SPatrick Sanan .seealso: `DMGetCellDS()`, `DMGetRegionDS()`
5419e5e52638SMatthew G. Knepley @*/
5420e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5421e5e52638SMatthew G. Knepley {
5422e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5423e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5424e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5425b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5426b0143b4dSMatthew G. Knepley     PetscDS ds;
5427b0143b4dSMatthew G. Knepley 
54289566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
54299566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, NULL, ds));
54309566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5431b0143b4dSMatthew G. Knepley   }
5432b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5433e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5434e5e52638SMatthew G. Knepley }
5435e5e52638SMatthew G. Knepley 
5436e5e52638SMatthew G. Knepley /*@
5437e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5438e5e52638SMatthew G. Knepley 
5439e5e52638SMatthew G. Knepley   Not collective
5440e5e52638SMatthew G. Knepley 
5441e5e52638SMatthew G. Knepley   Input Parameters:
5442e5e52638SMatthew G. Knepley + dm    - The DM
5443e5e52638SMatthew G. Knepley - point - Cell for the DS
5444e5e52638SMatthew G. Knepley 
5445e5e52638SMatthew G. Knepley   Output Parameter:
5446e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5447e5e52638SMatthew G. Knepley 
5448e5e52638SMatthew G. Knepley   Level: developer
5449e5e52638SMatthew G. Knepley 
5450db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMSetRegionDS()`
5451e5e52638SMatthew G. Knepley @*/
5452e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5453e5e52638SMatthew G. Knepley {
5454e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5455e5e52638SMatthew G. Knepley   PetscInt       s;
5456e5e52638SMatthew G. Knepley 
5457e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5458e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5459e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
546063a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
5461e5e52638SMatthew G. Knepley   *prob = NULL;
5462e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5463e5e52638SMatthew G. Knepley     PetscInt val;
5464e5e52638SMatthew G. Knepley 
5465e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5466e5e52638SMatthew G. Knepley     else {
54679566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
5468e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5469e5e52638SMatthew G. Knepley     }
5470e5e52638SMatthew G. Knepley   }
5471e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5472e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5473e5e52638SMatthew G. Knepley }
5474e5e52638SMatthew G. Knepley 
5475e5e52638SMatthew G. Knepley /*@
5476e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5477e5e52638SMatthew G. Knepley 
5478e5e52638SMatthew G. Knepley   Not collective
5479e5e52638SMatthew G. Knepley 
5480e5e52638SMatthew G. Knepley   Input Parameters:
5481e5e52638SMatthew G. Knepley + dm    - The DM
5482e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5483e5e52638SMatthew G. Knepley 
5484b3cf3223SMatthew G. Knepley   Output Parameters:
5485b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5486b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5487e5e52638SMatthew G. Knepley 
5488154ca461SJed Brown   Note:
5489154ca461SJed Brown   If a non-NULL label is given, but there is no PetscDS on that specific label,
5490154ca461SJed Brown   the PetscDS for the full domain (if present) is returned. Returns with
5491154ca461SJed Brown   fields=NULL and prob=NULL if there is no PetscDS for the full domain.
5492e5e52638SMatthew G. Knepley 
5493e5e52638SMatthew G. Knepley   Level: advanced
5494e5e52638SMatthew G. Knepley 
5495db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5496e5e52638SMatthew G. Knepley @*/
5497b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5498e5e52638SMatthew G. Knepley {
5499e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5500e5e52638SMatthew G. Knepley 
5501e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5502e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5503e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5504b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5505b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5506e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5507154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5508b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5509b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5510154ca461SJed Brown       if (dm->probs[s].label) PetscFunctionReturn(0);
5511b3cf3223SMatthew G. Knepley     }
5512e5e52638SMatthew G. Knepley   }
55132df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5514e5e52638SMatthew G. Knepley }
5515e5e52638SMatthew G. Knepley 
5516e5e52638SMatthew G. Knepley /*@
5517*bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5518083401c6SMatthew G. Knepley 
5519083401c6SMatthew G. Knepley   Collective on dm
5520083401c6SMatthew G. Knepley 
5521083401c6SMatthew G. Knepley   Input Parameters:
5522*bb7acecfSBarry Smith + dm     - The `DM`
5523*bb7acecfSBarry Smith . label  - The `DMLabel` defining the mesh region, or NULL for the entire mesh
5524*bb7acecfSBarry Smith . fields - The IS containing the `DM` field numbers for the fields in this `PetscDS`, or NULL for all fields
5525*bb7acecfSBarry Smith - prob   - The `PetscDS` defined on the given region
5526083401c6SMatthew G. Knepley 
5527*bb7acecfSBarry Smith   Note:
5528*bb7acecfSBarry 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,
5529083401c6SMatthew G. Knepley   the fields argument is ignored.
5530083401c6SMatthew G. Knepley 
5531083401c6SMatthew G. Knepley   Level: advanced
5532083401c6SMatthew G. Knepley 
5533db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5534083401c6SMatthew G. Knepley @*/
5535083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5536083401c6SMatthew G. Knepley {
5537083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5538083401c6SMatthew G. Knepley 
5539083401c6SMatthew G. Knepley   PetscFunctionBegin;
5540083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5541083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5542064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5543083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5544083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
55459566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
5546083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5547083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5548083401c6SMatthew G. Knepley     }
5549083401c6SMatthew G. Knepley   }
55509566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds+1));
55519566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
55529566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) fields));
55539566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) ds));
5554083401c6SMatthew G. Knepley   if (!label) {
5555083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5556083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5557083401c6SMatthew G. Knepley     Nds = 0;
5558083401c6SMatthew G. Knepley   }
5559083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5560083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5561083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5562083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5563083401c6SMatthew G. Knepley }
5564083401c6SMatthew G. Knepley 
5565083401c6SMatthew G. Knepley /*@
5566e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5567e5e52638SMatthew G. Knepley 
5568e5e52638SMatthew G. Knepley   Not collective
5569e5e52638SMatthew G. Knepley 
5570e5e52638SMatthew G. Knepley   Input Parameters:
5571e5e52638SMatthew G. Knepley + dm  - The DM
5572e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5573e5e52638SMatthew G. Knepley 
5574e5e52638SMatthew G. Knepley   Output Parameters:
5575b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5576b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5577083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5578e5e52638SMatthew G. Knepley 
5579e5e52638SMatthew G. Knepley   Level: advanced
5580e5e52638SMatthew G. Knepley 
5581db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5582e5e52638SMatthew G. Knepley @*/
5583b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5584e5e52638SMatthew G. Knepley {
5585e5e52638SMatthew G. Knepley   PetscInt       Nds;
5586e5e52638SMatthew G. Knepley 
5587e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5588e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55899566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
559063a3b9bcSJacob 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);
5591e5e52638SMatthew G. Knepley   if (label) {
5592e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5593e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5594e5e52638SMatthew G. Knepley   }
5595b3cf3223SMatthew G. Knepley   if (fields) {
5596b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5597b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5598b3cf3223SMatthew G. Knepley   }
5599e5e52638SMatthew G. Knepley   if (ds) {
5600b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5601e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5602e5e52638SMatthew G. Knepley   }
5603e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5604e5e52638SMatthew G. Knepley }
5605e5e52638SMatthew G. Knepley 
5606e5e52638SMatthew G. Knepley /*@
5607083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5608e5e52638SMatthew G. Knepley 
5609083401c6SMatthew G. Knepley   Not collective
5610e5e52638SMatthew G. Knepley 
5611e5e52638SMatthew G. Knepley   Input Parameters:
5612e5e52638SMatthew G. Knepley + dm     - The DM
5613083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5614083401c6SMatthew G. Knepley . label  - The region label, or NULL
5615083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5616083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5617e5e52638SMatthew G. Knepley 
5618e5e52638SMatthew G. Knepley   Level: advanced
5619e5e52638SMatthew G. Knepley 
5620db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5621e5e52638SMatthew G. Knepley @*/
5622083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5623e5e52638SMatthew G. Knepley {
5624083401c6SMatthew G. Knepley   PetscInt       Nds;
5625e5e52638SMatthew G. Knepley 
5626e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5627e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5628083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
56299566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
563063a3b9bcSJacob 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);
56319566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
56329566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5633083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5634083401c6SMatthew G. Knepley   if (fields) {
5635083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
56369566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) fields));
56379566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5638083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5639e5e52638SMatthew G. Knepley   }
5640083401c6SMatthew G. Knepley   if (ds) {
5641083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
56429566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) ds));
56439566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5644083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5645083401c6SMatthew G. Knepley   }
5646e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5647e5e52638SMatthew G. Knepley }
5648e5e52638SMatthew G. Knepley 
5649e5e52638SMatthew G. Knepley /*@
56501d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
56511d3af9e0SMatthew G. Knepley 
56521d3af9e0SMatthew G. Knepley   Not collective
56531d3af9e0SMatthew G. Knepley 
56541d3af9e0SMatthew G. Knepley   Input Parameters:
56551d3af9e0SMatthew G. Knepley + dm  - The DM
56561d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
56571d3af9e0SMatthew G. Knepley 
56581d3af9e0SMatthew G. Knepley   Output Parameter:
56591d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
56601d3af9e0SMatthew G. Knepley 
56611d3af9e0SMatthew G. Knepley   Level: advanced
56621d3af9e0SMatthew G. Knepley 
5663db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
56641d3af9e0SMatthew G. Knepley @*/
56651d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
56661d3af9e0SMatthew G. Knepley {
56671d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
56681d3af9e0SMatthew G. Knepley 
56691d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
56701d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56711d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
5672dadcf809SJacob Faibussowitsch   PetscValidIntPointer(num, 3);
56739566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
56741d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
56751d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
56761d3af9e0SMatthew G. Knepley   else          *num = n;
56771d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
56781d3af9e0SMatthew G. Knepley }
56791d3af9e0SMatthew G. Knepley 
56802df84da0SMatthew G. Knepley /*@C
5681*bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
56822df84da0SMatthew G. Knepley 
56832df84da0SMatthew G. Knepley   Not collective
56842df84da0SMatthew G. Knepley 
5685f1a722f8SMatthew G. Knepley   Input Parameters:
5686*bb7acecfSBarry Smith + dm     - The `DM`
56872df84da0SMatthew G. Knepley . Nc     - The number of components for the field
5688*bb7acecfSBarry Smith . prefix - The options prefix for the output `PetscFE`, or NULL
5689*bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
56902df84da0SMatthew G. Knepley 
56912df84da0SMatthew G. Knepley   Output Parameter:
5692*bb7acecfSBarry Smith . fem - The `PetscFE`
56932df84da0SMatthew G. Knepley 
5694*bb7acecfSBarry Smith   Note:
5695*bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
56962df84da0SMatthew G. Knepley 
56972df84da0SMatthew G. Knepley   Level: intermediate
56982df84da0SMatthew G. Knepley 
5699db781477SPatrick Sanan .seealso: `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
57002df84da0SMatthew G. Knepley @*/
57012df84da0SMatthew G. Knepley PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
57022df84da0SMatthew G. Knepley {
57032df84da0SMatthew G. Knepley   DMPolytopeType ct;
57042df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
57052df84da0SMatthew G. Knepley 
57062df84da0SMatthew G. Knepley   PetscFunctionBegin;
57072df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57082df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
57092df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
57102df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
57112df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
57129566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
57139566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
57149566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
57159566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
57162df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
57172df84da0SMatthew G. Knepley }
57182df84da0SMatthew G. Knepley 
57191d3af9e0SMatthew G. Knepley /*@
5720*bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5721e5e52638SMatthew G. Knepley 
5722d083f849SBarry Smith   Collective on dm
5723e5e52638SMatthew G. Knepley 
5724e5e52638SMatthew G. Knepley   Input Parameter:
5725*bb7acecfSBarry Smith . dm - The `DM`
5726e5e52638SMatthew G. Knepley 
572745480ffeSMatthew G. Knepley   Options Database Keys:
5728*bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
572945480ffeSMatthew G. Knepley 
5730*bb7acecfSBarry Smith   Note:
5731*bb7acecfSBarry Smith   If the label has a `PetscDS` defined, it will be replaced. Otherwise, it will be added to the `DM`.
5732e5e52638SMatthew G. Knepley 
5733e5e52638SMatthew G. Knepley   Level: intermediate
5734e5e52638SMatthew G. Knepley 
5735db781477SPatrick Sanan .seealso: `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5736e5e52638SMatthew G. Knepley @*/
5737e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5738e5e52638SMatthew G. Knepley {
5739e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5740083401c6SMatthew G. Knepley   PetscDS        dsDef;
5741083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5742f9244615SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5743f9244615SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE, flg;
5744e5e52638SMatthew G. Knepley 
5745e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5746e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5747e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
57489566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject) dm, &comm));
57499566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5750083401c6SMatthew G. Knepley   /* Determine how many regions we have */
57519566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5752083401c6SMatthew G. Knepley   Nl   = 0;
5753083401c6SMatthew G. Knepley   Ndef = 0;
5754083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5755083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5756083401c6SMatthew G. Knepley     PetscInt l;
5757083401c6SMatthew G. Knepley 
5758f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5759f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5760f918ec44SMatthew G. Knepley     {
5761f918ec44SMatthew G. Knepley       PetscClassId id;
5762f918ec44SMatthew G. Knepley 
57639566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5764f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5765f918ec44SMatthew G. Knepley         Ceed ceed;
5766f918ec44SMatthew G. Knepley 
57679566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
57689566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE) dm->fields[f].disc, ceed));
5769f918ec44SMatthew G. Knepley       }
5770f918ec44SMatthew G. Knepley     }
5771f918ec44SMatthew G. Knepley #endif
5772083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5773083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5774083401c6SMatthew G. Knepley     if (l < Nl) continue;
5775083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5776083401c6SMatthew G. Knepley   }
5777083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
57789566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
5779083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5780b3cf3223SMatthew G. Knepley     IS        fields;
5781b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5782b3cf3223SMatthew G. Knepley 
5783b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
57847a8be351SBarry Smith     PetscCheck(nf,comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
57859566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
5786b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
57879566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
57889566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_"));
57899566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
57909566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
579188f0c812SMatthew G. Knepley 
57929566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
57939566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef));
57949566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
57959566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
57962df9ee95SMatthew G. Knepley   }
57979566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
57989566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5799083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5800083401c6SMatthew G. Knepley   if (Ndef && Nl) {
58010122748bSMatthew G. Knepley     DM              plex;
5802083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5803083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5804083401c6SMatthew G. Knepley     PetscInt       *fields;
5805083401c6SMatthew G. Knepley     const PetscInt *cells;
5806083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
58070122748bSMatthew G. Knepley 
58089566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
58099566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
58109566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
58119566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
58125fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5813083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5814083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5815083401c6SMatthew G. Knepley       IS      pointIS;
5816083401c6SMatthew G. Knepley 
58179566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
58189566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
58199566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
58209566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5821083401c6SMatthew G. Knepley     }
58229566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5823083401c6SMatthew G. Knepley 
58249566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
58259566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
58269566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
58279566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
58289566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
58299566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
58309566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5831083401c6SMatthew G. Knepley 
58329566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
5833083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
58349566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
58359566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_"));
58369566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
58379566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5838083401c6SMatthew G. Knepley 
58399566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
58409566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef));
58419566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
58429566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
58439566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58449566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
58459566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5846083401c6SMatthew G. Knepley   }
5847083401c6SMatthew G. Knepley   /* Create label DSes
5848083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5849083401c6SMatthew G. Knepley   */
5850083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5851083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5852083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5853083401c6SMatthew G. Knepley     PetscDS   ds;
5854083401c6SMatthew G. Knepley     IS        fields;
5855083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5856083401c6SMatthew G. Knepley 
58579566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
5858083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
58599566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
5860083401c6SMatthew G. Knepley     for (f = 0, nf  = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
58619566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58629566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_"));
58639566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58649566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
58659566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, label, fields, ds));
58669566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
58679566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5868083401c6SMatthew G. Knepley     {
5869083401c6SMatthew G. Knepley       DMPolytopeType ct;
5870083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
58715fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
58720122748bSMatthew G. Knepley 
58739566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5874665f567fSMatthew G. Knepley       if (lStart >= 0) {
58759566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5876412e9a14SMatthew G. Knepley         switch (ct) {
5877412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5878412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5879412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5880412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
58815fedec97SMatthew G. Knepley             isCohesiveLocal = PETSC_TRUE;break;
5882083401c6SMatthew G. Knepley           default: break;
5883412e9a14SMatthew G. Knepley         }
5884665f567fSMatthew G. Knepley       }
58859566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
58865fedec97SMatthew G. Knepley       for (f = 0, nf  = 0; f < Nf; ++f) {
58875fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
58885fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
58899566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
58909566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
58915fedec97SMatthew G. Knepley           }
58925fedec97SMatthew G. Knepley           ++nf;
58935fedec97SMatthew G. Knepley         }
58945fedec97SMatthew G. Knepley       }
5895e5e52638SMatthew G. Knepley     }
58969566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5897e5e52638SMatthew G. Knepley   }
58989566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5899e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5900083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5901083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5902083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5903083401c6SMatthew G. Knepley     const PetscInt *fld;
59045fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
59055fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5906e5e52638SMatthew G. Knepley 
59079566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
59089566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
59099566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
59109566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5911083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5912083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
59135fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5914e5e52638SMatthew G. Knepley       PetscClassId id;
5915e5e52638SMatthew G. Knepley 
59165fedec97SMatthew G. Knepley       /* Handle DS with no fields */
59179566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
59185fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
59199566063dSJacob Faibussowitsch       if (isCohesive && !isCohesiveField) PetscCall(PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc));
59209566063dSJacob Faibussowitsch       PetscCall(PetscDSSetDiscretization(ds, f, disc));
5921083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
59229566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5923e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5924e5e52638SMatthew G. Knepley     }
59259566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
5926e5e52638SMatthew G. Knepley   }
5927f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
59289566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject) dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
5929f9244615SMatthew G. Knepley   if (flg) {
59303b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
59313b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
59323b4aee56SMatthew G. Knepley       PetscInt Nf, f;
59333b4aee56SMatthew G. Knepley 
59349566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
59359566063dSJacob Faibussowitsch       for (f = 0; f < Nf; ++f) PetscCall(PetscDSSetJetDegree(ds, f, k));
59363b4aee56SMatthew G. Knepley     }
5937f9244615SMatthew G. Knepley   }
5938e5e52638SMatthew G. Knepley   /* Setup DSes */
5939e5e52638SMatthew G. Knepley   if (doSetup) {
59409566063dSJacob Faibussowitsch     for (s = 0; s < dm->Nds; ++s) PetscCall(PetscDSSetUp(dm->probs[s].ds));
5941e5e52638SMatthew G. Knepley   }
5942e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5943e5e52638SMatthew G. Knepley }
5944e5e52638SMatthew G. Knepley 
5945e5e52638SMatthew G. Knepley /*@
5946*bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
59477f96f943SMatthew G. Knepley 
5948*bb7acecfSBarry Smith   Collective on `DM`
5949f2cacb80SMatthew G. Knepley 
59507f96f943SMatthew G. Knepley   Input Parameters:
5951*bb7acecfSBarry Smith + dm   - The `DM`
59527f96f943SMatthew G. Knepley - time - The time
59537f96f943SMatthew G. Knepley 
59547f96f943SMatthew G. Knepley   Output Parameters:
5955f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5956f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
59577f96f943SMatthew G. Knepley 
5958*bb7acecfSBarry Smith   Note:
5959*bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
59607f96f943SMatthew G. Knepley 
59617f96f943SMatthew G. Knepley   Level: developer
59627f96f943SMatthew G. Knepley 
5963db781477SPatrick Sanan .seealso: `PetscDSSetExactSolution()`
59647f96f943SMatthew G. Knepley @*/
5965f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
59667f96f943SMatthew G. Knepley {
59677f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
59687f96f943SMatthew G. Knepley   void            **ectxs;
59697f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
59707f96f943SMatthew G. Knepley 
59717f96f943SMatthew G. Knepley   PetscFunctionBegin;
5972f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5973f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5974f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
59759566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
59769566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
59779566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
59787f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
59797f96f943SMatthew G. Knepley     PetscDS         ds;
59807f96f943SMatthew G. Knepley     DMLabel         label;
59817f96f943SMatthew G. Knepley     IS              fieldIS;
59827f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
59837f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
59847f96f943SMatthew G. Knepley 
59859566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
59869566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
59879566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
59889566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
59899566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
5990f2cacb80SMatthew G. Knepley     if (u) {
59917f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
59927f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
59939566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]));
59947f96f943SMatthew G. Knepley       }
59959566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
59967f96f943SMatthew G. Knepley       if (label) {
59979566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u));
59987f96f943SMatthew G. Knepley       } else {
59999566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u));
60007f96f943SMatthew G. Knepley       }
60017f96f943SMatthew G. Knepley     }
6002f2cacb80SMatthew G. Knepley     if (u_t) {
60039566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
60049566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6005f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
6006f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
60079566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]));
6008f2cacb80SMatthew G. Knepley       }
60099566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
6010f2cacb80SMatthew G. Knepley       if (label) {
60119566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t));
6012f2cacb80SMatthew G. Knepley       } else {
60139566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t));
6014f2cacb80SMatthew G. Knepley       }
6015f2cacb80SMatthew G. Knepley     }
6016f2cacb80SMatthew G. Knepley   }
6017f2cacb80SMatthew G. Knepley   if (u) {
60189566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) u, "Exact Solution"));
60199566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) u, "exact_"));
6020f2cacb80SMatthew G. Knepley   }
6021f2cacb80SMatthew G. Knepley   if (u_t) {
60229566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative"));
60239566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_"));
6024f2cacb80SMatthew G. Knepley   }
60259566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
60267f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
60277f96f943SMatthew G. Knepley }
60287f96f943SMatthew G. Knepley 
602945480ffeSMatthew G. Knepley PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
603045480ffeSMatthew G. Knepley {
603145480ffeSMatthew G. Knepley   PetscDS        dsNew;
603245480ffeSMatthew G. Knepley   DSBoundary     b;
60336a02485aSMatthew G. Knepley   PetscInt       cdim, Nf, f, d;
60345fedec97SMatthew G. Knepley   PetscBool      isCohesive;
603545480ffeSMatthew G. Knepley   void          *ctx;
603645480ffeSMatthew G. Knepley 
603745480ffeSMatthew G. Knepley   PetscFunctionBegin;
60389566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject) ds), &dsNew));
60399566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyConstants(ds, dsNew));
60409566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyExactSolutions(ds, dsNew));
60419566063dSJacob Faibussowitsch   PetscCall(PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew));
60429566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyEquations(ds, dsNew));
60439566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
604445480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
60459566063dSJacob Faibussowitsch     PetscCall(PetscDSGetContext(ds, f, &ctx));
60469566063dSJacob Faibussowitsch     PetscCall(PetscDSSetContext(dsNew, f, ctx));
60479566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCohesive(ds, f, &isCohesive));
60489566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCohesive(dsNew, f, isCohesive));
60496a02485aSMatthew G. Knepley     PetscCall(PetscDSGetJetDegree(ds, f, &d));
60506a02485aSMatthew G. Knepley     PetscCall(PetscDSSetJetDegree(dsNew, f, d));
605145480ffeSMatthew G. Knepley   }
605245480ffeSMatthew G. Knepley   if (Nf) {
60539566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCoordinateDimension(ds, &cdim));
60549566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsNew, cdim));
605545480ffeSMatthew G. Knepley   }
60569566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew));
605745480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
60589566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, b->lname, &b->label));
605945480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
60607a8be351SBarry Smith     //PetscCheck(b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
606145480ffeSMatthew G. Knepley   }
606245480ffeSMatthew G. Knepley 
60639566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(dm, label, fields, dsNew));
60649566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
606545480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
606645480ffeSMatthew G. Knepley }
606745480ffeSMatthew G. Knepley 
60687f96f943SMatthew G. Knepley /*@
6069*bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6070e5e52638SMatthew G. Knepley 
6071d083f849SBarry Smith   Collective on dm
6072e5e52638SMatthew G. Knepley 
6073e5e52638SMatthew G. Knepley   Input Parameter:
6074*bb7acecfSBarry Smith . dm - The `DM`
6075e5e52638SMatthew G. Knepley 
6076e5e52638SMatthew G. Knepley   Output Parameter:
6077*bb7acecfSBarry Smith . newdm - The `DM`
6078e5e52638SMatthew G. Knepley 
6079e5e52638SMatthew G. Knepley   Level: advanced
6080e5e52638SMatthew G. Knepley 
6081db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6082e5e52638SMatthew G. Knepley @*/
6083e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
6084e5e52638SMatthew G. Knepley {
6085e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6086e5e52638SMatthew G. Knepley 
6087e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6088e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
60899566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
60909566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6091e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6092e5e52638SMatthew G. Knepley     DMLabel  label;
6093b3cf3223SMatthew G. Knepley     IS       fields;
609445480ffeSMatthew G. Knepley     PetscDS  ds, newds;
6095783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6096e5e52638SMatthew G. Knepley 
60979566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds));
6098b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
60999566063dSJacob Faibussowitsch     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds));
610045480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
61019566063dSJacob Faibussowitsch     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds));
61029566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6103783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6104b8025e53SMatthew G. Knepley       PetscWeakForm wf;
610545480ffeSMatthew G. Knepley       DMLabel       label;
6106783e2ec8SMatthew G. Knepley       PetscInt      field;
6107783e2ec8SMatthew G. Knepley 
61089566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
61099566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6110783e2ec8SMatthew G. Knepley     }
6111e5e52638SMatthew G. Knepley   }
6112799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
6113e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6114e5e52638SMatthew G. Knepley }
6115e5e52638SMatthew G. Knepley 
6116e5e52638SMatthew G. Knepley /*@
6117*bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6118e5e52638SMatthew G. Knepley 
6119d083f849SBarry Smith   Collective on dm
6120e5e52638SMatthew G. Knepley 
6121e5e52638SMatthew G. Knepley   Input Parameter:
6122*bb7acecfSBarry Smith . dm - The `DM`
6123e5e52638SMatthew G. Knepley 
6124e5e52638SMatthew G. Knepley   Output Parameter:
6125*bb7acecfSBarry Smith . newdm - The `DM`
6126e5e52638SMatthew G. Knepley 
6127e5e52638SMatthew G. Knepley   Level: advanced
6128e5e52638SMatthew G. Knepley 
6129*bb7acecfSBarry Smith   Developer Note:
6130*bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6131*bb7acecfSBarry Smith 
6132db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMCopyDS()`
6133e5e52638SMatthew G. Knepley @*/
6134e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6135e5e52638SMatthew G. Knepley {
6136e5e52638SMatthew G. Knepley   PetscFunctionBegin;
61379566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
61389566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
6139e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6140e5e52638SMatthew G. Knepley }
6141e5e52638SMatthew G. Knepley 
6142c73cfb54SMatthew G. Knepley /*@
6143*bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6144c73cfb54SMatthew G. Knepley 
6145c73cfb54SMatthew G. Knepley   Not collective
6146c73cfb54SMatthew G. Knepley 
6147c73cfb54SMatthew G. Knepley   Input Parameter:
6148*bb7acecfSBarry Smith . dm - The `DM`
6149c73cfb54SMatthew G. Knepley 
6150c73cfb54SMatthew G. Knepley   Output Parameter:
6151c73cfb54SMatthew G. Knepley . dim - The topological dimension
6152c73cfb54SMatthew G. Knepley 
6153c73cfb54SMatthew G. Knepley   Level: beginner
6154c73cfb54SMatthew G. Knepley 
6155db781477SPatrick Sanan .seealso: `DMSetDimension()`, `DMCreate()`
6156c73cfb54SMatthew G. Knepley @*/
6157c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6158c73cfb54SMatthew G. Knepley {
6159c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6160c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6161534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6162c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6163c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6164c73cfb54SMatthew G. Knepley }
6165c73cfb54SMatthew G. Knepley 
6166c73cfb54SMatthew G. Knepley /*@
6167*bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6168c73cfb54SMatthew G. Knepley 
6169c73cfb54SMatthew G. Knepley   Collective on dm
6170c73cfb54SMatthew G. Knepley 
6171c73cfb54SMatthew G. Knepley   Input Parameters:
6172*bb7acecfSBarry Smith + dm - The `DM`
6173c73cfb54SMatthew G. Knepley - dim - The topological dimension
6174c73cfb54SMatthew G. Knepley 
6175c73cfb54SMatthew G. Knepley   Level: beginner
6176c73cfb54SMatthew G. Knepley 
6177db781477SPatrick Sanan .seealso: `DMGetDimension()`, `DMCreate()`
6178c73cfb54SMatthew G. Knepley @*/
6179c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6180c73cfb54SMatthew G. Knepley {
6181e5e52638SMatthew G. Knepley   PetscDS        ds;
618245480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6183f17e8794SMatthew G. Knepley 
6184c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6185c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6186c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6187c73cfb54SMatthew G. Knepley   dm->dim = dim;
6188d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
61899566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
619045480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
61919566063dSJacob Faibussowitsch       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
61929566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
619345480ffeSMatthew G. Knepley     }
6194d17bd122SMatthew G. Knepley   }
6195c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6196c73cfb54SMatthew G. Knepley }
6197c73cfb54SMatthew G. Knepley 
6198793f3fe5SMatthew G. Knepley /*@
6199793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6200793f3fe5SMatthew G. Knepley 
6201d083f849SBarry Smith   Collective on dm
6202793f3fe5SMatthew G. Knepley 
6203793f3fe5SMatthew G. Knepley   Input Parameters:
6204*bb7acecfSBarry Smith + dm - the `DM`
6205793f3fe5SMatthew G. Knepley - dim - the dimension
6206793f3fe5SMatthew G. Knepley 
6207793f3fe5SMatthew G. Knepley   Output Parameters:
6208793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6209aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6210793f3fe5SMatthew G. Knepley 
6211793f3fe5SMatthew G. Knepley   Note:
6212793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6213a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6214793f3fe5SMatthew G. Knepley   then the interval is empty.
6215793f3fe5SMatthew G. Knepley 
6216793f3fe5SMatthew G. Knepley   Level: intermediate
6217793f3fe5SMatthew G. Knepley 
6218db781477SPatrick Sanan .seealso: `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6219793f3fe5SMatthew G. Knepley @*/
6220793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6221793f3fe5SMatthew G. Knepley {
6222793f3fe5SMatthew G. Knepley   PetscInt       d;
6223793f3fe5SMatthew G. Knepley 
6224793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6225793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62269566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
62277a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
62287a8be351SBarry Smith   PetscCheck(dm->ops->getdimpoints,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
62299566063dSJacob Faibussowitsch   PetscCall((*dm->ops->getdimpoints)(dm, dim, pStart, pEnd));
6230793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6231793f3fe5SMatthew G. Knepley }
6232793f3fe5SMatthew G. Knepley 
62336636e97aSMatthew G Knepley /*@
6234*bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6235f4d763aaSMatthew G. Knepley 
62368f700142SStefano Zampini   Collective on dm
62378f700142SStefano Zampini 
6238f4d763aaSMatthew G. Knepley   Input Parameter:
6239*bb7acecfSBarry Smith . dm - The original `DM`
6240f4d763aaSMatthew G. Knepley 
6241f4d763aaSMatthew G. Knepley   Output Parameter:
6242*bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6243f4d763aaSMatthew G. Knepley 
6244f4d763aaSMatthew G. Knepley   Level: intermediate
6245f4d763aaSMatthew G. Knepley 
6246*bb7acecfSBarry Smith   Note:
6247*bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6248*bb7acecfSBarry 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
6249*bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6250*bb7acecfSBarry Smith 
6251*bb7acecfSBarry Smith .seealso: `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6252f4d763aaSMatthew G. Knepley @*/
625314f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
625414f150ffSMatthew G. Knepley {
6255c26acbdeSMatthew G. Knepley   PetscSection   section;
62562d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
625714f150ffSMatthew G. Knepley 
625814f150ffSMatthew G. Knepley   PetscFunctionBegin;
625914f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
626014f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
62619566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
62629566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
62639566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm)));
62642d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6265c26acbdeSMatthew G. Knepley     *odm = dm;
6266c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6267c26acbdeSMatthew G. Knepley   }
626814f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6269c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
627014f150ffSMatthew G. Knepley     PetscSF      sf;
627114f150ffSMatthew G. Knepley 
62729566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
62739566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
62749566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
62759566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
62769566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
62779566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
62789566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
62799566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
62809566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
628114f150ffSMatthew G. Knepley   }
628214f150ffSMatthew G. Knepley   *odm = dm->dmBC;
628314f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
628414f150ffSMatthew G. Knepley }
6285f4d763aaSMatthew G. Knepley 
6286f4d763aaSMatthew G. Knepley /*@
6287cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6288f4d763aaSMatthew G. Knepley 
6289f4d763aaSMatthew G. Knepley   Input Parameter:
6290*bb7acecfSBarry Smith . dm - The original `DM`
6291f4d763aaSMatthew G. Knepley 
6292cdb7a50dSMatthew G. Knepley   Output Parameters:
6293cdb7a50dSMatthew G. Knepley + num - The output sequence number
6294cdb7a50dSMatthew G. Knepley - val - The output sequence value
6295f4d763aaSMatthew G. Knepley 
6296f4d763aaSMatthew G. Knepley   Level: intermediate
6297f4d763aaSMatthew G. Knepley 
6298*bb7acecfSBarry Smith   Note:
6299*bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6300*bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6301*bb7acecfSBarry Smith 
6302*bb7acecfSBarry Smith   Developer Note:
6303*bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6304*bb7acecfSBarry Smith   not directly related to the `DM`.
6305f4d763aaSMatthew G. Knepley 
6306db781477SPatrick Sanan .seealso: `VecView()`
6307f4d763aaSMatthew G. Knepley @*/
6308cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6309f4d763aaSMatthew G. Knepley {
6310f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6311f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6312534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6313534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6314f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6315f4d763aaSMatthew G. Knepley }
6316f4d763aaSMatthew G. Knepley 
6317f4d763aaSMatthew G. Knepley /*@
6318cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6319f4d763aaSMatthew G. Knepley 
6320f4d763aaSMatthew G. Knepley   Input Parameters:
6321*bb7acecfSBarry Smith + dm - The original `DM`
6322cdb7a50dSMatthew G. Knepley . num - The output sequence number
6323cdb7a50dSMatthew G. Knepley - val - The output sequence value
6324f4d763aaSMatthew G. Knepley 
6325f4d763aaSMatthew G. Knepley   Level: intermediate
6326f4d763aaSMatthew G. Knepley 
6327*bb7acecfSBarry Smith   Note:
6328*bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6329*bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6330f4d763aaSMatthew G. Knepley 
6331db781477SPatrick Sanan .seealso: `VecView()`
6332f4d763aaSMatthew G. Knepley @*/
6333cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6334f4d763aaSMatthew G. Knepley {
6335f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6336f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6337f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6338cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6339cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6340cdb7a50dSMatthew G. Knepley }
6341cdb7a50dSMatthew G. Knepley 
6342cdb7a50dSMatthew G. Knepley /*@C
6343*bb7acecfSBarry Smith  DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6344cdb7a50dSMatthew G. Knepley 
6345cdb7a50dSMatthew G. Knepley   Input Parameters:
6346*bb7acecfSBarry Smith + dm   - The original `DM`
6347cdb7a50dSMatthew G. Knepley . name - The sequence name
6348cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6349cdb7a50dSMatthew G. Knepley 
6350cdb7a50dSMatthew G. Knepley   Output Parameter:
6351cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6352cdb7a50dSMatthew G. Knepley 
6353cdb7a50dSMatthew G. Knepley   Level: intermediate
6354cdb7a50dSMatthew G. Knepley 
6355*bb7acecfSBarry Smith   Note:
6356*bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6357*bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6358*bb7acecfSBarry Smith 
6359*bb7acecfSBarry Smith   Developer Note:
6360*bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6361cdb7a50dSMatthew G. Knepley 
6362db781477SPatrick Sanan .seealso: `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6363cdb7a50dSMatthew G. Knepley @*/
6364cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6365cdb7a50dSMatthew G. Knepley {
6366cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6367cdb7a50dSMatthew G. Knepley 
6368cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6369cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6370cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6371064a246eSJacob Faibussowitsch   PetscValidRealPointer(val,5);
63729566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5));
6373cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6374cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6375cdb7a50dSMatthew G. Knepley     PetscScalar value;
6376cdb7a50dSMatthew G. Knepley 
63779566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
63784aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6379cdb7a50dSMatthew G. Knepley #endif
6380cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6381f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6382f4d763aaSMatthew G. Knepley }
63838e4ac7eaSMatthew G. Knepley 
63848e4ac7eaSMatthew G. Knepley /*@
6385*bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
63868e4ac7eaSMatthew G. Knepley 
63878e4ac7eaSMatthew G. Knepley   Not collective
63888e4ac7eaSMatthew G. Knepley 
63898e4ac7eaSMatthew G. Knepley   Input Parameter:
6390*bb7acecfSBarry Smith . dm - The `DM`
63918e4ac7eaSMatthew G. Knepley 
63928e4ac7eaSMatthew G. Knepley   Output Parameter:
6393*bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
63948e4ac7eaSMatthew G. Knepley 
63958e4ac7eaSMatthew G. Knepley   Level: beginner
63968e4ac7eaSMatthew G. Knepley 
6397db781477SPatrick Sanan .seealso: `DMSetUseNatural()`, `DMCreate()`
63988e4ac7eaSMatthew G. Knepley @*/
63998e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
64008e4ac7eaSMatthew G. Knepley {
64018e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64028e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6403534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
64048e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
64058e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
64068e4ac7eaSMatthew G. Knepley }
64078e4ac7eaSMatthew G. Knepley 
64088e4ac7eaSMatthew G. Knepley /*@
6409*bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64108e4ac7eaSMatthew G. Knepley 
64118e4ac7eaSMatthew G. Knepley   Collective on dm
64128e4ac7eaSMatthew G. Knepley 
64138e4ac7eaSMatthew G. Knepley   Input Parameters:
6414*bb7acecfSBarry Smith  + dm - The `DM`
6415*bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64168e4ac7eaSMatthew G. Knepley 
6417*bb7acecfSBarry Smith   Note:
6418*bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
64195d3b26e6SMatthew G. Knepley 
64208e4ac7eaSMatthew G. Knepley   Level: beginner
64218e4ac7eaSMatthew G. Knepley 
6422db781477SPatrick Sanan .seealso: `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
64238e4ac7eaSMatthew G. Knepley @*/
64248e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
64258e4ac7eaSMatthew G. Knepley {
64268e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64278e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64288833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
64298e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
64308e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
64318e4ac7eaSMatthew G. Knepley }
6432c58f1c22SToby Isaac 
6433c58f1c22SToby Isaac /*@C
6434*bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6435c58f1c22SToby Isaac 
6436c58f1c22SToby Isaac   Not Collective
6437c58f1c22SToby Isaac 
6438c58f1c22SToby Isaac   Input Parameters:
6439*bb7acecfSBarry Smith + dm   - The `DM` object
6440c58f1c22SToby Isaac - name - The label name
6441c58f1c22SToby Isaac 
6442c58f1c22SToby Isaac   Level: intermediate
6443c58f1c22SToby Isaac 
6444db781477SPatrick Sanan .seealso: `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6445c58f1c22SToby Isaac @*/
6446c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6447c58f1c22SToby Isaac {
64485d80c0bfSVaclav Hapla   PetscBool      flg;
64495d80c0bfSVaclav Hapla   DMLabel        label;
6450c58f1c22SToby Isaac 
6451c58f1c22SToby Isaac   PetscFunctionBegin;
6452c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6453c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
64549566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6455c58f1c22SToby Isaac   if (!flg) {
64569566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
64579566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
64589566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6459c58f1c22SToby Isaac   }
6460c58f1c22SToby Isaac   PetscFunctionReturn(0);
6461c58f1c22SToby Isaac }
6462c58f1c22SToby Isaac 
6463c58f1c22SToby Isaac /*@C
6464*bb7acecfSBarry 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.
64650fdc7489SMatthew Knepley 
64660fdc7489SMatthew Knepley   Not Collective
64670fdc7489SMatthew Knepley 
64680fdc7489SMatthew Knepley   Input Parameters:
6469*bb7acecfSBarry Smith + dm   - The `DM` object
64700fdc7489SMatthew Knepley . l    - The index for the label
64710fdc7489SMatthew Knepley - name - The label name
64720fdc7489SMatthew Knepley 
64730fdc7489SMatthew Knepley   Level: intermediate
64740fdc7489SMatthew Knepley 
6475db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
64760fdc7489SMatthew Knepley @*/
64770fdc7489SMatthew Knepley PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
64780fdc7489SMatthew Knepley {
64790fdc7489SMatthew Knepley   DMLabelLink    orig, prev = NULL;
64800fdc7489SMatthew Knepley   DMLabel        label;
64810fdc7489SMatthew Knepley   PetscInt       Nl, m;
64820fdc7489SMatthew Knepley   PetscBool      flg, match;
64830fdc7489SMatthew Knepley   const char    *lname;
64840fdc7489SMatthew Knepley 
64850fdc7489SMatthew Knepley   PetscFunctionBegin;
64860fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6487064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
64889566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
64890fdc7489SMatthew Knepley   if (!flg) {
64909566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
64919566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
64929566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
64930fdc7489SMatthew Knepley   }
64949566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
649563a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
64960fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
64979566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) orig->label, &lname));
64989566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
64990fdc7489SMatthew Knepley     if (match) break;
65000fdc7489SMatthew Knepley   }
65010fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
65020fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
65030fdc7489SMatthew Knepley   else    prev->next = orig->next;
65040fdc7489SMatthew Knepley   if (!l) {
65050fdc7489SMatthew Knepley     orig->next = dm->labels;
65060fdc7489SMatthew Knepley     dm->labels = orig;
65070fdc7489SMatthew Knepley   } else {
65080fdc7489SMatthew Knepley     for (m = 0, prev = dm->labels; m < l-1; ++m, prev = prev->next);
65090fdc7489SMatthew Knepley     orig->next = prev->next;
65100fdc7489SMatthew Knepley     prev->next = orig;
65110fdc7489SMatthew Knepley   }
65120fdc7489SMatthew Knepley   PetscFunctionReturn(0);
65130fdc7489SMatthew Knepley }
65140fdc7489SMatthew Knepley 
65150fdc7489SMatthew Knepley /*@C
6516*bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6517c58f1c22SToby Isaac 
6518c58f1c22SToby Isaac   Not Collective
6519c58f1c22SToby Isaac 
6520c58f1c22SToby Isaac   Input Parameters:
6521*bb7acecfSBarry Smith + dm   - The `DM` object
6522c58f1c22SToby Isaac . name - The label name
6523c58f1c22SToby Isaac - point - The mesh point
6524c58f1c22SToby Isaac 
6525c58f1c22SToby Isaac   Output Parameter:
6526c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6527c58f1c22SToby Isaac 
6528c58f1c22SToby Isaac   Level: beginner
6529c58f1c22SToby Isaac 
6530db781477SPatrick Sanan .seealso: `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6531c58f1c22SToby Isaac @*/
6532c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6533c58f1c22SToby Isaac {
6534c58f1c22SToby Isaac   DMLabel        label;
6535c58f1c22SToby Isaac 
6536c58f1c22SToby Isaac   PetscFunctionBegin;
6537c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6538c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
65399566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
65407a8be351SBarry Smith   PetscCheck(label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
65419566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
6542c58f1c22SToby Isaac   PetscFunctionReturn(0);
6543c58f1c22SToby Isaac }
6544c58f1c22SToby Isaac 
6545c58f1c22SToby Isaac /*@C
6546*bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6547c58f1c22SToby Isaac 
6548c58f1c22SToby Isaac   Not Collective
6549c58f1c22SToby Isaac 
6550c58f1c22SToby Isaac   Input Parameters:
6551*bb7acecfSBarry Smith + dm   - The `DM` object
6552c58f1c22SToby Isaac . name - The label name
6553c58f1c22SToby Isaac . point - The mesh point
6554c58f1c22SToby Isaac - value - The label value for this point
6555c58f1c22SToby Isaac 
6556c58f1c22SToby Isaac   Output Parameter:
6557c58f1c22SToby Isaac 
6558c58f1c22SToby Isaac   Level: beginner
6559c58f1c22SToby Isaac 
6560db781477SPatrick Sanan .seealso: `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6561c58f1c22SToby Isaac @*/
6562c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6563c58f1c22SToby Isaac {
6564c58f1c22SToby Isaac   DMLabel        label;
6565c58f1c22SToby Isaac 
6566c58f1c22SToby Isaac   PetscFunctionBegin;
6567c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6568c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
65699566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6570c58f1c22SToby Isaac   if (!label) {
65719566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
65729566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6573c58f1c22SToby Isaac   }
65749566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
6575c58f1c22SToby Isaac   PetscFunctionReturn(0);
6576c58f1c22SToby Isaac }
6577c58f1c22SToby Isaac 
6578c58f1c22SToby Isaac /*@C
6579*bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6580c58f1c22SToby Isaac 
6581c58f1c22SToby Isaac   Not Collective
6582c58f1c22SToby Isaac 
6583c58f1c22SToby Isaac   Input Parameters:
6584*bb7acecfSBarry Smith + dm   - The `DM` object
6585c58f1c22SToby Isaac . name - The label name
6586c58f1c22SToby Isaac . point - The mesh point
6587c58f1c22SToby Isaac - value - The label value for this point
6588c58f1c22SToby Isaac 
6589c58f1c22SToby Isaac   Level: beginner
6590c58f1c22SToby Isaac 
6591db781477SPatrick Sanan .seealso: `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6592c58f1c22SToby Isaac @*/
6593c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6594c58f1c22SToby Isaac {
6595c58f1c22SToby Isaac   DMLabel        label;
6596c58f1c22SToby Isaac 
6597c58f1c22SToby Isaac   PetscFunctionBegin;
6598c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6599c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66009566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6601c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
66029566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
6603c58f1c22SToby Isaac   PetscFunctionReturn(0);
6604c58f1c22SToby Isaac }
6605c58f1c22SToby Isaac 
6606c58f1c22SToby Isaac /*@C
6607*bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6608c58f1c22SToby Isaac 
6609c58f1c22SToby Isaac   Not Collective
6610c58f1c22SToby Isaac 
6611c58f1c22SToby Isaac   Input Parameters:
6612*bb7acecfSBarry Smith + dm   - The `DM` object
6613c58f1c22SToby Isaac - name - The label name
6614c58f1c22SToby Isaac 
6615c58f1c22SToby Isaac   Output Parameter:
6616c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6617c58f1c22SToby Isaac 
6618c58f1c22SToby Isaac   Level: beginner
6619c58f1c22SToby Isaac 
6620*bb7acecfSBarry Smith   Developer Note:
6621*bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6622*bb7acecfSBarry Smith 
6623*bb7acecfSBarry Smith .seealso: `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6624c58f1c22SToby Isaac @*/
6625c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6626c58f1c22SToby Isaac {
6627c58f1c22SToby Isaac   DMLabel        label;
6628c58f1c22SToby Isaac 
6629c58f1c22SToby Isaac   PetscFunctionBegin;
6630c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6631c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6632534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
66339566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6634c58f1c22SToby Isaac   *size = 0;
6635c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
66369566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
6637c58f1c22SToby Isaac   PetscFunctionReturn(0);
6638c58f1c22SToby Isaac }
6639c58f1c22SToby Isaac 
6640c58f1c22SToby Isaac /*@C
6641*bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6642c58f1c22SToby Isaac 
6643c58f1c22SToby Isaac   Not Collective
6644c58f1c22SToby Isaac 
6645c58f1c22SToby Isaac   Input Parameters:
6646*bb7acecfSBarry Smith + mesh - The `DM` object
6647c58f1c22SToby Isaac - name - The label name
6648c58f1c22SToby Isaac 
6649c58f1c22SToby Isaac   Output Parameter:
6650c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6651c58f1c22SToby Isaac 
6652c58f1c22SToby Isaac   Level: beginner
6653c58f1c22SToby Isaac 
6654db781477SPatrick Sanan .seealso: `DMLabelGetValueIS()`, `DMGetLabelSize()`
6655c58f1c22SToby Isaac @*/
6656c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6657c58f1c22SToby Isaac {
6658c58f1c22SToby Isaac   DMLabel        label;
6659c58f1c22SToby Isaac 
6660c58f1c22SToby Isaac   PetscFunctionBegin;
6661c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6662c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6663c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
66649566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6665c58f1c22SToby Isaac   *ids = NULL;
6666dab2e251SBlaise Bourdin   if (label) {
66679566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6668dab2e251SBlaise Bourdin   } else {
6669dab2e251SBlaise Bourdin     /* returning an empty IS */
66709566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids));
6671dab2e251SBlaise Bourdin   }
6672c58f1c22SToby Isaac   PetscFunctionReturn(0);
6673c58f1c22SToby Isaac }
6674c58f1c22SToby Isaac 
6675c58f1c22SToby Isaac /*@C
6676c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6677c58f1c22SToby Isaac 
6678c58f1c22SToby Isaac   Not Collective
6679c58f1c22SToby Isaac 
6680c58f1c22SToby Isaac   Input Parameters:
6681*bb7acecfSBarry Smith + dm - The `DM` object
6682c58f1c22SToby Isaac . name - The label name
6683c58f1c22SToby Isaac - value - The stratum value
6684c58f1c22SToby Isaac 
6685c58f1c22SToby Isaac   Output Parameter:
6686*bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6687c58f1c22SToby Isaac 
6688c58f1c22SToby Isaac   Level: beginner
6689c58f1c22SToby Isaac 
6690db781477SPatrick Sanan .seealso: `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6691c58f1c22SToby Isaac @*/
6692c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6693c58f1c22SToby Isaac {
6694c58f1c22SToby Isaac   DMLabel        label;
6695c58f1c22SToby Isaac 
6696c58f1c22SToby Isaac   PetscFunctionBegin;
6697c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6698c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6699534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
67009566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6701c58f1c22SToby Isaac   *size = 0;
6702c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
67039566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
6704c58f1c22SToby Isaac   PetscFunctionReturn(0);
6705c58f1c22SToby Isaac }
6706c58f1c22SToby Isaac 
6707c58f1c22SToby Isaac /*@C
6708c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6709c58f1c22SToby Isaac 
6710c58f1c22SToby Isaac   Not Collective
6711c58f1c22SToby Isaac 
6712c58f1c22SToby Isaac   Input Parameters:
6713*bb7acecfSBarry Smith + dm - The `DM` object
6714c58f1c22SToby Isaac . name - The label name
6715c58f1c22SToby Isaac - value - The stratum value
6716c58f1c22SToby Isaac 
6717c58f1c22SToby Isaac   Output Parameter:
6718c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6719c58f1c22SToby Isaac 
6720c58f1c22SToby Isaac   Level: beginner
6721c58f1c22SToby Isaac 
6722db781477SPatrick Sanan .seealso: `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6723c58f1c22SToby Isaac @*/
6724c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6725c58f1c22SToby Isaac {
6726c58f1c22SToby Isaac   DMLabel        label;
6727c58f1c22SToby Isaac 
6728c58f1c22SToby Isaac   PetscFunctionBegin;
6729c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6730c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6731c58f1c22SToby Isaac   PetscValidPointer(points, 4);
67329566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6733c58f1c22SToby Isaac   *points = NULL;
6734c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
67359566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
6736c58f1c22SToby Isaac   PetscFunctionReturn(0);
6737c58f1c22SToby Isaac }
6738c58f1c22SToby Isaac 
67394de306b1SToby Isaac /*@C
67409044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
67414de306b1SToby Isaac 
67424de306b1SToby Isaac   Not Collective
67434de306b1SToby Isaac 
67444de306b1SToby Isaac   Input Parameters:
6745*bb7acecfSBarry Smith + dm - The `DM` object
67464de306b1SToby Isaac . name - The label name
67474de306b1SToby Isaac . value - The stratum value
67484de306b1SToby Isaac - points - The stratum points
67494de306b1SToby Isaac 
67504de306b1SToby Isaac   Level: beginner
67514de306b1SToby Isaac 
6752*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
67534de306b1SToby Isaac @*/
67544de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
67554de306b1SToby Isaac {
67564de306b1SToby Isaac   DMLabel        label;
67574de306b1SToby Isaac 
67584de306b1SToby Isaac   PetscFunctionBegin;
67594de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67604de306b1SToby Isaac   PetscValidCharPointer(name, 2);
67614de306b1SToby Isaac   PetscValidPointer(points, 4);
67629566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67634de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
67649566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
67654de306b1SToby Isaac   PetscFunctionReturn(0);
67664de306b1SToby Isaac }
67674de306b1SToby Isaac 
6768c58f1c22SToby Isaac /*@C
6769*bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6770c58f1c22SToby Isaac 
6771c58f1c22SToby Isaac   Not Collective
6772c58f1c22SToby Isaac 
6773c58f1c22SToby Isaac   Input Parameters:
6774*bb7acecfSBarry Smith + dm   - The `DM` object
6775c58f1c22SToby Isaac . name - The label name
6776c58f1c22SToby Isaac - value - The label value for this point
6777c58f1c22SToby Isaac 
6778c58f1c22SToby Isaac   Output Parameter:
6779c58f1c22SToby Isaac 
6780c58f1c22SToby Isaac   Level: beginner
6781c58f1c22SToby Isaac 
6782*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6783c58f1c22SToby Isaac @*/
6784c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6785c58f1c22SToby Isaac {
6786c58f1c22SToby Isaac   DMLabel        label;
6787c58f1c22SToby Isaac 
6788c58f1c22SToby Isaac   PetscFunctionBegin;
6789c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6790c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
67919566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6792c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
67939566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
6794c58f1c22SToby Isaac   PetscFunctionReturn(0);
6795c58f1c22SToby Isaac }
6796c58f1c22SToby Isaac 
6797c58f1c22SToby Isaac /*@
6798*bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6799c58f1c22SToby Isaac 
6800c58f1c22SToby Isaac   Not Collective
6801c58f1c22SToby Isaac 
6802c58f1c22SToby Isaac   Input Parameter:
6803*bb7acecfSBarry Smith . dm   - The `DM` object
6804c58f1c22SToby Isaac 
6805c58f1c22SToby Isaac   Output Parameter:
6806c58f1c22SToby Isaac . numLabels - the number of Labels
6807c58f1c22SToby Isaac 
6808c58f1c22SToby Isaac   Level: intermediate
6809c58f1c22SToby Isaac 
6810*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6811c58f1c22SToby Isaac @*/
6812c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6813c58f1c22SToby Isaac {
68145d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6815c58f1c22SToby Isaac   PetscInt  n    = 0;
6816c58f1c22SToby Isaac 
6817c58f1c22SToby Isaac   PetscFunctionBegin;
6818c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6819534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
6820c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
6821c58f1c22SToby Isaac   *numLabels = n;
6822c58f1c22SToby Isaac   PetscFunctionReturn(0);
6823c58f1c22SToby Isaac }
6824c58f1c22SToby Isaac 
6825c58f1c22SToby Isaac /*@C
6826c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6827c58f1c22SToby Isaac 
6828c58f1c22SToby Isaac   Not Collective
6829c58f1c22SToby Isaac 
6830c58f1c22SToby Isaac   Input Parameters:
6831*bb7acecfSBarry Smith + dm - The `DM` object
6832c58f1c22SToby Isaac - n  - the label number
6833c58f1c22SToby Isaac 
6834c58f1c22SToby Isaac   Output Parameter:
6835c58f1c22SToby Isaac . name - the label name
6836c58f1c22SToby Isaac 
6837c58f1c22SToby Isaac   Level: intermediate
6838c58f1c22SToby Isaac 
6839*bb7acecfSBarry Smith   Developer Note:
6840*bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
6841*bb7acecfSBarry Smith 
6842*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6843c58f1c22SToby Isaac @*/
6844c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6845c58f1c22SToby Isaac {
68465d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
6847c58f1c22SToby Isaac   PetscInt       l    = 0;
6848c58f1c22SToby Isaac 
6849c58f1c22SToby Isaac   PetscFunctionBegin;
6850c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6851c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6852c58f1c22SToby Isaac   while (next) {
6853c58f1c22SToby Isaac     if (l == n) {
68549566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject) next->label, name));
6855c58f1c22SToby Isaac       PetscFunctionReturn(0);
6856c58f1c22SToby Isaac     }
6857c58f1c22SToby Isaac     ++l;
6858c58f1c22SToby Isaac     next = next->next;
6859c58f1c22SToby Isaac   }
686063a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6861c58f1c22SToby Isaac }
6862c58f1c22SToby Isaac 
6863c58f1c22SToby Isaac /*@C
6864*bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
6865c58f1c22SToby Isaac 
6866c58f1c22SToby Isaac   Not Collective
6867c58f1c22SToby Isaac 
6868c58f1c22SToby Isaac   Input Parameters:
6869*bb7acecfSBarry Smith + dm   - The `DM` object
6870c58f1c22SToby Isaac - name - The label name
6871c58f1c22SToby Isaac 
6872c58f1c22SToby Isaac   Output Parameter:
6873*bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
6874c58f1c22SToby Isaac 
6875c58f1c22SToby Isaac   Level: intermediate
6876c58f1c22SToby Isaac 
6877*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6878c58f1c22SToby Isaac @*/
6879c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6880c58f1c22SToby Isaac {
68815d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
6882d67d17b1SMatthew G. Knepley   const char    *lname;
6883c58f1c22SToby Isaac 
6884c58f1c22SToby Isaac   PetscFunctionBegin;
6885c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6886c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6887534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
6888c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6889c58f1c22SToby Isaac   while (next) {
68909566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
68919566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
6892c58f1c22SToby Isaac     if (*hasLabel) break;
6893c58f1c22SToby Isaac     next = next->next;
6894c58f1c22SToby Isaac   }
6895c58f1c22SToby Isaac   PetscFunctionReturn(0);
6896c58f1c22SToby Isaac }
6897c58f1c22SToby Isaac 
6898c58f1c22SToby Isaac /*@C
6899*bb7acecfSBarry Smith   DMGetLabel - Return the label of a given name, or NULL, from a `DM`
6900c58f1c22SToby Isaac 
6901c58f1c22SToby Isaac   Not Collective
6902c58f1c22SToby Isaac 
6903c58f1c22SToby Isaac   Input Parameters:
6904*bb7acecfSBarry Smith + dm   - The `DM` object
6905c58f1c22SToby Isaac - name - The label name
6906c58f1c22SToby Isaac 
6907c58f1c22SToby Isaac   Output Parameter:
6908*bb7acecfSBarry Smith . label - The `DMLabel`, or NULL if the label is absent
6909c58f1c22SToby Isaac 
6910*bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
6911*bb7acecfSBarry Smith +   "depth"       - Holds the depth (co-dimension) of each mesh point
6912*bb7acecfSBarry Smith .   "celltype"    - Holds the topological type of each cell
6913*bb7acecfSBarry Smith .   "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
6914*bb7acecfSBarry Smith .   "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
6915*bb7acecfSBarry Smith .   "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
6916*bb7acecfSBarry Smith -  "Vertex Sets" - Mirrors the vertex sets defined by GMsh
69176d7c9049SMatthew G. Knepley 
6918c58f1c22SToby Isaac   Level: intermediate
6919c58f1c22SToby Isaac 
6920*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
6921c58f1c22SToby Isaac @*/
6922c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6923c58f1c22SToby Isaac {
69245d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
6925c58f1c22SToby Isaac   PetscBool      hasLabel;
6926d67d17b1SMatthew G. Knepley   const char    *lname;
6927c58f1c22SToby Isaac 
6928c58f1c22SToby Isaac   PetscFunctionBegin;
6929c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6930c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6931c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6932c58f1c22SToby Isaac   *label = NULL;
6933c58f1c22SToby Isaac   while (next) {
69349566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
69359566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
6936c58f1c22SToby Isaac     if (hasLabel) {
6937c58f1c22SToby Isaac       *label = next->label;
6938c58f1c22SToby Isaac       break;
6939c58f1c22SToby Isaac     }
6940c58f1c22SToby Isaac     next = next->next;
6941c58f1c22SToby Isaac   }
6942c58f1c22SToby Isaac   PetscFunctionReturn(0);
6943c58f1c22SToby Isaac }
6944c58f1c22SToby Isaac 
6945c58f1c22SToby Isaac /*@C
6946*bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
6947c58f1c22SToby Isaac 
6948c58f1c22SToby Isaac   Not Collective
6949c58f1c22SToby Isaac 
6950c58f1c22SToby Isaac   Input Parameters:
6951*bb7acecfSBarry Smith + dm - The `DM` object
6952c58f1c22SToby Isaac - n  - the label number
6953c58f1c22SToby Isaac 
6954c58f1c22SToby Isaac   Output Parameter:
6955c58f1c22SToby Isaac . label - the label
6956c58f1c22SToby Isaac 
6957c58f1c22SToby Isaac   Level: intermediate
6958c58f1c22SToby Isaac 
6959*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6960c58f1c22SToby Isaac @*/
6961c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
6962c58f1c22SToby Isaac {
69635d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6964c58f1c22SToby Isaac   PetscInt    l    = 0;
6965c58f1c22SToby Isaac 
6966c58f1c22SToby Isaac   PetscFunctionBegin;
6967c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6968c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6969c58f1c22SToby Isaac   while (next) {
6970c58f1c22SToby Isaac     if (l == n) {
6971c58f1c22SToby Isaac       *label = next->label;
6972c58f1c22SToby Isaac       PetscFunctionReturn(0);
6973c58f1c22SToby Isaac     }
6974c58f1c22SToby Isaac     ++l;
6975c58f1c22SToby Isaac     next = next->next;
6976c58f1c22SToby Isaac   }
697763a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6978c58f1c22SToby Isaac }
6979c58f1c22SToby Isaac 
6980c58f1c22SToby Isaac /*@C
6981*bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
6982c58f1c22SToby Isaac 
6983c58f1c22SToby Isaac   Not Collective
6984c58f1c22SToby Isaac 
6985c58f1c22SToby Isaac   Input Parameters:
6986*bb7acecfSBarry Smith + dm   - The `DM` object
6987*bb7acecfSBarry Smith - label - The `DMLabel`
6988c58f1c22SToby Isaac 
6989c58f1c22SToby Isaac   Level: developer
6990c58f1c22SToby Isaac 
6991*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6992c58f1c22SToby Isaac @*/
6993c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
6994c58f1c22SToby Isaac {
69955d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
6996c58f1c22SToby Isaac   PetscBool      hasLabel;
6997d67d17b1SMatthew G. Knepley   const char    *lname;
69985d80c0bfSVaclav Hapla   PetscBool      flg;
6999c58f1c22SToby Isaac 
7000c58f1c22SToby Isaac   PetscFunctionBegin;
7001c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70029566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject) label, &lname));
70039566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
70047a8be351SBarry Smith   PetscCheck(!hasLabel,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
70059566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7006c58f1c22SToby Isaac   tmpLabel->label  = label;
7007c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
70085d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
70095d80c0bfSVaclav Hapla   *p = tmpLabel;
70109566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
70119566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
70125d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
70139566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7014ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7015c58f1c22SToby Isaac   PetscFunctionReturn(0);
7016c58f1c22SToby Isaac }
7017c58f1c22SToby Isaac 
7018c58f1c22SToby Isaac /*@C
70194a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
70204a7ee7d0SMatthew G. Knepley 
70214a7ee7d0SMatthew G. Knepley   Not Collective
70224a7ee7d0SMatthew G. Knepley 
70234a7ee7d0SMatthew G. Knepley   Input Parameters:
7024*bb7acecfSBarry Smith + dm    - The `DM` object
7025*bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
70264a7ee7d0SMatthew G. Knepley 
7027*bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7028*bb7acecfSBarry Smith +  "depth"       - Holds the depth (co-dimension) of each mesh point
7029*bb7acecfSBarry Smith .  "celltype"    - Holds the topological type of each cell
7030*bb7acecfSBarry Smith .  "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7031*bb7acecfSBarry Smith .  "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7032*bb7acecfSBarry Smith .  "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7033*bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
70344a7ee7d0SMatthew G. Knepley 
70354a7ee7d0SMatthew G. Knepley   Level: intermediate
70364a7ee7d0SMatthew G. Knepley 
7037*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
70384a7ee7d0SMatthew G. Knepley @*/
70394a7ee7d0SMatthew G. Knepley PetscErrorCode DMSetLabel(DM dm, DMLabel label)
70404a7ee7d0SMatthew G. Knepley {
70414a7ee7d0SMatthew G. Knepley   DMLabelLink    next = dm->labels;
70424a7ee7d0SMatthew G. Knepley   PetscBool      hasLabel, flg;
70434a7ee7d0SMatthew G. Knepley   const char    *name, *lname;
70444a7ee7d0SMatthew G. Knepley 
70454a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
70464a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70474a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
70489566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject) label, &name));
70494a7ee7d0SMatthew G. Knepley   while (next) {
70509566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
70519566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
70524a7ee7d0SMatthew G. Knepley     if (hasLabel) {
70539566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject) label));
70549566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
70554a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
70569566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
70574a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
70589566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
70594a7ee7d0SMatthew G. Knepley       next->label = label;
70604a7ee7d0SMatthew G. Knepley       break;
70614a7ee7d0SMatthew G. Knepley     }
70624a7ee7d0SMatthew G. Knepley     next = next->next;
70634a7ee7d0SMatthew G. Knepley   }
70644a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
70654a7ee7d0SMatthew G. Knepley }
70664a7ee7d0SMatthew G. Knepley 
70674a7ee7d0SMatthew G. Knepley /*@C
7068*bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7069c58f1c22SToby Isaac 
7070c58f1c22SToby Isaac   Not Collective
7071c58f1c22SToby Isaac 
7072c58f1c22SToby Isaac   Input Parameters:
7073*bb7acecfSBarry Smith + dm   - The `DM` object
7074c58f1c22SToby Isaac - name - The label name
7075c58f1c22SToby Isaac 
7076c58f1c22SToby Isaac   Output Parameter:
7077*bb7acecfSBarry Smith . label - The `DMLabel`, or NULL if the label is absent. Pass in NULL to call `DMLabelDestroy()` on the label, otherwise the
7078*bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7079c58f1c22SToby Isaac 
7080c58f1c22SToby Isaac   Level: developer
7081c58f1c22SToby Isaac 
7082*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7083c58f1c22SToby Isaac @*/
7084c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7085c58f1c22SToby Isaac {
708695d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7087c58f1c22SToby Isaac   PetscBool      hasLabel;
7088d67d17b1SMatthew G. Knepley   const char    *lname;
7089c58f1c22SToby Isaac 
7090c58f1c22SToby Isaac   PetscFunctionBegin;
7091c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7092e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7093e5472504SVaclav Hapla   if (label) {
7094e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7095c58f1c22SToby Isaac     *label = NULL;
7096e5472504SVaclav Hapla   }
70975d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
70989566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) link->label, &lname));
70999566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7100c58f1c22SToby Isaac     if (hasLabel) {
710195d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
71029566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
710395d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
71049566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7105ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
710695d578d6SVaclav Hapla       if (label) *label = link->label;
71079566063dSJacob Faibussowitsch       else       PetscCall(DMLabelDestroy(&link->label));
71089566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7109c58f1c22SToby Isaac       break;
7110c58f1c22SToby Isaac     }
7111c58f1c22SToby Isaac   }
7112c58f1c22SToby Isaac   PetscFunctionReturn(0);
7113c58f1c22SToby Isaac }
7114c58f1c22SToby Isaac 
7115306894acSVaclav Hapla /*@
7116*bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7117306894acSVaclav Hapla 
7118306894acSVaclav Hapla   Not Collective
7119306894acSVaclav Hapla 
7120306894acSVaclav Hapla   Input Parameters:
7121*bb7acecfSBarry Smith + dm   - The `DM` object
7122*bb7acecfSBarry Smith . label - The `DMLabel` to be removed from the `DM`
7123306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7124306894acSVaclav Hapla 
7125306894acSVaclav Hapla   Level: developer
7126306894acSVaclav Hapla 
7127*bb7acecfSBarry Smith   Note:
7128306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7129*bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7130306894acSVaclav Hapla   *label nullified.
7131306894acSVaclav Hapla 
7132*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7133306894acSVaclav Hapla @*/
7134306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7135306894acSVaclav Hapla {
713643e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7137306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7138306894acSVaclav Hapla 
7139306894acSVaclav Hapla   PetscFunctionBegin;
7140306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7141306894acSVaclav Hapla   PetscValidPointer(label, 2);
7142f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7143306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7144306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
71455d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
714643e45a93SVaclav Hapla     if (*label == link->label) {
7147306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
714843e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7149306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7150ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
715143e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
71529566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
71539566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7154306894acSVaclav Hapla       break;
7155306894acSVaclav Hapla     }
7156306894acSVaclav Hapla   }
71577a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7158306894acSVaclav Hapla   PetscFunctionReturn(0);
7159306894acSVaclav Hapla }
7160306894acSVaclav Hapla 
7161c58f1c22SToby Isaac /*@C
7162c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7163c58f1c22SToby Isaac 
7164c58f1c22SToby Isaac   Not Collective
7165c58f1c22SToby Isaac 
7166c58f1c22SToby Isaac   Input Parameters:
7167*bb7acecfSBarry Smith + dm   - The `DM` object
7168c58f1c22SToby Isaac - name - The label name
7169c58f1c22SToby Isaac 
7170c58f1c22SToby Isaac   Output Parameter:
7171c58f1c22SToby Isaac . output - The flag for output
7172c58f1c22SToby Isaac 
7173c58f1c22SToby Isaac   Level: developer
7174c58f1c22SToby Isaac 
7175*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7176c58f1c22SToby Isaac @*/
7177c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7178c58f1c22SToby Isaac {
71795d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7180d67d17b1SMatthew G. Knepley   const char    *lname;
7181c58f1c22SToby Isaac 
7182c58f1c22SToby Isaac   PetscFunctionBegin;
7183c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7184dadcf809SJacob Faibussowitsch   PetscValidCharPointer(name, 2);
7185dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(output, 3);
7186c58f1c22SToby Isaac   while (next) {
7187c58f1c22SToby Isaac     PetscBool flg;
7188c58f1c22SToby Isaac 
71899566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
71909566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
7191c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7192c58f1c22SToby Isaac     next = next->next;
7193c58f1c22SToby Isaac   }
719498921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7195c58f1c22SToby Isaac }
7196c58f1c22SToby Isaac 
7197c58f1c22SToby Isaac /*@C
7198*bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7199c58f1c22SToby Isaac 
7200c58f1c22SToby Isaac   Not Collective
7201c58f1c22SToby Isaac 
7202c58f1c22SToby Isaac   Input Parameters:
7203*bb7acecfSBarry Smith + dm     - The `DM` object
7204c58f1c22SToby Isaac . name   - The label name
7205*bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7206c58f1c22SToby Isaac 
7207c58f1c22SToby Isaac   Level: developer
7208c58f1c22SToby Isaac 
7209*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7210c58f1c22SToby Isaac @*/
7211c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7212c58f1c22SToby Isaac {
72135d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7214d67d17b1SMatthew G. Knepley   const char    *lname;
7215c58f1c22SToby Isaac 
7216c58f1c22SToby Isaac   PetscFunctionBegin;
7217c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7218534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7219c58f1c22SToby Isaac   while (next) {
7220c58f1c22SToby Isaac     PetscBool flg;
7221c58f1c22SToby Isaac 
72229566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
72239566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
7224c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7225c58f1c22SToby Isaac     next = next->next;
7226c58f1c22SToby Isaac   }
722798921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7228c58f1c22SToby Isaac }
7229c58f1c22SToby Isaac 
7230c58f1c22SToby Isaac /*@
7231*bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7232c58f1c22SToby Isaac 
7233d083f849SBarry Smith   Collective on dmA
7234c58f1c22SToby Isaac 
7235d8d19677SJose E. Roman   Input Parameters:
7236*bb7acecfSBarry Smith + dmA - The `DM` object with initial labels
7237*bb7acecfSBarry Smith . dmB - The `DM` object to which labels are copied
7238*bb7acecfSBarry Smith . mode - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7239*bb7acecfSBarry Smith . all  - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7240*bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7241c58f1c22SToby Isaac 
7242c58f1c22SToby Isaac   Level: intermediate
7243c58f1c22SToby Isaac 
7244*bb7acecfSBarry Smith   Note:
72452cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7246c58f1c22SToby Isaac 
7247*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7248c58f1c22SToby Isaac @*/
72492cbb9b06SVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7250c58f1c22SToby Isaac {
72512cbb9b06SVaclav Hapla   DMLabel        label, labelNew, labelOld;
7252c58f1c22SToby Isaac   const char    *name;
7253c58f1c22SToby Isaac   PetscBool      flg;
72545d80c0bfSVaclav Hapla   DMLabelLink    link;
7255c58f1c22SToby Isaac 
72565d80c0bfSVaclav Hapla   PetscFunctionBegin;
72575d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
72585d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
72595d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
72605d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
72617a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER,PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
72625d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
72635d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
72645d80c0bfSVaclav Hapla     label=link->label;
72659566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
72665d80c0bfSVaclav Hapla     if (!all) {
72679566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7268c58f1c22SToby Isaac       if (flg) continue;
72699566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
72707d5acc75SStefano Zampini       if (flg) continue;
72719566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7272ba2698f1SMatthew G. Knepley       if (flg) continue;
72735d80c0bfSVaclav Hapla     }
72749566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
72752cbb9b06SVaclav Hapla     if (labelOld) {
72762cbb9b06SVaclav Hapla       switch (emode) {
72772cbb9b06SVaclav Hapla         case DM_COPY_LABELS_KEEP:
72782cbb9b06SVaclav Hapla           continue;
72792cbb9b06SVaclav Hapla         case DM_COPY_LABELS_REPLACE:
72809566063dSJacob Faibussowitsch           PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
72812cbb9b06SVaclav Hapla           break;
72822cbb9b06SVaclav Hapla         case DM_COPY_LABELS_FAIL:
728398921bdaSJacob Faibussowitsch           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
72842cbb9b06SVaclav Hapla         default:
72857a8be351SBarry Smith           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
72862cbb9b06SVaclav Hapla       }
72872cbb9b06SVaclav Hapla     }
72885d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
72899566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
72905d80c0bfSVaclav Hapla     } else {
72915d80c0bfSVaclav Hapla       labelNew = label;
72925d80c0bfSVaclav Hapla     }
72939566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
72949566063dSJacob Faibussowitsch     if (mode==PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7295c58f1c22SToby Isaac   }
7296c58f1c22SToby Isaac   PetscFunctionReturn(0);
7297c58f1c22SToby Isaac }
7298461a15a0SLisandro Dalcin 
7299609dae6eSVaclav Hapla /*@C
7300*bb7acecfSBarry Smith   DMCompareLabels - Compare labels of two `DMPLEX` meshes
7301609dae6eSVaclav Hapla 
73025efe38ccSVaclav Hapla   Collective
7303609dae6eSVaclav Hapla 
7304609dae6eSVaclav Hapla   Input Parameters:
7305*bb7acecfSBarry Smith + dm0 - First `DM` object
7306*bb7acecfSBarry Smith - dm1 - Second `DM` object
7307609dae6eSVaclav Hapla 
7308609dae6eSVaclav Hapla   Output Parameters
73095efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
7310609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
7311609dae6eSVaclav Hapla 
7312609dae6eSVaclav Hapla   Level: intermediate
7313609dae6eSVaclav Hapla 
7314609dae6eSVaclav Hapla   Notes:
7315*bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7316*bb7acecfSBarry Smith 
7317*bb7acecfSBarry Smith   If equal is passed as NULL and difference is found, an error is thrown on all processes.
7318*bb7acecfSBarry Smith 
7319*bb7acecfSBarry Smith   Make sure to pass equal is NULL on all processes or none of them.
7320609dae6eSVaclav Hapla 
73215efe38ccSVaclav Hapla   The output message is set independently on each rank.
7322*bb7acecfSBarry Smith 
7323*bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7324*bb7acecfSBarry Smith 
7325*bb7acecfSBarry Smith   If message is passed as NULL and a difference is found, the difference description is printed to stderr in synchronized manner.
7326*bb7acecfSBarry Smith 
7327*bb7acecfSBarry Smith   Make sure to pass message as NULL on all processes or no processes.
7328609dae6eSVaclav Hapla 
7329609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7330*bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7331609dae6eSVaclav Hapla 
7332*bb7acecfSBarry Smith   Fortran Note:
7333*bb7acecfSBarry Smith   This function is not available from Fortran.
7334609dae6eSVaclav Hapla 
7335*bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7336609dae6eSVaclav Hapla @*/
73375efe38ccSVaclav Hapla PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7338609dae6eSVaclav Hapla {
73395efe38ccSVaclav Hapla   PetscInt        n, i;
7340609dae6eSVaclav Hapla   char            msg[PETSC_MAX_PATH_LEN] = "";
73415efe38ccSVaclav Hapla   PetscBool       eq;
7342609dae6eSVaclav Hapla   MPI_Comm        comm;
73435efe38ccSVaclav Hapla   PetscMPIInt     rank;
7344609dae6eSVaclav Hapla 
7345609dae6eSVaclav Hapla   PetscFunctionBegin;
7346609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0,DM_CLASSID,1);
7347609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1,DM_CLASSID,2);
7348609dae6eSVaclav Hapla   PetscCheckSameComm(dm0,1,dm1,2);
73495efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal,3);
7350609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
73519566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
73529566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
73535efe38ccSVaclav Hapla   {
73545efe38ccSVaclav Hapla     PetscInt n1;
73555efe38ccSVaclav Hapla 
73569566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
73579566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
73585efe38ccSVaclav Hapla     eq = (PetscBool) (n == n1);
73595efe38ccSVaclav Hapla     if (!eq) {
736063a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7361609dae6eSVaclav Hapla     }
73629566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
73635efe38ccSVaclav Hapla     if (!eq) goto finish;
73645efe38ccSVaclav Hapla   }
73655efe38ccSVaclav Hapla   for (i=0; i<n; i++) {
7366609dae6eSVaclav Hapla     DMLabel     l0, l1;
7367609dae6eSVaclav Hapla     const char *name;
7368609dae6eSVaclav Hapla     char       *msgInner;
7369609dae6eSVaclav Hapla 
7370609dae6eSVaclav Hapla     /* Ignore label order */
73719566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
73729566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
73739566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7374609dae6eSVaclav Hapla     if (!l1) {
737563a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
73765efe38ccSVaclav Hapla       eq = PETSC_FALSE;
73775efe38ccSVaclav Hapla       break;
7378609dae6eSVaclav Hapla     }
73799566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
73809566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
73819566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
73825efe38ccSVaclav Hapla     if (!eq) break;
7383609dae6eSVaclav Hapla   }
73849566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7385609dae6eSVaclav Hapla finish:
73865efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7387609dae6eSVaclav Hapla   if (message) {
7388609dae6eSVaclav Hapla     *message = NULL;
7389609dae6eSVaclav Hapla     if (msg[0]) {
73909566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(msg, message));
7391609dae6eSVaclav Hapla     }
73925efe38ccSVaclav Hapla   } else {
73935efe38ccSVaclav Hapla     if (msg[0]) {
73949566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
7395609dae6eSVaclav Hapla     }
73969566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
73975efe38ccSVaclav Hapla   }
73985efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
73995efe38ccSVaclav Hapla   if (equal) *equal = eq;
74007a8be351SBarry Smith   else PetscCheck(eq,comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
7401609dae6eSVaclav Hapla   PetscFunctionReturn(0);
7402609dae6eSVaclav Hapla }
7403609dae6eSVaclav Hapla 
7404461a15a0SLisandro Dalcin PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7405461a15a0SLisandro Dalcin {
7406461a15a0SLisandro Dalcin   PetscFunctionBegin;
7407461a15a0SLisandro Dalcin   PetscValidPointer(label,2);
7408461a15a0SLisandro Dalcin   if (!*label) {
74099566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
74109566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7411461a15a0SLisandro Dalcin   }
74129566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
7413461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
7414461a15a0SLisandro Dalcin }
7415461a15a0SLisandro Dalcin 
74160fdc7489SMatthew Knepley /*
74170fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
74180fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
74190fdc7489SMatthew Knepley   (label, id) pair in the DM.
74200fdc7489SMatthew Knepley 
74210fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
74220fdc7489SMatthew Knepley   each label.
74230fdc7489SMatthew Knepley */
74240fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
74250fdc7489SMatthew Knepley {
74260fdc7489SMatthew Knepley   DMUniversalLabel ul;
74270fdc7489SMatthew Knepley   PetscBool       *active;
74280fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
74290fdc7489SMatthew Knepley 
74300fdc7489SMatthew Knepley   PetscFunctionBegin;
74319566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
74329566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
74339566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
74349566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
74350fdc7489SMatthew Knepley   ul->Nl = 0;
74360fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
74370fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
74380fdc7489SMatthew Knepley     const char *name;
74390fdc7489SMatthew Knepley 
74409566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
74419566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
74429566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
74430fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
74440fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
74450fdc7489SMatthew Knepley   }
74469566063dSJacob 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));
74470fdc7489SMatthew Knepley   ul->Nv = 0;
74480fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
74490fdc7489SMatthew Knepley     DMLabel     label;
74500fdc7489SMatthew Knepley     PetscInt    nv;
74510fdc7489SMatthew Knepley     const char *name;
74520fdc7489SMatthew Knepley 
74530fdc7489SMatthew Knepley     if (!active[l]) continue;
74549566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
74559566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
74569566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
74579566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
74580fdc7489SMatthew Knepley     ul->indices[m]   = l;
74590fdc7489SMatthew Knepley     ul->Nv          += nv;
74600fdc7489SMatthew Knepley     ul->offsets[m+1] = nv;
74610fdc7489SMatthew Knepley     ul->bits[m+1]    = PetscCeilReal(PetscLog2Real(nv+1));
74620fdc7489SMatthew Knepley     ++m;
74630fdc7489SMatthew Knepley   }
74640fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
74650fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l-1] + ul->offsets[l];
74660fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l-1]    + ul->bits[l];
74670fdc7489SMatthew Knepley   }
74680fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
74690fdc7489SMatthew Knepley     PetscInt b;
74700fdc7489SMatthew Knepley 
74710fdc7489SMatthew Knepley     ul->masks[l] = 0;
74720fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l+1]; ++b) ul->masks[l] |= 1 << b;
74730fdc7489SMatthew Knepley   }
74749566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
74750fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
74760fdc7489SMatthew Knepley     DMLabel         label;
74770fdc7489SMatthew Knepley     IS              valueIS;
74780fdc7489SMatthew Knepley     const PetscInt *varr;
74790fdc7489SMatthew Knepley     PetscInt        nv, v;
74800fdc7489SMatthew Knepley 
74810fdc7489SMatthew Knepley     if (!active[l]) continue;
74829566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
74839566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
74849566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
74859566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
74860fdc7489SMatthew Knepley     for (v = 0; v < nv; ++v) {
74870fdc7489SMatthew Knepley       ul->values[ul->offsets[m]+v] = varr[v];
74880fdc7489SMatthew Knepley     }
74899566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
74909566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
74919566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
74920fdc7489SMatthew Knepley     ++m;
74930fdc7489SMatthew Knepley   }
74949566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
74950fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
74960fdc7489SMatthew Knepley     PetscInt  uval = 0;
74970fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
74980fdc7489SMatthew Knepley 
74990fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
75000fdc7489SMatthew Knepley       DMLabel  label;
75010649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
75020fdc7489SMatthew Knepley 
75030fdc7489SMatthew Knepley       if (!active[l]) continue;
75049566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
75059566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
75069566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
75070fdc7489SMatthew Knepley       if (val == defval) {++m; continue;}
75080649b39aSStefano Zampini       nv = ul->offsets[m+1]-ul->offsets[m];
75090fdc7489SMatthew Knepley       marked = PETSC_TRUE;
75109566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
751163a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
75120fdc7489SMatthew Knepley       uval += (loc+1) << ul->bits[m];
75130fdc7489SMatthew Knepley       ++m;
75140fdc7489SMatthew Knepley     }
75159566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
75160fdc7489SMatthew Knepley   }
75179566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
75180fdc7489SMatthew Knepley   *universal = ul;
75190fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75200fdc7489SMatthew Knepley }
75210fdc7489SMatthew Knepley 
75220fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
75230fdc7489SMatthew Knepley {
75240fdc7489SMatthew Knepley   PetscInt       l;
75250fdc7489SMatthew Knepley 
75260fdc7489SMatthew Knepley   PetscFunctionBegin;
75279566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
75289566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
75299566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
75309566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
75319566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
75320fdc7489SMatthew Knepley   *universal = NULL;
75330fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75340fdc7489SMatthew Knepley }
75350fdc7489SMatthew Knepley 
75360fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
75370fdc7489SMatthew Knepley {
75380fdc7489SMatthew Knepley   PetscFunctionBegin;
75390fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
75400fdc7489SMatthew Knepley   *ulabel = ul->label;
75410fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75420fdc7489SMatthew Knepley }
75430fdc7489SMatthew Knepley 
75440fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
75450fdc7489SMatthew Knepley {
75460fdc7489SMatthew Knepley   PetscInt       Nl = ul->Nl, l;
75470fdc7489SMatthew Knepley 
75480fdc7489SMatthew Knepley   PetscFunctionBegin;
7549064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
75500fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
75519566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
75529566063dSJacob Faibussowitsch     else               PetscCall(DMCreateLabel(dm, ul->names[l]));
75530fdc7489SMatthew Knepley   }
75540fdc7489SMatthew Knepley   if (preserveOrder) {
75550fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
75560fdc7489SMatthew Knepley       const char *name;
75570fdc7489SMatthew Knepley       PetscBool   match;
75580fdc7489SMatthew Knepley 
75599566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
75609566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
756163a3b9bcSJacob 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]);
75620fdc7489SMatthew Knepley     }
75630fdc7489SMatthew Knepley   }
75640fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75650fdc7489SMatthew Knepley }
75660fdc7489SMatthew Knepley 
75670fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
75680fdc7489SMatthew Knepley {
75690fdc7489SMatthew Knepley   PetscInt       l;
75700fdc7489SMatthew Knepley 
75710fdc7489SMatthew Knepley   PetscFunctionBegin;
75720fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
75730fdc7489SMatthew Knepley     DMLabel  label;
75740fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
75750fdc7489SMatthew Knepley 
75760fdc7489SMatthew Knepley     if (lval) {
75779566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
75789566063dSJacob Faibussowitsch       else          PetscCall(DMGetLabel(dm, ul->names[l], &label));
75799566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l]+lval-1]));
75800fdc7489SMatthew Knepley     }
75810fdc7489SMatthew Knepley   }
75820fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75830fdc7489SMatthew Knepley }
7584a8fb8f29SToby Isaac 
7585a8fb8f29SToby Isaac /*@
7586*bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7587*bb7acecfSBarry Smith 
7588*bb7acecfSBarry Smith   Not collective
7589a8fb8f29SToby Isaac 
7590a8fb8f29SToby Isaac   Input Parameter:
7591*bb7acecfSBarry Smith . dm - The `DM` object
7592a8fb8f29SToby Isaac 
7593a8fb8f29SToby Isaac   Output Parameter:
7594*bb7acecfSBarry Smith . cdm - The coarse `DM`
7595a8fb8f29SToby Isaac 
7596a8fb8f29SToby Isaac   Level: intermediate
7597a8fb8f29SToby Isaac 
7598*bb7acecfSBarry Smith .seealso: `DMSetCoarseDM()`, `DMCoarsen()`
7599a8fb8f29SToby Isaac @*/
7600a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7601a8fb8f29SToby Isaac {
7602a8fb8f29SToby Isaac   PetscFunctionBegin;
7603a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7604a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7605a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7606a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7607a8fb8f29SToby Isaac }
7608a8fb8f29SToby Isaac 
7609a8fb8f29SToby Isaac /*@
7610*bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7611a8fb8f29SToby Isaac 
7612a8fb8f29SToby Isaac   Input Parameters:
7613*bb7acecfSBarry Smith + dm - The `DM` object
7614*bb7acecfSBarry Smith - cdm - The coarse `DM`
7615a8fb8f29SToby Isaac 
7616a8fb8f29SToby Isaac   Level: intermediate
7617a8fb8f29SToby Isaac 
7618*bb7acecfSBarry Smith   Note:
7619*bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7620*bb7acecfSBarry Smith 
7621*bb7acecfSBarry Smith .seealso: `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7622a8fb8f29SToby Isaac @*/
7623a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7624a8fb8f29SToby Isaac {
7625a8fb8f29SToby Isaac   PetscFunctionBegin;
7626a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7627a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
76289566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
76299566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7630a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7631a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7632a8fb8f29SToby Isaac }
7633a8fb8f29SToby Isaac 
763488bdff64SToby Isaac /*@
7635*bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
763688bdff64SToby Isaac 
763788bdff64SToby Isaac   Input Parameter:
7638*bb7acecfSBarry Smith . dm - The `DM` object
763988bdff64SToby Isaac 
764088bdff64SToby Isaac   Output Parameter:
7641*bb7acecfSBarry Smith . fdm - The fine `DM`
764288bdff64SToby Isaac 
764388bdff64SToby Isaac   Level: intermediate
764488bdff64SToby Isaac 
7645*bb7acecfSBarry Smith .seealso: `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
764688bdff64SToby Isaac @*/
764788bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
764888bdff64SToby Isaac {
764988bdff64SToby Isaac   PetscFunctionBegin;
765088bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
765188bdff64SToby Isaac   PetscValidPointer(fdm, 2);
765288bdff64SToby Isaac   *fdm = dm->fineMesh;
765388bdff64SToby Isaac   PetscFunctionReturn(0);
765488bdff64SToby Isaac }
765588bdff64SToby Isaac 
765688bdff64SToby Isaac /*@
7657*bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
765888bdff64SToby Isaac 
765988bdff64SToby Isaac   Input Parameters:
7660*bb7acecfSBarry Smith + dm - The `DM` object
7661*bb7acecfSBarry Smith - fdm - The fine `DM`
766288bdff64SToby Isaac 
7663*bb7acecfSBarry Smith   Level: developer
766488bdff64SToby Isaac 
7665*bb7acecfSBarry Smith   Note:
7666*bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7667*bb7acecfSBarry Smith 
7668*bb7acecfSBarry Smith .seealso: `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
766988bdff64SToby Isaac @*/
767088bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
767188bdff64SToby Isaac {
767288bdff64SToby Isaac   PetscFunctionBegin;
767388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
767488bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
76759566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
76769566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
767788bdff64SToby Isaac   dm->fineMesh = fdm;
767888bdff64SToby Isaac   PetscFunctionReturn(0);
767988bdff64SToby Isaac }
768088bdff64SToby Isaac 
7681a6ba4734SToby Isaac /*@C
7682*bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7683a6ba4734SToby Isaac 
7684783e2ec8SMatthew G. Knepley   Collective on dm
7685783e2ec8SMatthew G. Knepley 
7686a6ba4734SToby Isaac   Input Parameters:
7687*bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7688*bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7689a6ba4734SToby Isaac . name     - The BC name
769045480ffeSMatthew G. Knepley . label    - The label defining constrained points
7691*bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
769245480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7693a6ba4734SToby Isaac . field    - The field to constrain
769445480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7695a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7696a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
769756cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7698a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7699a6ba4734SToby Isaac 
770045480ffeSMatthew G. Knepley   Output Parameter:
770145480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
770245480ffeSMatthew G. Knepley 
7703a6ba4734SToby Isaac   Options Database Keys:
7704a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7705a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7706a6ba4734SToby Isaac 
7707*bb7acecfSBarry Smith   Notes:
7708*bb7acecfSBarry Smith   Both bcFunc abd bcFunc_t will depend on the boundary condition type. If the type if `DM_BC_ESSENTIAL`, then the calling sequence is:
770956cf3b9cSMatthew G. Knepley 
771056cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
771156cf3b9cSMatthew G. Knepley 
7712*bb7acecfSBarry Smith   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is:
771356cf3b9cSMatthew G. Knepley 
771456cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
771556cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
771656cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
771756cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
771856cf3b9cSMatthew G. Knepley 
771956cf3b9cSMatthew G. Knepley + dim - the spatial dimension
772056cf3b9cSMatthew G. Knepley . Nf - the number of fields
772156cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
772256cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
772356cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
772456cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
772556cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
772656cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
772756cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
772856cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
772956cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
773056cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
773156cf3b9cSMatthew G. Knepley . t - current time
773256cf3b9cSMatthew G. Knepley . x - coordinates of the current point
773356cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
773456cf3b9cSMatthew G. Knepley . constants - constant parameters
773556cf3b9cSMatthew G. Knepley - bcval - output values at the current point
773656cf3b9cSMatthew G. Knepley 
7737ed808b8fSJed Brown   Level: intermediate
7738a6ba4734SToby Isaac 
7739db781477SPatrick Sanan .seealso: `DSGetBoundary()`, `PetscDSAddBoundary()`
7740a6ba4734SToby Isaac @*/
774145480ffeSMatthew G. Knepley 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)
7742a6ba4734SToby Isaac {
7743e5e52638SMatthew G. Knepley   PetscDS        ds;
7744a6ba4734SToby Isaac 
7745a6ba4734SToby Isaac   PetscFunctionBegin;
7746a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7747783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
774845480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
774945480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
775045480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
775145480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
775201a5d20dSJed Brown   PetscCheck(!dm->localSection,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot add boundary to DM after creating local section");
77539566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7754799db056SMatthew G. Knepley   /* Complete label */
7755799db056SMatthew G. Knepley   if (label) {
7756799db056SMatthew G. Knepley     PetscObject  obj;
7757799db056SMatthew G. Knepley     PetscClassId id;
7758799db056SMatthew G. Knepley 
7759799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7760799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7761799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7762799db056SMatthew G. Knepley       DM plex;
7763799db056SMatthew G. Knepley 
7764799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7765799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7766799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7767799db056SMatthew G. Knepley     }
7768799db056SMatthew G. Knepley   }
77699566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
7770a6ba4734SToby Isaac   PetscFunctionReturn(0);
7771a6ba4734SToby Isaac }
7772a6ba4734SToby Isaac 
777345480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7774e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7775e6f8dbb6SToby Isaac {
7776e5e52638SMatthew G. Knepley   PetscDS        ds;
7777dff059c6SToby Isaac   DMBoundary    *lastnext;
7778e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7779e6f8dbb6SToby Isaac 
7780e6f8dbb6SToby Isaac   PetscFunctionBegin;
77819566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7782e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
778347a1f5adSToby Isaac   if (dm->boundary) {
778447a1f5adSToby Isaac     DMBoundary next = dm->boundary;
778547a1f5adSToby Isaac 
778647a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
778747a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
778847a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
778947a1f5adSToby Isaac     while (next) {
779047a1f5adSToby Isaac       DMBoundary b = next;
779147a1f5adSToby Isaac 
779247a1f5adSToby Isaac       next = b->next;
77939566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7794a6ba4734SToby Isaac     }
779547a1f5adSToby Isaac     dm->boundary = NULL;
7796a6ba4734SToby Isaac   }
779747a1f5adSToby Isaac 
7798dff059c6SToby Isaac   lastnext = &(dm->boundary);
7799e6f8dbb6SToby Isaac   while (dsbound) {
7800e6f8dbb6SToby Isaac     DMBoundary dmbound;
7801e6f8dbb6SToby Isaac 
78029566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7803e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
780445480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
780547a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7806dff059c6SToby Isaac     *lastnext = dmbound;
7807dff059c6SToby Isaac     lastnext = &(dmbound->next);
7808dff059c6SToby Isaac     dsbound = dsbound->next;
7809a6ba4734SToby Isaac   }
7810a6ba4734SToby Isaac   PetscFunctionReturn(0);
7811a6ba4734SToby Isaac }
7812a6ba4734SToby Isaac 
7813*bb7acecfSBarry Smith /* TODO: missing manual page */
7814a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7815a6ba4734SToby Isaac {
7816b95f2879SToby Isaac   DMBoundary     b;
7817a6ba4734SToby Isaac 
7818a6ba4734SToby Isaac   PetscFunctionBegin;
7819a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7820534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7821a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
78229566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7823b95f2879SToby Isaac   b = dm->boundary;
7824a6ba4734SToby Isaac   while (b && !(*isBd)) {
7825e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7826e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7827a6ba4734SToby Isaac     PetscInt   i;
7828a6ba4734SToby Isaac 
782945480ffeSMatthew G. Knepley     if (label) {
78309566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7831a6ba4734SToby Isaac     }
7832a6ba4734SToby Isaac     b = b->next;
7833a6ba4734SToby Isaac   }
7834a6ba4734SToby Isaac   PetscFunctionReturn(0);
7835a6ba4734SToby Isaac }
78364d6f44ffSToby Isaac 
78374d6f44ffSToby Isaac /*@C
7838*bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
7839a6e0b375SMatthew G. Knepley 
7840*bb7acecfSBarry Smith   Collective on dm
78414d6f44ffSToby Isaac 
78424d6f44ffSToby Isaac   Input Parameters:
7843*bb7acecfSBarry Smith + dm      - The `DM`
78440709b2feSToby Isaac . time    - The time
78454d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
78464d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
78474d6f44ffSToby Isaac - mode    - The insertion mode for values
78484d6f44ffSToby Isaac 
78494d6f44ffSToby Isaac   Output Parameter:
78504d6f44ffSToby Isaac . X - vector
78514d6f44ffSToby Isaac 
78524d6f44ffSToby Isaac    Calling sequence of func:
785377b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
78544d6f44ffSToby Isaac 
78554d6f44ffSToby Isaac +  dim - The spatial dimension
78568ec8862eSJed Brown .  time - The time at which to sample
78574d6f44ffSToby Isaac .  x   - The coordinates
785877b739a6SMatthew Knepley .  Nc  - The number of components
78594d6f44ffSToby Isaac .  u   - The output field values
78604d6f44ffSToby Isaac -  ctx - optional user-defined function context
78614d6f44ffSToby Isaac 
78624d6f44ffSToby Isaac   Level: developer
78634d6f44ffSToby Isaac 
7864*bb7acecfSBarry Smith   Developer Notes:
7865*bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7866*bb7acecfSBarry Smith 
7867*bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7868*bb7acecfSBarry Smith 
7869db781477SPatrick Sanan .seealso: `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
78704d6f44ffSToby Isaac @*/
78710709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
78724d6f44ffSToby Isaac {
78734d6f44ffSToby Isaac   Vec            localX;
78744d6f44ffSToby Isaac 
78754d6f44ffSToby Isaac   PetscFunctionBegin;
78764d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78779566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
78789566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
78799566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
78809566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
78819566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
78824d6f44ffSToby Isaac   PetscFunctionReturn(0);
78834d6f44ffSToby Isaac }
78844d6f44ffSToby Isaac 
7885a6e0b375SMatthew G. Knepley /*@C
7886*bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
7887a6e0b375SMatthew G. Knepley 
7888a6e0b375SMatthew G. Knepley   Not collective
7889a6e0b375SMatthew G. Knepley 
7890a6e0b375SMatthew G. Knepley   Input Parameters:
7891*bb7acecfSBarry Smith + dm      - The `DM`
7892a6e0b375SMatthew G. Knepley . time    - The time
7893a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7894a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7895a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7896a6e0b375SMatthew G. Knepley 
7897a6e0b375SMatthew G. Knepley   Output Parameter:
7898a6e0b375SMatthew G. Knepley . localX - vector
7899a6e0b375SMatthew G. Knepley 
7900a6e0b375SMatthew G. Knepley    Calling sequence of func:
790177b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7902a6e0b375SMatthew G. Knepley 
7903a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7904a6e0b375SMatthew G. Knepley .  x   - The coordinates
790577b739a6SMatthew Knepley .  Nc  - The number of components
7906a6e0b375SMatthew G. Knepley .  u   - The output field values
7907a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7908a6e0b375SMatthew G. Knepley 
7909a6e0b375SMatthew G. Knepley   Level: developer
7910a6e0b375SMatthew G. Knepley 
7911*bb7acecfSBarry Smith   Developer Notes:
7912*bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7913*bb7acecfSBarry Smith 
7914*bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7915*bb7acecfSBarry Smith 
7916db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7917a6e0b375SMatthew G. Knepley @*/
79180709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
79194d6f44ffSToby Isaac {
79204d6f44ffSToby Isaac   PetscFunctionBegin;
79214d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7922064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
79237a8be351SBarry Smith   PetscCheck(dm->ops->projectfunctionlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
79249566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX));
79254d6f44ffSToby Isaac   PetscFunctionReturn(0);
79264d6f44ffSToby Isaac }
79274d6f44ffSToby Isaac 
7928a6e0b375SMatthew G. Knepley /*@C
7929*bb7acecfSBarry 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.
7930a6e0b375SMatthew G. Knepley 
7931*bb7acecfSBarry Smith   Collective on dm
7932a6e0b375SMatthew G. Knepley 
7933a6e0b375SMatthew G. Knepley   Input Parameters:
7934*bb7acecfSBarry Smith + dm      - The `DM`
7935a6e0b375SMatthew G. Knepley . time    - The time
7936*bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
7937a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7938*bb7acecfSBarry Smith . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
7939a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7940a6e0b375SMatthew G. Knepley 
7941a6e0b375SMatthew G. Knepley   Output Parameter:
7942a6e0b375SMatthew G. Knepley . X - vector
7943a6e0b375SMatthew G. Knepley 
7944a6e0b375SMatthew G. Knepley    Calling sequence of func:
794577b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7946a6e0b375SMatthew G. Knepley 
7947a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7948a6e0b375SMatthew G. Knepley .  x   - The coordinates
794977b739a6SMatthew Knepley .  Nc  - The number of components
7950a6e0b375SMatthew G. Knepley .  u   - The output field values
7951a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7952a6e0b375SMatthew G. Knepley 
7953a6e0b375SMatthew G. Knepley   Level: developer
7954a6e0b375SMatthew G. Knepley 
7955*bb7acecfSBarry Smith   Developer Notes:
7956*bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7957*bb7acecfSBarry Smith 
7958*bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7959*bb7acecfSBarry Smith 
7960db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
7961a6e0b375SMatthew G. Knepley @*/
79622c53366bSMatthew G. Knepley PetscErrorCode DMProjectFunctionLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
79632c53366bSMatthew G. Knepley {
79642c53366bSMatthew G. Knepley   Vec            localX;
79652c53366bSMatthew G. Knepley 
79662c53366bSMatthew G. Knepley   PetscFunctionBegin;
79672c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79689566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
79699566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
79709566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
79719566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
79729566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
79732c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
79742c53366bSMatthew G. Knepley }
79752c53366bSMatthew G. Knepley 
7976a6e0b375SMatthew G. Knepley /*@C
7977*bb7acecfSBarry 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.
7978a6e0b375SMatthew G. Knepley 
7979a6e0b375SMatthew G. Knepley   Not collective
7980a6e0b375SMatthew G. Knepley 
7981a6e0b375SMatthew G. Knepley   Input Parameters:
7982*bb7acecfSBarry Smith + dm      - The `DM`
7983a6e0b375SMatthew G. Knepley . time    - The time
7984*bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
7985a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7986a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7987a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7988a6e0b375SMatthew G. Knepley 
7989a6e0b375SMatthew G. Knepley   Output Parameter:
7990a6e0b375SMatthew G. Knepley . localX - vector
7991a6e0b375SMatthew G. Knepley 
7992a6e0b375SMatthew G. Knepley    Calling sequence of func:
799377b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7994a6e0b375SMatthew G. Knepley 
7995a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7996a6e0b375SMatthew G. Knepley .  x   - The coordinates
799777b739a6SMatthew Knepley .  Nc  - The number of components
7998a6e0b375SMatthew G. Knepley .  u   - The output field values
7999a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8000a6e0b375SMatthew G. Knepley 
8001a6e0b375SMatthew G. Knepley   Level: developer
8002a6e0b375SMatthew G. Knepley 
8003*bb7acecfSBarry Smith   Developer Notes:
8004*bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8005*bb7acecfSBarry Smith 
8006*bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8007*bb7acecfSBarry Smith 
8008db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8009a6e0b375SMatthew G. Knepley @*/
80101c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
80114d6f44ffSToby Isaac {
80124d6f44ffSToby Isaac   PetscFunctionBegin;
80134d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8014064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
80157a8be351SBarry Smith   PetscCheck(dm->ops->projectfunctionlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
80169566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
80174d6f44ffSToby Isaac   PetscFunctionReturn(0);
80184d6f44ffSToby Isaac }
80192716604bSToby Isaac 
8020a6e0b375SMatthew G. Knepley /*@C
8021*bb7acecfSBarry 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.
8022a6e0b375SMatthew G. Knepley 
8023a6e0b375SMatthew G. Knepley   Not collective
8024a6e0b375SMatthew G. Knepley 
8025a6e0b375SMatthew G. Knepley   Input Parameters:
8026*bb7acecfSBarry Smith + dm      - The `DM`
8027a6e0b375SMatthew G. Knepley . time    - The time
8028a6e0b375SMatthew G. Knepley . localU  - The input field vector
8029a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8030a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8031a6e0b375SMatthew G. Knepley 
8032a6e0b375SMatthew G. Knepley   Output Parameter:
8033a6e0b375SMatthew G. Knepley . localX  - The output vector
8034a6e0b375SMatthew G. Knepley 
8035a6e0b375SMatthew G. Knepley    Calling sequence of func:
8036a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8037a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8038a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8039a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8040a6e0b375SMatthew G. Knepley 
8041a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8042a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8043a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8044a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8045a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8046a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8047a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8048a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8049a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8050a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8051a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8052a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8053a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8054a6e0b375SMatthew G. Knepley .  t            - The current time
8055a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8056a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8057a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8058a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8059a6e0b375SMatthew G. Knepley 
8060*bb7acecfSBarry Smith   Note:
8061*bb7acecfSBarry 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.
8062*bb7acecfSBarry 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
8063*bb7acecfSBarry 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
8064a6e0b375SMatthew 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.
8065a6e0b375SMatthew G. Knepley 
8066a6e0b375SMatthew G. Knepley   Level: intermediate
8067a6e0b375SMatthew G. Knepley 
8068*bb7acecfSBarry Smith   Developer Notes:
8069*bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8070*bb7acecfSBarry Smith 
8071*bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8072*bb7acecfSBarry Smith 
8073db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8074a6e0b375SMatthew G. Knepley @*/
80758c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
80768c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
80778c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
80788c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8079191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
80808c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
80818c6c5593SMatthew G. Knepley {
80828c6c5593SMatthew G. Knepley   PetscFunctionBegin;
80838c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
80848c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
80858c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
80867a8be351SBarry Smith   PetscCheck(dm->ops->projectfieldlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
80879566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX));
80888c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
80898c6c5593SMatthew G. Knepley }
80908c6c5593SMatthew G. Knepley 
8091a6e0b375SMatthew G. Knepley /*@C
8092a6e0b375SMatthew 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.
8093a6e0b375SMatthew G. Knepley 
8094a6e0b375SMatthew G. Knepley   Not collective
8095a6e0b375SMatthew G. Knepley 
8096a6e0b375SMatthew G. Knepley   Input Parameters:
8097*bb7acecfSBarry Smith + dm      - The `DM`
8098a6e0b375SMatthew G. Knepley . time    - The time
8099*bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8100a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8101a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8102*bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8103a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8104a6e0b375SMatthew G. Knepley . localU  - The input field vector
8105a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8106a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8107a6e0b375SMatthew G. Knepley 
8108a6e0b375SMatthew G. Knepley   Output Parameter:
8109a6e0b375SMatthew G. Knepley . localX  - The output vector
8110a6e0b375SMatthew G. Knepley 
8111a6e0b375SMatthew G. Knepley    Calling sequence of func:
8112a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8113a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8114a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8115a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8116a6e0b375SMatthew G. Knepley 
8117a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8118a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8119a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8120a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8121a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8122a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8123a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8124a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8125a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8126a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8127a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8128a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8129a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8130a6e0b375SMatthew G. Knepley .  t            - The current time
8131a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8132a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8133a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8134a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8135a6e0b375SMatthew G. Knepley 
8136*bb7acecfSBarry Smith   Note:
8137*bb7acecfSBarry 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.
8138*bb7acecfSBarry 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
8139*bb7acecfSBarry 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
8140a6e0b375SMatthew 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.
8141a6e0b375SMatthew G. Knepley 
8142a6e0b375SMatthew G. Knepley   Level: intermediate
8143a6e0b375SMatthew G. Knepley 
8144*bb7acecfSBarry Smith   Developer Notes:
8145*bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8146*bb7acecfSBarry Smith 
8147*bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8148*bb7acecfSBarry Smith 
8149d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8150a6e0b375SMatthew G. Knepley @*/
81511c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
81528c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
81538c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
81548c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8155191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
81568c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
81578c6c5593SMatthew G. Knepley {
81588c6c5593SMatthew G. Knepley   PetscFunctionBegin;
81598c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8160064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
8161064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
81627a8be351SBarry Smith   PetscCheck(dm->ops->projectfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
81639566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
81648c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
81658c6c5593SMatthew G. Knepley }
81668c6c5593SMatthew G. Knepley 
81672716604bSToby Isaac /*@C
8168d29d7c6eSMatthew 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.
8169d29d7c6eSMatthew G. Knepley 
8170d29d7c6eSMatthew G. Knepley   Not collective
8171d29d7c6eSMatthew G. Knepley 
8172d29d7c6eSMatthew G. Knepley   Input Parameters:
8173*bb7acecfSBarry Smith + dm      - The `DM`
8174d29d7c6eSMatthew G. Knepley . time    - The time
8175*bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8176d29d7c6eSMatthew G. Knepley . numIds  - The number of label ids to use
8177d29d7c6eSMatthew G. Knepley . ids     - The label ids to use for marking
8178*bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8179d29d7c6eSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8180d29d7c6eSMatthew G. Knepley . U       - The input field vector
8181d29d7c6eSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8182d29d7c6eSMatthew G. Knepley - mode    - The insertion mode for values
8183d29d7c6eSMatthew G. Knepley 
8184d29d7c6eSMatthew G. Knepley   Output Parameter:
8185d29d7c6eSMatthew G. Knepley . X       - The output vector
8186d29d7c6eSMatthew G. Knepley 
8187d29d7c6eSMatthew G. Knepley    Calling sequence of func:
8188d29d7c6eSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8189d29d7c6eSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8190d29d7c6eSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8191d29d7c6eSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8192d29d7c6eSMatthew G. Knepley 
8193d29d7c6eSMatthew G. Knepley +  dim          - The spatial dimension
8194d29d7c6eSMatthew G. Knepley .  Nf           - The number of input fields
8195d29d7c6eSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8196d29d7c6eSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8197d29d7c6eSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8198d29d7c6eSMatthew G. Knepley .  u            - The field values at this point in space
8199d29d7c6eSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8200d29d7c6eSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8201d29d7c6eSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8202d29d7c6eSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8203d29d7c6eSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8204d29d7c6eSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8205d29d7c6eSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8206d29d7c6eSMatthew G. Knepley .  t            - The current time
8207d29d7c6eSMatthew G. Knepley .  x            - The coordinates of this point
8208d29d7c6eSMatthew G. Knepley .  numConstants - The number of constants
8209d29d7c6eSMatthew G. Knepley .  constants    - The value of each constant
8210d29d7c6eSMatthew G. Knepley -  f            - The value of the function at this point in space
8211d29d7c6eSMatthew G. Knepley 
8212*bb7acecfSBarry Smith   Note:
8213*bb7acecfSBarry 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.
8214*bb7acecfSBarry 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
8215*bb7acecfSBarry 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
8216d29d7c6eSMatthew 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.
8217d29d7c6eSMatthew G. Knepley 
8218d29d7c6eSMatthew G. Knepley   Level: intermediate
8219d29d7c6eSMatthew G. Knepley 
8220*bb7acecfSBarry Smith   Developer Notes:
8221*bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8222*bb7acecfSBarry Smith 
8223*bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8224*bb7acecfSBarry Smith 
8225d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8226d29d7c6eSMatthew G. Knepley @*/
8227d29d7c6eSMatthew G. Knepley PetscErrorCode DMProjectFieldLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec U,
8228d29d7c6eSMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
8229d29d7c6eSMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8230d29d7c6eSMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8231d29d7c6eSMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
8232d29d7c6eSMatthew G. Knepley                                    InsertMode mode, Vec X)
8233d29d7c6eSMatthew G. Knepley {
8234d29d7c6eSMatthew G. Knepley   DM  dmIn;
8235d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8236d29d7c6eSMatthew G. Knepley 
8237d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8238d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8239d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8240d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8241d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8242d29d7c6eSMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dm, U, mode, localU));
8243d29d7c6eSMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dm, U, mode, localU));
8244d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8245d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8246d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8247d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8248d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
8249d29d7c6eSMatthew G. Knepley   PetscFunctionReturn(0);
8250d29d7c6eSMatthew G. Knepley }
8251d29d7c6eSMatthew G. Knepley 
8252d29d7c6eSMatthew G. Knepley /*@C
8253ece3a9fcSMatthew 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.
8254ece3a9fcSMatthew G. Knepley 
8255ece3a9fcSMatthew G. Knepley   Not collective
8256ece3a9fcSMatthew G. Knepley 
8257ece3a9fcSMatthew G. Knepley   Input Parameters:
8258*bb7acecfSBarry Smith + dm      - The `DM`
8259ece3a9fcSMatthew G. Knepley . time    - The time
8260*bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain boundary to output
8261ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8262ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8263*bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8264ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8265ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8266ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8267ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8268ece3a9fcSMatthew G. Knepley 
8269ece3a9fcSMatthew G. Knepley   Output Parameter:
8270ece3a9fcSMatthew G. Knepley . localX  - The output vector
8271ece3a9fcSMatthew G. Knepley 
8272ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8273ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8274ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8275ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8276ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8277ece3a9fcSMatthew G. Knepley 
8278ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8279ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8280ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8281ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8282ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8283ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8284ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8285ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8286ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8287ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8288ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8289ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8290ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8291ece3a9fcSMatthew G. Knepley .  t            - The current time
8292ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8293ece3a9fcSMatthew G. Knepley .  n            - The face normal
8294ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8295ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8296ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8297ece3a9fcSMatthew G. Knepley 
8298ece3a9fcSMatthew G. Knepley   Note:
8299*bb7acecfSBarry 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.
8300*bb7acecfSBarry 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
8301*bb7acecfSBarry 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
8302ece3a9fcSMatthew 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.
8303ece3a9fcSMatthew G. Knepley 
8304ece3a9fcSMatthew G. Knepley   Level: intermediate
8305ece3a9fcSMatthew G. Knepley 
8306*bb7acecfSBarry Smith   Developer Notes:
8307*bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8308*bb7acecfSBarry Smith 
8309*bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8310*bb7acecfSBarry Smith 
8311db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8312ece3a9fcSMatthew G. Knepley @*/
8313ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
8314ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
8315ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8316ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8317ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
8318ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
8319ece3a9fcSMatthew G. Knepley {
8320ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8321ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8322064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
8323064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
83247a8be351SBarry Smith   PetscCheck(dm->ops->projectbdfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
83259566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8326ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8327ece3a9fcSMatthew G. Knepley }
8328ece3a9fcSMatthew G. Knepley 
8329ece3a9fcSMatthew G. Knepley /*@C
83302716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
83312716604bSToby Isaac 
8332*bb7acecfSBarry Smith   Collective on dm
8333*bb7acecfSBarry Smith 
83342716604bSToby Isaac   Input Parameters:
8335*bb7acecfSBarry Smith + dm    - The `DM`
83360709b2feSToby Isaac . time  - The time
83372716604bSToby Isaac . funcs - The functions to evaluate for each field component
83382716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8339574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
83402716604bSToby Isaac 
83412716604bSToby Isaac   Output Parameter:
83422716604bSToby Isaac . diff - The diff ||u - u_h||_2
83432716604bSToby Isaac 
83442716604bSToby Isaac   Level: developer
83452716604bSToby Isaac 
8346*bb7acecfSBarry Smith   Developer Notes:
8347*bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8348*bb7acecfSBarry Smith 
8349*bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8350*bb7acecfSBarry Smith 
8351db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
83522716604bSToby Isaac @*/
83530709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
83542716604bSToby Isaac {
83552716604bSToby Isaac   PetscFunctionBegin;
83562716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8357b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
83587a8be351SBarry Smith   PetscCheck(dm->ops->computel2diff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
83599566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff));
83602716604bSToby Isaac   PetscFunctionReturn(0);
83612716604bSToby Isaac }
8362b698f381SToby Isaac 
8363b698f381SToby Isaac /*@C
8364b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8365b698f381SToby Isaac 
8366d083f849SBarry Smith   Collective on dm
8367d083f849SBarry Smith 
8368b698f381SToby Isaac   Input Parameters:
8369*bb7acecfSBarry Smith + dm    - The `DM`
8370b698f381SToby Isaac , time  - The time
8371b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8372b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8373574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8374b698f381SToby Isaac - n     - The vector to project along
8375b698f381SToby Isaac 
8376b698f381SToby Isaac   Output Parameter:
8377b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8378b698f381SToby Isaac 
8379b698f381SToby Isaac   Level: developer
8380b698f381SToby Isaac 
8381*bb7acecfSBarry Smith   Developer Notes:
8382*bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8383*bb7acecfSBarry Smith 
8384*bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8385*bb7acecfSBarry Smith 
8386*bb7acecfSBarry Smith .seealso: `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8387b698f381SToby Isaac @*/
8388b698f381SToby Isaac 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)
8389b698f381SToby Isaac {
8390b698f381SToby Isaac   PetscFunctionBegin;
8391b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8392b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
83937a8be351SBarry Smith   PetscCheck(dm->ops->computel2gradientdiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
83949566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff));
8395b698f381SToby Isaac   PetscFunctionReturn(0);
8396b698f381SToby Isaac }
8397b698f381SToby Isaac 
83982a16baeaSToby Isaac /*@C
83992a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
84002a16baeaSToby Isaac 
8401d083f849SBarry Smith   Collective on dm
8402d083f849SBarry Smith 
84032a16baeaSToby Isaac   Input Parameters:
8404*bb7acecfSBarry Smith + dm    - The `DM`
84052a16baeaSToby Isaac . time  - The time
84062a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
84072a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8408574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84092a16baeaSToby Isaac 
84102a16baeaSToby Isaac   Output Parameter:
84112a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
84122a16baeaSToby Isaac 
84132a16baeaSToby Isaac   Level: developer
84142a16baeaSToby Isaac 
8415*bb7acecfSBarry Smith   Developer Notes:
8416*bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8417*bb7acecfSBarry Smith 
8418*bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8419*bb7acecfSBarry Smith 
8420db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
84212a16baeaSToby Isaac @*/
84221189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
84232a16baeaSToby Isaac {
84242a16baeaSToby Isaac   PetscFunctionBegin;
84252a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84262a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
84277a8be351SBarry Smith   PetscCheck(dm->ops->computel2fielddiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
84289566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff));
84292a16baeaSToby Isaac   PetscFunctionReturn(0);
84302a16baeaSToby Isaac }
84312a16baeaSToby Isaac 
8432df0b854cSToby Isaac /*@C
8433*bb7acecfSBarry Smith  DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8434502a2867SDave May 
8435502a2867SDave May  Not Collective
8436502a2867SDave May 
8437502a2867SDave May  Input Parameter:
8438*bb7acecfSBarry Smith .  dm    - The `DM`
8439502a2867SDave May 
84400a19bb7dSprj-  Output Parameters:
84410a19bb7dSprj- +  nranks - the number of neighbours
84420a19bb7dSprj- -  ranks - the neighbors ranks
8443502a2867SDave May 
8444*bb7acecfSBarry Smith  Note:
8445*bb7acecfSBarry Smith  Do not free the array, it is freed when the `DM` is destroyed.
8446502a2867SDave May 
8447502a2867SDave May  Level: beginner
8448502a2867SDave May 
8449db781477SPatrick Sanan  .seealso: `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8450502a2867SDave May @*/
8451502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8452502a2867SDave May {
8453502a2867SDave May   PetscFunctionBegin;
8454502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84557a8be351SBarry Smith   PetscCheck(dm->ops->getneighbors,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
84569566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm,nranks,ranks));
8457502a2867SDave May   PetscFunctionReturn(0);
8458502a2867SDave May }
8459502a2867SDave May 
8460531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8461531c7667SBarry Smith 
8462531c7667SBarry Smith /*
8463531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8464531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8465531c7667SBarry Smith */
8466531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8467531c7667SBarry Smith {
8468531c7667SBarry Smith   PetscFunctionBegin;
8469531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8470531c7667SBarry Smith     Vec x1local;
8471531c7667SBarry Smith     DM  dm;
84729566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J,&dm));
84737a8be351SBarry Smith     PetscCheck(dm,PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
84749566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm,&x1local));
84759566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local));
84769566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local));
8477531c7667SBarry Smith     x1   = x1local;
8478531c7667SBarry Smith   }
84799566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J,coloring,x1,sctx));
8480531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8481531c7667SBarry Smith     DM  dm;
84829566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J,&dm));
84839566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm,&x1));
8484531c7667SBarry Smith   }
8485531c7667SBarry Smith   PetscFunctionReturn(0);
8486531c7667SBarry Smith }
8487531c7667SBarry Smith 
8488531c7667SBarry Smith /*@
8489*bb7acecfSBarry Smith     MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8490531c7667SBarry Smith 
8491531c7667SBarry Smith     Input Parameter:
8492*bb7acecfSBarry Smith .    coloring - the `MatFDColoring` object
8493531c7667SBarry Smith 
8494*bb7acecfSBarry Smith     Developer Note:
8495*bb7acecfSBarry Smith     this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8496531c7667SBarry Smith 
84971b266c99SBarry Smith     Level: advanced
84981b266c99SBarry Smith 
8499db781477SPatrick Sanan .seealso: `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8500531c7667SBarry Smith @*/
8501531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8502531c7667SBarry Smith {
8503531c7667SBarry Smith   PetscFunctionBegin;
8504531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8505531c7667SBarry Smith   PetscFunctionReturn(0);
8506531c7667SBarry Smith }
85078320bc6fSPatrick Sanan 
85088320bc6fSPatrick Sanan /*@
8509*bb7acecfSBarry Smith     DMGetCompatibility - determine if two `DM`s are compatible
85108320bc6fSPatrick Sanan 
85118320bc6fSPatrick Sanan     Collective
85128320bc6fSPatrick Sanan 
85138320bc6fSPatrick Sanan     Input Parameters:
8514*bb7acecfSBarry Smith +    dm1 - the first `DM`
8515*bb7acecfSBarry Smith -    dm2 - the second `DM`
85168320bc6fSPatrick Sanan 
85178320bc6fSPatrick Sanan     Output Parameters:
8518*bb7acecfSBarry Smith +    compatible - whether or not the two `DM`s are compatible
8519*bb7acecfSBarry Smith -    set - whether or not the compatible value was actually determined and set
85208320bc6fSPatrick Sanan 
85218320bc6fSPatrick Sanan     Notes:
8522*bb7acecfSBarry Smith     Two `DM`s are deemed compatible if they represent the same parallel decomposition
85233d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
85248320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
8525*bb7acecfSBarry Smith     Loosely speaking, compatible `DM`s represent the same domain and parallel
85263d862458SPatrick Sanan     decomposition, but hold different data.
85278320bc6fSPatrick Sanan 
85288320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
8529*bb7acecfSBarry Smith     over a pair of vectors obtained from different `DM`s.
85308320bc6fSPatrick Sanan 
8531*bb7acecfSBarry Smith     For example, two `DMDA` objects are compatible if they have the same local
85328320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
85338320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
8534*bb7acecfSBarry Smith     either `DM` in bounds for a loop over vectors derived from either `DM`.
85358320bc6fSPatrick Sanan 
8536*bb7acecfSBarry Smith     Consider the operation of summing data living on a 2-dof `DMDA` to data living
8537*bb7acecfSBarry Smith     on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
85388320bc6fSPatrick Sanan .vb
85398320bc6fSPatrick Sanan   ...
85409566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
85418320bc6fSPatrick Sanan   if (set && compatible)  {
85429566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
85439566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
85449566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
85458320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
85468320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
85478320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
85488320bc6fSPatrick Sanan       }
85498320bc6fSPatrick Sanan     }
85509566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
85519566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
85528320bc6fSPatrick Sanan   } else {
85538320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
85548320bc6fSPatrick Sanan   }
85558320bc6fSPatrick Sanan   ...
85568320bc6fSPatrick Sanan .ve
85578320bc6fSPatrick Sanan 
8558*bb7acecfSBarry Smith     Checking compatibility might be expensive for a given implementation of `DM`,
85598320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
85608320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
85618320bc6fSPatrick Sanan     always check the "set" output parameter.
85628320bc6fSPatrick Sanan 
8563*bb7acecfSBarry Smith     A `DM` is always compatible with itself.
85648320bc6fSPatrick Sanan 
8565*bb7acecfSBarry Smith     In the current implementation, `DM`s which live on "unequal" communicators
85668320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
85678320bc6fSPatrick Sanan     incompatible.
85688320bc6fSPatrick Sanan 
85698320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
8570*bb7acecfSBarry Smith     is required on each rank. However, in `DM` implementations which store all this
85718320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
85728320bc6fSPatrick Sanan 
8573*bb7acecfSBarry Smith     Developer Note:
8574*bb7acecfSBarry Smith     Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
85753d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8576a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
8577*bb7acecfSBarry Smith     compatibility. It is left to `DM` implementers to ensure that symmetry is
85788320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
85793d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
8580*bb7acecfSBarry Smith     of other `DM` types and let *set = PETSC_FALSE if found.
85818320bc6fSPatrick Sanan 
85828320bc6fSPatrick Sanan     Level: advanced
85838320bc6fSPatrick Sanan 
8584db781477SPatrick Sanan .seealso: `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
85858320bc6fSPatrick Sanan @*/
8586a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
85878320bc6fSPatrick Sanan {
85888320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
85898320bc6fSPatrick Sanan   DMType         type,type2;
85908320bc6fSPatrick Sanan   PetscBool      sameType;
85918320bc6fSPatrick Sanan 
85928320bc6fSPatrick Sanan   PetscFunctionBegin;
8593a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
85948320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
85958320bc6fSPatrick Sanan 
85968320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8597a5bc1bf3SBarry Smith   if (dm1 == dm2) {
85988320bc6fSPatrick Sanan     *set = PETSC_TRUE;
85998320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
86008320bc6fSPatrick Sanan     PetscFunctionReturn(0);
86018320bc6fSPatrick Sanan   }
86028320bc6fSPatrick Sanan 
86038320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
86048320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
86058320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
86068320bc6fSPatrick Sanan      determined by the implementation-specific logic */
86079566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult));
86088320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
86098320bc6fSPatrick Sanan     *set = PETSC_TRUE;
86108320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
86118320bc6fSPatrick Sanan     PetscFunctionReturn(0);
86128320bc6fSPatrick Sanan   }
86138320bc6fSPatrick Sanan 
86148320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8615a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
86169566063dSJacob Faibussowitsch     PetscCall((*dm1->ops->getcompatibility)(dm1,dm2,compatible,set));
8617b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
86188320bc6fSPatrick Sanan   }
86198320bc6fSPatrick Sanan 
8620a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
86218320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
86229566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1,&type));
86239566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2,&type2));
86249566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type,type2,&sameType));
86258320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
86269566063dSJacob Faibussowitsch     PetscCall((*dm2->ops->getcompatibility)(dm2,dm1,compatible,set)); /* Note argument order */
86278320bc6fSPatrick Sanan   } else {
86288320bc6fSPatrick Sanan     *set = PETSC_FALSE;
86298320bc6fSPatrick Sanan   }
86308320bc6fSPatrick Sanan   PetscFunctionReturn(0);
86318320bc6fSPatrick Sanan }
8632c0f0dcc3SMatthew G. Knepley 
8633c0f0dcc3SMatthew G. Knepley /*@C
8634*bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8635c0f0dcc3SMatthew G. Knepley 
8636*bb7acecfSBarry Smith   Logically Collective on dm
8637c0f0dcc3SMatthew G. Knepley 
8638c0f0dcc3SMatthew G. Knepley   Input Parameters:
8639*bb7acecfSBarry Smith + DM - the `DM`
8640c0f0dcc3SMatthew G. Knepley . f - the monitor function
8641c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8642c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8643c0f0dcc3SMatthew G. Knepley 
8644c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8645*bb7acecfSBarry Smith - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8646c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8647c0f0dcc3SMatthew G. Knepley 
8648*bb7acecfSBarry Smith   Note:
8649c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8650*bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8651c0f0dcc3SMatthew G. Knepley   order in which they were set.
8652c0f0dcc3SMatthew G. Knepley 
8653*bb7acecfSBarry Smith   Fortran Note:
8654*bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8655*bb7acecfSBarry Smith 
8656*bb7acecfSBarry Smith   Developer Note:
8657*bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8658c0f0dcc3SMatthew G. Knepley 
8659c0f0dcc3SMatthew G. Knepley   Level: intermediate
8660c0f0dcc3SMatthew G. Knepley 
8661*bb7acecfSBarry Smith .seealso: `DMMonitorCancel()`,`DMMonitorSetFromOptions()`, `DMMonitor()`
8662c0f0dcc3SMatthew G. Knepley @*/
8663c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
8664c0f0dcc3SMatthew G. Knepley {
8665c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8666c0f0dcc3SMatthew G. Knepley 
8667c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8668c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8669c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8670c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8671c0f0dcc3SMatthew G. Knepley 
86729566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
8673c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8674c0f0dcc3SMatthew G. Knepley   }
86757a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8676c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8677c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8678c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
8679c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8680c0f0dcc3SMatthew G. Knepley }
8681c0f0dcc3SMatthew G. Knepley 
8682c0f0dcc3SMatthew G. Knepley /*@
8683*bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8684c0f0dcc3SMatthew G. Knepley 
8685*bb7acecfSBarry Smith   Logically Collective on dm
8686c0f0dcc3SMatthew G. Knepley 
8687c0f0dcc3SMatthew G. Knepley   Input Parameter:
8688c0f0dcc3SMatthew G. Knepley . dm - the DM
8689c0f0dcc3SMatthew G. Knepley 
8690c0f0dcc3SMatthew G. Knepley   Options Database Key:
8691c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8692*bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8693c0f0dcc3SMatthew G. Knepley   set via the options database
8694c0f0dcc3SMatthew G. Knepley 
8695*bb7acecfSBarry Smith   Note:
8696*bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8697c0f0dcc3SMatthew G. Knepley 
8698c0f0dcc3SMatthew G. Knepley   Level: intermediate
8699c0f0dcc3SMatthew G. Knepley 
8700*bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`,  `DMMonitor()`
8701c0f0dcc3SMatthew G. Knepley @*/
8702c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
8703c0f0dcc3SMatthew G. Knepley {
8704c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8705c0f0dcc3SMatthew G. Knepley 
8706c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8707c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8708c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
87099566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8710c0f0dcc3SMatthew G. Knepley   }
8711c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8712c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8713c0f0dcc3SMatthew G. Knepley }
8714c0f0dcc3SMatthew G. Knepley 
8715c0f0dcc3SMatthew G. Knepley /*@C
8716c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8717c0f0dcc3SMatthew G. Knepley 
8718*bb7acecfSBarry Smith   Collective on dm
8719c0f0dcc3SMatthew G. Knepley 
8720c0f0dcc3SMatthew G. Knepley   Input Parameters:
8721*bb7acecfSBarry Smith + dm   - `DM` object you wish to monitor
8722c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8723c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8724c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8725c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8726*bb7acecfSBarry 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
8727c0f0dcc3SMatthew G. Knepley 
8728c0f0dcc3SMatthew G. Knepley   Output Parameter:
8729c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8730c0f0dcc3SMatthew G. Knepley 
8731c0f0dcc3SMatthew G. Knepley   Level: developer
8732c0f0dcc3SMatthew G. Knepley 
8733db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8734db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
8735db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
8736db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8737c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8738db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8739*bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8740c0f0dcc3SMatthew G. Knepley @*/
8741c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8742c0f0dcc3SMatthew G. Knepley {
8743c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8744c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8745c0f0dcc3SMatthew G. Knepley 
8746c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8747c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87489566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg));
8749c0f0dcc3SMatthew G. Knepley   if (*flg) {
8750c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8751c0f0dcc3SMatthew G. Knepley 
87529566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
87539566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject) viewer));
87549566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
87559566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy));
8756c0f0dcc3SMatthew G. Knepley   }
8757c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8758c0f0dcc3SMatthew G. Knepley }
8759c0f0dcc3SMatthew G. Knepley 
8760c0f0dcc3SMatthew G. Knepley /*@
8761c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8762c0f0dcc3SMatthew G. Knepley 
8763*bb7acecfSBarry Smith    Collective on dm
8764c0f0dcc3SMatthew G. Knepley 
8765c0f0dcc3SMatthew G. Knepley    Input Parameters:
8766*bb7acecfSBarry Smith .  dm - The `DM`
8767c0f0dcc3SMatthew G. Knepley 
8768c0f0dcc3SMatthew G. Knepley    Level: developer
8769c0f0dcc3SMatthew G. Knepley 
8770*bb7acecfSBarry Smith    Question:
8771*bb7acecfSBarry Smith    Note should indicate when during the life of the `DM` the monitor is run. It appears to be related to the discretization process seems rather specialized
8772*bb7acecfSBarry Smith    since some `DM` have no concept of discretization
8773*bb7acecfSBarry Smith 
8774*bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8775c0f0dcc3SMatthew G. Knepley @*/
8776c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
8777c0f0dcc3SMatthew G. Knepley {
8778c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8779c0f0dcc3SMatthew G. Knepley 
8780c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8781c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8782c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8783c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
87849566063dSJacob Faibussowitsch     PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
8785c0f0dcc3SMatthew G. Knepley   }
8786c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8787c0f0dcc3SMatthew G. Knepley }
87882e4af2aeSMatthew G. Knepley 
87892e4af2aeSMatthew G. Knepley /*@
8790*bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
87912e4af2aeSMatthew G. Knepley 
8792*bb7acecfSBarry Smith   Collective on dm
87932e4af2aeSMatthew G. Knepley 
87942e4af2aeSMatthew G. Knepley   Input Parameters:
8795*bb7acecfSBarry Smith + dm     - The `DM`
87966b867d5aSJose E. Roman - sol    - The solution vector
87972e4af2aeSMatthew G. Knepley 
87986b867d5aSJose E. Roman   Input/Output Parameter:
87996b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
88006b867d5aSJose E. Roman            contains the error in each field
88016b867d5aSJose E. Roman 
88026b867d5aSJose E. Roman   Output Parameter:
88036b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
88042e4af2aeSMatthew G. Knepley 
8805*bb7acecfSBarry Smith   Note:
8806*bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
88072e4af2aeSMatthew G. Knepley 
88082e4af2aeSMatthew G. Knepley   Level: developer
88092e4af2aeSMatthew G. Knepley 
8810db781477SPatrick Sanan .seealso: `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
88112e4af2aeSMatthew G. Knepley @*/
88122e4af2aeSMatthew G. Knepley PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
88132e4af2aeSMatthew G. Knepley {
88142e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
88152e4af2aeSMatthew G. Knepley   void            **ctxs;
88162e4af2aeSMatthew G. Knepley   PetscReal         time;
88172e4af2aeSMatthew G. Knepley   PetscInt          Nf, f, Nds, s;
88182e4af2aeSMatthew G. Knepley 
88192e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
88209566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
88219566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
88229566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
88232e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
88242e4af2aeSMatthew G. Knepley     PetscDS         ds;
88252e4af2aeSMatthew G. Knepley     DMLabel         label;
88262e4af2aeSMatthew G. Knepley     IS              fieldIS;
88272e4af2aeSMatthew G. Knepley     const PetscInt *fields;
88282e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
88292e4af2aeSMatthew G. Knepley 
88309566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
88319566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
88329566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
88332e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
88342e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
88359566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
88362e4af2aeSMatthew G. Knepley     }
88379566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
88382e4af2aeSMatthew G. Knepley   }
88392e4af2aeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
884063a3b9bcSJacob Faibussowitsch     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);
88412e4af2aeSMatthew G. Knepley   }
88429566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
88439566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
88442e4af2aeSMatthew G. Knepley   if (errorVec) {
88452e4af2aeSMatthew G. Knepley     DM             edm;
88462e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
88472e4af2aeSMatthew G. Knepley     PetscBool      simplex;
88482e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
88492e4af2aeSMatthew G. Knepley 
88509566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
88519566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
88529566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
88539566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
88542e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
88559566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
88562e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
88572e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
88582e4af2aeSMatthew G. Knepley       PetscQuadrature q;
88592e4af2aeSMatthew G. Knepley       const char     *name;
88602e4af2aeSMatthew G. Knepley 
88619566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *) &fe));
88629566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
88639566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject) fe, &name));
88649566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject) efe, name));
88659566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
88669566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
88679566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject) efe));
88689566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
88692e4af2aeSMatthew G. Knepley     }
88709566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
88712e4af2aeSMatthew G. Knepley 
88729566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
88739566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) *errorVec, "Error"));
88749566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
88759566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
88762e4af2aeSMatthew G. Knepley   }
88779566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
88782e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
88792e4af2aeSMatthew G. Knepley }
88809a2a23afSMatthew G. Knepley 
88819a2a23afSMatthew G. Knepley /*@
8882*bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
88839a2a23afSMatthew G. Knepley 
88849a2a23afSMatthew G. Knepley   Not collective
88859a2a23afSMatthew G. Knepley 
88869a2a23afSMatthew G. Knepley   Input Parameter:
8887*bb7acecfSBarry Smith . dm     - The `DM`
88889a2a23afSMatthew G. Knepley 
88899a2a23afSMatthew G. Knepley   Output Parameter:
8890a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
88919a2a23afSMatthew G. Knepley 
88929a2a23afSMatthew G. Knepley   Level: advanced
88939a2a23afSMatthew G. Knepley 
8894*bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
88959a2a23afSMatthew G. Knepley @*/
88969a2a23afSMatthew G. Knepley PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
88979a2a23afSMatthew G. Knepley {
88989a2a23afSMatthew G. Knepley   PetscFunctionBegin;
88999a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89009566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
89019a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
89029a2a23afSMatthew G. Knepley }
89039a2a23afSMatthew G. Knepley 
89049a2a23afSMatthew G. Knepley /*@
8905ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
89069a2a23afSMatthew G. Knepley 
89079a2a23afSMatthew G. Knepley   Not collective
89089a2a23afSMatthew G. Knepley 
89099a2a23afSMatthew G. Knepley   Input Parameters:
8910*bb7acecfSBarry Smith + dm     - The `DM`
8911*bb7acecfSBarry Smith . label  - The `DMLabel`
8912ac17215fSMatthew G. Knepley . value  - The label value indicating the region
8913ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
89149a2a23afSMatthew G. Knepley 
89159a2a23afSMatthew G. Knepley   Output Parameter:
8916*bb7acecfSBarry Smith . aux    - The `Vec` holding auxiliary field data
89179a2a23afSMatthew G. Knepley 
8918*bb7acecfSBarry Smith   Note:
8919*bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
892004c51a94SMatthew G. Knepley 
89219a2a23afSMatthew G. Knepley   Level: advanced
89229a2a23afSMatthew G. Knepley 
8923*bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
89249a2a23afSMatthew G. Knepley @*/
8925ac17215fSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
89269a2a23afSMatthew G. Knepley {
8927ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
892804c51a94SMatthew G. Knepley   PetscBool       has;
89299a2a23afSMatthew G. Knepley 
89309a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89319a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89329a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
89339a2a23afSMatthew G. Knepley   key.label = label;
89349a2a23afSMatthew G. Knepley   key.value = value;
8935ac17215fSMatthew G. Knepley   key.part  = part;
89369566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
89379566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key,  aux));
89389566063dSJacob Faibussowitsch   else     PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
89399a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
89409a2a23afSMatthew G. Knepley }
89419a2a23afSMatthew G. Knepley 
89429a2a23afSMatthew G. Knepley /*@
8943*bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
89449a2a23afSMatthew G. Knepley 
8945*bb7acecfSBarry Smith   Not collective because auxilary vectors are not parallel
89469a2a23afSMatthew G. Knepley 
89479a2a23afSMatthew G. Knepley   Input Parameters:
8948*bb7acecfSBarry Smith + dm     - The `DM`
8949*bb7acecfSBarry Smith . label  - The `DMLabel`
89509a2a23afSMatthew G. Knepley . value  - The label value indicating the region
8951ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
8952*bb7acecfSBarry Smith - aux    - The `Vec` holding auxiliary field data
89539a2a23afSMatthew G. Knepley 
89549a2a23afSMatthew G. Knepley   Level: advanced
89559a2a23afSMatthew G. Knepley 
8956*bb7acecfSBarry Smith .seealso: `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
89579a2a23afSMatthew G. Knepley @*/
8958ac17215fSMatthew G. Knepley PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
89599a2a23afSMatthew G. Knepley {
89609a2a23afSMatthew G. Knepley   Vec             old;
89619a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
89629a2a23afSMatthew G. Knepley 
89639a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89649a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89659a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
89669a2a23afSMatthew G. Knepley   key.label = label;
89679a2a23afSMatthew G. Knepley   key.value = value;
8968ac17215fSMatthew G. Knepley   key.part  = part;
89699566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
89709566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) aux));
89719566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject) old));
89729566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
89739566063dSJacob Faibussowitsch   else      PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
89749a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
89759a2a23afSMatthew G. Knepley }
89769a2a23afSMatthew G. Knepley 
89779a2a23afSMatthew G. Knepley /*@C
8978*bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
89799a2a23afSMatthew G. Knepley 
89809a2a23afSMatthew G. Knepley   Not collective
89819a2a23afSMatthew G. Knepley 
89829a2a23afSMatthew G. Knepley   Input Parameter:
8983*bb7acecfSBarry Smith . dm      - The `DM`
89849a2a23afSMatthew G. Knepley 
89859a2a23afSMatthew G. Knepley   Output Parameters:
8986*bb7acecfSBarry Smith + labels  - The `DMLabel`s for each `Vec`
8987*bb7acecfSBarry Smith . values  - The label values for each `Vec`
8988*bb7acecfSBarry Smith - parts   - The equation parts for each `Vec`
89899a2a23afSMatthew G. Knepley 
8990*bb7acecfSBarry Smith   Note:
8991*bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
89929a2a23afSMatthew G. Knepley 
89939a2a23afSMatthew G. Knepley   Level: advanced
89949a2a23afSMatthew G. Knepley 
8995*bb7acecfSBarry Smith .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMSetAuxiliaryVec()`, DMCopyAuxiliaryVec()`
89969a2a23afSMatthew G. Knepley @*/
8997ac17215fSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
89989a2a23afSMatthew G. Knepley {
89999a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
90009a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
90019a2a23afSMatthew G. Knepley 
90029a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90039a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90049a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
9005dadcf809SJacob Faibussowitsch   PetscValidIntPointer(values, 3);
9006dadcf809SJacob Faibussowitsch   PetscValidIntPointer(parts,  4);
90079566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
90089566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
90099566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
9010ac17215fSMatthew G. Knepley   for (i = 0; i < n; ++i) {labels[i] = keys[i].label; values[i] = keys[i].value; parts[i] = keys[i].part;}
90119566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
90129a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
90139a2a23afSMatthew G. Knepley }
90149a2a23afSMatthew G. Knepley 
90159a2a23afSMatthew G. Knepley /*@
9016*bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
90179a2a23afSMatthew G. Knepley 
90189a2a23afSMatthew G. Knepley   Not collective
90199a2a23afSMatthew G. Knepley 
90209a2a23afSMatthew G. Knepley   Input Parameter:
9021*bb7acecfSBarry Smith . dm    - The `DM`
90229a2a23afSMatthew G. Knepley 
90239a2a23afSMatthew G. Knepley   Output Parameter:
9024*bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
90259a2a23afSMatthew G. Knepley 
90269a2a23afSMatthew G. Knepley   Level: advanced
90279a2a23afSMatthew G. Knepley 
9028*bb7acecfSBarry Smith   Note:
9029*bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9030*bb7acecfSBarry Smith 
9031db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
90329a2a23afSMatthew G. Knepley @*/
90339a2a23afSMatthew G. Knepley PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
90349a2a23afSMatthew G. Knepley {
90359a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90369a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90379566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
90389566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
90399a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
90409a2a23afSMatthew G. Knepley }
9041b5a892a1SMatthew G. Knepley 
9042b5a892a1SMatthew G. Knepley /*@C
9043*bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9044b5a892a1SMatthew G. Knepley 
9045b5a892a1SMatthew G. Knepley   Not collective
9046b5a892a1SMatthew G. Knepley 
9047b5a892a1SMatthew G. Knepley   Input Parameters:
9048*bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9049b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9050b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9051b5a892a1SMatthew G. Knepley 
9052b5a892a1SMatthew G. Knepley   Output Parameters:
9053*bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9054b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9055b5a892a1SMatthew G. Knepley 
9056b5a892a1SMatthew G. Knepley   Level: advanced
9057b5a892a1SMatthew G. Knepley 
9058*bb7acecfSBarry Smith   Note:
9059*bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9060*bb7acecfSBarry Smith 
9061*bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9062*bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9063*bb7acecfSBarry Smith 
9064*bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9065*bb7acecfSBarry Smith 
9066*bb7acecfSBarry Smith .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9067b5a892a1SMatthew G. Knepley @*/
9068b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9069b5a892a1SMatthew G. Knepley {
9070b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9071b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
9072b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9073b5a892a1SMatthew G. Knepley 
9074b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9075b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
9076b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9077b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9078b5a892a1SMatthew G. Knepley 
9079b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceCone[arr[c*2]] != targetCone[c]) break;
9080b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
9081b5a892a1SMatthew G. Knepley   }
9082b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9083b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9084b5a892a1SMatthew G. Knepley }
9085b5a892a1SMatthew G. Knepley 
9086b5a892a1SMatthew G. Knepley /*@C
9087*bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9088b5a892a1SMatthew G. Knepley 
9089b5a892a1SMatthew G. Knepley   Not collective
9090b5a892a1SMatthew G. Knepley 
9091b5a892a1SMatthew G. Knepley   Input Parameters:
9092*bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9093b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9094b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9095b5a892a1SMatthew G. Knepley 
9096b5a892a1SMatthew G. Knepley   Output Parameters:
9097*bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9098b5a892a1SMatthew G. Knepley 
9099b5a892a1SMatthew G. Knepley   Level: advanced
9100b5a892a1SMatthew G. Knepley 
9101*bb7acecfSBarry Smith   Note:
9102*bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9103*bb7acecfSBarry Smith 
9104*bb7acecfSBarry Smith   Developer Note:
9105*bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9106*bb7acecfSBarry Smith 
9107*bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9108b5a892a1SMatthew G. Knepley @*/
9109b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9110b5a892a1SMatthew G. Knepley {
9111b5a892a1SMatthew G. Knepley   PetscBool      found;
9112b5a892a1SMatthew G. Knepley 
9113b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91149566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
91157a8be351SBarry Smith   PetscCheck(found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9116b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9117b5a892a1SMatthew G. Knepley }
9118b5a892a1SMatthew G. Knepley 
9119b5a892a1SMatthew G. Knepley /*@C
9120*bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9121b5a892a1SMatthew G. Knepley 
9122b5a892a1SMatthew G. Knepley   Not collective
9123b5a892a1SMatthew G. Knepley 
9124b5a892a1SMatthew G. Knepley   Input Parameters:
9125*bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9126b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9127b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9128b5a892a1SMatthew G. Knepley 
9129b5a892a1SMatthew G. Knepley   Output Parameters:
9130*bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9131b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9132b5a892a1SMatthew G. Knepley 
9133b5a892a1SMatthew G. Knepley   Level: advanced
9134b5a892a1SMatthew G. Knepley 
9135*bb7acecfSBarry Smith   Note:
9136*bb7acecfSBarry Smith   An arrangement is a vertex order
9137*bb7acecfSBarry Smith 
9138*bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9139*bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9140*bb7acecfSBarry Smith 
9141*bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9142*bb7acecfSBarry Smith 
9143*bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangment()`
9144b5a892a1SMatthew G. Knepley @*/
9145b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9146b5a892a1SMatthew G. Knepley {
9147b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9148b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
9149b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9150b5a892a1SMatthew G. Knepley 
9151b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9152b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
9153b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9154b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9155b5a892a1SMatthew G. Knepley 
9156b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceVert[arr[c]] != targetVert[c]) break;
9157b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
9158b5a892a1SMatthew G. Knepley   }
9159b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9160b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9161b5a892a1SMatthew G. Knepley }
9162b5a892a1SMatthew G. Knepley 
9163b5a892a1SMatthew G. Knepley /*@C
9164*bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9165b5a892a1SMatthew G. Knepley 
9166b5a892a1SMatthew G. Knepley   Not collective
9167b5a892a1SMatthew G. Knepley 
9168b5a892a1SMatthew G. Knepley   Input Parameters:
9169*bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9170b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9171b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9172b5a892a1SMatthew G. Knepley 
9173b5a892a1SMatthew G. Knepley   Output Parameters:
9174*bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9175b5a892a1SMatthew G. Knepley 
9176b5a892a1SMatthew G. Knepley   Level: advanced
9177b5a892a1SMatthew G. Knepley 
9178*bb7acecfSBarry Smith   Note:
9179*bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9180*bb7acecfSBarry Smith 
9181*bb7acecfSBarry Smith   Developer Note:
9182*bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9183*bb7acecfSBarry Smith 
9184*bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9185b5a892a1SMatthew G. Knepley @*/
9186b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9187b5a892a1SMatthew G. Knepley {
9188b5a892a1SMatthew G. Knepley   PetscBool      found;
9189b5a892a1SMatthew G. Knepley 
9190b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91919566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
91927a8be351SBarry Smith   PetscCheck(found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9193b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9194b5a892a1SMatthew G. Knepley }
9195012bc364SMatthew G. Knepley 
9196012bc364SMatthew G. Knepley /*@C
9197012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9198012bc364SMatthew G. Knepley 
9199012bc364SMatthew G. Knepley   Not collective
9200012bc364SMatthew G. Knepley 
9201012bc364SMatthew G. Knepley   Input Parameters:
9202*bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9203012bc364SMatthew G. Knepley - point - Coordinates of the point
9204012bc364SMatthew G. Knepley 
9205012bc364SMatthew G. Knepley   Output Parameters:
9206012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
9207012bc364SMatthew G. Knepley 
9208012bc364SMatthew G. Knepley   Level: advanced
9209012bc364SMatthew G. Knepley 
9210*bb7acecfSBarry Smith .seealso: `DM`, `DMPolytopeType`, `DMLocatePoints()`
9211012bc364SMatthew G. Knepley @*/
9212012bc364SMatthew G. Knepley PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9213012bc364SMatthew G. Knepley {
9214012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9215012bc364SMatthew G. Knepley   PetscInt  d;
9216012bc364SMatthew G. Knepley 
9217012bc364SMatthew G. Knepley   PetscFunctionBegin;
9218012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9219012bc364SMatthew G. Knepley   switch (ct) {
9220012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9221012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9222012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
9223012bc364SMatthew G. Knepley       if (point[d] < -1.0) {*inside = PETSC_FALSE; break;}
9224012bc364SMatthew G. Knepley       sum += point[d];
9225012bc364SMatthew G. Knepley     }
9226012bc364SMatthew G. Knepley     if (sum > PETSC_SMALL) {*inside = PETSC_FALSE; break;}
9227012bc364SMatthew G. Knepley     break;
9228012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9229012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9230012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
9231012bc364SMatthew G. Knepley       if (PetscAbsReal(point[d]) > 1.+PETSC_SMALL) {*inside = PETSC_FALSE; break;}
9232012bc364SMatthew G. Knepley     break;
9233012bc364SMatthew G. Knepley   default:
923498921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9235012bc364SMatthew G. Knepley   }
9236012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
9237012bc364SMatthew G. Knepley }
9238