xref: /petsc/src/dm/interface/dm.c (revision 4fb89dddf56594b92bdd2ca7e24874fafe134f45)
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 /*@
28de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
29a4121054SBarry Smith 
30a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
31a4121054SBarry Smith    error when you try to use the vector.
32a4121054SBarry Smith 
33d083f849SBarry Smith   Collective
34a4121054SBarry Smith 
35a4121054SBarry Smith   Input Parameter:
36a4121054SBarry Smith . comm - The communicator for the DM object
37a4121054SBarry Smith 
38a4121054SBarry Smith   Output Parameter:
39a4121054SBarry Smith . dm - The DM object
40a4121054SBarry Smith 
41a4121054SBarry Smith   Level: beginner
42a4121054SBarry Smith 
43db781477SPatrick Sanan .seealso: `DMSetType()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK`
44a4121054SBarry Smith @*/
457087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
46a4121054SBarry Smith {
47a4121054SBarry Smith   DM             v;
48e5e52638SMatthew G. Knepley   PetscDS        ds;
49a4121054SBarry Smith 
50a4121054SBarry Smith   PetscFunctionBegin;
511411c6eeSJed Brown   PetscValidPointer(dm,2);
520298fd71SBarry Smith   *dm = NULL;
539566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
54a4121054SBarry Smith 
559566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
56e7c4fc90SDmitry Karpeev 
5749be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
5849be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
590298fd71SBarry Smith   v->ltogmap                  = NULL;
60a4ea9b21SRichard Tran Mills   v->bind_below               = 0;
611411c6eeSJed Brown   v->bs                       = 1;
62171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
639566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
649566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
65c58f1c22SToby Isaac   v->labels                   = NULL;
6634aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
6734aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
68c58f1c22SToby Isaac   v->depthLabel               = NULL;
69ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
701bb6d2a8SBarry Smith   v->localSection             = NULL;
711bb6d2a8SBarry Smith   v->globalSection            = NULL;
723b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
733b8ba7d1SJed Brown   v->defaultConstraint.mat    = NULL;
7479769bd5SJed Brown   v->defaultConstraint.bias   = NULL;
756858538eSMatthew G. Knepley   v->coordinates[0].dim       = PETSC_DEFAULT;
766858538eSMatthew G. Knepley   v->coordinates[1].dim       = PETSC_DEFAULT;
776858538eSMatthew G. Knepley   v->sparseLocalize           = PETSC_TRUE;
7896173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
79435a35e8SMatthew G Knepley   {
80435a35e8SMatthew G Knepley     PetscInt i;
81435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
820298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
83f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
84435a35e8SMatthew G Knepley     }
85435a35e8SMatthew G Knepley   }
869566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
879566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(v, NULL, NULL, ds));
889566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
899566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
9014f150ffSMatthew G. Knepley   v->dmBC = NULL;
91a8fb8f29SToby Isaac   v->coarseMesh = NULL;
92f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
93cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
949566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v,VECSTANDARD));
959566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v,MATAIJ));
964a7a4c06SLawrence Mitchell 
971411c6eeSJed Brown   *dm = v;
98a4121054SBarry Smith   PetscFunctionReturn(0);
99a4121054SBarry Smith }
100a4121054SBarry Smith 
10138221697SMatthew G. Knepley /*@
10238221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
10338221697SMatthew G. Knepley 
104d083f849SBarry Smith   Collective
10538221697SMatthew G. Knepley 
10638221697SMatthew G. Knepley   Input Parameter:
10738221697SMatthew G. Knepley . dm - The original DM object
10838221697SMatthew G. Knepley 
10938221697SMatthew G. Knepley   Output Parameter:
11038221697SMatthew G. Knepley . newdm  - The new DM object
11138221697SMatthew G. Knepley 
11238221697SMatthew G. Knepley   Level: beginner
11338221697SMatthew G. Knepley 
1141cb8cacdSPatrick Sanan   Notes:
1151cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1161cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1171cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1181bb6d2a8SBarry Smith 
11989706ed2SPatrick Sanan   The clone is considered set up iff the original is.
12089706ed2SPatrick Sanan 
121db781477SPatrick Sanan .seealso: `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`
1221bb6d2a8SBarry Smith 
12338221697SMatthew G. Knepley @*/
12438221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
12538221697SMatthew G. Knepley {
12638221697SMatthew G. Knepley   PetscSF        sf;
12738221697SMatthew G. Knepley   Vec            coords;
12838221697SMatthew G. Knepley   void          *ctx;
1296858538eSMatthew G. Knepley   PetscInt       dim, cdim, i;
13038221697SMatthew G. Knepley 
13138221697SMatthew G. Knepley   PetscFunctionBegin;
13238221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13338221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
1349566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject) dm), newdm));
1359566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
136ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
137ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
138c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
1399566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1409566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype,(char**)&(*newdm)->vectype));
1419566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1429566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype,(char**)&(*newdm)->mattype));
1439566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1449566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
1451baa6e33SBarry Smith   if (dm->ops->clone) PetscCall((*dm->ops->clone)(dm, newdm));
1463f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1479566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1489566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1499566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1509566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
1516858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1526858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
153be4c1c3eSMatthew G. Knepley       DM           ncdm;
154be4c1c3eSMatthew G. Knepley       PetscSection cs;
1555a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
156be4c1c3eSMatthew G. Knepley 
1576858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1589566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
1599566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm)));
1605a0206caSToby Isaac       if (pEndMax >= 0) {
1616858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1626858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1639566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1646858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1656858538eSMatthew G. Knepley         else   PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1669566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
167be4c1c3eSMatthew G. Knepley       }
168be4c1c3eSMatthew G. Knepley     }
1696858538eSMatthew G. Knepley   }
1709566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1719566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1729566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
17338221697SMatthew G. Knepley   if (coords) {
1749566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
17538221697SMatthew G. Knepley   } else {
1769566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1779566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
17838221697SMatthew G. Knepley   }
1796858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
1806858538eSMatthew G. Knepley   if (coords) {
1816858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
1826858538eSMatthew G. Knepley   } else {
1836858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
1846858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
1856858538eSMatthew G. Knepley   }
18690b157c4SStefano Zampini   {
187*4fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
1886858538eSMatthew G. Knepley 
189*4fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm,    &maxCell, &Lstart, &L));
190*4fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell,  Lstart,  L));
191c6b900c6SMatthew G. Knepley   }
19234aa8a36SMatthew G. Knepley   {
19334aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
19434aa8a36SMatthew G. Knepley 
1959566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
1969566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
19734aa8a36SMatthew G. Knepley   }
19838221697SMatthew G. Knepley   PetscFunctionReturn(0);
19938221697SMatthew G. Knepley }
20038221697SMatthew G. Knepley 
2019a42bb27SBarry Smith /*@C
202564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
2039a42bb27SBarry Smith 
204d083f849SBarry Smith    Logically Collective on da
2059a42bb27SBarry Smith 
206147403d9SBarry Smith    Input Parameters:
2079a42bb27SBarry Smith +  da - initial distributed array
208147403d9SBarry Smith -  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
2099a42bb27SBarry Smith 
2109a42bb27SBarry Smith    Options Database:
211147403d9SBarry Smith .   -dm_vec_type ctype - the type of vector to create
2129a42bb27SBarry Smith 
2139a42bb27SBarry Smith    Level: intermediate
2149a42bb27SBarry Smith 
215db781477SPatrick Sanan .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`
2169a42bb27SBarry Smith @*/
21719fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2189a42bb27SBarry Smith {
2199a42bb27SBarry Smith   PetscFunctionBegin;
2209a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2219566063dSJacob Faibussowitsch   PetscCall(PetscFree(da->vectype));
2229566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype,(char**)&da->vectype));
2239a42bb27SBarry Smith   PetscFunctionReturn(0);
2249a42bb27SBarry Smith }
2259a42bb27SBarry Smith 
226c0dedaeaSBarry Smith /*@C
227c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
228c0dedaeaSBarry Smith 
229d083f849SBarry Smith    Logically Collective on da
230c0dedaeaSBarry Smith 
231c0dedaeaSBarry Smith    Input Parameter:
232c0dedaeaSBarry Smith .  da - initial distributed array
233c0dedaeaSBarry Smith 
234c0dedaeaSBarry Smith    Output Parameter:
235c0dedaeaSBarry Smith .  ctype - the vector type
236c0dedaeaSBarry Smith 
237c0dedaeaSBarry Smith    Level: intermediate
238c0dedaeaSBarry Smith 
239db781477SPatrick Sanan .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
240c0dedaeaSBarry Smith @*/
241c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
242c0dedaeaSBarry Smith {
243c0dedaeaSBarry Smith   PetscFunctionBegin;
244c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
245c0dedaeaSBarry Smith   *ctype = da->vectype;
246c0dedaeaSBarry Smith   PetscFunctionReturn(0);
247c0dedaeaSBarry Smith }
248c0dedaeaSBarry Smith 
2495f1ad066SMatthew G Knepley /*@
25034f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2515f1ad066SMatthew G Knepley 
2525f1ad066SMatthew G Knepley   Not collective
2535f1ad066SMatthew G Knepley 
2545f1ad066SMatthew G Knepley   Input Parameter:
2555f1ad066SMatthew G Knepley . v - The Vec
2565f1ad066SMatthew G Knepley 
2575f1ad066SMatthew G Knepley   Output Parameter:
2585f1ad066SMatthew G Knepley . dm - The DM
2595f1ad066SMatthew G Knepley 
2605f1ad066SMatthew G Knepley   Level: intermediate
2615f1ad066SMatthew G Knepley 
262db781477SPatrick Sanan .seealso: `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2635f1ad066SMatthew G Knepley @*/
2645f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2655f1ad066SMatthew G Knepley {
2665f1ad066SMatthew G Knepley   PetscFunctionBegin;
2675f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2685f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2699566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm));
2705f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2715f1ad066SMatthew G Knepley }
2725f1ad066SMatthew G Knepley 
2735f1ad066SMatthew G Knepley /*@
274d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2755f1ad066SMatthew G Knepley 
2765f1ad066SMatthew G Knepley   Not collective
2775f1ad066SMatthew G Knepley 
2785f1ad066SMatthew G Knepley   Input Parameters:
2795f1ad066SMatthew G Knepley + v - The Vec
2805f1ad066SMatthew G Knepley - dm - The DM
2815f1ad066SMatthew G Knepley 
282d9805387SMatthew G. Knepley   Note: 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.
283d9805387SMatthew G. Knepley 
2845f1ad066SMatthew G Knepley   Level: intermediate
2855f1ad066SMatthew G Knepley 
286db781477SPatrick Sanan .seealso: `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2875f1ad066SMatthew G Knepley @*/
2885f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2895f1ad066SMatthew G Knepley {
2905f1ad066SMatthew G Knepley   PetscFunctionBegin;
2915f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
292d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2939566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm));
2945f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2955f1ad066SMatthew G Knepley }
2965f1ad066SMatthew G Knepley 
297521d9a4cSLisandro Dalcin /*@C
2988f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2998f1509bcSBarry Smith 
300d083f849SBarry Smith    Logically Collective on dm
3018f1509bcSBarry Smith 
3028f1509bcSBarry Smith    Input Parameters:
3038f1509bcSBarry Smith +  dm - the DM context
3048f1509bcSBarry Smith -  ctype - the matrix type
3058f1509bcSBarry Smith 
3068f1509bcSBarry Smith    Options Database:
3078f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3088f1509bcSBarry Smith 
3098f1509bcSBarry Smith    Level: intermediate
3108f1509bcSBarry Smith 
311db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
312db781477SPatrick Sanan           `DMGetISColoringType()`
3138f1509bcSBarry Smith @*/
3148f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3158f1509bcSBarry Smith {
3168f1509bcSBarry Smith   PetscFunctionBegin;
3178f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3188f1509bcSBarry Smith   dm->coloringtype = ctype;
3198f1509bcSBarry Smith   PetscFunctionReturn(0);
3208f1509bcSBarry Smith }
3218f1509bcSBarry Smith 
3228f1509bcSBarry Smith /*@C
3238f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
324521d9a4cSLisandro Dalcin 
325d083f849SBarry Smith    Logically Collective on dm
326521d9a4cSLisandro Dalcin 
327521d9a4cSLisandro Dalcin    Input Parameter:
3288f1509bcSBarry Smith .  dm - the DM context
3298f1509bcSBarry Smith 
3308f1509bcSBarry Smith    Output Parameter:
3318f1509bcSBarry Smith .  ctype - the matrix type
3328f1509bcSBarry Smith 
3338f1509bcSBarry Smith    Options Database:
3348f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3358f1509bcSBarry Smith 
3368f1509bcSBarry Smith    Level: intermediate
3378f1509bcSBarry Smith 
338db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
339db781477SPatrick Sanan           `DMGetISColoringType()`
3408f1509bcSBarry Smith @*/
3418f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3428f1509bcSBarry Smith {
3438f1509bcSBarry Smith   PetscFunctionBegin;
3448f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3458f1509bcSBarry Smith   *ctype = dm->coloringtype;
3468f1509bcSBarry Smith   PetscFunctionReturn(0);
3478f1509bcSBarry Smith }
3488f1509bcSBarry Smith 
3498f1509bcSBarry Smith /*@C
3508f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3518f1509bcSBarry Smith 
352d083f849SBarry Smith    Logically Collective on dm
3538f1509bcSBarry Smith 
3548f1509bcSBarry Smith    Input Parameters:
355521d9a4cSLisandro Dalcin +  dm - the DM context
356a2b5a043SBarry Smith -  ctype - the matrix type
357521d9a4cSLisandro Dalcin 
358521d9a4cSLisandro Dalcin    Options Database:
359147403d9SBarry Smith .   -dm_mat_type ctype - the type of the matrix to create
360521d9a4cSLisandro Dalcin 
361521d9a4cSLisandro Dalcin    Level: intermediate
362521d9a4cSLisandro Dalcin 
363db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`, `DMSetMatType()`, `DMGetMatType()`
364521d9a4cSLisandro Dalcin @*/
36519fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
366521d9a4cSLisandro Dalcin {
367521d9a4cSLisandro Dalcin   PetscFunctionBegin;
368521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3699566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->mattype));
3709566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype,(char**)&dm->mattype));
371521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
372521d9a4cSLisandro Dalcin }
373521d9a4cSLisandro Dalcin 
374c0dedaeaSBarry Smith /*@C
375c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
376c0dedaeaSBarry Smith 
377d083f849SBarry Smith    Logically Collective on dm
378c0dedaeaSBarry Smith 
379c0dedaeaSBarry Smith    Input Parameter:
380c0dedaeaSBarry Smith .  dm - the DM context
381c0dedaeaSBarry Smith 
382c0dedaeaSBarry Smith    Output Parameter:
383c0dedaeaSBarry Smith .  ctype - the matrix type
384c0dedaeaSBarry Smith 
385c0dedaeaSBarry Smith    Options Database:
386147403d9SBarry Smith .   -dm_mat_type ctype - the matrix type
387c0dedaeaSBarry Smith 
388c0dedaeaSBarry Smith    Level: intermediate
389c0dedaeaSBarry Smith 
390db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`, `DMSetMatType()`, `DMGetMatType()`
391c0dedaeaSBarry Smith @*/
392c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
393c0dedaeaSBarry Smith {
394c0dedaeaSBarry Smith   PetscFunctionBegin;
395c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
396c0dedaeaSBarry Smith   *ctype = dm->mattype;
397c0dedaeaSBarry Smith   PetscFunctionReturn(0);
398c0dedaeaSBarry Smith }
399c0dedaeaSBarry Smith 
400c688c046SMatthew G Knepley /*@
40134f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
402c688c046SMatthew G Knepley 
403c688c046SMatthew G Knepley   Not collective
404c688c046SMatthew G Knepley 
405c688c046SMatthew G Knepley   Input Parameter:
406c688c046SMatthew G Knepley . A - The Mat
407c688c046SMatthew G Knepley 
408c688c046SMatthew G Knepley   Output Parameter:
409c688c046SMatthew G Knepley . dm - The DM
410c688c046SMatthew G Knepley 
411c688c046SMatthew G Knepley   Level: intermediate
412c688c046SMatthew G Knepley 
4138f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4148f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4158f1509bcSBarry Smith 
416db781477SPatrick Sanan .seealso: `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
417c688c046SMatthew G Knepley @*/
418c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
419c688c046SMatthew G Knepley {
420c688c046SMatthew G Knepley   PetscFunctionBegin;
421c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
422c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
4239566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm));
424c688c046SMatthew G Knepley   PetscFunctionReturn(0);
425c688c046SMatthew G Knepley }
426c688c046SMatthew G Knepley 
427c688c046SMatthew G Knepley /*@
428c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
429c688c046SMatthew G Knepley 
430c688c046SMatthew G Knepley   Not collective
431c688c046SMatthew G Knepley 
432c688c046SMatthew G Knepley   Input Parameters:
433c688c046SMatthew G Knepley + A - The Mat
434c688c046SMatthew G Knepley - dm - The DM
435c688c046SMatthew G Knepley 
436c688c046SMatthew G Knepley   Level: intermediate
437c688c046SMatthew G Knepley 
4388f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4398f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4408f1509bcSBarry Smith 
441db781477SPatrick Sanan .seealso: `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
442c688c046SMatthew G Knepley @*/
443c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
444c688c046SMatthew G Knepley {
445c688c046SMatthew G Knepley   PetscFunctionBegin;
446c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4478865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
4489566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm));
449c688c046SMatthew G Knepley   PetscFunctionReturn(0);
450c688c046SMatthew G Knepley }
451c688c046SMatthew G Knepley 
4529a42bb27SBarry Smith /*@C
4539a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4546757b960SDave May    DM options in the database.
4559a42bb27SBarry Smith 
456d083f849SBarry Smith    Logically Collective on dm
4579a42bb27SBarry Smith 
458d8d19677SJose E. Roman    Input Parameters:
4598353ddbbSDave May +  da - the DM context
4609a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4619a42bb27SBarry Smith 
4629a42bb27SBarry Smith    Notes:
4639a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4649a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4659a42bb27SBarry Smith 
4669a42bb27SBarry Smith    Level: advanced
4679a42bb27SBarry Smith 
468db781477SPatrick Sanan .seealso: `DMSetFromOptions()`
4699a42bb27SBarry Smith @*/
4707087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4719a42bb27SBarry Smith {
4729a42bb27SBarry Smith   PetscFunctionBegin;
4739a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4749566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm,prefix));
4751baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix));
4761baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix));
4779a42bb27SBarry Smith   PetscFunctionReturn(0);
4789a42bb27SBarry Smith }
4799a42bb27SBarry Smith 
48031697293SDave May /*@C
48131697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
48231697293SDave May    DM options in the database.
48331697293SDave May 
484d083f849SBarry Smith    Logically Collective on dm
48531697293SDave May 
48631697293SDave May    Input Parameters:
48731697293SDave May +  dm - the DM context
48831697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48931697293SDave May 
49031697293SDave May    Notes:
49131697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
49231697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
49331697293SDave May 
49431697293SDave May    Level: advanced
49531697293SDave May 
496db781477SPatrick Sanan .seealso: `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`
49731697293SDave May @*/
49831697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49931697293SDave May {
50031697293SDave May   PetscFunctionBegin;
50131697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5029566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix));
50331697293SDave May   PetscFunctionReturn(0);
50431697293SDave May }
50531697293SDave May 
50631697293SDave May /*@C
50731697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50831697293SDave May    DM options in the database.
50931697293SDave May 
51031697293SDave May    Not Collective
51131697293SDave May 
51231697293SDave May    Input Parameters:
51331697293SDave May .  dm - the DM context
51431697293SDave May 
51531697293SDave May    Output Parameters:
51631697293SDave May .  prefix - pointer to the prefix string used is returned
51731697293SDave May 
51895452b02SPatrick Sanan    Notes:
51995452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
52031697293SDave May    sufficient length to hold the prefix.
52131697293SDave May 
52231697293SDave May    Level: advanced
52331697293SDave May 
524db781477SPatrick Sanan .seealso: `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`
52531697293SDave May @*/
52631697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52731697293SDave May {
52831697293SDave May   PetscFunctionBegin;
52931697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5309566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm,prefix));
53131697293SDave May   PetscFunctionReturn(0);
53231697293SDave May }
53331697293SDave May 
53488bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53588bdff64SToby Isaac {
5366eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
53788bdff64SToby Isaac 
53888bdff64SToby Isaac   PetscFunctionBegin;
539aab5bcd8SJed Brown   *ncrefct = 0;
54088bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
54188bdff64SToby Isaac     refct--;
54288bdff64SToby Isaac     if (recurseCoarse) {
54388bdff64SToby Isaac       PetscInt coarseCount;
54488bdff64SToby Isaac 
5459566063dSJacob Faibussowitsch       PetscCall(DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount));
54688bdff64SToby Isaac       refct += coarseCount;
54788bdff64SToby Isaac     }
54888bdff64SToby Isaac   }
54988bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
55088bdff64SToby Isaac     refct--;
55188bdff64SToby Isaac     if (recurseFine) {
55288bdff64SToby Isaac       PetscInt fineCount;
55388bdff64SToby Isaac 
5549566063dSJacob Faibussowitsch       PetscCall(DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount));
55588bdff64SToby Isaac       refct += fineCount;
55688bdff64SToby Isaac     }
55788bdff64SToby Isaac   }
55888bdff64SToby Isaac   *ncrefct = refct;
55988bdff64SToby Isaac   PetscFunctionReturn(0);
56088bdff64SToby Isaac }
56188bdff64SToby Isaac 
562f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
563354557abSToby Isaac {
5645d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
565354557abSToby Isaac 
566354557abSToby Isaac   PetscFunctionBegin;
567354557abSToby Isaac   /* destroy the labels */
568354557abSToby Isaac   while (next) {
569354557abSToby Isaac     DMLabelLink tmp = next->next;
570354557abSToby Isaac 
5715d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
572ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
5739566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
5749566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
575354557abSToby Isaac     next = tmp;
576354557abSToby Isaac   }
5775d80c0bfSVaclav Hapla   dm->labels = NULL;
578354557abSToby Isaac   PetscFunctionReturn(0);
579354557abSToby Isaac }
580354557abSToby Isaac 
5816858538eSMatthew G. Knepley PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
5826858538eSMatthew G. Knepley {
5836858538eSMatthew G. Knepley   PetscFunctionBegin;
5846858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
5856858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
5866858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
5876858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
5886858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
5896858538eSMatthew G. Knepley   PetscFunctionReturn(0);
5906858538eSMatthew G. Knepley }
5916858538eSMatthew G. Knepley 
5921fb7b255SJunchao Zhang /*@C
5938472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
59447c6ae99SBarry Smith 
595d083f849SBarry Smith     Collective on dm
59647c6ae99SBarry Smith 
59747c6ae99SBarry Smith     Input Parameter:
59847c6ae99SBarry Smith .   dm - the DM object to destroy
59947c6ae99SBarry Smith 
60047c6ae99SBarry Smith     Level: developer
60147c6ae99SBarry Smith 
602db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
60347c6ae99SBarry Smith 
60447c6ae99SBarry Smith @*/
605fcfd50ebSBarry Smith PetscErrorCode DMDestroy(DM *dm)
60647c6ae99SBarry Smith {
6076eb26441SStefano Zampini   PetscInt       cnt;
608dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
60947c6ae99SBarry Smith 
61047c6ae99SBarry Smith   PetscFunctionBegin;
6116bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6126bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
61387e657c6SBarry Smith 
61488bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
6159566063dSJacob Faibussowitsch   PetscCall(DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt));
61688bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
617ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
6186bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6196bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6206eb26441SStefano Zampini 
6219566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6229566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
6236eb26441SStefano Zampini 
624f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6250298fd71SBarry Smith   (*dm)->namedglobal = NULL;
626f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6272348bcf4SPeter Brune     nnext = nlink->next;
6287a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN,((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
6299566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6309566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6319566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
6322348bcf4SPeter Brune   }
633f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6340298fd71SBarry Smith   (*dm)->namedlocal = NULL;
635f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
636f490541aSPeter Brune     nnext = nlink->next;
6377a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN,((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
6389566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6399566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6409566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
641f490541aSPeter Brune   }
6422348bcf4SPeter Brune 
643b17ce1afSJed Brown   /* Destroy the list of hooks */
644c833c3b5SJed Brown   {
645c833c3b5SJed Brown     DMCoarsenHookLink link,next;
646b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
647b17ce1afSJed Brown       next = link->next;
6489566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
649b17ce1afSJed Brown     }
6500298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
651c833c3b5SJed Brown   }
652c833c3b5SJed Brown   {
653c833c3b5SJed Brown     DMRefineHookLink link,next;
654c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
655c833c3b5SJed Brown       next = link->next;
6569566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
657c833c3b5SJed Brown     }
6580298fd71SBarry Smith     (*dm)->refinehook = NULL;
659c833c3b5SJed Brown   }
660be081cd6SPeter Brune   {
661be081cd6SPeter Brune     DMSubDomainHookLink link,next;
662be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
663be081cd6SPeter Brune       next = link->next;
6649566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
665be081cd6SPeter Brune     }
6660298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
667be081cd6SPeter Brune   }
668baf369e7SPeter Brune   {
669baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
670baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
671baf369e7SPeter Brune       next = link->next;
6729566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
673baf369e7SPeter Brune     }
6740298fd71SBarry Smith     (*dm)->gtolhook = NULL;
675baf369e7SPeter Brune   }
676d4d07f1eSToby Isaac   {
677d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
678d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
679d4d07f1eSToby Isaac       next = link->next;
6809566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
681d4d07f1eSToby Isaac     }
682d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
683d4d07f1eSToby Isaac   }
684aa1993deSMatthew G Knepley   /* Destroy the work arrays */
685aa1993deSMatthew G Knepley   {
686aa1993deSMatthew G Knepley     DMWorkLink link,next;
6877a8be351SBarry Smith     PetscCheck(!(*dm)->workout,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
688aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
689aa1993deSMatthew G Knepley       next = link->next;
6909566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
6919566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
692aa1993deSMatthew G Knepley     }
6930298fd71SBarry Smith     (*dm)->workin = NULL;
694aa1993deSMatthew G Knepley   }
695c58f1c22SToby Isaac   /* destroy the labels */
6969566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
697f4cdcedcSVaclav Hapla   /* destroy the fields */
6989566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
699f4cdcedcSVaclav Hapla   /* destroy the boundaries */
700e6f8dbb6SToby Isaac   {
701e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
702e6f8dbb6SToby Isaac     while (next) {
703e6f8dbb6SToby Isaac       DMBoundary b = next;
704e6f8dbb6SToby Isaac 
705e6f8dbb6SToby Isaac       next = b->next;
7069566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
707e6f8dbb6SToby Isaac     }
708e6f8dbb6SToby Isaac   }
709b17ce1afSJed Brown 
7109566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7119566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7129566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
71352536dc3SBarry Smith 
7141a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7159566063dSJacob Faibussowitsch     PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7161a266240SBarry Smith   }
7179566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7189566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7199566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7209566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
72188ed4aceSMatthew G Knepley 
7229566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7239566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
7249566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7259566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7269566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7279566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7289566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
729736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
730736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7319566063dSJacob Faibussowitsch       PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
732736995cdSBlaise Bourdin     }
7339566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject) (*dm)->sfMigration));
734736995cdSBlaise Bourdin   }
7359a2a23afSMatthew G. Knepley   {
7369a2a23afSMatthew G. Knepley     Vec     *auxData;
7379a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7389a2a23afSMatthew G. Knepley 
7399566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetSize((*dm)->auxData, &n));
7409566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &auxData));
7419566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetVals((*dm)->auxData, &off, auxData));
7429566063dSJacob Faibussowitsch     for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
7439566063dSJacob Faibussowitsch     PetscCall(PetscFree(auxData));
7449566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
7459a2a23afSMatthew G. Knepley   }
74688bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
7479566063dSJacob Faibussowitsch     PetscCall(DMSetFineDM((*dm)->coarseMesh,NULL));
74888bdff64SToby Isaac   }
7496eb26441SStefano Zampini 
7509566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
75188bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
7529566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM((*dm)->fineMesh,NULL));
75388bdff64SToby Isaac   }
7549566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
755*4fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7569566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7579566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7586858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7596858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7609566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7619566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7629566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7636636e97aSMatthew G Knepley 
7649566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7659566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
766e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7679566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
768732e2eb9SMatthew G Knepley 
769ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7709566063dSJacob Faibussowitsch     PetscCall((*(*dm)->ops->destroy)(*dm));
771ed3c66a1SDave May   }
7729566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
773f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7749566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7759566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
776f918ec44SMatthew G. Knepley #endif
777435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7789566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
77947c6ae99SBarry Smith   PetscFunctionReturn(0);
78047c6ae99SBarry Smith }
78147c6ae99SBarry Smith 
782d7bf68aeSBarry Smith /*@
783d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
784d7bf68aeSBarry Smith 
785d083f849SBarry Smith     Collective on dm
786d7bf68aeSBarry Smith 
787d7bf68aeSBarry Smith     Input Parameter:
788d7bf68aeSBarry Smith .   dm - the DM object to setup
789d7bf68aeSBarry Smith 
790d7bf68aeSBarry Smith     Level: developer
791d7bf68aeSBarry Smith 
792db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
793d7bf68aeSBarry Smith 
794d7bf68aeSBarry Smith @*/
7957087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
796d7bf68aeSBarry Smith {
797d7bf68aeSBarry Smith   PetscFunctionBegin;
798171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7998387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
8001baa6e33SBarry Smith   if (dm->ops->setup) PetscCall((*dm->ops->setup)(dm));
8018387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
802d7bf68aeSBarry Smith   PetscFunctionReturn(0);
803d7bf68aeSBarry Smith }
804d7bf68aeSBarry Smith 
805d7bf68aeSBarry Smith /*@
806d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
807d7bf68aeSBarry Smith 
808d083f849SBarry Smith     Collective on dm
809d7bf68aeSBarry Smith 
810d7bf68aeSBarry Smith     Input Parameter:
811d7bf68aeSBarry Smith .   dm - the DM object to set options for
812d7bf68aeSBarry Smith 
813732e2eb9SMatthew G Knepley     Options Database:
8145b8ffe73SMark Adams +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix() and DMCreateMassMatrix(), but do not fill it with zeros
8154164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8164164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
817a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
818a4ea9b21SRichard Tran Mills -   -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
819732e2eb9SMatthew G Knepley 
8209318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8219318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8229318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
823cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
8249318fe57SMatthew G. Knepley . -dm_plex_shape <shape>            - The domain shape, such as DM_SHAPE_BOX, DM_SHAPE_SPHERE, etc.
8259318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8269318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8279318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
8289318fe57SMatthew G. Knepley . -dm_plex_simplex <bool>           - PETSC_TRUE for simplex elements, PETSC_FALSE for tensor elements
8299318fe57SMatthew G. Knepley . -dm_plex_interpolate <bool>       - PETSC_TRUE turns on topological interpolation (creating edges and faces)
8309318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8319318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8329318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8339318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
8349318fe57SMatthew G. Knepley . -dm_plex_box_bd <bx,by,bz>        - Specify the DMBoundaryType for each direction
8359318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8369318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8379318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8389318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
839bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8409318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8419318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8429318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8439318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
844bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
845d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
846d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
847d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
848d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
849d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
850909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
851909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8529318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8539318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8549318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8559318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8569318fe57SMatthew G. Knepley 
857384a6580SVaclav Hapla     DMPLEX Specific Checks
858384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
859384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
860384a6580SVaclav Hapla .   -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()
861384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
862384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
863384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
864384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
865d7bf68aeSBarry Smith 
86695eb5ee5SVaclav Hapla     Level: intermediate
86795eb5ee5SVaclav Hapla 
868db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
869db781477SPatrick Sanan          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`
870d7bf68aeSBarry Smith 
871d7bf68aeSBarry Smith @*/
8727087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
873d7bf68aeSBarry Smith {
8747781c08eSBarry Smith   char           typeName[256];
875ca266f36SBarry Smith   PetscBool      flg;
876d7bf68aeSBarry Smith 
877d7bf68aeSBarry Smith   PetscFunctionBegin;
878171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87949be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
8809566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
8819566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
882d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
8839566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,NULL));
8849566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg));
8851baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm,typeName));
8869566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg));
8871baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm,typeName));
8889566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL));
8899566063dSJacob 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));
8901baa6e33SBarry Smith   if (dm->ops->setfromoptions) PetscCall((*dm->ops->setfromoptions)(PetscOptionsObject,dm));
891f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8929566063dSJacob Faibussowitsch   PetscCall(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm));
893d0609cedSBarry Smith   PetscOptionsEnd();
894d7bf68aeSBarry Smith   PetscFunctionReturn(0);
895d7bf68aeSBarry Smith }
896d7bf68aeSBarry Smith 
897fc9bc008SSatish Balay /*@C
898fe2efc57SMark    DMViewFromOptions - View from Options
899fe2efc57SMark 
900fe2efc57SMark    Collective on DM
901fe2efc57SMark 
902fe2efc57SMark    Input Parameters:
903fe2efc57SMark +  dm - the DM object
904736c3998SJose E. Roman .  obj - Optional object
905736c3998SJose E. Roman -  name - command line option
906fe2efc57SMark 
907fe2efc57SMark    Level: intermediate
908db781477SPatrick Sanan .seealso: `DM`, `DMView`, `PetscObjectViewFromOptions()`, `DMCreate()`
909fe2efc57SMark @*/
910fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
911fe2efc57SMark {
912fe2efc57SMark   PetscFunctionBegin;
913fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9149566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm,obj,name));
915fe2efc57SMark   PetscFunctionReturn(0);
916fe2efc57SMark }
917fe2efc57SMark 
918fe2efc57SMark /*@C
919224748a4SBarry Smith     DMView - Views a DM
92047c6ae99SBarry Smith 
921d083f849SBarry Smith     Collective on dm
92247c6ae99SBarry Smith 
923d8d19677SJose E. Roman     Input Parameters:
92447c6ae99SBarry Smith +   dm - the DM object to view
92547c6ae99SBarry Smith -   v - the viewer
92647c6ae99SBarry Smith 
927cd7e8a5eSksagiyam     Notes:
928cd7e8a5eSksagiyam     Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
929cd7e8a5eSksagiyam     meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
930cd7e8a5eSksagiyam     before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
931cd7e8a5eSksagiyam 
932224748a4SBarry Smith     Level: beginner
93347c6ae99SBarry Smith 
934db781477SPatrick Sanan .seealso `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith @*/
9377087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
93847c6ae99SBarry Smith {
93932c0f0efSBarry Smith   PetscBool         isbinary;
94076a8abe0SBarry Smith   PetscMPIInt       size;
94176a8abe0SBarry Smith   PetscViewerFormat format;
94247c6ae99SBarry Smith 
94347c6ae99SBarry Smith   PetscFunctionBegin;
944171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9453014e516SBarry Smith   if (!v) {
9469566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v));
9473014e516SBarry Smith   }
948b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
94974903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
95074903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
95174903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
95274903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
95374903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
95474903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
95574903a4fSStefano Zampini      in an error here */
95674903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9579566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
958b1b135c8SBarry Smith 
9599566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v,&format));
9609566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size));
96176a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
9629566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm,v));
9639566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary));
96432c0f0efSBarry Smith   if (isbinary) {
96555849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
96632c0f0efSBarry Smith     char     type[256];
96732c0f0efSBarry Smith 
9689566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v,&classid,1,PETSC_INT));
9699566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(type,((PetscObject)dm)->type_name,256));
9709566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v,type,256,PETSC_CHAR));
97132c0f0efSBarry Smith   }
9721baa6e33SBarry Smith   if (dm->ops->view) PetscCall((*dm->ops->view)(dm,v));
97347c6ae99SBarry Smith   PetscFunctionReturn(0);
97447c6ae99SBarry Smith }
97547c6ae99SBarry Smith 
97647c6ae99SBarry Smith /*@
9778472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
97847c6ae99SBarry Smith 
979d083f849SBarry Smith     Collective on dm
98047c6ae99SBarry Smith 
98147c6ae99SBarry Smith     Input Parameter:
98247c6ae99SBarry Smith .   dm - the DM object
98347c6ae99SBarry Smith 
98447c6ae99SBarry Smith     Output Parameter:
98547c6ae99SBarry Smith .   vec - the global vector
98647c6ae99SBarry Smith 
987073dac72SJed Brown     Level: beginner
98847c6ae99SBarry Smith 
989db781477SPatrick Sanan .seealso `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
99047c6ae99SBarry Smith 
99147c6ae99SBarry Smith @*/
9927087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
99347c6ae99SBarry Smith {
99447c6ae99SBarry Smith   PetscFunctionBegin;
995171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
996b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
9977a8be351SBarry Smith   PetscCheck(dm->ops->createglobalvector,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
9989566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createglobalvector)(dm,vec));
99976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1000c6b011d8SStefano Zampini     DM vdm;
1001c6b011d8SStefano Zampini 
10029566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec,&vdm));
10037a8be351SBarry Smith     PetscCheck(vdm,PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the vector",((PetscObject)dm)->type_name);
1004c6b011d8SStefano Zampini   }
100547c6ae99SBarry Smith   PetscFunctionReturn(0);
100647c6ae99SBarry Smith }
100747c6ae99SBarry Smith 
100847c6ae99SBarry Smith /*@
10098472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith     Not Collective
101247c6ae99SBarry Smith 
101347c6ae99SBarry Smith     Input Parameter:
101447c6ae99SBarry Smith .   dm - the DM object
101547c6ae99SBarry Smith 
101647c6ae99SBarry Smith     Output Parameter:
101747c6ae99SBarry Smith .   vec - the local vector
101847c6ae99SBarry Smith 
1019073dac72SJed Brown     Level: beginner
102047c6ae99SBarry Smith 
1021db781477SPatrick Sanan .seealso `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
102247c6ae99SBarry Smith 
102347c6ae99SBarry Smith @*/
10247087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
102547c6ae99SBarry Smith {
102647c6ae99SBarry Smith   PetscFunctionBegin;
1027171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1028b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
10297a8be351SBarry Smith   PetscCheck(dm->ops->createlocalvector,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
10309566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createlocalvector)(dm,vec));
103176bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1032c6b011d8SStefano Zampini     DM vdm;
1033c6b011d8SStefano Zampini 
10349566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec,&vdm));
10357a8be351SBarry Smith     PetscCheck(vdm,PETSC_COMM_SELF,PETSC_ERR_LIB,"DM type '%s' did not attach the DM to the vector",((PetscObject)dm)->type_name);
1036c6b011d8SStefano Zampini   }
103747c6ae99SBarry Smith   PetscFunctionReturn(0);
103847c6ae99SBarry Smith }
103947c6ae99SBarry Smith 
10401411c6eeSJed Brown /*@
10411411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10421411c6eeSJed Brown 
1043d083f849SBarry Smith    Collective on dm
10441411c6eeSJed Brown 
10451411c6eeSJed Brown    Input Parameter:
10461411c6eeSJed Brown .  dm - the DM that provides the mapping
10471411c6eeSJed Brown 
10481411c6eeSJed Brown    Output Parameter:
10491411c6eeSJed Brown .  ltog - the mapping
10501411c6eeSJed Brown 
10511411c6eeSJed Brown    Level: intermediate
10521411c6eeSJed Brown 
10531411c6eeSJed Brown    Notes:
10541411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10551411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10561411c6eeSJed Brown 
1057db781477SPatrick Sanan .seealso: `DMCreateLocalVector()`
10581411c6eeSJed Brown @*/
10597087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10601411c6eeSJed Brown {
10610be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10621411c6eeSJed Brown 
10631411c6eeSJed Brown   PetscFunctionBegin;
10641411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10651411c6eeSJed Brown   PetscValidPointer(ltog,2);
10661411c6eeSJed Brown   if (!dm->ltogmap) {
106737d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
106837d0c07bSMatthew G Knepley 
10699566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
107037d0c07bSMatthew G Knepley     if (section) {
1071a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
107237d0c07bSMatthew G Knepley       PetscInt       *ltog;
1073ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
107437d0c07bSMatthew G Knepley 
10759566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
10769566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
10779566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
10789566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
107937d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1080e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
108137d0c07bSMatthew G Knepley 
108237d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
10839566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
10849566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
10859566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
10869566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
10871a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10881a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10891a7dc684SMatthew G. Knepley         if (dof) {
10905b8243e1SJed Brown           bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
10911a7dc684SMatthew G. Knepley         }
10925227eafbSStefano Zampini 
1093e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
10945227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1095e6befd46SJed Brown             ltog[l] = off < 0 ? off-c : -(off+c+1);
1096e6befd46SJed Brown             cind++;
1097e6befd46SJed Brown           } else {
10985227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off+1) : off) + c - cind;
1099e6befd46SJed Brown           }
110037d0c07bSMatthew G Knepley         }
110137d0c07bSMatthew G Knepley       }
1102bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11030be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11049566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax));
11050be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11060be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
11077591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11087591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1109ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1110ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1111ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1112ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l]/bs : -(-(ltog[l]+1)/bs + 1);
1113ca469d19SJed Brown         }
1114ccf3bd66SMatthew G. Knepley         n /= bs;
1115ccf3bd66SMatthew G. Knepley       }
11169566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
11179566063dSJacob Faibussowitsch       PetscCall(PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap));
111837d0c07bSMatthew G Knepley     } else {
11197a8be351SBarry Smith       PetscCheck(dm->ops->getlocaltoglobalmapping,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
11209566063dSJacob Faibussowitsch       PetscCall((*dm->ops->getlocaltoglobalmapping)(dm));
11211411c6eeSJed Brown     }
112237d0c07bSMatthew G Knepley   }
11231411c6eeSJed Brown   *ltog = dm->ltogmap;
11241411c6eeSJed Brown   PetscFunctionReturn(0);
11251411c6eeSJed Brown }
11261411c6eeSJed Brown 
11271411c6eeSJed Brown /*@
11281411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
11291411c6eeSJed Brown 
11301411c6eeSJed Brown    Not Collective
11311411c6eeSJed Brown 
11321411c6eeSJed Brown    Input Parameter:
11331411c6eeSJed Brown .  dm - the DM with block structure
11341411c6eeSJed Brown 
11351411c6eeSJed Brown    Output Parameter:
11361411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11371411c6eeSJed Brown 
11381411c6eeSJed Brown    Level: intermediate
11391411c6eeSJed Brown 
1140db781477SPatrick Sanan .seealso: `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11411411c6eeSJed Brown @*/
11427087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
11431411c6eeSJed Brown {
11441411c6eeSJed Brown   PetscFunctionBegin;
11451411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1146534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11477a8be351SBarry Smith   PetscCheck(dm->bs >= 1,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
11481411c6eeSJed Brown   *bs = dm->bs;
11491411c6eeSJed Brown   PetscFunctionReturn(0);
11501411c6eeSJed Brown }
11511411c6eeSJed Brown 
115248eeb7c8SBarry Smith /*@C
11538472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
115447c6ae99SBarry Smith 
1155a5bc1bf3SBarry Smith     Collective on dmc
115647c6ae99SBarry Smith 
1157d8d19677SJose E. Roman     Input Parameters:
1158a5bc1bf3SBarry Smith +   dmc - the DM object
1159a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
116047c6ae99SBarry Smith 
1161d8d19677SJose E. Roman     Output Parameters:
116247c6ae99SBarry Smith +  mat - the interpolation
116347c6ae99SBarry Smith -  vec - the scaling (optional)
116447c6ae99SBarry Smith 
116547c6ae99SBarry Smith     Level: developer
116647c6ae99SBarry Smith 
116795452b02SPatrick Sanan     Notes:
116895452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
116985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1170d52bd9f3SBarry Smith 
11711f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1172d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
117385afcc9aSBarry Smith 
1174db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
117547c6ae99SBarry Smith 
117647c6ae99SBarry Smith @*/
1177a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
117847c6ae99SBarry Smith {
117947c6ae99SBarry Smith   PetscFunctionBegin;
1180a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1181a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1182c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
11837a8be351SBarry Smith   PetscCheck(dmc->ops->createinterpolation,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
11849566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0));
11859566063dSJacob Faibussowitsch   PetscCall((*dmc->ops->createinterpolation)(dmc,dmf,mat,vec));
11869566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0));
118747c6ae99SBarry Smith   PetscFunctionReturn(0);
118847c6ae99SBarry Smith }
118947c6ae99SBarry Smith 
11903ad4599aSBarry Smith /*@
1191d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
11922ed6491fSPatrick Sanan 
11932ed6491fSPatrick Sanan   Input Parameters:
11942ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
11952ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
11962ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
11972ed6491fSPatrick Sanan 
11982ed6491fSPatrick Sanan   Output Parameter:
11992ed6491fSPatrick Sanan .    scale - the scaled vector
12002ed6491fSPatrick Sanan 
12012ed6491fSPatrick Sanan   Level: developer
12022ed6491fSPatrick Sanan 
1203e9c74fd6SRichard Tran Mills   Developer Notes:
1204e9c74fd6SRichard Tran Mills   If the fine-scale DMDA has the -dm_bind_below option set to true, then DMCreateInterpolationScale() calls MatSetBindingPropagates()
1205e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1206e9c74fd6SRichard Tran Mills 
1207db781477SPatrick Sanan .seealso: `DMCreateInterpolation()`
12082ed6491fSPatrick Sanan 
12092ed6491fSPatrick Sanan @*/
12102ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
12112ed6491fSPatrick Sanan {
12122ed6491fSPatrick Sanan   Vec            fine;
12132ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
12149704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1215e9c74fd6SRichard Tran Mills   PetscBool      bindingpropagates,isbound;
12169704db99SRichard Tran Mills #endif
12172ed6491fSPatrick Sanan 
12182ed6491fSPatrick Sanan   PetscFunctionBegin;
12199566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf,&fine));
12209566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac,scale));
12219566063dSJacob Faibussowitsch   PetscCall(VecSet(fine,one));
12229704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12239704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12249704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12259704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12269566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine,&bindingpropagates));
1227e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12289566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat,PETSC_TRUE));
12299566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine,&isbound));
12309566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat,isbound));
123183aa49f4SRichard Tran Mills   }
12329704db99SRichard Tran Mills #endif
12339566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat,fine,*scale));
12349566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12359566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12362ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12372ed6491fSPatrick Sanan }
12382ed6491fSPatrick Sanan 
12392ed6491fSPatrick Sanan /*@
12403ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
12413ad4599aSBarry Smith 
1242a5bc1bf3SBarry Smith     Collective on dmc
12433ad4599aSBarry Smith 
1244d8d19677SJose E. Roman     Input Parameters:
1245a5bc1bf3SBarry Smith +   dmc - the DM object
1246a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
12473ad4599aSBarry Smith 
12483ad4599aSBarry Smith     Output Parameter:
12493ad4599aSBarry Smith .  mat - the restriction
12503ad4599aSBarry Smith 
12513ad4599aSBarry Smith     Level: developer
12523ad4599aSBarry Smith 
125395452b02SPatrick Sanan     Notes:
125495452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
12553ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
12563ad4599aSBarry Smith 
1257db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12583ad4599aSBarry Smith 
12593ad4599aSBarry Smith @*/
1260a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12613ad4599aSBarry Smith {
12623ad4599aSBarry Smith   PetscFunctionBegin;
1263a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1264a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12655a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
12667a8be351SBarry Smith   PetscCheck(dmc->ops->createrestriction,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
12679566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0));
12689566063dSJacob Faibussowitsch   PetscCall((*dmc->ops->createrestriction)(dmc,dmf,mat));
12699566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0));
12703ad4599aSBarry Smith   PetscFunctionReturn(0);
12713ad4599aSBarry Smith }
12723ad4599aSBarry Smith 
127347c6ae99SBarry Smith /*@
12748472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
127547c6ae99SBarry Smith 
1276a5bc1bf3SBarry Smith     Collective on dac
127747c6ae99SBarry Smith 
1278d8d19677SJose E. Roman     Input Parameters:
1279a5bc1bf3SBarry Smith +   dac - the DM object
1280a5bc1bf3SBarry Smith -   daf - the second, finer DM object
128147c6ae99SBarry Smith 
128247c6ae99SBarry Smith     Output Parameter:
12836dbf9973SLawrence Mitchell .   mat - the injection
128447c6ae99SBarry Smith 
128547c6ae99SBarry Smith     Level: developer
128647c6ae99SBarry Smith 
128795452b02SPatrick Sanan    Notes:
128895452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
128985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
129085afcc9aSBarry Smith 
1291db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`
129247c6ae99SBarry Smith 
129347c6ae99SBarry Smith @*/
1294a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
129547c6ae99SBarry Smith {
129647c6ae99SBarry Smith   PetscFunctionBegin;
1297a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1298a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
12995a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
13007a8be351SBarry Smith   PetscCheck(dac->ops->createinjection,PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
13019566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0));
13029566063dSJacob Faibussowitsch   PetscCall((*dac->ops->createinjection)(dac,daf,mat));
13039566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0));
130447c6ae99SBarry Smith   PetscFunctionReturn(0);
130547c6ae99SBarry Smith }
130647c6ae99SBarry Smith 
1307b412c318SBarry Smith /*@
1308bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1309bd041c0cSMatthew G. Knepley 
1310a5bc1bf3SBarry Smith   Collective on dac
1311bd041c0cSMatthew G. Knepley 
1312d8d19677SJose E. Roman   Input Parameters:
1313b4937a87SMatthew G. Knepley + dmc - the target DM object
1314b4937a87SMatthew G. Knepley - dmf - the source DM object
1315bd041c0cSMatthew G. Knepley 
1316bd041c0cSMatthew G. Knepley   Output Parameter:
1317b4937a87SMatthew G. Knepley . mat - the mass matrix
1318bd041c0cSMatthew G. Knepley 
1319bd041c0cSMatthew G. Knepley   Level: developer
1320bd041c0cSMatthew G. Knepley 
1321db781477SPatrick Sanan .seealso `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1322bd041c0cSMatthew G. Knepley @*/
1323b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1324bd041c0cSMatthew G. Knepley {
1325bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1326b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1327b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13285a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
13292c71b3e2SJacob Faibussowitsch   PetscCheck(dmc->ops->createmassmatrix,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dmc)->type_name);
13305b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix,0,0,0,0));
13319566063dSJacob Faibussowitsch   PetscCall((*dmc->ops->createmassmatrix)(dmc, dmf, mat));
13325b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix,0,0,0,0));
1333b4937a87SMatthew G. Knepley   PetscFunctionReturn(0);
1334b4937a87SMatthew G. Knepley }
1335b4937a87SMatthew G. Knepley 
1336b4937a87SMatthew G. Knepley /*@
1337b4937a87SMatthew G. Knepley   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given DM
1338b4937a87SMatthew G. Knepley 
1339b4937a87SMatthew G. Knepley   Collective on dm
1340b4937a87SMatthew G. Knepley 
1341b4937a87SMatthew G. Knepley   Input Parameter:
1342b4937a87SMatthew G. Knepley . dm - the DM object
1343b4937a87SMatthew G. Knepley 
1344b4937a87SMatthew G. Knepley   Output Parameter:
1345b4937a87SMatthew G. Knepley . lm - the lumped mass matrix
1346b4937a87SMatthew G. Knepley 
1347b4937a87SMatthew G. Knepley   Level: developer
1348b4937a87SMatthew G. Knepley 
1349db781477SPatrick Sanan .seealso `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1350b4937a87SMatthew G. Knepley @*/
1351b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1352b4937a87SMatthew G. Knepley {
1353b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1354b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1355b4937a87SMatthew G. Knepley   PetscValidPointer(lm,2);
13562c71b3e2SJacob Faibussowitsch   PetscCheck(dm->ops->createmassmatrixlumped,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrixLumped",((PetscObject)dm)->type_name);
13579566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createmassmatrixlumped)(dm, lm));
1358bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1359bd041c0cSMatthew G. Knepley }
1360bd041c0cSMatthew G. Knepley 
1361bd041c0cSMatthew G. Knepley /*@
1362b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
136347c6ae99SBarry Smith 
1364d083f849SBarry Smith     Collective on dm
136547c6ae99SBarry Smith 
1366d8d19677SJose E. Roman     Input Parameters:
136747c6ae99SBarry Smith +   dm - the DM object
13685bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
136947c6ae99SBarry Smith 
137047c6ae99SBarry Smith     Output Parameter:
137147c6ae99SBarry Smith .   coloring - the coloring
137247c6ae99SBarry Smith 
1373ec5066bdSBarry Smith     Notes:
1374ec5066bdSBarry Smith        Coloring of matrices can be computed directly from the sparse matrix nonzero structure via the MatColoring object or from the mesh from which the
1375ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1376ec5066bdSBarry Smith 
1377ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1378ec5066bdSBarry Smith 
137947c6ae99SBarry Smith     Level: developer
138047c6ae99SBarry Smith 
1381db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
138247c6ae99SBarry Smith 
1383aab9d709SJed Brown @*/
1384b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
138547c6ae99SBarry Smith {
138647c6ae99SBarry Smith   PetscFunctionBegin;
1387171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13885a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
13897a8be351SBarry Smith   PetscCheck(dm->ops->getcoloring,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
13909566063dSJacob Faibussowitsch   PetscCall((*dm->ops->getcoloring)(dm,ctype,coloring));
139147c6ae99SBarry Smith   PetscFunctionReturn(0);
139247c6ae99SBarry Smith }
139347c6ae99SBarry Smith 
1394b412c318SBarry Smith /*@
13958472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
139647c6ae99SBarry Smith 
1397d083f849SBarry Smith     Collective on dm
139847c6ae99SBarry Smith 
139947c6ae99SBarry Smith     Input Parameter:
1400b412c318SBarry Smith .   dm - the DM object
140147c6ae99SBarry Smith 
140247c6ae99SBarry Smith     Output Parameter:
140347c6ae99SBarry Smith .   mat - the empty Jacobian
140447c6ae99SBarry Smith 
1405073dac72SJed Brown     Level: beginner
140647c6ae99SBarry Smith 
1407f27dd7c6SMatthew G. Knepley     Options Database Keys:
14085b8ffe73SMark Adams . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix() and DMCreateMassMatrix(), but do not fill it with zeros
1409f27dd7c6SMatthew G. Knepley 
141095452b02SPatrick Sanan     Notes:
141195452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
141294013140SBarry Smith        do not need to do it yourself.
141394013140SBarry Smith 
141494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
14157889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
141694013140SBarry Smith 
141794013140SBarry Smith        For structured grid problems, when you call MatView() on this matrix it is displayed using the global natural ordering, NOT in the ordering used
141894013140SBarry Smith        internally by PETSc.
141994013140SBarry Smith 
142094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1421aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
142294013140SBarry Smith 
1423db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
142447c6ae99SBarry Smith 
1425aab9d709SJed Brown @*/
1426b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
142747c6ae99SBarry Smith {
142847c6ae99SBarry Smith   PetscFunctionBegin;
1429171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1430064a246eSJacob Faibussowitsch   PetscValidPointer(mat,2);
14317a8be351SBarry Smith   PetscCheck(dm->ops->creatematrix,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
14329566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14339566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix,0,0,0,0));
14349566063dSJacob Faibussowitsch   PetscCall((*dm->ops->creatematrix)(dm,mat));
143576bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1436c6b011d8SStefano Zampini     DM mdm;
1437c6b011d8SStefano Zampini 
14389566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat,&mdm));
14397a8be351SBarry Smith     PetscCheck(mdm,PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the matrix",((PetscObject)dm)->type_name);
1440c6b011d8SStefano Zampini   }
1441e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1442e5e52638SMatthew G. Knepley   if (dm->Nf) {
1443e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1444649ef022SMatthew Knepley     PetscInt     Nf, f;
1445e571a35bSMatthew G. Knepley 
14469566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1447649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1448649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14499566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14509566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14519566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1452649ef022SMatthew Knepley         break;
1453e571a35bSMatthew G. Knepley       }
1454649ef022SMatthew Knepley     }
1455649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1456649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
14579566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
14589566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
14599566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1460e571a35bSMatthew G. Knepley       }
1461e571a35bSMatthew G. Knepley     }
1462e571a35bSMatthew G. Knepley   }
14639566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix,0,0,0,0));
146447c6ae99SBarry Smith   PetscFunctionReturn(0);
146547c6ae99SBarry Smith }
146647c6ae99SBarry Smith 
1467732e2eb9SMatthew G Knepley /*@
1468aa0f6e3cSJed Brown   DMSetMatrixPreallocateSkip - When DMCreateMatrix() is called the matrix sizes and ISLocalToGlobalMapping will be
1469aa0f6e3cSJed Brown   properly set, but the entries will not be preallocated. This is most useful to reduce initialization costs when
1470aa0f6e3cSJed Brown   MatSetPreallocationCOO() and MatSetValuesCOO() will be used.
1471aa0f6e3cSJed Brown 
1472aa0f6e3cSJed Brown   Logically Collective on dm
1473aa0f6e3cSJed Brown 
1474aa0f6e3cSJed Brown   Input Parameters:
1475aa0f6e3cSJed Brown + dm - the DM
1476aa0f6e3cSJed Brown - skip - PETSC_TRUE to skip preallocation
1477aa0f6e3cSJed Brown 
1478aa0f6e3cSJed Brown   Level: developer
1479aa0f6e3cSJed Brown 
1480db781477SPatrick Sanan .seealso `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`
1481aa0f6e3cSJed Brown @*/
1482aa0f6e3cSJed Brown PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1483aa0f6e3cSJed Brown {
1484aa0f6e3cSJed Brown   PetscFunctionBegin;
1485aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1486aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
1487aa0f6e3cSJed Brown   PetscFunctionReturn(0);
1488aa0f6e3cSJed Brown }
1489aa0f6e3cSJed Brown 
1490aa0f6e3cSJed Brown /*@
1491950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1492732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1493732e2eb9SMatthew G Knepley 
1494d083f849SBarry Smith   Logically Collective on dm
1495732e2eb9SMatthew G Knepley 
1496d8d19677SJose E. Roman   Input Parameters:
1497732e2eb9SMatthew G Knepley + dm - the DM
1498732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1499732e2eb9SMatthew G Knepley 
1500732e2eb9SMatthew G Knepley   Level: developer
1501f27dd7c6SMatthew G. Knepley 
1502f27dd7c6SMatthew G. Knepley   Options Database Keys:
15035b8ffe73SMark Adams . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), DMCreateMassMatrix(), but do not fill it with zeros
1504f27dd7c6SMatthew G. Knepley 
1505db781477SPatrick Sanan .seealso `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`
1506732e2eb9SMatthew G Knepley @*/
1507732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1508732e2eb9SMatthew G Knepley {
1509732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1510732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1511732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1512732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1513732e2eb9SMatthew G Knepley }
1514732e2eb9SMatthew G Knepley 
1515b06ff27eSHong Zhang /*@
1516b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1517b06ff27eSHong Zhang     but the array for values will not be allocated.
1518b06ff27eSHong Zhang 
1519d083f849SBarry Smith   Logically Collective on dm
1520b06ff27eSHong Zhang 
1521d8d19677SJose E. Roman   Input Parameters:
1522b06ff27eSHong Zhang + dm - the DM
1523b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1524b06ff27eSHong Zhang 
1525b06ff27eSHong Zhang   Level: developer
1526db781477SPatrick Sanan .seealso `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`
1527b06ff27eSHong Zhang @*/
1528b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1529b06ff27eSHong Zhang {
1530b06ff27eSHong Zhang   PetscFunctionBegin;
1531b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1532b06ff27eSHong Zhang   dm->structure_only = only;
1533b06ff27eSHong Zhang   PetscFunctionReturn(0);
1534b06ff27eSHong Zhang }
1535b06ff27eSHong Zhang 
1536a89ea682SMatthew G Knepley /*@C
1537aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1538a89ea682SMatthew G Knepley 
1539a89ea682SMatthew G Knepley   Not Collective
1540a89ea682SMatthew G Knepley 
1541a89ea682SMatthew G Knepley   Input Parameters:
1542a89ea682SMatthew G Knepley + dm - the DM object
1543a5b23f4aSJose E. Roman . count - The minimum size
154469291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1545a89ea682SMatthew G Knepley 
1546a89ea682SMatthew G Knepley   Output Parameter:
1547a89ea682SMatthew G Knepley . array - the work array
1548a89ea682SMatthew G Knepley 
1549a89ea682SMatthew G Knepley   Level: developer
1550a89ea682SMatthew G Knepley 
1551db781477SPatrick Sanan .seealso `DMDestroy()`, `DMCreate()`
1552a89ea682SMatthew G Knepley @*/
155369291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1554a89ea682SMatthew G Knepley {
1555aa1993deSMatthew G Knepley   DMWorkLink     link;
155669291d52SBarry Smith   PetscMPIInt    dsize;
1557a89ea682SMatthew G Knepley 
1558a89ea682SMatthew G Knepley   PetscFunctionBegin;
1559a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1560aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1561aa1993deSMatthew G Knepley   if (dm->workin) {
1562aa1993deSMatthew G Knepley     link       = dm->workin;
1563aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1564aa1993deSMatthew G Knepley   } else {
15659566063dSJacob Faibussowitsch     PetscCall(PetscNewLog(dm,&link));
1566a89ea682SMatthew G Knepley   }
15679566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dtype,&dsize));
15685056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
15699566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
15709566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize*count,&link->mem));
1571854ce69bSBarry Smith     link->bytes = dsize*count;
1572aa1993deSMatthew G Knepley   }
1573aa1993deSMatthew G Knepley   link->next   = dm->workout;
1574aa1993deSMatthew G Knepley   dm->workout  = link;
1575cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
157600d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
157700d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
157800d952a4SJed Brown #endif
1579aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1580a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1581a89ea682SMatthew G Knepley }
1582a89ea682SMatthew G Knepley 
1583aa1993deSMatthew G Knepley /*@C
1584aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1585aa1993deSMatthew G Knepley 
1586aa1993deSMatthew G Knepley   Not Collective
1587aa1993deSMatthew G Knepley 
1588aa1993deSMatthew G Knepley   Input Parameters:
1589aa1993deSMatthew G Knepley + dm - the DM object
1590a5b23f4aSJose E. Roman . count - The minimum size
159169291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1592aa1993deSMatthew G Knepley 
1593aa1993deSMatthew G Knepley   Output Parameter:
1594aa1993deSMatthew G Knepley . array - the work array
1595aa1993deSMatthew G Knepley 
1596aa1993deSMatthew G Knepley   Level: developer
1597aa1993deSMatthew G Knepley 
159895452b02SPatrick Sanan   Developer Notes:
159995452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1600147403d9SBarry Smith 
1601db781477SPatrick Sanan .seealso `DMDestroy()`, `DMCreate()`
1602aa1993deSMatthew G Knepley @*/
160369291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1604aa1993deSMatthew G Knepley {
1605aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1606aa1993deSMatthew G Knepley 
1607aa1993deSMatthew G Knepley   PetscFunctionBegin;
1608aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1609aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1610aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1611aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1612aa1993deSMatthew G Knepley       *p           = link->next;
1613aa1993deSMatthew G Knepley       link->next   = dm->workin;
1614aa1993deSMatthew G Knepley       dm->workin   = link;
16150298fd71SBarry Smith       *(void**)mem = NULL;
1616aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1617aa1993deSMatthew G Knepley     }
1618aa1993deSMatthew G Knepley   }
1619aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1620aa1993deSMatthew G Knepley }
1621e7c4fc90SDmitry Karpeev 
16228cda7954SMatthew G. Knepley /*@C
1623147403d9SBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field when function spaces are joined or split, such as in DMCreateSubDM()
16248cda7954SMatthew G. Knepley 
16258cda7954SMatthew G. Knepley   Logically collective on DM
16268cda7954SMatthew G. Knepley 
16278cda7954SMatthew G. Knepley   Input Parameters:
16288cda7954SMatthew G. Knepley + dm     - The DM
16298cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16308cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
16318cda7954SMatthew G. Knepley 
1632147403d9SBarry Smith   Calling sequence of nullsp:
1633147403d9SBarry Smith .vb
1634147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1635147403d9SBarry Smith .ve
1636147403d9SBarry Smith +  dm        - The present DM
1637147403d9SBarry Smith .  origField - The field number given above, in the original DM
1638147403d9SBarry Smith .  field     - The field number in dm
1639147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16408cda7954SMatthew G. Knepley 
16418cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16428cda7954SMatthew G. Knepley 
164349762cbcSSatish Balay    Level: intermediate
164449762cbcSSatish Balay 
1645db781477SPatrick Sanan .seealso: `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1646147403d9SBarry Smith @*/
1647147403d9SBarry Smith PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace*))
1648435a35e8SMatthew G Knepley {
1649435a35e8SMatthew G Knepley   PetscFunctionBegin;
1650435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16517a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1652435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1653435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1654435a35e8SMatthew G Knepley }
1655435a35e8SMatthew G Knepley 
16568cda7954SMatthew G. Knepley /*@C
16578cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
16588cda7954SMatthew G. Knepley 
16598cda7954SMatthew G. Knepley   Not collective
16608cda7954SMatthew G. Knepley 
16618cda7954SMatthew G. Knepley   Input Parameters:
16628cda7954SMatthew G. Knepley + dm     - The DM
16638cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16648cda7954SMatthew G. Knepley 
16658cda7954SMatthew G. Knepley   Output Parameter:
16668cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
16678cda7954SMatthew G. Knepley 
1668147403d9SBarry Smith   Calling sequence of nullsp:
1669147403d9SBarry Smith .vb
1670147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1671147403d9SBarry Smith .ve
1672147403d9SBarry Smith +  dm        - The present DM
1673147403d9SBarry Smith .  origField - The field number given above, in the original DM
1674147403d9SBarry Smith .  field     - The field number in dm
1675147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16768cda7954SMatthew G. Knepley 
16778cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16788cda7954SMatthew G. Knepley 
167949762cbcSSatish Balay    Level: intermediate
168049762cbcSSatish Balay 
1681db781477SPatrick Sanan .seealso: `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1682147403d9SBarry Smith @*/
1683147403d9SBarry Smith PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
16840a50eb56SMatthew G. Knepley {
16850a50eb56SMatthew G. Knepley   PetscFunctionBegin;
16860a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1687f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
16887a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
16890a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
16900a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
16910a50eb56SMatthew G. Knepley }
16920a50eb56SMatthew G. Knepley 
16938cda7954SMatthew G. Knepley /*@C
16948cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
16958cda7954SMatthew G. Knepley 
16968cda7954SMatthew G. Knepley   Logically collective on DM
16978cda7954SMatthew G. Knepley 
16988cda7954SMatthew G. Knepley   Input Parameters:
16998cda7954SMatthew G. Knepley + dm     - The DM
17008cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17018cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
17028cda7954SMatthew G. Knepley 
1703147403d9SBarry Smith   Calling sequence of nullsp:
1704147403d9SBarry Smith .vb
1705147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1706147403d9SBarry Smith .ve
1707147403d9SBarry Smith +  dm        - The present DM
1708147403d9SBarry Smith .  origField - The field number given above, in the original DM
1709147403d9SBarry Smith .  field     - The field number in dm
1710147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17118cda7954SMatthew G. Knepley 
17128cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
17138cda7954SMatthew G. Knepley 
171449762cbcSSatish Balay    Level: intermediate
171549762cbcSSatish Balay 
1716db781477SPatrick Sanan .seealso: `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1717147403d9SBarry Smith @*/
1718147403d9SBarry Smith PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1719f9d4088aSMatthew G. Knepley {
1720f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1721f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17227a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1723f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1724f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1725f9d4088aSMatthew G. Knepley }
1726f9d4088aSMatthew G. Knepley 
17278cda7954SMatthew G. Knepley /*@C
17288cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
17298cda7954SMatthew G. Knepley 
17308cda7954SMatthew G. Knepley   Not collective
17318cda7954SMatthew G. Knepley 
17328cda7954SMatthew G. Knepley   Input Parameters:
17338cda7954SMatthew G. Knepley + dm     - The DM
17348cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17358cda7954SMatthew G. Knepley 
17368cda7954SMatthew G. Knepley   Output Parameter:
17378cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
17388cda7954SMatthew G. Knepley 
1739147403d9SBarry Smith   Calling sequence of nullsp:
1740147403d9SBarry Smith .vb
1741147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1742147403d9SBarry Smith .ve
1743147403d9SBarry Smith +  dm        - The present DM
1744147403d9SBarry Smith .  origField - The field number given above, in the original DM
1745147403d9SBarry Smith .  field     - The field number in dm
1746147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17478cda7954SMatthew G. Knepley 
17488cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
17498cda7954SMatthew G. Knepley 
175049762cbcSSatish Balay    Level: intermediate
175149762cbcSSatish Balay 
1752db781477SPatrick Sanan .seealso: `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1753147403d9SBarry Smith @*/
1754147403d9SBarry Smith PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1755f9d4088aSMatthew G. Knepley {
1756f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1757f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1758f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
17597a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1760f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1761f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1762f9d4088aSMatthew G. Knepley }
1763f9d4088aSMatthew G. Knepley 
17644f3b5142SJed Brown /*@C
17654d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
17664d343eeaSMatthew G Knepley 
17674d343eeaSMatthew G Knepley   Not collective
17684d343eeaSMatthew G Knepley 
17694d343eeaSMatthew G Knepley   Input Parameter:
17704d343eeaSMatthew G Knepley . dm - the DM object
17714d343eeaSMatthew G Knepley 
17724d343eeaSMatthew G Knepley   Output Parameters:
17730298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
17740298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
17750298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
17764d343eeaSMatthew G Knepley 
17774d343eeaSMatthew G Knepley   Level: intermediate
17784d343eeaSMatthew G Knepley 
177921c9b008SJed Brown   Notes:
178021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
178121c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
178221c9b008SJed Brown   PetscFree().
178321c9b008SJed Brown 
1784db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
17854d343eeaSMatthew G Knepley @*/
178637d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
17874d343eeaSMatthew G Knepley {
178837d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
17894d343eeaSMatthew G Knepley 
17904d343eeaSMatthew G Knepley   PetscFunctionBegin;
17914d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
179269ca1f37SDmitry Karpeev   if (numFields) {
1793534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
179469ca1f37SDmitry Karpeev     *numFields = 0;
179569ca1f37SDmitry Karpeev   }
179637d0c07bSMatthew G Knepley   if (fieldNames) {
179737d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
17980298fd71SBarry Smith     *fieldNames = NULL;
179969ca1f37SDmitry Karpeev   }
180069ca1f37SDmitry Karpeev   if (fields) {
180169ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
18020298fd71SBarry Smith     *fields = NULL;
180369ca1f37SDmitry Karpeev   }
18049566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
180537d0c07bSMatthew G Knepley   if (section) {
18063a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
180737d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
180837d0c07bSMatthew G Knepley 
18099566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
18109566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
18119566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices));
18129566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
181337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
181437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
18159566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
181637d0c07bSMatthew G Knepley     }
181737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
181837d0c07bSMatthew G Knepley       PetscInt gdof;
181937d0c07bSMatthew G Knepley 
18209566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
182137d0c07bSMatthew G Knepley       if (gdof > 0) {
182237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
18233a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
182437d0c07bSMatthew G Knepley 
18259566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
18269566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
18273a544194SStefano Zampini           fpdof = fdof-fcdof;
18283a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
18293a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
18303a544194SStefano Zampini             fieldNc[f] = 1;
18313a544194SStefano Zampini           }
18323a544194SStefano Zampini           fieldSizes[f] += fpdof;
183337d0c07bSMatthew G Knepley         }
183437d0c07bSMatthew G Knepley       }
183537d0c07bSMatthew G Knepley     }
183637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
18379566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
183837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
183937d0c07bSMatthew G Knepley     }
184037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
184137d0c07bSMatthew G Knepley       PetscInt gdof, goff;
184237d0c07bSMatthew G Knepley 
18439566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
184437d0c07bSMatthew G Knepley       if (gdof > 0) {
18459566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
184637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
184737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
184837d0c07bSMatthew G Knepley 
18499566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
18509566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
185137d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
185237d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
185337d0c07bSMatthew G Knepley           }
185437d0c07bSMatthew G Knepley         }
185537d0c07bSMatthew G Knepley       }
185637d0c07bSMatthew G Knepley     }
18578865f1eaSKarl Rupp     if (numFields) *numFields = nF;
185837d0c07bSMatthew G Knepley     if (fieldNames) {
18599566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
186037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
186137d0c07bSMatthew G Knepley         const char *fieldName;
186237d0c07bSMatthew G Knepley 
18639566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
18649566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]));
186537d0c07bSMatthew G Knepley       }
186637d0c07bSMatthew G Knepley     }
186737d0c07bSMatthew G Knepley     if (fields) {
18689566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
186937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
18703a544194SStefano Zampini         PetscInt bs, in[2], out[2];
18713a544194SStefano Zampini 
18729566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
18733a544194SStefano Zampini         in[0] = -fieldNc[f];
18743a544194SStefano Zampini         in[1] = fieldNc[f];
18751c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
18763a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
18779566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
187837d0c07bSMatthew G Knepley       }
187937d0c07bSMatthew G Knepley     }
18809566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes,fieldNc,fieldIndices));
18811baa6e33SBarry Smith   } else if (dm->ops->createfieldis) PetscCall((*dm->ops->createfieldis)(dm, numFields, fieldNames, fields));
18824d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
18834d343eeaSMatthew G Knepley }
18844d343eeaSMatthew G Knepley 
188516621825SDmitry Karpeev /*@C
188616621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
188716621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
188816621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1889e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1890e7c4fc90SDmitry Karpeev 
1891e7c4fc90SDmitry Karpeev   Not collective
1892e7c4fc90SDmitry Karpeev 
1893e7c4fc90SDmitry Karpeev   Input Parameter:
1894e7c4fc90SDmitry Karpeev . dm - the DM object
1895e7c4fc90SDmitry Karpeev 
1896e7c4fc90SDmitry Karpeev   Output Parameters:
18970298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
18980298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
18990298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
19000298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1901e7c4fc90SDmitry Karpeev 
1902e7c4fc90SDmitry Karpeev   Level: intermediate
1903e7c4fc90SDmitry Karpeev 
1904e7c4fc90SDmitry Karpeev   Notes:
1905e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1906e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1907e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1908e7c4fc90SDmitry Karpeev 
1909db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
1910e7c4fc90SDmitry Karpeev @*/
191116621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1912e7c4fc90SDmitry Karpeev {
1913e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1914e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19158865f1eaSKarl Rupp   if (len) {
1916534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
19178865f1eaSKarl Rupp     *len = 0;
19188865f1eaSKarl Rupp   }
19198865f1eaSKarl Rupp   if (namelist) {
19208865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1921ea78f98cSLisandro Dalcin     *namelist = NULL;
19228865f1eaSKarl Rupp   }
19238865f1eaSKarl Rupp   if (islist) {
19248865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1925ea78f98cSLisandro Dalcin     *islist = NULL;
19268865f1eaSKarl Rupp   }
19278865f1eaSKarl Rupp   if (dmlist) {
19288865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1929ea78f98cSLisandro Dalcin     *dmlist = NULL;
19308865f1eaSKarl Rupp   }
1931f3f0edfdSDmitry Karpeev   /*
1932f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1933f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1934f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1935f3f0edfdSDmitry Karpeev    */
19367a8be351SBarry Smith   PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
193716621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1938435a35e8SMatthew G Knepley     PetscSection section;
1939435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1940435a35e8SMatthew G Knepley 
19419566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
19429566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
1943435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1944f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
19459566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields,namelist));
19469566063dSJacob Faibussowitsch       if (islist)   PetscCall(PetscMalloc1(numFields,islist));
19479566063dSJacob Faibussowitsch       if (dmlist)   PetscCall(PetscMalloc1(numFields,dmlist));
1948435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1949435a35e8SMatthew G Knepley         const char *fieldName;
1950435a35e8SMatthew G Knepley 
19519566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
195203dc3394SMatthew G. Knepley         if (namelist) {
19539566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19549566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char**) &(*namelist)[f]));
1955435a35e8SMatthew G Knepley         }
195603dc3394SMatthew G. Knepley       }
1957435a35e8SMatthew G Knepley     } else {
19589566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
1959e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
19600298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1961e7c4fc90SDmitry Karpeev     }
19628865f1eaSKarl Rupp   } else {
19639566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist));
196416621825SDmitry Karpeev   }
196516621825SDmitry Karpeev   PetscFunctionReturn(0);
196616621825SDmitry Karpeev }
196716621825SDmitry Karpeev 
1968564cec59SMatthew G. Knepley /*@
1969435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1970435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1971435a35e8SMatthew G Knepley 
1972435a35e8SMatthew G Knepley   Not collective
1973435a35e8SMatthew G Knepley 
1974435a35e8SMatthew G Knepley   Input Parameters:
19752adcc780SMatthew G. Knepley + dm        - The DM object
19762adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
19772adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1978435a35e8SMatthew G Knepley 
1979435a35e8SMatthew G Knepley   Output Parameters:
19802adcc780SMatthew G. Knepley + is - The global indices for the subproblem
19812adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1982435a35e8SMatthew G Knepley 
19835d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19845d3b26e6SMatthew G. Knepley 
1985435a35e8SMatthew G Knepley   Level: intermediate
1986435a35e8SMatthew G Knepley 
1987db781477SPatrick Sanan .seealso `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
1988435a35e8SMatthew G Knepley @*/
198937bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1990435a35e8SMatthew G Knepley {
1991435a35e8SMatthew G Knepley   PetscFunctionBegin;
1992435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1993dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields,3);
19948865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
19958865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
19967a8be351SBarry Smith   PetscCheck(dm->ops->createsubdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
19979566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createsubdm)(dm, numFields, fields, is, subdm));
1998435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1999435a35e8SMatthew G Knepley }
2000435a35e8SMatthew G Knepley 
20012adcc780SMatthew G. Knepley /*@C
20022adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
20032adcc780SMatthew G. Knepley 
20042adcc780SMatthew G. Knepley   Not collective
20052adcc780SMatthew G. Knepley 
2006d8d19677SJose E. Roman   Input Parameters:
20072adcc780SMatthew G. Knepley + dms - The DM objects
20082adcc780SMatthew G. Knepley - len - The number of DMs
20092adcc780SMatthew G. Knepley 
20102adcc780SMatthew G. Knepley   Output Parameters:
2011a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
20122adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
20132adcc780SMatthew G. Knepley 
20145d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
20155d3b26e6SMatthew G. Knepley 
20162adcc780SMatthew G. Knepley   Level: intermediate
20172adcc780SMatthew G. Knepley 
2018db781477SPatrick Sanan .seealso `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
20192adcc780SMatthew G. Knepley @*/
20202adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
20212adcc780SMatthew G. Knepley {
20222adcc780SMatthew G. Knepley   PetscInt       i;
20232adcc780SMatthew G. Knepley 
20242adcc780SMatthew G. Knepley   PetscFunctionBegin;
20252adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
20262adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
20272adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
2028a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
202963a3b9bcSJacob Faibussowitsch   PetscCheck(len >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, len);
20302adcc780SMatthew G. Knepley   if (len) {
2031b9d85ea2SLisandro Dalcin     DM dm = dms[0];
20327a8be351SBarry Smith     PetscCheck(dm->ops->createsuperdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
20339566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createsuperdm)(dms, len, is, superdm));
20342adcc780SMatthew G. Knepley   }
20352adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
20362adcc780SMatthew G. Knepley }
20372adcc780SMatthew G. Knepley 
203816621825SDmitry Karpeev /*@C
20398d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
20408d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
20418d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
20428d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
20438d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
204416621825SDmitry Karpeev 
204516621825SDmitry Karpeev   Not collective
204616621825SDmitry Karpeev 
204716621825SDmitry Karpeev   Input Parameter:
204816621825SDmitry Karpeev . dm - the DM object
204916621825SDmitry Karpeev 
205016621825SDmitry Karpeev   Output Parameters:
20510298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
20520298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
20530298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
20540298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
20550298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
205616621825SDmitry Karpeev 
205716621825SDmitry Karpeev   Level: intermediate
205816621825SDmitry Karpeev 
205916621825SDmitry Karpeev   Notes:
206016621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
206116621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
206216621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
206316621825SDmitry Karpeev 
2064db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldDecomposition()`
206516621825SDmitry Karpeev @*/
20668d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
206716621825SDmitry Karpeev {
2068be081cd6SPeter Brune   DMSubDomainHookLink link;
2069be081cd6SPeter Brune   PetscInt            i,l;
207016621825SDmitry Karpeev 
207116621825SDmitry Karpeev   PetscFunctionBegin;
207216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2073dadcf809SJacob Faibussowitsch   if (len)           {PetscValidIntPointer(len,2);            *len         = 0;}
20740298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
20750298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
20760298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
20770298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
2078f3f0edfdSDmitry Karpeev   /*
2079f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2080f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2081f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2082f3f0edfdSDmitry Karpeev    */
20837a8be351SBarry Smith   PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
208416621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
20859566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist));
208614a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2087f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2088be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2089be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
20909566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm,(*dmlist)[i],link->ctx));
2091be081cd6SPeter Brune         }
2092648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2093e7c4fc90SDmitry Karpeev       }
209414a18fd3SPeter Brune     }
209514a18fd3SPeter Brune     if (len) *len = l;
209614a18fd3SPeter Brune   }
2097e30e807fSPeter Brune   PetscFunctionReturn(0);
2098e30e807fSPeter Brune }
2099e30e807fSPeter Brune 
2100e30e807fSPeter Brune /*@C
2101e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2102e30e807fSPeter Brune 
2103e30e807fSPeter Brune   Not collective
2104e30e807fSPeter Brune 
2105e30e807fSPeter Brune   Input Parameters:
2106e30e807fSPeter Brune + dm - the DM object
2107e30e807fSPeter Brune . n  - the number of subdomain scatters
2108e30e807fSPeter Brune - subdms - the local subdomains
2109e30e807fSPeter Brune 
2110e30e807fSPeter Brune   Output Parameters:
21116b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2112e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2113e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2114e30e807fSPeter Brune 
211595452b02SPatrick Sanan   Notes:
211695452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2117e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2118e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2119e30e807fSPeter Brune   solution and residual data.
2120e30e807fSPeter Brune 
2121e30e807fSPeter Brune   Level: developer
2122e30e807fSPeter Brune 
2123db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2124e30e807fSPeter Brune @*/
2125e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2126e30e807fSPeter Brune {
2127e30e807fSPeter Brune   PetscFunctionBegin;
2128e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2129e30e807fSPeter Brune   PetscValidPointer(subdms,3);
21307a8be351SBarry Smith   PetscCheck(dm->ops->createddscatters,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
21319566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat));
2132e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2133e7c4fc90SDmitry Karpeev }
2134e7c4fc90SDmitry Karpeev 
213547c6ae99SBarry Smith /*@
213647c6ae99SBarry Smith   DMRefine - Refines a DM object
213747c6ae99SBarry Smith 
2138d083f849SBarry Smith   Collective on dm
213947c6ae99SBarry Smith 
2140d8d19677SJose E. Roman   Input Parameters:
214147c6ae99SBarry Smith + dm   - the DM object
214291d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
214347c6ae99SBarry Smith 
214447c6ae99SBarry Smith   Output Parameter:
21450298fd71SBarry Smith . dmf - the refined DM, or NULL
2146ae0a1c52SMatthew G Knepley 
2147f27dd7c6SMatthew G. Knepley   Options Database Keys:
2148412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2149412e9a14SMatthew G. Knepley 
21500298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
215147c6ae99SBarry Smith 
215247c6ae99SBarry Smith   Level: developer
215347c6ae99SBarry Smith 
2154db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
215547c6ae99SBarry Smith @*/
21567087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
215747c6ae99SBarry Smith {
2158c833c3b5SJed Brown   DMRefineHookLink link;
215947c6ae99SBarry Smith 
216047c6ae99SBarry Smith   PetscFunctionBegin;
2161732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21627a8be351SBarry Smith   PetscCheck(dm->ops->refine,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
21639566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine,dm,0,0,0));
21649566063dSJacob Faibussowitsch   PetscCall((*dm->ops->refine)(dm,comm,dmf));
21654057135bSMatthew G Knepley   if (*dmf) {
216643842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
21678865f1eaSKarl Rupp 
21689566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf));
21698865f1eaSKarl Rupp 
2170644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
21710598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2172656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
21738865f1eaSKarl Rupp 
21749566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf,dm->mattype));
2175c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
21761baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm,*dmf,link->ctx));
2177c833c3b5SJed Brown     }
2178c833c3b5SJed Brown   }
21799566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine,dm,0,0,0));
2180c833c3b5SJed Brown   PetscFunctionReturn(0);
2181c833c3b5SJed Brown }
2182c833c3b5SJed Brown 
2183bb9467b5SJed Brown /*@C
2184c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2185c833c3b5SJed Brown 
2186c833c3b5SJed Brown    Logically Collective
2187c833c3b5SJed Brown 
21884165533cSJose E. Roman    Input Parameters:
2189c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2190c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2191c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21920298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2193c833c3b5SJed Brown 
2194c833c3b5SJed Brown    Calling sequence of refinehook:
2195c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2196c833c3b5SJed Brown 
2197c833c3b5SJed Brown +  coarse - coarse level DM
2198c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2199c833c3b5SJed Brown -  ctx - optional user-defined function context
2200c833c3b5SJed Brown 
2201c833c3b5SJed Brown    Calling sequence for interphook:
2202c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2203c833c3b5SJed Brown 
2204c833c3b5SJed Brown +  coarse - coarse level DM
2205c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2206c833c3b5SJed Brown .  fine - fine level DM to update
2207c833c3b5SJed Brown -  ctx - optional user-defined function context
2208c833c3b5SJed Brown 
2209c833c3b5SJed Brown    Level: advanced
2210c833c3b5SJed Brown 
2211c833c3b5SJed Brown    Notes:
2212c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2213c833c3b5SJed Brown 
2214c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2215c833c3b5SJed Brown 
2216bb9467b5SJed Brown    This function is currently not available from Fortran.
2217bb9467b5SJed Brown 
2218db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2219c833c3b5SJed Brown @*/
2220c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2221c833c3b5SJed Brown {
2222c833c3b5SJed Brown   DMRefineHookLink link,*p;
2223c833c3b5SJed Brown 
2224c833c3b5SJed Brown   PetscFunctionBegin;
2225c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22263d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
22273d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
22283d8e3701SJed Brown   }
22299566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2230c833c3b5SJed Brown   link->refinehook = refinehook;
2231c833c3b5SJed Brown   link->interphook = interphook;
2232c833c3b5SJed Brown   link->ctx        = ctx;
22330298fd71SBarry Smith   link->next       = NULL;
2234c833c3b5SJed Brown   *p               = link;
2235c833c3b5SJed Brown   PetscFunctionReturn(0);
2236c833c3b5SJed Brown }
2237c833c3b5SJed Brown 
22383d8e3701SJed Brown /*@C
22393d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
22403d8e3701SJed Brown 
22413d8e3701SJed Brown    Logically Collective
22423d8e3701SJed Brown 
22434165533cSJose E. Roman    Input Parameters:
22443d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
22453d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
22463d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
22473d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
22483d8e3701SJed Brown 
22493d8e3701SJed Brown    Level: advanced
22503d8e3701SJed Brown 
22513d8e3701SJed Brown    Notes:
22523d8e3701SJed Brown    This function does nothing if the hook is not in the list.
22533d8e3701SJed Brown 
22543d8e3701SJed Brown    This function is currently not available from Fortran.
22553d8e3701SJed Brown 
2256db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
22573d8e3701SJed Brown @*/
22583d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
22593d8e3701SJed Brown {
22603d8e3701SJed Brown   DMRefineHookLink link,*p;
22613d8e3701SJed Brown 
22623d8e3701SJed Brown   PetscFunctionBegin;
22633d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22643d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
22653d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
22663d8e3701SJed Brown       link = *p;
22673d8e3701SJed Brown       *p = link->next;
22689566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
22693d8e3701SJed Brown       break;
22703d8e3701SJed Brown     }
22713d8e3701SJed Brown   }
22723d8e3701SJed Brown   PetscFunctionReturn(0);
22733d8e3701SJed Brown }
22743d8e3701SJed Brown 
2275c833c3b5SJed Brown /*@
2276c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2277c833c3b5SJed Brown 
2278c833c3b5SJed Brown    Collective if any hooks are
2279c833c3b5SJed Brown 
22804165533cSJose E. Roman    Input Parameters:
2281c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2282e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2283c833c3b5SJed Brown -  fine - finer DM to update
2284c833c3b5SJed Brown 
2285c833c3b5SJed Brown    Level: developer
2286c833c3b5SJed Brown 
2287db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `MatInterpolate()`
2288c833c3b5SJed Brown @*/
2289c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2290c833c3b5SJed Brown {
2291c833c3b5SJed Brown   DMRefineHookLink link;
2292c833c3b5SJed Brown 
2293c833c3b5SJed Brown   PetscFunctionBegin;
2294c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
22951baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse,interp,fine,link->ctx));
22964057135bSMatthew G Knepley   }
229747c6ae99SBarry Smith   PetscFunctionReturn(0);
229847c6ae99SBarry Smith }
229947c6ae99SBarry Smith 
2300eb3f98d2SBarry Smith /*@
23011f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
23021f3379b2SToby Isaac 
23031f3379b2SToby Isaac    Collective on DM
23041f3379b2SToby Isaac 
23054165533cSJose E. Roman    Input Parameters:
23061f3379b2SToby Isaac +  coarse - coarse DM
23071f3379b2SToby Isaac .  fine   - fine DM
23081f3379b2SToby Isaac .  interp - (optional) the matrix computed by DMCreateInterpolation().  Implementations may not need this, but if it
23091f3379b2SToby Isaac             is available it can avoid some recomputation.  If it is provided, MatInterpolate() will be used if
23101f3379b2SToby Isaac             the coarse DM does not have a specialized implementation.
23111f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
23121f3379b2SToby Isaac 
23134165533cSJose E. Roman    Output Parameter:
23141f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
23151f3379b2SToby Isaac 
23161f3379b2SToby Isaac    Level: developer
23171f3379b2SToby Isaac 
23181f3379b2SToby Isaac    Note: This function exists because the interpolation of a solution vector between meshes is not always a linear
23191f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
23201f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
23211f3379b2SToby Isaac    slope-limiting reconstruction.
23221f3379b2SToby Isaac 
2323db781477SPatrick Sanan .seealso `DMInterpolate()`, `DMCreateInterpolation()`
23241f3379b2SToby Isaac @*/
23251f3379b2SToby Isaac PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
23261f3379b2SToby Isaac {
23271f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM,DM,Mat,Vec,Vec) = NULL;
23281f3379b2SToby Isaac 
23291f3379b2SToby Isaac   PetscFunctionBegin;
23301f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
23311f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp,MAT_CLASSID,3);
23321f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol,VEC_CLASSID,4);
23331f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol,VEC_CLASSID,5);
23341f3379b2SToby Isaac 
23359566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse,"DMInterpolateSolution_C", &interpsol));
23361f3379b2SToby Isaac   if (interpsol) {
23379566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
23381f3379b2SToby Isaac   } else if (interp) {
23399566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
234098921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
23411f3379b2SToby Isaac   PetscFunctionReturn(0);
23421f3379b2SToby Isaac }
23431f3379b2SToby Isaac 
23441f3379b2SToby Isaac /*@
2345aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2346eb3f98d2SBarry Smith 
2347eb3f98d2SBarry Smith     Not Collective
2348eb3f98d2SBarry Smith 
2349eb3f98d2SBarry Smith     Input Parameter:
2350eb3f98d2SBarry Smith .   dm - the DM object
2351eb3f98d2SBarry Smith 
2352eb3f98d2SBarry Smith     Output Parameter:
2353eb3f98d2SBarry Smith .   level - number of refinements
2354eb3f98d2SBarry Smith 
2355eb3f98d2SBarry Smith     Level: developer
2356eb3f98d2SBarry Smith 
2357db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2358eb3f98d2SBarry Smith 
2359eb3f98d2SBarry Smith @*/
2360eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2361eb3f98d2SBarry Smith {
2362eb3f98d2SBarry Smith   PetscFunctionBegin;
2363eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2364eb3f98d2SBarry Smith   *level = dm->levelup;
2365eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2366eb3f98d2SBarry Smith }
2367eb3f98d2SBarry Smith 
2368fef3a512SBarry Smith /*@
2369aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2370fef3a512SBarry Smith 
2371fef3a512SBarry Smith     Not Collective
2372fef3a512SBarry Smith 
2373d8d19677SJose E. Roman     Input Parameters:
2374fef3a512SBarry Smith +   dm - the DM object
2375fef3a512SBarry Smith -   level - number of refinements
2376fef3a512SBarry Smith 
2377fef3a512SBarry Smith     Level: advanced
2378fef3a512SBarry Smith 
237995452b02SPatrick Sanan     Notes:
238095452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2381fef3a512SBarry Smith 
2382db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2383fef3a512SBarry Smith 
2384fef3a512SBarry Smith @*/
2385fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2386fef3a512SBarry Smith {
2387fef3a512SBarry Smith   PetscFunctionBegin;
2388fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2389fef3a512SBarry Smith   dm->levelup = level;
2390fef3a512SBarry Smith   PetscFunctionReturn(0);
2391fef3a512SBarry Smith }
2392fef3a512SBarry Smith 
2393d410b0cfSMatthew G. Knepley /*@
2394d410b0cfSMatthew G. Knepley   DMExtrude - Extrude a DM object from a surface
2395d410b0cfSMatthew G. Knepley 
2396d410b0cfSMatthew G. Knepley   Collective on dm
2397d410b0cfSMatthew G. Knepley 
2398f1a722f8SMatthew G. Knepley   Input Parameters:
2399d410b0cfSMatthew G. Knepley + dm     - the DM object
2400d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2401d410b0cfSMatthew G. Knepley 
2402d410b0cfSMatthew G. Knepley   Output Parameter:
2403d410b0cfSMatthew G. Knepley . dme - the extruded DM, or NULL
2404d410b0cfSMatthew G. Knepley 
2405d410b0cfSMatthew G. Knepley   Note: If no extrusion was done, the return value is NULL
2406d410b0cfSMatthew G. Knepley 
2407d410b0cfSMatthew G. Knepley   Level: developer
2408d410b0cfSMatthew G. Knepley 
2409db781477SPatrick Sanan .seealso `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2410d410b0cfSMatthew G. Knepley @*/
2411d410b0cfSMatthew G. Knepley PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2412d410b0cfSMatthew G. Knepley {
2413d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2414d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24157a8be351SBarry Smith   PetscCheck(dm->ops->extrude,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "DM type %s does not implement DMExtrude", ((PetscObject) dm)->type_name);
24169566063dSJacob Faibussowitsch   PetscCall((*dm->ops->extrude)(dm, layers, dme));
2417d410b0cfSMatthew G. Knepley   if (*dme) {
2418d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
24199566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject) dm, (PetscObject) *dme));
2420d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
24219566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2422d410b0cfSMatthew G. Knepley   }
2423d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2424d410b0cfSMatthew G. Knepley }
2425d410b0cfSMatthew G. Knepley 
2426ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2427ca3d3a14SMatthew G. Knepley {
2428ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2429ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2430ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2431ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2432ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2433ca3d3a14SMatthew G. Knepley }
2434ca3d3a14SMatthew G. Knepley 
2435ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2436ca3d3a14SMatthew G. Knepley {
2437ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2438ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2439ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2440ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2441ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2442ca3d3a14SMatthew G. Knepley }
2443ca3d3a14SMatthew G. Knepley 
2444ca3d3a14SMatthew G. Knepley /*@
2445c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2446ca3d3a14SMatthew G. Knepley 
2447ca3d3a14SMatthew G. Knepley   Input Parameter:
2448ca3d3a14SMatthew G. Knepley . dm - The DM
2449ca3d3a14SMatthew G. Knepley 
2450ca3d3a14SMatthew G. Knepley   Output Parameter:
2451ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2452ca3d3a14SMatthew G. Knepley 
2453ca3d3a14SMatthew G. Knepley   Level: developer
2454ca3d3a14SMatthew G. Knepley 
2455db781477SPatrick Sanan .seealso: `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2456ca3d3a14SMatthew G. Knepley @*/
2457ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2458ca3d3a14SMatthew G. Knepley {
2459ca3d3a14SMatthew G. Knepley   Vec            tv;
2460ca3d3a14SMatthew G. Knepley 
2461ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2462ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2463534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
24649566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2465ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2466ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2467ca3d3a14SMatthew G. Knepley }
2468ca3d3a14SMatthew G. Knepley 
2469ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2470ca3d3a14SMatthew G. Knepley {
2471ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2472ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2473ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2474ca3d3a14SMatthew G. Knepley 
2475ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
24769566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
24779566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
24789566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
24799566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
24809566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
24819566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
24829566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
24839566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2484ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
24859566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2486ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2487ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2488ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
24899566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2490ca3d3a14SMatthew G. Knepley       if (!dof) continue;
24919566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
24929566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2493ca3d3a14SMatthew G. Knepley     }
2494ca3d3a14SMatthew G. Knepley   }
24959566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
24969566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
24979566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2498ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2499ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
25009566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2501ca3d3a14SMatthew G. Knepley       if (dof) {
2502ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2503ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2504ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2505ca3d3a14SMatthew G. Knepley 
2506ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
25079566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
25089566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva));
25099566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2510ca3d3a14SMatthew G. Knepley       }
2511ca3d3a14SMatthew G. Knepley     }
2512ca3d3a14SMatthew G. Knepley   }
25139566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
2514ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2515ca3d3a14SMatthew G. Knepley }
2516ca3d3a14SMatthew G. Knepley 
2517ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2518ca3d3a14SMatthew G. Knepley {
2519ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2520ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2521ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2522ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2523ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2524ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2525ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
25269566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
2527ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2528ca3d3a14SMatthew G. Knepley }
2529ca3d3a14SMatthew G. Knepley 
2530bb9467b5SJed Brown /*@C
2531baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2532baf369e7SPeter Brune 
2533baf369e7SPeter Brune    Logically Collective
2534baf369e7SPeter Brune 
25354165533cSJose E. Roman    Input Parameters:
2536baf369e7SPeter Brune +  dm - the DM
2537baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2538baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
25390298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2540baf369e7SPeter Brune 
2541baf369e7SPeter Brune    Calling sequence for beginhook:
2542baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2543baf369e7SPeter Brune 
2544baf369e7SPeter Brune +  dm - global DM
2545baf369e7SPeter Brune .  g - global vector
2546baf369e7SPeter Brune .  mode - mode
2547baf369e7SPeter Brune .  l - local vector
2548baf369e7SPeter Brune -  ctx - optional user-defined function context
2549baf369e7SPeter Brune 
2550baf369e7SPeter Brune    Calling sequence for endhook:
2551ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2552baf369e7SPeter Brune 
2553baf369e7SPeter Brune +  global - global DM
2554baf369e7SPeter Brune -  ctx - optional user-defined function context
2555baf369e7SPeter Brune 
2556baf369e7SPeter Brune    Level: advanced
2557baf369e7SPeter Brune 
2558db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2559baf369e7SPeter Brune @*/
2560baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2561baf369e7SPeter Brune {
2562baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2563baf369e7SPeter Brune 
2564baf369e7SPeter Brune   PetscFunctionBegin;
2565baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2566baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
25679566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2568baf369e7SPeter Brune   link->beginhook = beginhook;
2569baf369e7SPeter Brune   link->endhook   = endhook;
2570baf369e7SPeter Brune   link->ctx       = ctx;
25710298fd71SBarry Smith   link->next      = NULL;
2572baf369e7SPeter Brune   *p              = link;
2573baf369e7SPeter Brune   PetscFunctionReturn(0);
2574baf369e7SPeter Brune }
2575baf369e7SPeter Brune 
25764c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
25774c274da1SToby Isaac {
25784c274da1SToby Isaac   Mat cMat;
257979769bd5SJed Brown   Vec cVec, cBias;
25804c274da1SToby Isaac   PetscSection section, cSec;
25814c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25824c274da1SToby Isaac 
25834c274da1SToby Isaac   PetscFunctionBegin;
25844c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25859566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm,&cSec,&cMat,&cBias));
25864c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
25875db9a05bSToby Isaac     PetscInt nRows;
25885db9a05bSToby Isaac 
25899566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat,&nRows,NULL));
25905db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
25919566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm,&section));
25929566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat,NULL,&cVec));
25939566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat,l,cVec));
25949566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec,1.,cBias));
25959566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec,&pStart,&pEnd));
25964c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
25979566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec,p,&dof));
25984c274da1SToby Isaac       if (dof) {
25994c274da1SToby Isaac         PetscScalar *vals;
26009566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec,cSec,p,&vals));
26019566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES));
26024c274da1SToby Isaac       }
26034c274da1SToby Isaac     }
26049566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
26054c274da1SToby Isaac   }
26064c274da1SToby Isaac   PetscFunctionReturn(0);
26074c274da1SToby Isaac }
26084c274da1SToby Isaac 
260947c6ae99SBarry Smith /*@
261001729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
261101729b5cSPatrick Sanan 
2612d083f849SBarry Smith     Neighbor-wise Collective on dm
261301729b5cSPatrick Sanan 
261401729b5cSPatrick Sanan     Input Parameters:
261501729b5cSPatrick Sanan +   dm - the DM object
261601729b5cSPatrick Sanan .   g - the global vector
261701729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
261801729b5cSPatrick Sanan -   l - the local vector
261901729b5cSPatrick Sanan 
262001729b5cSPatrick Sanan     Notes:
262101729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
262201729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
262301729b5cSPatrick Sanan 
262401729b5cSPatrick Sanan     Level: beginner
262501729b5cSPatrick Sanan 
2626db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
262701729b5cSPatrick Sanan 
262801729b5cSPatrick Sanan @*/
262901729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
263001729b5cSPatrick Sanan {
263101729b5cSPatrick Sanan   PetscFunctionBegin;
26329566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm,g,mode,l));
26339566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm,g,mode,l));
263401729b5cSPatrick Sanan   PetscFunctionReturn(0);
263501729b5cSPatrick Sanan }
263601729b5cSPatrick Sanan 
263701729b5cSPatrick Sanan /*@
263847c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
263947c6ae99SBarry Smith 
2640d083f849SBarry Smith     Neighbor-wise Collective on dm
264147c6ae99SBarry Smith 
264247c6ae99SBarry Smith     Input Parameters:
264347c6ae99SBarry Smith +   dm - the DM object
264447c6ae99SBarry Smith .   g - the global vector
264547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
264647c6ae99SBarry Smith -   l - the local vector
264747c6ae99SBarry Smith 
264801729b5cSPatrick Sanan     Level: intermediate
264947c6ae99SBarry Smith 
2650db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
265147c6ae99SBarry Smith 
265247c6ae99SBarry Smith @*/
26537087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
265447c6ae99SBarry Smith {
26557128ae9fSMatthew G Knepley   PetscSF                 sf;
2656baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
265747c6ae99SBarry Smith 
265847c6ae99SBarry Smith   PetscFunctionBegin;
2659171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2660baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
26611baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm,g,mode,l,link->ctx));
2662baf369e7SPeter Brune   }
26639566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
26647128ae9fSMatthew G Knepley   if (sf) {
2665ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2666ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2667d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
26687128ae9fSMatthew G Knepley 
26697a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
26709566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
26719566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
26729566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
26739566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
26749566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
26757128ae9fSMatthew G Knepley   } else {
26767a8be351SBarry Smith     PetscCheck(dm->ops->globaltolocalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
26779566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
26787128ae9fSMatthew G Knepley   }
267947c6ae99SBarry Smith   PetscFunctionReturn(0);
268047c6ae99SBarry Smith }
268147c6ae99SBarry Smith 
268247c6ae99SBarry Smith /*@
268347c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
268447c6ae99SBarry Smith 
2685d083f849SBarry Smith     Neighbor-wise Collective on dm
268647c6ae99SBarry Smith 
268747c6ae99SBarry Smith     Input Parameters:
268847c6ae99SBarry Smith +   dm - the DM object
268947c6ae99SBarry Smith .   g - the global vector
269047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
269147c6ae99SBarry Smith -   l - the local vector
269247c6ae99SBarry Smith 
269301729b5cSPatrick Sanan     Level: intermediate
269447c6ae99SBarry Smith 
2695db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
269647c6ae99SBarry Smith 
269747c6ae99SBarry Smith @*/
26987087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
269947c6ae99SBarry Smith {
27007128ae9fSMatthew G Knepley   PetscSF                 sf;
2701ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2702ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2703ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2704baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2705d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
270647c6ae99SBarry Smith 
270747c6ae99SBarry Smith   PetscFunctionBegin;
2708171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27099566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
27109566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
27117128ae9fSMatthew G Knepley   if (sf) {
27127a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
27137128ae9fSMatthew G Knepley 
27149566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
27159566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
27169566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray,MPI_REPLACE));
27179566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
27189566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
27199566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
27207128ae9fSMatthew G Knepley   } else {
27217a8be351SBarry Smith     PetscCheck(dm->ops->globaltolocalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
27229566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
27237128ae9fSMatthew G Knepley   }
27249566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL));
2725baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
27269566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm,g,mode,l,link->ctx));
2727baf369e7SPeter Brune   }
272847c6ae99SBarry Smith   PetscFunctionReturn(0);
272947c6ae99SBarry Smith }
273047c6ae99SBarry Smith 
2731d4d07f1eSToby Isaac /*@C
2732d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2733d4d07f1eSToby Isaac 
2734d4d07f1eSToby Isaac    Logically Collective
2735d4d07f1eSToby Isaac 
27364165533cSJose E. Roman    Input Parameters:
2737d4d07f1eSToby Isaac +  dm - the DM
2738d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2739d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2740d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2741d4d07f1eSToby Isaac 
2742d4d07f1eSToby Isaac    Calling sequence for beginhook:
2743d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2744d4d07f1eSToby Isaac 
2745d4d07f1eSToby Isaac +  dm - global DM
2746d4d07f1eSToby Isaac .  l - local vector
2747d4d07f1eSToby Isaac .  mode - mode
2748d4d07f1eSToby Isaac .  g - global vector
2749d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2750d4d07f1eSToby Isaac 
2751d4d07f1eSToby Isaac    Calling sequence for endhook:
2752d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2753d4d07f1eSToby Isaac 
2754d4d07f1eSToby Isaac +  global - global DM
2755d4d07f1eSToby Isaac .  l - local vector
2756d4d07f1eSToby Isaac .  mode - mode
2757d4d07f1eSToby Isaac .  g - global vector
2758d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2759d4d07f1eSToby Isaac 
2760d4d07f1eSToby Isaac    Level: advanced
2761d4d07f1eSToby Isaac 
2762db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2763d4d07f1eSToby Isaac @*/
2764d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2765d4d07f1eSToby Isaac {
2766d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2767d4d07f1eSToby Isaac 
2768d4d07f1eSToby Isaac   PetscFunctionBegin;
2769d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2770d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
27719566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2772d4d07f1eSToby Isaac   link->beginhook = beginhook;
2773d4d07f1eSToby Isaac   link->endhook   = endhook;
2774d4d07f1eSToby Isaac   link->ctx       = ctx;
2775d4d07f1eSToby Isaac   link->next      = NULL;
2776d4d07f1eSToby Isaac   *p              = link;
2777d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2778d4d07f1eSToby Isaac }
2779d4d07f1eSToby Isaac 
27804c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
27814c274da1SToby Isaac {
27824c274da1SToby Isaac   Mat cMat;
27834c274da1SToby Isaac   Vec cVec;
27844c274da1SToby Isaac   PetscSection section, cSec;
27854c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
27864c274da1SToby Isaac 
27874c274da1SToby Isaac   PetscFunctionBegin;
27884c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27899566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm,&cSec,&cMat,NULL));
27904c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
27915db9a05bSToby Isaac     PetscInt nRows;
27925db9a05bSToby Isaac 
27939566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat,&nRows,NULL));
27945db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
27959566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm,&section));
27969566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat,NULL,&cVec));
27979566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec,&pStart,&pEnd));
27984c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27999566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec,p,&dof));
28004c274da1SToby Isaac       if (dof) {
28014c274da1SToby Isaac         PetscInt d;
28024c274da1SToby Isaac         PetscScalar *vals;
28039566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l,section,p,&vals));
28049566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec,cSec,p,vals,mode));
28054c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
28064c274da1SToby Isaac          * we just extracted */
28074c274da1SToby Isaac         for (d = 0; d < dof; d++) {
28084c274da1SToby Isaac           vals[d] = 0.;
28094c274da1SToby Isaac         }
28104c274da1SToby Isaac       }
28114c274da1SToby Isaac     }
28129566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat,cVec,l,l));
28139566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
28144c274da1SToby Isaac   }
28154c274da1SToby Isaac   PetscFunctionReturn(0);
28164c274da1SToby Isaac }
281701729b5cSPatrick Sanan /*@
281801729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
281901729b5cSPatrick Sanan 
2820d083f849SBarry Smith     Neighbor-wise Collective on dm
282101729b5cSPatrick Sanan 
282201729b5cSPatrick Sanan     Input Parameters:
282301729b5cSPatrick Sanan +   dm - the DM object
282401729b5cSPatrick Sanan .   l - the local vector
282501729b5cSPatrick Sanan .   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.
282601729b5cSPatrick Sanan -   g - the global vector
282701729b5cSPatrick Sanan 
282801729b5cSPatrick Sanan     Notes:
282901729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
283001729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
283101729b5cSPatrick Sanan 
283201729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
283301729b5cSPatrick Sanan            INSERT_VALUES is not supported for DMDA; in that case simply compute the values directly into a global vector instead of a local one.
283401729b5cSPatrick Sanan 
283501729b5cSPatrick Sanan     Level: beginner
283601729b5cSPatrick Sanan 
2837db781477SPatrick Sanan .seealso `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
283801729b5cSPatrick Sanan 
283901729b5cSPatrick Sanan @*/
284001729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
284101729b5cSPatrick Sanan {
284201729b5cSPatrick Sanan   PetscFunctionBegin;
28439566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm,l,mode,g));
28449566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm,l,mode,g));
284501729b5cSPatrick Sanan   PetscFunctionReturn(0);
284601729b5cSPatrick Sanan }
28474c274da1SToby Isaac 
284847c6ae99SBarry Smith /*@
284901729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
28509a42bb27SBarry Smith 
2851d083f849SBarry Smith     Neighbor-wise Collective on dm
28529a42bb27SBarry Smith 
28539a42bb27SBarry Smith     Input Parameters:
28549a42bb27SBarry Smith +   dm - the DM object
2855f6813fd5SJed Brown .   l - the local vector
28561eb28f2eSBarry 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.
28571eb28f2eSBarry Smith -   g - the global vector
28589a42bb27SBarry Smith 
285995452b02SPatrick Sanan     Notes:
286095452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
286184330215SMatthew G. Knepley            INSERT_VALUES is not supported for DMDA, in that case simply compute the values directly into a global vector instead of a local one.
28629a42bb27SBarry Smith 
286301729b5cSPatrick Sanan     Level: intermediate
28649a42bb27SBarry Smith 
2865db781477SPatrick Sanan .seealso `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
28669a42bb27SBarry Smith 
28679a42bb27SBarry Smith @*/
28687087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
28699a42bb27SBarry Smith {
28707128ae9fSMatthew G Knepley   PetscSF                 sf;
287184330215SMatthew G. Knepley   PetscSection            s, gs;
2872d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2873ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2874ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2875ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2876fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
2877d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
28789a42bb27SBarry Smith 
28799a42bb27SBarry Smith   PetscFunctionBegin;
2880171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2881d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
28821baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm,l,mode,g,link->ctx));
2883d4d07f1eSToby Isaac   }
28849566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL));
28859566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28869566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
28877128ae9fSMatthew G Knepley   switch (mode) {
28887128ae9fSMatthew G Knepley   case INSERT_VALUES:
28897128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2890304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
289184330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
28927128ae9fSMatthew G Knepley   case ADD_VALUES:
28937128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2894304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
289584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
28967128ae9fSMatthew G Knepley   default:
289763a3b9bcSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
28987128ae9fSMatthew G Knepley   }
2899ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
29009566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
2901ca3d3a14SMatthew G. Knepley     if (transform) {
29029566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
29039566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
29049566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
29059566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
2906fa88e482SJed Brown     } else if (isInsert) {
29079566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
2908fa88e482SJed Brown     } else {
29099566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
2910fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2911ca3d3a14SMatthew G. Knepley     }
2912fa88e482SJed Brown     if (s && isInsert) {
29139566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
2914fa88e482SJed Brown     } else {
29159566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
2916fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2917fa88e482SJed Brown     }
2918ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
29199566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
292084330215SMatthew G. Knepley     } else if (s && isInsert) {
292184330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
292284330215SMatthew G. Knepley 
29239566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
29249566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
29259566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
292684330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2927b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
292884330215SMatthew G. Knepley 
29299566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
29309566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
29319566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
29329566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
29339566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
29349566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
2935b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
293603442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
29377a8be351SBarry 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);
2938b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2939b3b16f48SMatthew G. Knepley         if (!gcdof) {
294084330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2941b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2942b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
294384330215SMatthew G. Knepley           const PetscInt *cdofs;
294484330215SMatthew G. Knepley           PetscInt        cind = 0;
294584330215SMatthew G. Knepley 
29469566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
2947b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
294884330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2949b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
295084330215SMatthew G. Knepley           }
29517a8be351SBarry 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);
295284330215SMatthew G. Knepley       }
2953ca3d3a14SMatthew G. Knepley     }
2954fa88e482SJed Brown     if (g_inplace) {
29559566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
2956fa88e482SJed Brown     } else {
29579566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
2958fa88e482SJed Brown     }
2959ca3d3a14SMatthew G. Knepley     if (transform) {
29609566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
29619566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
2962fa88e482SJed Brown     } else if (l_inplace) {
29639566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
2964ca3d3a14SMatthew G. Knepley     } else {
29659566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
2966ca3d3a14SMatthew G. Knepley     }
29677128ae9fSMatthew G Knepley   } else {
29687a8be351SBarry Smith     PetscCheck(dm->ops->localtoglobalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
29699566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g));
29707128ae9fSMatthew G Knepley   }
29719a42bb27SBarry Smith   PetscFunctionReturn(0);
29729a42bb27SBarry Smith }
29739a42bb27SBarry Smith 
29749a42bb27SBarry Smith /*@
29759a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
297647c6ae99SBarry Smith 
2977d083f849SBarry Smith     Neighbor-wise Collective on dm
297847c6ae99SBarry Smith 
297947c6ae99SBarry Smith     Input Parameters:
298047c6ae99SBarry Smith +   dm - the DM object
2981f6813fd5SJed Brown .   l - the local vector
298247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2983f6813fd5SJed Brown -   g - the global vector
298447c6ae99SBarry Smith 
298501729b5cSPatrick Sanan     Level: intermediate
298647c6ae99SBarry Smith 
2987db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalEnd()`
298847c6ae99SBarry Smith 
298947c6ae99SBarry Smith @*/
29907087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
299147c6ae99SBarry Smith {
29927128ae9fSMatthew G Knepley   PetscSF                 sf;
299384330215SMatthew G. Knepley   PetscSection            s;
2994d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2995ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
299647c6ae99SBarry Smith 
299747c6ae99SBarry Smith   PetscFunctionBegin;
2998171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29999566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30009566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30017128ae9fSMatthew G Knepley   switch (mode) {
30027128ae9fSMatthew G Knepley   case INSERT_VALUES:
30037128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
300484330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
30057128ae9fSMatthew G Knepley   case ADD_VALUES:
30067128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
300784330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
30087128ae9fSMatthew G Knepley   default:
300963a3b9bcSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30107128ae9fSMatthew G Knepley   }
301184330215SMatthew G. Knepley   if (sf && !isInsert) {
3012ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3013ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3014ca3d3a14SMatthew G. Knepley     Vec                tmpl;
301584330215SMatthew G. Knepley 
30169566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3017ca3d3a14SMatthew G. Knepley     if (transform) {
30189566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30199566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3020ca3d3a14SMatthew G. Knepley     } else {
30219566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3022ca3d3a14SMatthew G. Knepley     }
30239566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
30249566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3025ca3d3a14SMatthew G. Knepley     if (transform) {
30269566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
30279566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3028ca3d3a14SMatthew G. Knepley     } else {
30299566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3030ca3d3a14SMatthew G. Knepley     }
30319566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
303284330215SMatthew G. Knepley   } else if (s && isInsert) {
30337128ae9fSMatthew G Knepley   } else {
30347a8be351SBarry Smith     PetscCheck(dm->ops->localtoglobalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
30359566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g));
30367128ae9fSMatthew G Knepley   }
3037d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
30389566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm,g,mode,l,link->ctx));
3039d4d07f1eSToby Isaac   }
304047c6ae99SBarry Smith   PetscFunctionReturn(0);
304147c6ae99SBarry Smith }
304247c6ae99SBarry Smith 
3043f089877aSRichard Tran Mills /*@
3044bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
3045bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3046d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
3047f089877aSRichard Tran Mills 
3048d083f849SBarry Smith    Neighbor-wise Collective on dm
3049f089877aSRichard Tran Mills 
3050f089877aSRichard Tran Mills    Input Parameters:
3051f089877aSRichard Tran Mills +  dm - the DM object
3052bc0a1609SRichard Tran Mills .  g - the original local vector
3053bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3054f089877aSRichard Tran Mills 
3055bc0a1609SRichard Tran Mills    Output Parameter:
3056bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3057f089877aSRichard Tran Mills 
3058f089877aSRichard Tran Mills    Level: intermediate
3059f089877aSRichard Tran Mills 
3060bc0a1609SRichard Tran Mills    Notes:
3061bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3062bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3063bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3064bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3065bc0a1609SRichard Tran Mills 
3066db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalEnd()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3067f089877aSRichard Tran Mills 
3068f089877aSRichard Tran Mills @*/
3069f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
3070f089877aSRichard Tran Mills {
3071f089877aSRichard Tran Mills   PetscFunctionBegin;
3072f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30737a8be351SBarry Smith   PetscCheck(dm->ops->localtolocalbegin,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
30749566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
3075f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3076f089877aSRichard Tran Mills }
3077f089877aSRichard Tran Mills 
3078f089877aSRichard Tran Mills /*@
3079bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
3080bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3081d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
3082f089877aSRichard Tran Mills 
3083d083f849SBarry Smith    Neighbor-wise Collective on dm
3084f089877aSRichard Tran Mills 
3085f089877aSRichard Tran Mills    Input Parameters:
3086bc0a1609SRichard Tran Mills +  da - the DM object
3087bc0a1609SRichard Tran Mills .  g - the original local vector
3088bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3089f089877aSRichard Tran Mills 
3090bc0a1609SRichard Tran Mills    Output Parameter:
3091bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3092f089877aSRichard Tran Mills 
3093f089877aSRichard Tran Mills    Level: intermediate
3094f089877aSRichard Tran Mills 
3095bc0a1609SRichard Tran Mills    Notes:
3096bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3097bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3098bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3099bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3100bc0a1609SRichard Tran Mills 
3101db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalBegin()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3102f089877aSRichard Tran Mills 
3103f089877aSRichard Tran Mills @*/
3104f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
3105f089877aSRichard Tran Mills {
3106f089877aSRichard Tran Mills   PetscFunctionBegin;
3107f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31087a8be351SBarry Smith   PetscCheck(dm->ops->localtolocalend,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
31099566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
3110f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3111f089877aSRichard Tran Mills }
3112f089877aSRichard Tran Mills 
311347c6ae99SBarry Smith /*@
311447c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
311547c6ae99SBarry Smith 
3116d083f849SBarry Smith     Collective on dm
311747c6ae99SBarry Smith 
3118d8d19677SJose E. Roman     Input Parameters:
311947c6ae99SBarry Smith +   dm - the DM object
312091d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
312147c6ae99SBarry Smith 
312247c6ae99SBarry Smith     Output Parameter:
312347c6ae99SBarry Smith .   dmc - the coarsened DM
312447c6ae99SBarry Smith 
312547c6ae99SBarry Smith     Level: developer
312647c6ae99SBarry Smith 
3127db781477SPatrick Sanan .seealso `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
312847c6ae99SBarry Smith 
312947c6ae99SBarry Smith @*/
31307087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
313147c6ae99SBarry Smith {
3132b17ce1afSJed Brown   DMCoarsenHookLink link;
313347c6ae99SBarry Smith 
313447c6ae99SBarry Smith   PetscFunctionBegin;
3135171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31367a8be351SBarry Smith   PetscCheck(dm->ops->coarsen,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
31379566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen,dm,0,0,0));
31389566063dSJacob Faibussowitsch   PetscCall((*dm->ops->coarsen)(dm, comm, dmc));
3139b9d85ea2SLisandro Dalcin   if (*dmc) {
3140a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
31419566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm,*dmc));
314243842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
31439566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc));
3144644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
31450598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3146656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
31479566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc,dm->mattype));
3148b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
31499566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm,*dmc,link->ctx));
3150b17ce1afSJed Brown     }
3151b9d85ea2SLisandro Dalcin   }
31529566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen,dm,0,0,0));
31537a8be351SBarry Smith   PetscCheck(*dmc,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3154b17ce1afSJed Brown   PetscFunctionReturn(0);
3155b17ce1afSJed Brown }
3156b17ce1afSJed Brown 
3157bb9467b5SJed Brown /*@C
3158b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3159b17ce1afSJed Brown 
3160b17ce1afSJed Brown    Logically Collective
3161b17ce1afSJed Brown 
31624165533cSJose E. Roman    Input Parameters:
3163b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3164b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3165b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
31660298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3167b17ce1afSJed Brown 
3168b17ce1afSJed Brown    Calling sequence of coarsenhook:
3169b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3170b17ce1afSJed Brown 
3171b17ce1afSJed Brown +  fine - fine level DM
3172b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3173b17ce1afSJed Brown -  ctx - optional user-defined function context
3174b17ce1afSJed Brown 
3175b17ce1afSJed Brown    Calling sequence for restricthook:
3176c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3177b17ce1afSJed Brown 
3178b17ce1afSJed Brown +  fine - fine level DM
3179b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3180c833c3b5SJed Brown .  rscale - scaling vector for restriction
3181c833c3b5SJed Brown .  inject - matrix restricting by injection
3182b17ce1afSJed Brown .  coarse - coarse level DM to update
3183b17ce1afSJed Brown -  ctx - optional user-defined function context
3184b17ce1afSJed Brown 
3185b17ce1afSJed Brown    Level: advanced
3186b17ce1afSJed Brown 
3187b17ce1afSJed Brown    Notes:
3188b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3189b17ce1afSJed Brown 
3190b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3191b17ce1afSJed Brown 
3192b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3193b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3194b17ce1afSJed Brown 
3195bb9467b5SJed Brown    This function is currently not available from Fortran.
3196bb9467b5SJed Brown 
3197db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3198b17ce1afSJed Brown @*/
3199b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3200b17ce1afSJed Brown {
3201b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3202b17ce1afSJed Brown 
3203b17ce1afSJed Brown   PetscFunctionBegin;
3204b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
32051e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
32061e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
32071e3d8eccSJed Brown   }
32089566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3209b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3210b17ce1afSJed Brown   link->restricthook = restricthook;
3211b17ce1afSJed Brown   link->ctx          = ctx;
32120298fd71SBarry Smith   link->next         = NULL;
3213b17ce1afSJed Brown   *p                 = link;
3214b17ce1afSJed Brown   PetscFunctionReturn(0);
3215b17ce1afSJed Brown }
3216b17ce1afSJed Brown 
3217dc822a44SJed Brown /*@C
3218dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3219dc822a44SJed Brown 
3220dc822a44SJed Brown    Logically Collective
3221dc822a44SJed Brown 
32224165533cSJose E. Roman    Input Parameters:
3223dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3224dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3225dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3226dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3227dc822a44SJed Brown 
3228dc822a44SJed Brown    Level: advanced
3229dc822a44SJed Brown 
3230dc822a44SJed Brown    Notes:
3231dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3232dc822a44SJed Brown 
3233dc822a44SJed Brown    This function is currently not available from Fortran.
3234dc822a44SJed Brown 
3235db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3236dc822a44SJed Brown @*/
3237dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3238dc822a44SJed Brown {
3239dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3240dc822a44SJed Brown 
3241dc822a44SJed Brown   PetscFunctionBegin;
3242dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3243dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3244dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3245dc822a44SJed Brown       link = *p;
3246dc822a44SJed Brown       *p = link->next;
32479566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3248dc822a44SJed Brown       break;
3249dc822a44SJed Brown     }
3250dc822a44SJed Brown   }
3251dc822a44SJed Brown   PetscFunctionReturn(0);
3252dc822a44SJed Brown }
3253dc822a44SJed Brown 
3254b17ce1afSJed Brown /*@
3255b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3256b17ce1afSJed Brown 
3257b17ce1afSJed Brown    Collective if any hooks are
3258b17ce1afSJed Brown 
32594165533cSJose E. Roman    Input Parameters:
3260b17ce1afSJed Brown +  fine - finer DM to use as a base
3261b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3262e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3263b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3264e91eccc2SStefano Zampini -  coarse - coarser DM to update
3265b17ce1afSJed Brown 
3266b17ce1afSJed Brown    Level: developer
3267b17ce1afSJed Brown 
3268db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
3269b17ce1afSJed Brown @*/
3270b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3271b17ce1afSJed Brown {
3272b17ce1afSJed Brown   DMCoarsenHookLink link;
3273b17ce1afSJed Brown 
3274b17ce1afSJed Brown   PetscFunctionBegin;
3275b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
32761baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx));
3277b17ce1afSJed Brown   }
327847c6ae99SBarry Smith   PetscFunctionReturn(0);
327947c6ae99SBarry Smith }
328047c6ae99SBarry Smith 
3281bb9467b5SJed Brown /*@C
3282be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
32835dbd56e3SPeter Brune 
3284d083f849SBarry Smith    Logically Collective on global
32855dbd56e3SPeter Brune 
32864165533cSJose E. Roman    Input Parameters:
32875dbd56e3SPeter Brune +  global - global DM
3288ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
32895dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
32900298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
32915dbd56e3SPeter Brune 
3292ec4806b8SPeter Brune    Calling sequence for ddhook:
3293ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3294ec4806b8SPeter Brune 
3295ec4806b8SPeter Brune +  global - global DM
3296ec4806b8SPeter Brune .  block  - block DM
3297ec4806b8SPeter Brune -  ctx - optional user-defined function context
3298ec4806b8SPeter Brune 
32995dbd56e3SPeter Brune    Calling sequence for restricthook:
3300ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
33015dbd56e3SPeter Brune 
33025dbd56e3SPeter Brune +  global - global DM
33035dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
33045dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3305ec4806b8SPeter Brune .  block  - block DM
33065dbd56e3SPeter Brune -  ctx - optional user-defined function context
33075dbd56e3SPeter Brune 
33085dbd56e3SPeter Brune    Level: advanced
33095dbd56e3SPeter Brune 
33105dbd56e3SPeter Brune    Notes:
3311ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
33125dbd56e3SPeter Brune 
33135dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
33145dbd56e3SPeter Brune 
33155dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3316ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
33175dbd56e3SPeter Brune 
3318bb9467b5SJed Brown    This function is currently not available from Fortran.
3319bb9467b5SJed Brown 
3320db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
33215dbd56e3SPeter Brune @*/
3322be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
33235dbd56e3SPeter Brune {
3324be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
33255dbd56e3SPeter Brune 
33265dbd56e3SPeter Brune   PetscFunctionBegin;
33275dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3328b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3329b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3330b3a6b972SJed Brown   }
33319566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
33325dbd56e3SPeter Brune   link->restricthook = restricthook;
3333be081cd6SPeter Brune   link->ddhook       = ddhook;
33345dbd56e3SPeter Brune   link->ctx          = ctx;
33350298fd71SBarry Smith   link->next         = NULL;
33365dbd56e3SPeter Brune   *p                 = link;
33375dbd56e3SPeter Brune   PetscFunctionReturn(0);
33385dbd56e3SPeter Brune }
33395dbd56e3SPeter Brune 
3340b3a6b972SJed Brown /*@C
3341b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3342b3a6b972SJed Brown 
3343b3a6b972SJed Brown    Logically Collective
3344b3a6b972SJed Brown 
33454165533cSJose E. Roman    Input Parameters:
3346b3a6b972SJed Brown +  global - global DM
3347b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3348b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3349b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3350b3a6b972SJed Brown 
3351b3a6b972SJed Brown    Level: advanced
3352b3a6b972SJed Brown 
3353b3a6b972SJed Brown    Notes:
3354b3a6b972SJed Brown 
3355b3a6b972SJed Brown    This function is currently not available from Fortran.
3356b3a6b972SJed Brown 
3357db781477SPatrick Sanan .seealso: `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3358b3a6b972SJed Brown @*/
3359b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3360b3a6b972SJed Brown {
3361b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3362b3a6b972SJed Brown 
3363b3a6b972SJed Brown   PetscFunctionBegin;
3364b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3365b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3366b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3367b3a6b972SJed Brown       link = *p;
3368b3a6b972SJed Brown       *p = link->next;
33699566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3370b3a6b972SJed Brown       break;
3371b3a6b972SJed Brown     }
3372b3a6b972SJed Brown   }
3373b3a6b972SJed Brown   PetscFunctionReturn(0);
3374b3a6b972SJed Brown }
3375b3a6b972SJed Brown 
33765dbd56e3SPeter Brune /*@
3377be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
33785dbd56e3SPeter Brune 
33795dbd56e3SPeter Brune    Collective if any hooks are
33805dbd56e3SPeter Brune 
33814165533cSJose E. Roman    Input Parameters:
33825dbd56e3SPeter Brune +  fine - finer DM to use as a base
3383be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3384be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
33855dbd56e3SPeter Brune -  coarse - coarer DM to update
33865dbd56e3SPeter Brune 
33875dbd56e3SPeter Brune    Level: developer
33885dbd56e3SPeter Brune 
3389db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
33905dbd56e3SPeter Brune @*/
3391be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
33925dbd56e3SPeter Brune {
3393be081cd6SPeter Brune   DMSubDomainHookLink link;
33945dbd56e3SPeter Brune 
33955dbd56e3SPeter Brune   PetscFunctionBegin;
3396be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
33971baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx));
33985dbd56e3SPeter Brune   }
33995dbd56e3SPeter Brune   PetscFunctionReturn(0);
34005dbd56e3SPeter Brune }
34015dbd56e3SPeter Brune 
34025fe1f584SPeter Brune /*@
34036a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
34045fe1f584SPeter Brune 
34055fe1f584SPeter Brune     Not Collective
34065fe1f584SPeter Brune 
34075fe1f584SPeter Brune     Input Parameter:
34085fe1f584SPeter Brune .   dm - the DM object
34095fe1f584SPeter Brune 
34105fe1f584SPeter Brune     Output Parameter:
34116a7d9d85SPeter Brune .   level - number of coarsenings
34125fe1f584SPeter Brune 
34135fe1f584SPeter Brune     Level: developer
34145fe1f584SPeter Brune 
3415db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
34165fe1f584SPeter Brune 
34175fe1f584SPeter Brune @*/
34185fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
34195fe1f584SPeter Brune {
34205fe1f584SPeter Brune   PetscFunctionBegin;
34215fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3422b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
34235fe1f584SPeter Brune   *level = dm->leveldown;
34245fe1f584SPeter Brune   PetscFunctionReturn(0);
34255fe1f584SPeter Brune }
34265fe1f584SPeter Brune 
34279a64c4a8SMatthew G. Knepley /*@
34289a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
34299a64c4a8SMatthew G. Knepley 
34309a64c4a8SMatthew G. Knepley     Not Collective
34319a64c4a8SMatthew G. Knepley 
34329a64c4a8SMatthew G. Knepley     Input Parameters:
34339a64c4a8SMatthew G. Knepley +   dm - the DM object
34349a64c4a8SMatthew G. Knepley -   level - number of coarsenings
34359a64c4a8SMatthew G. Knepley 
34369a64c4a8SMatthew G. Knepley     Level: developer
34379a64c4a8SMatthew G. Knepley 
3438db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
34399a64c4a8SMatthew G. Knepley @*/
34409a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
34419a64c4a8SMatthew G. Knepley {
34429a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
34439a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34449a64c4a8SMatthew G. Knepley   dm->leveldown = level;
34459a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
34469a64c4a8SMatthew G. Knepley }
34479a64c4a8SMatthew G. Knepley 
344847c6ae99SBarry Smith /*@C
344947c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
345047c6ae99SBarry Smith 
3451d083f849SBarry Smith     Collective on dm
345247c6ae99SBarry Smith 
3453d8d19677SJose E. Roman     Input Parameters:
345447c6ae99SBarry Smith +   dm - the DM object
345547c6ae99SBarry Smith -   nlevels - the number of levels of refinement
345647c6ae99SBarry Smith 
345747c6ae99SBarry Smith     Output Parameter:
345847c6ae99SBarry Smith .   dmf - the refined DM hierarchy
345947c6ae99SBarry Smith 
346047c6ae99SBarry Smith     Level: developer
346147c6ae99SBarry Smith 
3462db781477SPatrick Sanan .seealso `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
346347c6ae99SBarry Smith 
346447c6ae99SBarry Smith @*/
34657087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
346647c6ae99SBarry Smith {
346747c6ae99SBarry Smith   PetscFunctionBegin;
3468171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34697a8be351SBarry Smith   PetscCheck(nlevels >= 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
347047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3471b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
347247c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
34739566063dSJacob Faibussowitsch     PetscCall((*dm->ops->refinehierarchy)(dm,nlevels,dmf));
347447c6ae99SBarry Smith   } else if (dm->ops->refine) {
347547c6ae99SBarry Smith     PetscInt i;
347647c6ae99SBarry Smith 
34779566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]));
347847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
34799566063dSJacob Faibussowitsch       PetscCall(DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]));
348047c6ae99SBarry Smith     }
3481ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
348247c6ae99SBarry Smith   PetscFunctionReturn(0);
348347c6ae99SBarry Smith }
348447c6ae99SBarry Smith 
348547c6ae99SBarry Smith /*@C
348647c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
348747c6ae99SBarry Smith 
3488d083f849SBarry Smith     Collective on dm
348947c6ae99SBarry Smith 
3490d8d19677SJose E. Roman     Input Parameters:
349147c6ae99SBarry Smith +   dm - the DM object
349247c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
349347c6ae99SBarry Smith 
349447c6ae99SBarry Smith     Output Parameter:
349547c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
349647c6ae99SBarry Smith 
349747c6ae99SBarry Smith     Level: developer
349847c6ae99SBarry Smith 
3499db781477SPatrick Sanan .seealso `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
350047c6ae99SBarry Smith 
350147c6ae99SBarry Smith @*/
35027087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
350347c6ae99SBarry Smith {
350447c6ae99SBarry Smith   PetscFunctionBegin;
3505171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35067a8be351SBarry Smith   PetscCheck(nlevels >= 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
350747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
350847c6ae99SBarry Smith   PetscValidPointer(dmc,3);
350947c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
35109566063dSJacob Faibussowitsch     PetscCall((*dm->ops->coarsenhierarchy)(dm, nlevels, dmc));
351147c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
351247c6ae99SBarry Smith     PetscInt i;
351347c6ae99SBarry Smith 
35149566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]));
351547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
35169566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]));
351747c6ae99SBarry Smith     }
3518ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
351947c6ae99SBarry Smith   PetscFunctionReturn(0);
352047c6ae99SBarry Smith }
352147c6ae99SBarry Smith 
35221a266240SBarry Smith /*@C
35231a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
35241a266240SBarry Smith 
35251a266240SBarry Smith     Not Collective
35261a266240SBarry Smith 
35271a266240SBarry Smith     Input Parameters:
35281a266240SBarry Smith +   dm - the DM object
35291a266240SBarry Smith -   destroy - the destroy function
35301a266240SBarry Smith 
35311a266240SBarry Smith     Level: intermediate
35321a266240SBarry Smith 
3533db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
35341a266240SBarry Smith 
3535f07f9ceaSJed Brown @*/
35361a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
35371a266240SBarry Smith {
35381a266240SBarry Smith   PetscFunctionBegin;
3539171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35401a266240SBarry Smith   dm->ctxdestroy = destroy;
35411a266240SBarry Smith   PetscFunctionReturn(0);
35421a266240SBarry Smith }
35431a266240SBarry Smith 
3544b07ff414SBarry Smith /*@
35451b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
354647c6ae99SBarry Smith 
354747c6ae99SBarry Smith     Not Collective
354847c6ae99SBarry Smith 
354947c6ae99SBarry Smith     Input Parameters:
355047c6ae99SBarry Smith +   dm - the DM object
355147c6ae99SBarry Smith -   ctx - the user context
355247c6ae99SBarry Smith 
355347c6ae99SBarry Smith     Level: intermediate
355447c6ae99SBarry Smith 
3555db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
355647c6ae99SBarry Smith 
355747c6ae99SBarry Smith @*/
35581b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
355947c6ae99SBarry Smith {
356047c6ae99SBarry Smith   PetscFunctionBegin;
3561171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
356247c6ae99SBarry Smith   dm->ctx = ctx;
356347c6ae99SBarry Smith   PetscFunctionReturn(0);
356447c6ae99SBarry Smith }
356547c6ae99SBarry Smith 
356647c6ae99SBarry Smith /*@
35671b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
356847c6ae99SBarry Smith 
356947c6ae99SBarry Smith     Not Collective
357047c6ae99SBarry Smith 
357147c6ae99SBarry Smith     Input Parameter:
357247c6ae99SBarry Smith .   dm - the DM object
357347c6ae99SBarry Smith 
357447c6ae99SBarry Smith     Output Parameter:
357547c6ae99SBarry Smith .   ctx - the user context
357647c6ae99SBarry Smith 
357747c6ae99SBarry Smith     Level: intermediate
357847c6ae99SBarry Smith 
3579db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
358047c6ae99SBarry Smith 
358147c6ae99SBarry Smith @*/
35821b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
358347c6ae99SBarry Smith {
358447c6ae99SBarry Smith   PetscFunctionBegin;
3585171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35861b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
358747c6ae99SBarry Smith   PetscFunctionReturn(0);
358847c6ae99SBarry Smith }
358947c6ae99SBarry Smith 
359008da532bSDmitry Karpeev /*@C
3591df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
359208da532bSDmitry Karpeev 
3593d083f849SBarry Smith     Logically Collective on dm
359408da532bSDmitry Karpeev 
3595d8d19677SJose E. Roman     Input Parameters:
359608da532bSDmitry Karpeev +   dm - the DM object
35970298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
359808da532bSDmitry Karpeev 
359908da532bSDmitry Karpeev     Level: intermediate
360008da532bSDmitry Karpeev 
3601db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3602db781477SPatrick Sanan          `DMSetJacobian()`
360308da532bSDmitry Karpeev 
360408da532bSDmitry Karpeev @*/
360508da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
360608da532bSDmitry Karpeev {
360708da532bSDmitry Karpeev   PetscFunctionBegin;
36085a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
360908da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
361008da532bSDmitry Karpeev   PetscFunctionReturn(0);
361108da532bSDmitry Karpeev }
361208da532bSDmitry Karpeev 
361308da532bSDmitry Karpeev /*@
361408da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
361508da532bSDmitry Karpeev 
361608da532bSDmitry Karpeev     Not Collective
361708da532bSDmitry Karpeev 
361808da532bSDmitry Karpeev     Input Parameter:
361908da532bSDmitry Karpeev .   dm - the DM object to destroy
362008da532bSDmitry Karpeev 
362108da532bSDmitry Karpeev     Output Parameter:
362208da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
362308da532bSDmitry Karpeev 
362408da532bSDmitry Karpeev     Level: developer
362508da532bSDmitry Karpeev 
3626db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
362708da532bSDmitry Karpeev 
362808da532bSDmitry Karpeev @*/
362908da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
363008da532bSDmitry Karpeev {
363108da532bSDmitry Karpeev   PetscFunctionBegin;
36325a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3633534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
363408da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
363508da532bSDmitry Karpeev   PetscFunctionReturn(0);
363608da532bSDmitry Karpeev }
363708da532bSDmitry Karpeev 
363808da532bSDmitry Karpeev /*@C
363908da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
364008da532bSDmitry Karpeev 
3641d083f849SBarry Smith     Logically Collective on dm
364208da532bSDmitry Karpeev 
3643f899ff85SJose E. Roman     Input Parameter:
3644907376e6SBarry Smith .   dm - the DM object
364508da532bSDmitry Karpeev 
364608da532bSDmitry Karpeev     Output parameters:
364708da532bSDmitry Karpeev +   xl - lower bound
364808da532bSDmitry Karpeev -   xu - upper bound
364908da532bSDmitry Karpeev 
3650907376e6SBarry Smith     Level: advanced
3651907376e6SBarry Smith 
365295452b02SPatrick Sanan     Notes:
365395452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
365408da532bSDmitry Karpeev 
3655db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
365608da532bSDmitry Karpeev 
365708da532bSDmitry Karpeev @*/
365808da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
365908da532bSDmitry Karpeev {
366008da532bSDmitry Karpeev   PetscFunctionBegin;
36615a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
366208da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
36635a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
36647a8be351SBarry Smith   PetscCheck(dm->ops->computevariablebounds,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
36659566063dSJacob Faibussowitsch   PetscCall((*dm->ops->computevariablebounds)(dm, xl,xu));
366608da532bSDmitry Karpeev   PetscFunctionReturn(0);
366708da532bSDmitry Karpeev }
366808da532bSDmitry Karpeev 
3669b0ae01b7SPeter Brune /*@
3670b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3671b0ae01b7SPeter Brune 
3672b0ae01b7SPeter Brune     Not Collective
3673b0ae01b7SPeter Brune 
3674b0ae01b7SPeter Brune     Input Parameter:
3675b0ae01b7SPeter Brune .   dm - the DM object
3676b0ae01b7SPeter Brune 
3677b0ae01b7SPeter Brune     Output Parameter:
3678b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3679b0ae01b7SPeter Brune 
3680b0ae01b7SPeter Brune     Level: developer
3681b0ae01b7SPeter Brune 
3682db781477SPatrick Sanan .seealso `DMCreateColoring()`
3683b0ae01b7SPeter Brune 
3684b0ae01b7SPeter Brune @*/
3685b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3686b0ae01b7SPeter Brune {
3687b0ae01b7SPeter Brune   PetscFunctionBegin;
36885a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3689534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3690b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3691b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3692b0ae01b7SPeter Brune }
3693b0ae01b7SPeter Brune 
36943ad4599aSBarry Smith /*@
36953ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
36963ad4599aSBarry Smith 
36973ad4599aSBarry Smith     Not Collective
36983ad4599aSBarry Smith 
36993ad4599aSBarry Smith     Input Parameter:
37003ad4599aSBarry Smith .   dm - the DM object
37013ad4599aSBarry Smith 
37023ad4599aSBarry Smith     Output Parameter:
37033ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
37043ad4599aSBarry Smith 
37053ad4599aSBarry Smith     Level: developer
37063ad4599aSBarry Smith 
3707db781477SPatrick Sanan .seealso `DMCreateRestriction()`
37083ad4599aSBarry Smith 
37093ad4599aSBarry Smith @*/
37103ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
37113ad4599aSBarry Smith {
37123ad4599aSBarry Smith   PetscFunctionBegin;
37135a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3714534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37153ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
37163ad4599aSBarry Smith   PetscFunctionReturn(0);
37173ad4599aSBarry Smith }
37183ad4599aSBarry Smith 
3719a7058e45SLawrence Mitchell /*@
3720a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3721a7058e45SLawrence Mitchell 
3722a7058e45SLawrence Mitchell     Not Collective
3723a7058e45SLawrence Mitchell 
3724a7058e45SLawrence Mitchell     Input Parameter:
3725a7058e45SLawrence Mitchell .   dm - the DM object
3726a7058e45SLawrence Mitchell 
3727a7058e45SLawrence Mitchell     Output Parameter:
3728a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3729a7058e45SLawrence Mitchell 
3730a7058e45SLawrence Mitchell     Level: developer
3731a7058e45SLawrence Mitchell 
3732db781477SPatrick Sanan .seealso `DMCreateInjection()`
3733a7058e45SLawrence Mitchell 
3734a7058e45SLawrence Mitchell @*/
3735a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3736a7058e45SLawrence Mitchell {
3737a7058e45SLawrence Mitchell   PetscFunctionBegin;
37385a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3739534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37405a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
37419566063dSJacob Faibussowitsch     PetscCall((*dm->ops->hascreateinjection)(dm,flg));
37425a84ad33SLisandro Dalcin   } else {
37435a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
37445a84ad33SLisandro Dalcin   }
3745a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3746a7058e45SLawrence Mitchell }
3747a7058e45SLawrence Mitchell 
37480298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3749264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3750264ace61SBarry Smith 
3751264ace61SBarry Smith /*@C
3752264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3753264ace61SBarry Smith 
3754d083f849SBarry Smith   Collective on dm
3755264ace61SBarry Smith 
3756264ace61SBarry Smith   Input Parameters:
3757264ace61SBarry Smith + dm     - The DM object
3758264ace61SBarry Smith - method - The name of the DM type
3759264ace61SBarry Smith 
3760264ace61SBarry Smith   Options Database Key:
3761264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3762264ace61SBarry Smith 
3763264ace61SBarry Smith   Notes:
3764e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3765264ace61SBarry Smith 
3766264ace61SBarry Smith   Level: intermediate
3767264ace61SBarry Smith 
3768db781477SPatrick Sanan .seealso: `DMGetType()`, `DMCreate()`
3769264ace61SBarry Smith @*/
377019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3771264ace61SBarry Smith {
3772264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3773264ace61SBarry Smith   PetscBool      match;
3774264ace61SBarry Smith 
3775264ace61SBarry Smith   PetscFunctionBegin;
3776264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
37779566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) dm, method, &match));
3778264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3779264ace61SBarry Smith 
37809566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
37819566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList,method,&r));
37827a8be351SBarry Smith   PetscCheck(r,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3783264ace61SBarry Smith 
37841baa6e33SBarry Smith   if (dm->ops->destroy) PetscCall((*dm->ops->destroy)(dm));
37859566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops,sizeof(*dm->ops)));
37869566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm,method));
37879566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
3788264ace61SBarry Smith   PetscFunctionReturn(0);
3789264ace61SBarry Smith }
3790264ace61SBarry Smith 
3791264ace61SBarry Smith /*@C
3792264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3793264ace61SBarry Smith 
3794264ace61SBarry Smith   Not Collective
3795264ace61SBarry Smith 
3796264ace61SBarry Smith   Input Parameter:
3797264ace61SBarry Smith . dm  - The DM
3798264ace61SBarry Smith 
3799264ace61SBarry Smith   Output Parameter:
3800264ace61SBarry Smith . type - The DM type name
3801264ace61SBarry Smith 
3802264ace61SBarry Smith   Level: intermediate
3803264ace61SBarry Smith 
3804db781477SPatrick Sanan .seealso: `DMSetType()`, `DMCreate()`
3805264ace61SBarry Smith @*/
380619fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3807264ace61SBarry Smith {
3808264ace61SBarry Smith   PetscFunctionBegin;
3809264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3810c959eef4SJed Brown   PetscValidPointer(type,2);
38119566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3812264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3813264ace61SBarry Smith   PetscFunctionReturn(0);
3814264ace61SBarry Smith }
3815264ace61SBarry Smith 
381667a56275SMatthew G Knepley /*@C
381767a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
381867a56275SMatthew G Knepley 
3819d083f849SBarry Smith   Collective on dm
382067a56275SMatthew G Knepley 
382167a56275SMatthew G Knepley   Input Parameters:
382267a56275SMatthew G Knepley + dm - the DM
382367a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
382467a56275SMatthew G Knepley 
382567a56275SMatthew G Knepley   Output Parameter:
382667a56275SMatthew G Knepley . M - pointer to new DM
382767a56275SMatthew G Knepley 
382867a56275SMatthew G Knepley   Notes:
382967a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
383067a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
383167a56275SMatthew G Knepley   of the input DM.
383267a56275SMatthew G Knepley 
383367a56275SMatthew G Knepley   Level: intermediate
383467a56275SMatthew G Knepley 
3835db781477SPatrick Sanan .seealso: `DMCreate()`
383667a56275SMatthew G Knepley @*/
383719fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
383867a56275SMatthew G Knepley {
383967a56275SMatthew G Knepley   DM             B;
384067a56275SMatthew G Knepley   char           convname[256];
3841c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
384267a56275SMatthew G Knepley 
384367a56275SMatthew G Knepley   PetscFunctionBegin;
384467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
384567a56275SMatthew G Knepley   PetscValidType(dm,1);
384667a56275SMatthew G Knepley   PetscValidPointer(M,3);
38479566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype));
38489566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3849c067b6caSMatthew G. Knepley   if (sametype) {
3850c067b6caSMatthew G. Knepley     *M   = dm;
38519566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) dm));
3852c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3853c067b6caSMatthew G. Knepley   } else {
38540298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
385567a56275SMatthew G Knepley 
385667a56275SMatthew G Knepley     /*
385767a56275SMatthew G Knepley        Order of precedence:
385867a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
385967a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
386067a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
386167a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
386267a56275SMatthew G Knepley        5) Use a really basic converter.
386367a56275SMatthew G Knepley     */
386467a56275SMatthew G Knepley 
386567a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
38669566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname,"DMConvert_",sizeof(convname)));
38679566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname)));
38689566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_",sizeof(convname)));
38699566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,newtype,sizeof(convname)));
38709566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_C",sizeof(convname)));
38719566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm,convname,&conv));
387267a56275SMatthew G Knepley     if (conv) goto foundconv;
387367a56275SMatthew G Knepley 
387467a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
38759566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
38769566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
38779566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname,"DMConvert_",sizeof(convname)));
38789566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname)));
38799566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_",sizeof(convname)));
38809566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,newtype,sizeof(convname)));
38819566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_C",sizeof(convname)));
38829566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B,convname,&conv));
388367a56275SMatthew G Knepley     if (conv) {
38849566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
388567a56275SMatthew G Knepley       goto foundconv;
388667a56275SMatthew G Knepley     }
388767a56275SMatthew G Knepley 
388867a56275SMatthew G Knepley #if 0
388967a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
389067a56275SMatthew G Knepley     conv = B->ops->convertfrom;
38919566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
389267a56275SMatthew G Knepley     if (conv) goto foundconv;
389367a56275SMatthew G Knepley 
389467a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
389567a56275SMatthew G Knepley     if (dm->ops->convert) {
389667a56275SMatthew G Knepley       conv = dm->ops->convert;
389767a56275SMatthew G Knepley     }
389867a56275SMatthew G Knepley     if (conv) goto foundconv;
389967a56275SMatthew G Knepley #endif
390067a56275SMatthew G Knepley 
390167a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
390298921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
390367a56275SMatthew G Knepley 
390467a56275SMatthew G Knepley foundconv:
39059566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert,dm,0,0,0));
39069566063dSJacob Faibussowitsch     PetscCall((*conv)(dm,newtype,M));
390712fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
390890b157c4SStefano Zampini     {
3909*4fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
39106858538eSMatthew G. Knepley 
3911*4fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
3912*4fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M,  maxCell,  Lstart,  L));
3913c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
39149566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
39159566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype,(char**)&(*M)->vectype));
39169566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
39179566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype,(char**)&(*M)->mattype));
391812fa691eSMatthew G. Knepley     }
39199566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert,dm,0,0,0));
392067a56275SMatthew G Knepley   }
39219566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject) *M));
392267a56275SMatthew G Knepley   PetscFunctionReturn(0);
392367a56275SMatthew G Knepley }
3924264ace61SBarry Smith 
3925264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3926264ace61SBarry Smith 
3927264ace61SBarry Smith /*@C
39281c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
39291c84c290SBarry Smith 
39301c84c290SBarry Smith   Not Collective
39311c84c290SBarry Smith 
39321c84c290SBarry Smith   Input Parameters:
39331c84c290SBarry Smith + name        - The name of a new user-defined creation routine
39341c84c290SBarry Smith - create_func - The creation routine itself
39351c84c290SBarry Smith 
39361c84c290SBarry Smith   Notes:
39371c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
39381c84c290SBarry Smith 
39391c84c290SBarry Smith   Sample usage:
39401c84c290SBarry Smith .vb
3941bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
39421c84c290SBarry Smith .ve
39431c84c290SBarry Smith 
39441c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
39451c84c290SBarry Smith .vb
39461c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
39471c84c290SBarry Smith     DMSetType(DM,"my_da");
39481c84c290SBarry Smith .ve
39491c84c290SBarry Smith    or at runtime via the option
39501c84c290SBarry Smith .vb
39511c84c290SBarry Smith     -da_type my_da
39521c84c290SBarry Smith .ve
3953264ace61SBarry Smith 
3954264ace61SBarry Smith   Level: advanced
39551c84c290SBarry Smith 
3956db781477SPatrick Sanan .seealso: `DMRegisterAll()`, `DMRegisterDestroy()`
39571c84c290SBarry Smith 
3958264ace61SBarry Smith @*/
3959bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3960264ace61SBarry Smith {
3961264ace61SBarry Smith   PetscFunctionBegin;
39629566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
39639566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList,sname,function));
3964264ace61SBarry Smith   PetscFunctionReturn(0);
3965264ace61SBarry Smith }
3966264ace61SBarry Smith 
3967b859378eSBarry Smith /*@C
396855849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3969b859378eSBarry Smith 
3970d083f849SBarry Smith   Collective on viewer
3971b859378eSBarry Smith 
3972b859378eSBarry Smith   Input Parameters:
3973b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3974b859378eSBarry Smith            some related function before a call to DMLoad().
3975b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3976b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3977b859378eSBarry Smith 
3978b859378eSBarry Smith    Level: intermediate
3979b859378eSBarry Smith 
3980b859378eSBarry Smith   Notes:
398155849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
3982b859378eSBarry Smith 
3983cd7e8a5eSksagiyam   Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
3984cd7e8a5eSksagiyam   meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
3985cd7e8a5eSksagiyam   before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
3986cd7e8a5eSksagiyam 
3987b859378eSBarry Smith   Notes for advanced users:
3988b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3989b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3990b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3991b859378eSBarry Smith   format is
3992b859378eSBarry Smith .vb
3993b859378eSBarry Smith      has not yet been determined
3994b859378eSBarry Smith .ve
3995b859378eSBarry Smith 
3996db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
3997b859378eSBarry Smith @*/
3998b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3999b859378eSBarry Smith {
40009331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
4001b859378eSBarry Smith 
4002b859378eSBarry Smith   PetscFunctionBegin;
4003b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
4004b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
40059566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
40069566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary));
40079566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5));
40089566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load,viewer,0,0,0));
40099331c7a4SMatthew G. Knepley   if (isbinary) {
40109331c7a4SMatthew G. Knepley     PetscInt classid;
40119331c7a4SMatthew G. Knepley     char     type[256];
4012b859378eSBarry Smith 
40139566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT));
40147a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID,PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
40159566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR));
40169566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
40179566063dSJacob Faibussowitsch     if (newdm->ops->load) PetscCall((*newdm->ops->load)(newdm,viewer));
40189331c7a4SMatthew G. Knepley   } else if (ishdf5) {
40199566063dSJacob Faibussowitsch     if (newdm->ops->load) PetscCall((*newdm->ops->load)(newdm,viewer));
40209331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
40219566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load,viewer,0,0,0));
4022b859378eSBarry Smith   PetscFunctionReturn(0);
4023b859378eSBarry Smith }
4024b859378eSBarry Smith 
40257da65231SMatthew G Knepley /******************************** FEM Support **********************************/
40267da65231SMatthew G Knepley 
4027a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4028a6dfd86eSKarl Rupp {
40291d47ebbbSSatish Balay   PetscInt       f;
40301b30c384SMatthew G Knepley 
40317da65231SMatthew G Knepley   PetscFunctionBegin;
403263a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
40331d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
40349566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
40357da65231SMatthew G Knepley   }
40367da65231SMatthew G Knepley   PetscFunctionReturn(0);
40377da65231SMatthew G Knepley }
40387da65231SMatthew G Knepley 
4039a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4040a6dfd86eSKarl Rupp {
40411b30c384SMatthew G Knepley   PetscInt       f, g;
40427da65231SMatthew G Knepley 
40437da65231SMatthew G Knepley   PetscFunctionBegin;
404463a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
40451d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
40469566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
40471d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
404863a3b9bcSJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f*cols+g])));
40497da65231SMatthew G Knepley     }
40509566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
40517da65231SMatthew G Knepley   }
40527da65231SMatthew G Knepley   PetscFunctionReturn(0);
40537da65231SMatthew G Knepley }
4054e7c4fc90SDmitry Karpeev 
40556113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4056e759306cSMatthew G. Knepley {
40570c5b8624SToby Isaac   PetscInt          localSize, bs;
40580c5b8624SToby Isaac   PetscMPIInt       size;
40590c5b8624SToby Isaac   Vec               x, xglob;
40600c5b8624SToby Isaac   const PetscScalar *xarray;
4061e759306cSMatthew G. Knepley 
4062e759306cSMatthew G. Knepley   PetscFunctionBegin;
40639566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size));
40649566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
40659566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
40669566063dSJacob Faibussowitsch   PetscCall(VecChop(x, tol));
40679566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name));
40680c5b8624SToby Isaac   if (size > 1) {
40699566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x,&localSize));
40709566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x,&xarray));
40719566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x,&bs));
40729566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob));
40730c5b8624SToby Isaac   } else {
40740c5b8624SToby Isaac     xglob = x;
40750c5b8624SToby Isaac   }
40769566063dSJacob Faibussowitsch   PetscCall(VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm))));
40770c5b8624SToby Isaac   if (size > 1) {
40789566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
40799566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x,&xarray));
40800c5b8624SToby Isaac   }
40819566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
4082e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4083e759306cSMatthew G. Knepley }
4084e759306cSMatthew G. Knepley 
408588ed4aceSMatthew G Knepley /*@
40861bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4087061576a5SJed Brown 
4088061576a5SJed Brown   Input Parameter:
4089061576a5SJed Brown . dm - The DM
4090061576a5SJed Brown 
4091061576a5SJed Brown   Output Parameter:
4092061576a5SJed Brown . section - The PetscSection
4093061576a5SJed Brown 
4094061576a5SJed Brown   Options Database Keys:
4095061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4096061576a5SJed Brown 
4097061576a5SJed Brown   Level: advanced
4098061576a5SJed Brown 
4099061576a5SJed Brown   Notes:
4100061576a5SJed Brown   Use DMGetLocalSection() in new code.
4101061576a5SJed Brown 
4102061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4103061576a5SJed Brown 
4104db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4105061576a5SJed Brown @*/
4106061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4107061576a5SJed Brown {
4108061576a5SJed Brown   PetscFunctionBegin;
41099566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm,section));
4110061576a5SJed Brown   PetscFunctionReturn(0);
4111061576a5SJed Brown }
4112061576a5SJed Brown 
4113061576a5SJed Brown /*@
4114061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
411588ed4aceSMatthew G Knepley 
411688ed4aceSMatthew G Knepley   Input Parameter:
411788ed4aceSMatthew G Knepley . dm - The DM
411888ed4aceSMatthew G Knepley 
411988ed4aceSMatthew G Knepley   Output Parameter:
412088ed4aceSMatthew G Knepley . section - The PetscSection
412188ed4aceSMatthew G Knepley 
4122e5893cccSMatthew G. Knepley   Options Database Keys:
4123e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4124e5893cccSMatthew G. Knepley 
412588ed4aceSMatthew G Knepley   Level: intermediate
412688ed4aceSMatthew G Knepley 
412788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
412888ed4aceSMatthew G Knepley 
4129db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetGlobalSection()`
413088ed4aceSMatthew G Knepley @*/
4131061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
41320adebc6cSBarry Smith {
413388ed4aceSMatthew G Knepley   PetscFunctionBegin;
413488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
413588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
41361bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4137e5e52638SMatthew G. Knepley     PetscInt d;
4138e5e52638SMatthew G. Knepley 
413945480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
414045480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject) dm;
414145480ffeSMatthew G. Knepley       PetscViewer       viewer;
414245480ffeSMatthew G. Knepley       PetscViewerFormat format;
414345480ffeSMatthew G. Knepley       PetscBool         flg;
414445480ffeSMatthew G. Knepley 
41459566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
41469566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
414745480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
41489566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
41499566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
415045480ffeSMatthew G. Knepley       }
415145480ffeSMatthew G. Knepley       if (flg) {
41529566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
41539566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
41549566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
415545480ffeSMatthew G. Knepley       }
415645480ffeSMatthew G. Knepley     }
41579566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createlocalsection)(dm));
41589566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view"));
41592f0f8703SMatthew G. Knepley   }
41601bb6d2a8SBarry Smith   *section = dm->localSection;
416188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
416288ed4aceSMatthew G Knepley }
416388ed4aceSMatthew G Knepley 
416488ed4aceSMatthew G Knepley /*@
41651bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4166061576a5SJed Brown 
4167061576a5SJed Brown   Input Parameters:
4168061576a5SJed Brown + dm - The DM
4169061576a5SJed Brown - section - The PetscSection
4170061576a5SJed Brown 
4171061576a5SJed Brown   Level: advanced
4172061576a5SJed Brown 
4173061576a5SJed Brown   Notes:
4174061576a5SJed Brown   Use DMSetLocalSection() in new code.
4175061576a5SJed Brown 
4176061576a5SJed Brown   Any existing Section will be destroyed
4177061576a5SJed Brown 
4178db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4179061576a5SJed Brown @*/
4180061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4181061576a5SJed Brown {
4182061576a5SJed Brown   PetscFunctionBegin;
41839566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm,section));
4184061576a5SJed Brown   PetscFunctionReturn(0);
4185061576a5SJed Brown }
4186061576a5SJed Brown 
4187061576a5SJed Brown /*@
4188061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
418988ed4aceSMatthew G Knepley 
419088ed4aceSMatthew G Knepley   Input Parameters:
419188ed4aceSMatthew G Knepley + dm - The DM
419288ed4aceSMatthew G Knepley - section - The PetscSection
419388ed4aceSMatthew G Knepley 
419488ed4aceSMatthew G Knepley   Level: intermediate
419588ed4aceSMatthew G Knepley 
419688ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
419788ed4aceSMatthew G Knepley 
4198db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetGlobalSection()`
419988ed4aceSMatthew G Knepley @*/
4200061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
42010adebc6cSBarry Smith {
4202c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4203af122d2aSMatthew G Knepley   PetscInt       f;
420488ed4aceSMatthew G Knepley 
420588ed4aceSMatthew G Knepley   PetscFunctionBegin;
420688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4207b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42089566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
42099566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
42101bb6d2a8SBarry Smith   dm->localSection = section;
42119566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4212af122d2aSMatthew G Knepley   if (numFields) {
42139566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4214af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
42150f21e855SMatthew G. Knepley       PetscObject disc;
4216af122d2aSMatthew G Knepley       const char *name;
4217af122d2aSMatthew G Knepley 
42189566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
42199566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
42209566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4221af122d2aSMatthew G Knepley     }
4222af122d2aSMatthew G Knepley   }
4223e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
42249566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
422588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
422688ed4aceSMatthew G Knepley }
422788ed4aceSMatthew G Knepley 
42289435951eSToby Isaac /*@
4229b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
42309435951eSToby Isaac 
4231e228b242SToby Isaac   not collective
4232e228b242SToby Isaac 
42339435951eSToby Isaac   Input Parameter:
42349435951eSToby Isaac . dm - The DM
42359435951eSToby Isaac 
4236d8d19677SJose E. Roman   Output Parameters:
42379435951eSToby Isaac + 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.
423879769bd5SJed Brown . 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.
423979769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
42409435951eSToby Isaac 
42419435951eSToby Isaac   Level: advanced
42429435951eSToby Isaac 
424379769bd5SJed Brown   Note: This gets borrowed references, so the user should not destroy the PetscSection, Mat, or Vec.
42449435951eSToby Isaac 
4245db781477SPatrick Sanan .seealso: `DMSetDefaultConstraints()`
42469435951eSToby Isaac @*/
424779769bd5SJed Brown PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
42489435951eSToby Isaac {
42499435951eSToby Isaac   PetscFunctionBegin;
42509435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42519566063dSJacob Faibussowitsch   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscCall((*dm->ops->createdefaultconstraints)(dm));
42523b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
42533b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
425479769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
42559435951eSToby Isaac   PetscFunctionReturn(0);
42569435951eSToby Isaac }
42579435951eSToby Isaac 
42589435951eSToby Isaac /*@
4259b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
42609435951eSToby Isaac 
426179769bd5SJed Brown   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().
42629435951eSToby Isaac 
426379769bd5SJed Brown   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.
42649435951eSToby Isaac 
4265e228b242SToby Isaac   collective on dm
4266e228b242SToby Isaac 
42679435951eSToby Isaac   Input Parameters:
42689435951eSToby Isaac + dm - The DM
426979769bd5SJed Brown . 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).
427079769bd5SJed Brown . 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).
427179769bd5SJed Brown - 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).
42729435951eSToby Isaac 
42739435951eSToby Isaac   Level: advanced
42749435951eSToby Isaac 
427579769bd5SJed Brown   Note: This increments the references of the PetscSection, Mat, and Vec, so they user can destroy them.
42769435951eSToby Isaac 
4277db781477SPatrick Sanan .seealso: `DMGetDefaultConstraints()`
42789435951eSToby Isaac @*/
427979769bd5SJed Brown PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
42809435951eSToby Isaac {
4281e228b242SToby Isaac   PetscMPIInt result;
42829435951eSToby Isaac 
42839435951eSToby Isaac   PetscFunctionBegin;
42849435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4285e228b242SToby Isaac   if (section) {
4286e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42879566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result));
42887a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4289e228b242SToby Isaac   }
4290e228b242SToby Isaac   if (mat) {
4291e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
42929566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result));
42937a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4294e228b242SToby Isaac   }
429579769bd5SJed Brown   if (bias) {
429679769bd5SJed Brown     PetscValidHeaderSpecific(bias,VEC_CLASSID,4);
42979566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)bias),&result));
429879769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint bias must have local communicator");
429979769bd5SJed Brown   }
43009566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43019566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
43023b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
43039566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
43049566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
43053b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
43069566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
43079566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
430879769bd5SJed Brown   dm->defaultConstraint.bias = bias;
43099435951eSToby Isaac   PetscFunctionReturn(0);
43109435951eSToby Isaac }
43119435951eSToby Isaac 
4312497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4313507e4973SMatthew G. Knepley /*
4314507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4315507e4973SMatthew G. Knepley 
4316507e4973SMatthew G. Knepley   Input Parameters:
4317507e4973SMatthew G. Knepley + dm - The DM
4318507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4319507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4320507e4973SMatthew G. Knepley 
4321507e4973SMatthew G. Knepley   Level: intermediate
4322507e4973SMatthew G. Knepley 
4323db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`
4324507e4973SMatthew G. Knepley */
4325f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4326507e4973SMatthew G. Knepley {
4327507e4973SMatthew G. Knepley   MPI_Comm        comm;
4328507e4973SMatthew G. Knepley   PetscLayout     layout;
4329507e4973SMatthew G. Knepley   const PetscInt *ranges;
4330507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4331507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4332507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4333507e4973SMatthew G. Knepley 
4334507e4973SMatthew G. Knepley   PetscFunctionBegin;
43359566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
4336507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43379566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
43389566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
43399566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
43409566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
43419566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
43429566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
43439566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
43449566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
43459566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4346507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4347f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4348507e4973SMatthew G. Knepley 
43499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
43509566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
43519566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
43529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
43539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
43549566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4355507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
435663a3b9bcSJacob 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;}
435763a3b9bcSJacob 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;}
4358507e4973SMatthew G. Knepley     if (gdof < 0) {
4359507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4360507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4361507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4362507e4973SMatthew G. Knepley 
43639566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset,size+1,ranges,&r));
4364507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
436563a3b9bcSJacob 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;}
4366507e4973SMatthew G. Knepley       }
4367507e4973SMatthew G. Knepley     }
4368507e4973SMatthew G. Knepley   }
43699566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
43709566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
43711c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4372507e4973SMatthew G. Knepley   if (!gvalid) {
43739566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4374507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4375507e4973SMatthew G. Knepley   }
4376507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4377507e4973SMatthew G. Knepley }
4378f741bcd2SMatthew G. Knepley #endif
4379507e4973SMatthew G. Knepley 
438088ed4aceSMatthew G Knepley /*@
4381e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
438288ed4aceSMatthew G Knepley 
4383d083f849SBarry Smith   Collective on dm
43848b1ab98fSJed Brown 
438588ed4aceSMatthew G Knepley   Input Parameter:
438688ed4aceSMatthew G Knepley . dm - The DM
438788ed4aceSMatthew G Knepley 
438888ed4aceSMatthew G Knepley   Output Parameter:
438988ed4aceSMatthew G Knepley . section - The PetscSection
439088ed4aceSMatthew G Knepley 
439188ed4aceSMatthew G Knepley   Level: intermediate
439288ed4aceSMatthew G Knepley 
439388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
439488ed4aceSMatthew G Knepley 
4395db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`
439688ed4aceSMatthew G Knepley @*/
4397e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
43980adebc6cSBarry Smith {
439988ed4aceSMatthew G Knepley   PetscFunctionBegin;
440088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
440188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
44021bb6d2a8SBarry Smith   if (!dm->globalSection) {
4403fd59a867SMatthew G. Knepley     PetscSection s;
4404fd59a867SMatthew G. Knepley 
44059566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
44067a8be351SBarry Smith     PetscCheck(s,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
44077a8be351SBarry Smith     PetscCheck(dm->sf,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
44089566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
44099566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
44109566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
44119566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
441288ed4aceSMatthew G Knepley   }
44131bb6d2a8SBarry Smith   *section = dm->globalSection;
441488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
441588ed4aceSMatthew G Knepley }
441688ed4aceSMatthew G Knepley 
4417b21d0597SMatthew G Knepley /*@
4418e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4419b21d0597SMatthew G Knepley 
4420b21d0597SMatthew G Knepley   Input Parameters:
4421b21d0597SMatthew G Knepley + dm - The DM
44225080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4423b21d0597SMatthew G Knepley 
4424b21d0597SMatthew G Knepley   Level: intermediate
4425b21d0597SMatthew G Knepley 
4426b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4427b21d0597SMatthew G Knepley 
4428db781477SPatrick Sanan .seealso: `DMGetGlobalSection()`, `DMSetLocalSection()`
4429b21d0597SMatthew G Knepley @*/
4430e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
44310adebc6cSBarry Smith {
4432b21d0597SMatthew G Knepley   PetscFunctionBegin;
4433b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44345080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
44359566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44369566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
44371bb6d2a8SBarry Smith   dm->globalSection = section;
4438497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
44399566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4440507e4973SMatthew G. Knepley #endif
4441b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4442b21d0597SMatthew G Knepley }
4443b21d0597SMatthew G Knepley 
444488ed4aceSMatthew G Knepley /*@
44451bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
444688ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
444788ed4aceSMatthew G Knepley 
444888ed4aceSMatthew G Knepley   Input Parameter:
444988ed4aceSMatthew G Knepley . dm - The DM
445088ed4aceSMatthew G Knepley 
445188ed4aceSMatthew G Knepley   Output Parameter:
445288ed4aceSMatthew G Knepley . sf - The PetscSF
445388ed4aceSMatthew G Knepley 
445488ed4aceSMatthew G Knepley   Level: intermediate
445588ed4aceSMatthew G Knepley 
445688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
445788ed4aceSMatthew G Knepley 
4458db781477SPatrick Sanan .seealso: `DMSetSectionSF()`, `DMCreateSectionSF()`
445988ed4aceSMatthew G Knepley @*/
44601bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
44610adebc6cSBarry Smith {
446288ed4aceSMatthew G Knepley   PetscInt       nroots;
446388ed4aceSMatthew G Knepley 
446488ed4aceSMatthew G Knepley   PetscFunctionBegin;
446588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
446688ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
44671bb6d2a8SBarry Smith   if (!dm->sectionSF) {
44689566063dSJacob Faibussowitsch     PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF));
446933907cc2SStefano Zampini   }
44709566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
447188ed4aceSMatthew G Knepley   if (nroots < 0) {
447288ed4aceSMatthew G Knepley     PetscSection section, gSection;
447388ed4aceSMatthew G Knepley 
44749566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
447531ea6d37SMatthew G Knepley     if (section) {
44769566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
44779566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
447831ea6d37SMatthew G Knepley     } else {
44790298fd71SBarry Smith       *sf = NULL;
448031ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
448131ea6d37SMatthew G Knepley     }
448288ed4aceSMatthew G Knepley   }
44831bb6d2a8SBarry Smith   *sf = dm->sectionSF;
448488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
448588ed4aceSMatthew G Knepley }
448688ed4aceSMatthew G Knepley 
448788ed4aceSMatthew G Knepley /*@
44881bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
448988ed4aceSMatthew G Knepley 
449088ed4aceSMatthew G Knepley   Input Parameters:
449188ed4aceSMatthew G Knepley + dm - The DM
449288ed4aceSMatthew G Knepley - sf - The PetscSF
449388ed4aceSMatthew G Knepley 
449488ed4aceSMatthew G Knepley   Level: intermediate
449588ed4aceSMatthew G Knepley 
449688ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
449788ed4aceSMatthew G Knepley 
4498db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMCreateSectionSF()`
449988ed4aceSMatthew G Knepley @*/
45001bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
45010adebc6cSBarry Smith {
450288ed4aceSMatthew G Knepley   PetscFunctionBegin;
450388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4504b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
45059566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
45069566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
45071bb6d2a8SBarry Smith   dm->sectionSF = sf;
450888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
450988ed4aceSMatthew G Knepley }
451088ed4aceSMatthew G Knepley 
451188ed4aceSMatthew G Knepley /*@C
45121bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
451388ed4aceSMatthew G Knepley   describing the data layout.
451488ed4aceSMatthew G Knepley 
451588ed4aceSMatthew G Knepley   Input Parameters:
451688ed4aceSMatthew G Knepley + dm - The DM
451788ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
451888ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
451988ed4aceSMatthew G Knepley 
45201bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
452188ed4aceSMatthew G Knepley 
45221bb6d2a8SBarry Smith   Level: developer
45231bb6d2a8SBarry Smith 
45241bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
45251bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
45261bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
45271bb6d2a8SBarry Smith 
4528db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
452988ed4aceSMatthew G Knepley @*/
45301bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
453188ed4aceSMatthew G Knepley {
453288ed4aceSMatthew G Knepley   PetscFunctionBegin;
453388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45349566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
453588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
453688ed4aceSMatthew G Knepley }
4537af122d2aSMatthew G Knepley 
4538b21d0597SMatthew G Knepley /*@
4539b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4540b21d0597SMatthew G Knepley 
4541b21d0597SMatthew G Knepley   Input Parameter:
4542b21d0597SMatthew G Knepley . dm - The DM
4543b21d0597SMatthew G Knepley 
4544b21d0597SMatthew G Knepley   Output Parameter:
4545b21d0597SMatthew G Knepley . sf - The PetscSF
4546b21d0597SMatthew G Knepley 
4547b21d0597SMatthew G Knepley   Level: intermediate
4548b21d0597SMatthew G Knepley 
4549b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4550b21d0597SMatthew G Knepley 
4551db781477SPatrick Sanan .seealso: `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4552b21d0597SMatthew G Knepley @*/
45530adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
45540adebc6cSBarry Smith {
4555b21d0597SMatthew G Knepley   PetscFunctionBegin;
4556b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4557b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4558b21d0597SMatthew G Knepley   *sf = dm->sf;
4559b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4560b21d0597SMatthew G Knepley }
4561b21d0597SMatthew G Knepley 
4562057b4bcdSMatthew G Knepley /*@
4563057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4564057b4bcdSMatthew G Knepley 
4565057b4bcdSMatthew G Knepley   Input Parameters:
4566057b4bcdSMatthew G Knepley + dm - The DM
4567057b4bcdSMatthew G Knepley - sf - The PetscSF
4568057b4bcdSMatthew G Knepley 
4569057b4bcdSMatthew G Knepley   Level: intermediate
4570057b4bcdSMatthew G Knepley 
4571db781477SPatrick Sanan .seealso: `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4572057b4bcdSMatthew G Knepley @*/
45730adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
45740adebc6cSBarry Smith {
4575057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4576057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4577b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
45789566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
45799566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4580057b4bcdSMatthew G Knepley   dm->sf = sf;
4581057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4582057b4bcdSMatthew G Knepley }
4583057b4bcdSMatthew G Knepley 
45844f37162bSMatthew G. Knepley /*@
45854f37162bSMatthew G. Knepley   DMGetNaturalSF - Get the PetscSF encoding the map back to the original mesh ordering
45864f37162bSMatthew G. Knepley 
45874f37162bSMatthew G. Knepley   Input Parameter:
45884f37162bSMatthew G. Knepley . dm - The DM
45894f37162bSMatthew G. Knepley 
45904f37162bSMatthew G. Knepley   Output Parameter:
45914f37162bSMatthew G. Knepley . sf - The PetscSF
45924f37162bSMatthew G. Knepley 
45934f37162bSMatthew G. Knepley   Level: intermediate
45944f37162bSMatthew G. Knepley 
45954f37162bSMatthew G. Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
45964f37162bSMatthew G. Knepley 
4597db781477SPatrick Sanan .seealso: `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
45984f37162bSMatthew G. Knepley @*/
45994f37162bSMatthew G. Knepley PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
46004f37162bSMatthew G. Knepley {
46014f37162bSMatthew G. Knepley   PetscFunctionBegin;
46024f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46034f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
46044f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
46054f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
46064f37162bSMatthew G. Knepley }
46074f37162bSMatthew G. Knepley 
46084f37162bSMatthew G. Knepley /*@
46094f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
46104f37162bSMatthew G. Knepley 
46114f37162bSMatthew G. Knepley   Input Parameters:
46124f37162bSMatthew G. Knepley + dm - The DM
46134f37162bSMatthew G. Knepley - sf - The PetscSF
46144f37162bSMatthew G. Knepley 
46154f37162bSMatthew G. Knepley   Level: intermediate
46164f37162bSMatthew G. Knepley 
4617db781477SPatrick Sanan .seealso: `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
46184f37162bSMatthew G. Knepley @*/
46194f37162bSMatthew G. Knepley PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
46204f37162bSMatthew G. Knepley {
46214f37162bSMatthew G. Knepley   PetscFunctionBegin;
46224f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46234f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46249566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
46259566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
46264f37162bSMatthew G. Knepley   dm->sfNatural = sf;
46274f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
46284f37162bSMatthew G. Knepley }
46294f37162bSMatthew G. Knepley 
463034aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
463134aa8a36SMatthew G. Knepley {
463234aa8a36SMatthew G. Knepley   PetscClassId   id;
463334aa8a36SMatthew G. Knepley 
463434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
46359566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
463634aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
46379566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
463834aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
46399566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
464017c1d62eSMatthew G. Knepley   } else {
46419566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
464234aa8a36SMatthew G. Knepley   }
464334aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
464434aa8a36SMatthew G. Knepley }
464534aa8a36SMatthew G. Knepley 
464644a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
464744a7f3ddSMatthew G. Knepley {
464844a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
464944a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
465044a7f3ddSMatthew G. Knepley 
465144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
465244a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
46539566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
465444a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4655e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
46569566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
465744a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
465844a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
465944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
466044a7f3ddSMatthew G. Knepley }
466144a7f3ddSMatthew G. Knepley 
466244a7f3ddSMatthew G. Knepley /*@
466344a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
466444a7f3ddSMatthew G. Knepley 
4665d083f849SBarry Smith   Logically collective on dm
466644a7f3ddSMatthew G. Knepley 
466744a7f3ddSMatthew G. Knepley   Input Parameter:
466844a7f3ddSMatthew G. Knepley . dm - The DM
466944a7f3ddSMatthew G. Knepley 
467044a7f3ddSMatthew G. Knepley   Level: intermediate
467144a7f3ddSMatthew G. Knepley 
4672db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
467344a7f3ddSMatthew G. Knepley @*/
467444a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
467544a7f3ddSMatthew G. Knepley {
467644a7f3ddSMatthew G. Knepley   PetscInt       f;
467744a7f3ddSMatthew G. Knepley 
467844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
467944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
468044a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
46819566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
46829566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
468344a7f3ddSMatthew G. Knepley   }
46849566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
468544a7f3ddSMatthew G. Knepley   dm->fields = NULL;
468644a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
468744a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
468844a7f3ddSMatthew G. Knepley }
468944a7f3ddSMatthew G. Knepley 
4690689b5837SMatthew G. Knepley /*@
4691689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4692689b5837SMatthew G. Knepley 
4693689b5837SMatthew G. Knepley   Not collective
4694689b5837SMatthew G. Knepley 
4695689b5837SMatthew G. Knepley   Input Parameter:
4696689b5837SMatthew G. Knepley . dm - The DM
4697689b5837SMatthew G. Knepley 
4698689b5837SMatthew G. Knepley   Output Parameter:
4699689b5837SMatthew G. Knepley . Nf - The number of fields
4700689b5837SMatthew G. Knepley 
4701689b5837SMatthew G. Knepley   Level: intermediate
4702689b5837SMatthew G. Knepley 
4703db781477SPatrick Sanan .seealso: `DMSetNumFields()`, `DMSetField()`
4704689b5837SMatthew G. Knepley @*/
47050f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
47060f21e855SMatthew G. Knepley {
47070f21e855SMatthew G. Knepley   PetscFunctionBegin;
47080f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4709534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
471044a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4711af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4712af122d2aSMatthew G Knepley }
4713af122d2aSMatthew G Knepley 
4714689b5837SMatthew G. Knepley /*@
4715689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4716689b5837SMatthew G. Knepley 
4717d083f849SBarry Smith   Logically collective on dm
4718689b5837SMatthew G. Knepley 
4719689b5837SMatthew G. Knepley   Input Parameters:
4720689b5837SMatthew G. Knepley + dm - The DM
4721689b5837SMatthew G. Knepley - Nf - The number of fields
4722689b5837SMatthew G. Knepley 
4723689b5837SMatthew G. Knepley   Level: intermediate
4724689b5837SMatthew G. Knepley 
4725db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetField()`
4726689b5837SMatthew G. Knepley @*/
4727af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4728af122d2aSMatthew G Knepley {
47290f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4730af122d2aSMatthew G Knepley 
4731af122d2aSMatthew G Knepley   PetscFunctionBegin;
4732af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47339566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
47340f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
47350f21e855SMatthew G. Knepley     PetscContainer obj;
47360f21e855SMatthew G. Knepley 
47379566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj));
47389566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject) obj));
47399566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4740af122d2aSMatthew G Knepley   }
4741af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4742af122d2aSMatthew G Knepley }
4743af122d2aSMatthew G Knepley 
4744c1929be8SMatthew G. Knepley /*@
4745c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4746c1929be8SMatthew G. Knepley 
4747c1929be8SMatthew G. Knepley   Not collective
4748c1929be8SMatthew G. Knepley 
4749c1929be8SMatthew G. Knepley   Input Parameters:
4750c1929be8SMatthew G. Knepley + dm - The DM
4751c1929be8SMatthew G. Knepley - f  - The field number
4752c1929be8SMatthew G. Knepley 
475344a7f3ddSMatthew G. Knepley   Output Parameters:
475444a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
475544a7f3ddSMatthew G. Knepley - field - The discretization object
4756c1929be8SMatthew G. Knepley 
475744a7f3ddSMatthew G. Knepley   Level: intermediate
4758c1929be8SMatthew G. Knepley 
4759db781477SPatrick Sanan .seealso: `DMAddField()`, `DMSetField()`
4760c1929be8SMatthew G. Knepley @*/
476144a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4762af122d2aSMatthew G Knepley {
4763af122d2aSMatthew G Knepley   PetscFunctionBegin;
4764af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4765064a246eSJacob Faibussowitsch   PetscValidPointer(field, 4);
47667a8be351SBarry 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);
476744a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
476844a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4769decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4770decb47aaSMatthew G. Knepley }
4771decb47aaSMatthew G. Knepley 
4772083401c6SMatthew G. Knepley /* Does not clear the DS */
4773083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4774083401c6SMatthew G. Knepley {
4775083401c6SMatthew G. Knepley   PetscFunctionBegin;
47769566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f+1));
47779566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
47789566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4779083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4780083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
47819566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
47829566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) field));
4783083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4784083401c6SMatthew G. Knepley }
4785083401c6SMatthew G. Knepley 
4786c1929be8SMatthew G. Knepley /*@
4787c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4788c1929be8SMatthew G. Knepley 
4789d083f849SBarry Smith   Logically collective on dm
4790c1929be8SMatthew G. Knepley 
4791c1929be8SMatthew G. Knepley   Input Parameters:
4792c1929be8SMatthew G. Knepley + dm    - The DM
4793c1929be8SMatthew G. Knepley . f     - The field number
479444a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4795c1929be8SMatthew G. Knepley - field - The discretization object
4796c1929be8SMatthew G. Knepley 
479744a7f3ddSMatthew G. Knepley   Level: intermediate
4798c1929be8SMatthew G. Knepley 
4799db781477SPatrick Sanan .seealso: `DMAddField()`, `DMGetField()`
4800c1929be8SMatthew G. Knepley @*/
480144a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4802decb47aaSMatthew G. Knepley {
4803decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4804decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4805e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
480644a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
48077a8be351SBarry Smith   PetscCheck(f >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
48089566063dSJacob Faibussowitsch   PetscCall(DMSetField_Internal(dm, f, label, field));
48099566063dSJacob Faibussowitsch   PetscCall(DMSetDefaultAdjacency_Private(dm, f, field));
48109566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
481144a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
481244a7f3ddSMatthew G. Knepley }
481344a7f3ddSMatthew G. Knepley 
481444a7f3ddSMatthew G. Knepley /*@
481544a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
481644a7f3ddSMatthew G. Knepley 
4817d083f849SBarry Smith   Logically collective on dm
481844a7f3ddSMatthew G. Knepley 
481944a7f3ddSMatthew G. Knepley   Input Parameters:
482044a7f3ddSMatthew G. Knepley + dm    - The DM
482144a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
482244a7f3ddSMatthew G. Knepley - field - The discretization object
482344a7f3ddSMatthew G. Knepley 
482444a7f3ddSMatthew G. Knepley   Level: intermediate
482544a7f3ddSMatthew G. Knepley 
4826db781477SPatrick Sanan .seealso: `DMSetField()`, `DMGetField()`
482744a7f3ddSMatthew G. Knepley @*/
482844a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
482944a7f3ddSMatthew G. Knepley {
483044a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
483144a7f3ddSMatthew G. Knepley 
483244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
483344a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4834064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
483544a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
48369566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf+1));
483744a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
483844a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
48399566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
48409566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) field));
48419566063dSJacob Faibussowitsch   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, field));
48429566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
4843af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4844af122d2aSMatthew G Knepley }
48456636e97aSMatthew G Knepley 
4846e5e52638SMatthew G. Knepley /*@
4847e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4848e0b68406SMatthew Knepley 
4849e0b68406SMatthew Knepley   Logically collective on dm
4850e0b68406SMatthew Knepley 
4851e0b68406SMatthew Knepley   Input Parameters:
4852e0b68406SMatthew Knepley + dm          - The DM
4853e0b68406SMatthew Knepley . f           - The field index
4854e0b68406SMatthew Knepley - avoidTensor - The flag to avoid defining the field on tensor cells
4855e0b68406SMatthew Knepley 
4856e0b68406SMatthew Knepley   Level: intermediate
4857e0b68406SMatthew Knepley 
4858db781477SPatrick Sanan .seealso: `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
4859e0b68406SMatthew Knepley @*/
4860e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
4861e0b68406SMatthew Knepley {
4862e0b68406SMatthew Knepley   PetscFunctionBegin;
486363a3b9bcSJacob 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);
4864e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
4865e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4866e0b68406SMatthew Knepley }
4867e0b68406SMatthew Knepley 
4868e0b68406SMatthew Knepley /*@
4869e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
4870e0b68406SMatthew Knepley 
4871e0b68406SMatthew Knepley   Logically collective on dm
4872e0b68406SMatthew Knepley 
4873e0b68406SMatthew Knepley   Input Parameters:
4874e0b68406SMatthew Knepley + dm          - The DM
4875e0b68406SMatthew Knepley - f           - The field index
4876e0b68406SMatthew Knepley 
4877e0b68406SMatthew Knepley   Output Parameter:
4878e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
4879e0b68406SMatthew Knepley 
4880e0b68406SMatthew Knepley   Level: intermediate
4881e0b68406SMatthew Knepley 
4882db781477SPatrick Sanan .seealso: `DMSetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
4883e0b68406SMatthew Knepley @*/
4884e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
4885e0b68406SMatthew Knepley {
4886e0b68406SMatthew Knepley   PetscFunctionBegin;
488763a3b9bcSJacob 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);
4888e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
4889e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4890e0b68406SMatthew Knepley }
4891e0b68406SMatthew Knepley 
4892e0b68406SMatthew Knepley /*@
4893e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4894e5e52638SMatthew G. Knepley 
4895d083f849SBarry Smith   Collective on dm
4896e5e52638SMatthew G. Knepley 
4897e5e52638SMatthew G. Knepley   Input Parameter:
4898e5e52638SMatthew G. Knepley . dm - The DM
4899e5e52638SMatthew G. Knepley 
4900e5e52638SMatthew G. Knepley   Output Parameter:
4901e5e52638SMatthew G. Knepley . newdm - The DM
4902e5e52638SMatthew G. Knepley 
4903e5e52638SMatthew G. Knepley   Level: advanced
4904e5e52638SMatthew G. Knepley 
4905db781477SPatrick Sanan .seealso: `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
4906e5e52638SMatthew G. Knepley @*/
4907e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4908e5e52638SMatthew G. Knepley {
4909e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4910e5e52638SMatthew G. Knepley 
4911e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4912e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
49139566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49149566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
4915e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4916e5e52638SMatthew G. Knepley     DMLabel     label;
4917e5e52638SMatthew G. Knepley     PetscObject field;
491834aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4919e5e52638SMatthew G. Knepley 
49209566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
49219566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
49229566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
49239566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
492434aa8a36SMatthew G. Knepley   }
492534aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
492634aa8a36SMatthew G. Knepley }
492734aa8a36SMatthew G. Knepley 
492834aa8a36SMatthew G. Knepley /*@
492934aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
493034aa8a36SMatthew G. Knepley 
493134aa8a36SMatthew G. Knepley   Not collective
493234aa8a36SMatthew G. Knepley 
493334aa8a36SMatthew G. Knepley   Input Parameters:
493434aa8a36SMatthew G. Knepley + dm - The DM object
493534aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
493634aa8a36SMatthew G. Knepley 
4937d8d19677SJose E. Roman   Output Parameters:
493834aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
493934aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
494034aa8a36SMatthew G. Knepley 
494134aa8a36SMatthew G. Knepley   Notes:
494234aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
494334aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
494434aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4945979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
494634aa8a36SMatthew G. Knepley 
494734aa8a36SMatthew G. Knepley   Level: developer
494834aa8a36SMatthew G. Knepley 
4949db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
495034aa8a36SMatthew G. Knepley @*/
495134aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
495234aa8a36SMatthew G. Knepley {
495334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
495434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4955534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4956534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
495734aa8a36SMatthew G. Knepley   if (f < 0) {
495834aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
495934aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
496034aa8a36SMatthew G. Knepley   } else {
496134aa8a36SMatthew G. Knepley     PetscInt       Nf;
496234aa8a36SMatthew G. Knepley 
49639566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
49647a8be351SBarry Smith     PetscCheck(f < Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
496534aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
496634aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
496734aa8a36SMatthew G. Knepley   }
496834aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
496934aa8a36SMatthew G. Knepley }
497034aa8a36SMatthew G. Knepley 
497134aa8a36SMatthew G. Knepley /*@
497234aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
497334aa8a36SMatthew G. Knepley 
497434aa8a36SMatthew G. Knepley   Not collective
497534aa8a36SMatthew G. Knepley 
497634aa8a36SMatthew G. Knepley   Input Parameters:
497734aa8a36SMatthew G. Knepley + dm         - The DM object
497834aa8a36SMatthew G. Knepley . f          - The field number
497934aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
498034aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
498134aa8a36SMatthew G. Knepley 
498234aa8a36SMatthew G. Knepley   Notes:
498334aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
498434aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
498534aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4986979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
498734aa8a36SMatthew G. Knepley 
498834aa8a36SMatthew G. Knepley   Level: developer
498934aa8a36SMatthew G. Knepley 
4990db781477SPatrick Sanan .seealso: `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
499134aa8a36SMatthew G. Knepley @*/
499234aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
499334aa8a36SMatthew G. Knepley {
499434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
499534aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
499634aa8a36SMatthew G. Knepley   if (f < 0) {
499734aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
499834aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
499934aa8a36SMatthew G. Knepley   } else {
500034aa8a36SMatthew G. Knepley     PetscInt       Nf;
500134aa8a36SMatthew G. Knepley 
50029566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
50037a8be351SBarry Smith     PetscCheck(f < Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
500434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
500534aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5006e5e52638SMatthew G. Knepley   }
5007e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5008e5e52638SMatthew G. Knepley }
5009e5e52638SMatthew G. Knepley 
5010b0441da4SMatthew G. Knepley /*@
5011b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5012b0441da4SMatthew G. Knepley 
5013b0441da4SMatthew G. Knepley   Not collective
5014b0441da4SMatthew G. Knepley 
5015f899ff85SJose E. Roman   Input Parameter:
5016b0441da4SMatthew G. Knepley . dm - The DM object
5017b0441da4SMatthew G. Knepley 
5018d8d19677SJose E. Roman   Output Parameters:
5019b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5020b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5021b0441da4SMatthew G. Knepley 
5022b0441da4SMatthew G. Knepley   Notes:
5023b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5024b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5025b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5026b0441da4SMatthew G. Knepley 
5027b0441da4SMatthew G. Knepley   Level: developer
5028b0441da4SMatthew G. Knepley 
5029db781477SPatrick Sanan .seealso: `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5030b0441da4SMatthew G. Knepley @*/
5031b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5032b0441da4SMatthew G. Knepley {
5033b0441da4SMatthew G. Knepley   PetscInt       Nf;
5034b0441da4SMatthew G. Knepley 
5035b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5036b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5037064a246eSJacob Faibussowitsch   if (useCone)    PetscValidBoolPointer(useCone, 2);
5038064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
50399566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5040b0441da4SMatthew G. Knepley   if (!Nf) {
50419566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5042b0441da4SMatthew G. Knepley   } else {
50439566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5044b0441da4SMatthew G. Knepley   }
5045b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5046b0441da4SMatthew G. Knepley }
5047b0441da4SMatthew G. Knepley 
5048b0441da4SMatthew G. Knepley /*@
5049b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5050b0441da4SMatthew G. Knepley 
5051b0441da4SMatthew G. Knepley   Not collective
5052b0441da4SMatthew G. Knepley 
5053b0441da4SMatthew G. Knepley   Input Parameters:
5054b0441da4SMatthew G. Knepley + dm         - The DM object
5055b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5056b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5057b0441da4SMatthew G. Knepley 
5058b0441da4SMatthew G. Knepley   Notes:
5059b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5060b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5061b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5062b0441da4SMatthew G. Knepley 
5063b0441da4SMatthew G. Knepley   Level: developer
5064b0441da4SMatthew G. Knepley 
5065db781477SPatrick Sanan .seealso: `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5066b0441da4SMatthew G. Knepley @*/
5067b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5068b0441da4SMatthew G. Knepley {
5069b0441da4SMatthew G. Knepley   PetscInt       Nf;
5070b0441da4SMatthew G. Knepley 
5071b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5072b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50739566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5074b0441da4SMatthew G. Knepley   if (!Nf) {
50759566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5076b0441da4SMatthew G. Knepley   } else {
50779566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5078e5e52638SMatthew G. Knepley   }
5079e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5080e5e52638SMatthew G. Knepley }
5081e5e52638SMatthew G. Knepley 
5082799db056SMatthew G. Knepley PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5083783e2ec8SMatthew G. Knepley {
5084799db056SMatthew G. Knepley   DM           plex;
5085799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5086799db056SMatthew G. Knepley   const char **names;
5087799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5088799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5089799db056SMatthew G. Knepley   size_t       len;
5090799db056SMatthew G. Knepley   MPI_Comm     comm;
5091799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5092783e2ec8SMatthew G. Knepley 
5093783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5094799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject) dm, &comm));
5095799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5096799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5097799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5098799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5099799db056SMatthew G. Knepley     PetscDS  dsBC;
5100799db056SMatthew G. Knepley     PetscInt numBd;
5101799db056SMatthew G. Knepley 
5102799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5103799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5104799db056SMatthew G. Knepley     maxLabels += numBd;
5105799db056SMatthew G. Knepley   }
5106799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5107799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5108799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5109799db056SMatthew G. Knepley     PetscDS  dsBC;
5110799db056SMatthew G. Knepley     PetscInt numBd, bd;
5111799db056SMatthew G. Knepley 
5112799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5113799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5114799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5115799db056SMatthew G. Knepley       DMLabel      label;
5116799db056SMatthew G. Knepley       PetscInt     field;
5117799db056SMatthew G. Knepley       PetscObject  obj;
5118799db056SMatthew G. Knepley       PetscClassId id;
5119799db056SMatthew G. Knepley 
5120799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
51219566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
51229566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5123799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
5124799db056SMatthew G. Knepley       for (l = 0; l < Nl; ++l) if (labels[l] == label) break;
5125799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5126783e2ec8SMatthew G. Knepley     }
5127799db056SMatthew G. Knepley   }
5128799db056SMatthew G. Knepley   /* Get label names */
5129799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5130799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject) labels[l], &names[l]));
5131799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {PetscCall(PetscStrlen(names[l], &len)); maxLen = PetscMax(maxLen, (PetscInt) len+2);}
5132799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5133799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5134799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5135799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscStrcpy(&sendNames[gmaxLen*l], names[l]));
5136799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5137799db056SMatthew G. Knepley   /* Put all names on all processes */
5138799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size+1, &displs));
5139799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5140799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p+1] = displs[p] + counts[p];
5141799db056SMatthew G. Knepley   gNl = displs[size];
5142799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) {counts[p] *= gmaxLen; displs[p] *= gmaxLen;}
5143799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5144799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5145799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5146799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5147799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5148799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l*gmaxLen], &glabels[gl]));
5149799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l*gmaxLen], rank);
5150799db056SMatthew G. Knepley     for (m = 0; m < gl; ++m) if (glabels[m] == glabels[gl]) continue;
51519566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5152799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
51539566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5154799db056SMatthew G. Knepley     ++gl;
5155783e2ec8SMatthew G. Knepley   }
5156799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
5157783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5158783e2ec8SMatthew G. Knepley }
5159783e2ec8SMatthew G. Knepley 
5160e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5161e5e52638SMatthew G. Knepley {
5162e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5163e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5164e5e52638SMatthew G. Knepley 
5165e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5166e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
51679566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5168e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5169b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
51709566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5171e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5172e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5173e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5174e5e52638SMatthew G. Knepley }
5175e5e52638SMatthew G. Knepley 
5176e5e52638SMatthew G. Knepley /*@
5177e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5178e5e52638SMatthew G. Knepley 
5179e5e52638SMatthew G. Knepley   Not collective
5180e5e52638SMatthew G. Knepley 
5181e5e52638SMatthew G. Knepley   Input Parameter:
5182e5e52638SMatthew G. Knepley . dm - The DM
5183e5e52638SMatthew G. Knepley 
5184e5e52638SMatthew G. Knepley   Output Parameter:
5185e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5186e5e52638SMatthew G. Knepley 
5187e5e52638SMatthew G. Knepley   Level: intermediate
5188e5e52638SMatthew G. Knepley 
5189db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMGetCellDS()`
5190e5e52638SMatthew G. Knepley @*/
5191e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5192e5e52638SMatthew G. Knepley {
5193e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5194e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5195534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5196e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5197e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5198e5e52638SMatthew G. Knepley }
5199e5e52638SMatthew G. Knepley 
5200e5e52638SMatthew G. Knepley /*@
5201e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5202e5e52638SMatthew G. Knepley 
5203d083f849SBarry Smith   Logically collective on dm
5204e5e52638SMatthew G. Knepley 
5205e5e52638SMatthew G. Knepley   Input Parameter:
5206e5e52638SMatthew G. Knepley . dm - The DM
5207e5e52638SMatthew G. Knepley 
5208e5e52638SMatthew G. Knepley   Level: intermediate
5209e5e52638SMatthew G. Knepley 
5210db781477SPatrick Sanan .seealso: `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5211e5e52638SMatthew G. Knepley @*/
5212e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5213e5e52638SMatthew G. Knepley {
5214e5e52638SMatthew G. Knepley   PetscInt       s;
5215e5e52638SMatthew G. Knepley 
5216e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5217e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5218e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
52199566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
52209566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
52219566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5222e5e52638SMatthew G. Knepley   }
52239566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5224e5e52638SMatthew G. Knepley   dm->probs = NULL;
5225e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5226e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5227e5e52638SMatthew G. Knepley }
5228e5e52638SMatthew G. Knepley 
5229e5e52638SMatthew G. Knepley /*@
5230e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5231e5e52638SMatthew G. Knepley 
5232e5e52638SMatthew G. Knepley   Not collective
5233e5e52638SMatthew G. Knepley 
5234e5e52638SMatthew G. Knepley   Input Parameter:
5235e5e52638SMatthew G. Knepley . dm    - The DM
5236e5e52638SMatthew G. Knepley 
5237e5e52638SMatthew G. Knepley   Output Parameter:
5238e5e52638SMatthew G. Knepley . prob - The default PetscDS
5239e5e52638SMatthew G. Knepley 
5240e5e52638SMatthew G. Knepley   Level: intermediate
5241e5e52638SMatthew G. Knepley 
5242db781477SPatrick Sanan .seealso: `DMGetCellDS()`, `DMGetRegionDS()`
5243e5e52638SMatthew G. Knepley @*/
5244e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5245e5e52638SMatthew G. Knepley {
5246e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5247e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5248e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5249b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5250b0143b4dSMatthew G. Knepley     PetscDS ds;
5251b0143b4dSMatthew G. Knepley 
52529566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
52539566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, NULL, ds));
52549566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5255b0143b4dSMatthew G. Knepley   }
5256b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5257e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5258e5e52638SMatthew G. Knepley }
5259e5e52638SMatthew G. Knepley 
5260e5e52638SMatthew G. Knepley /*@
5261e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5262e5e52638SMatthew G. Knepley 
5263e5e52638SMatthew G. Knepley   Not collective
5264e5e52638SMatthew G. Knepley 
5265e5e52638SMatthew G. Knepley   Input Parameters:
5266e5e52638SMatthew G. Knepley + dm    - The DM
5267e5e52638SMatthew G. Knepley - point - Cell for the DS
5268e5e52638SMatthew G. Knepley 
5269e5e52638SMatthew G. Knepley   Output Parameter:
5270e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5271e5e52638SMatthew G. Knepley 
5272e5e52638SMatthew G. Knepley   Level: developer
5273e5e52638SMatthew G. Knepley 
5274db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMSetRegionDS()`
5275e5e52638SMatthew G. Knepley @*/
5276e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5277e5e52638SMatthew G. Knepley {
5278e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5279e5e52638SMatthew G. Knepley   PetscInt       s;
5280e5e52638SMatthew G. Knepley 
5281e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5282e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5283e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
528463a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
5285e5e52638SMatthew G. Knepley   *prob = NULL;
5286e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5287e5e52638SMatthew G. Knepley     PetscInt val;
5288e5e52638SMatthew G. Knepley 
5289e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5290e5e52638SMatthew G. Knepley     else {
52919566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
5292e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5293e5e52638SMatthew G. Knepley     }
5294e5e52638SMatthew G. Knepley   }
5295e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5296e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5297e5e52638SMatthew G. Knepley }
5298e5e52638SMatthew G. Knepley 
5299e5e52638SMatthew G. Knepley /*@
5300e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5301e5e52638SMatthew G. Knepley 
5302e5e52638SMatthew G. Knepley   Not collective
5303e5e52638SMatthew G. Knepley 
5304e5e52638SMatthew G. Knepley   Input Parameters:
5305e5e52638SMatthew G. Knepley + dm    - The DM
5306e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5307e5e52638SMatthew G. Knepley 
5308b3cf3223SMatthew G. Knepley   Output Parameters:
5309b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5310b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5311e5e52638SMatthew G. Knepley 
5312154ca461SJed Brown   Note:
5313154ca461SJed Brown   If a non-NULL label is given, but there is no PetscDS on that specific label,
5314154ca461SJed Brown   the PetscDS for the full domain (if present) is returned. Returns with
5315154ca461SJed Brown   fields=NULL and prob=NULL if there is no PetscDS for the full domain.
5316e5e52638SMatthew G. Knepley 
5317e5e52638SMatthew G. Knepley   Level: advanced
5318e5e52638SMatthew G. Knepley 
5319db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5320e5e52638SMatthew G. Knepley @*/
5321b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5322e5e52638SMatthew G. Knepley {
5323e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5324e5e52638SMatthew G. Knepley 
5325e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5326e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5327e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5328b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5329b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5330e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5331154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5332b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5333b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5334154ca461SJed Brown       if (dm->probs[s].label) PetscFunctionReturn(0);
5335b3cf3223SMatthew G. Knepley     }
5336e5e52638SMatthew G. Knepley   }
53372df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5338e5e52638SMatthew G. Knepley }
5339e5e52638SMatthew G. Knepley 
5340e5e52638SMatthew G. Knepley /*@
5341083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5342083401c6SMatthew G. Knepley 
5343083401c6SMatthew G. Knepley   Collective on dm
5344083401c6SMatthew G. Knepley 
5345083401c6SMatthew G. Knepley   Input Parameters:
5346083401c6SMatthew G. Knepley + dm     - The DM
5347083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5348083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5349083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5350083401c6SMatthew G. Knepley 
5351083401c6SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM. If DS is replaced,
5352083401c6SMatthew G. Knepley   the fields argument is ignored.
5353083401c6SMatthew G. Knepley 
5354083401c6SMatthew G. Knepley   Level: advanced
5355083401c6SMatthew G. Knepley 
5356db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5357083401c6SMatthew G. Knepley @*/
5358083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5359083401c6SMatthew G. Knepley {
5360083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5361083401c6SMatthew G. Knepley 
5362083401c6SMatthew G. Knepley   PetscFunctionBegin;
5363083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5364083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5365064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5366083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5367083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
53689566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
5369083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5370083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5371083401c6SMatthew G. Knepley     }
5372083401c6SMatthew G. Knepley   }
53739566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds+1));
53749566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
53759566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) fields));
53769566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) ds));
5377083401c6SMatthew G. Knepley   if (!label) {
5378083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5379083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5380083401c6SMatthew G. Knepley     Nds = 0;
5381083401c6SMatthew G. Knepley   }
5382083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5383083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5384083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5385083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5386083401c6SMatthew G. Knepley }
5387083401c6SMatthew G. Knepley 
5388083401c6SMatthew G. Knepley /*@
5389e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5390e5e52638SMatthew G. Knepley 
5391e5e52638SMatthew G. Knepley   Not collective
5392e5e52638SMatthew G. Knepley 
5393e5e52638SMatthew G. Knepley   Input Parameters:
5394e5e52638SMatthew G. Knepley + dm  - The DM
5395e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5396e5e52638SMatthew G. Knepley 
5397e5e52638SMatthew G. Knepley   Output Parameters:
5398b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5399b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5400083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5401e5e52638SMatthew G. Knepley 
5402e5e52638SMatthew G. Knepley   Level: advanced
5403e5e52638SMatthew G. Knepley 
5404db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5405e5e52638SMatthew G. Knepley @*/
5406b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5407e5e52638SMatthew G. Knepley {
5408e5e52638SMatthew G. Knepley   PetscInt       Nds;
5409e5e52638SMatthew G. Knepley 
5410e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5411e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54129566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
541363a3b9bcSJacob 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);
5414e5e52638SMatthew G. Knepley   if (label) {
5415e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5416e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5417e5e52638SMatthew G. Knepley   }
5418b3cf3223SMatthew G. Knepley   if (fields) {
5419b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5420b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5421b3cf3223SMatthew G. Knepley   }
5422e5e52638SMatthew G. Knepley   if (ds) {
5423b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5424e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5425e5e52638SMatthew G. Knepley   }
5426e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5427e5e52638SMatthew G. Knepley }
5428e5e52638SMatthew G. Knepley 
5429e5e52638SMatthew G. Knepley /*@
5430083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5431e5e52638SMatthew G. Knepley 
5432083401c6SMatthew G. Knepley   Not collective
5433e5e52638SMatthew G. Knepley 
5434e5e52638SMatthew G. Knepley   Input Parameters:
5435e5e52638SMatthew G. Knepley + dm     - The DM
5436083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5437083401c6SMatthew G. Knepley . label  - The region label, or NULL
5438083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5439083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5440e5e52638SMatthew G. Knepley 
5441e5e52638SMatthew G. Knepley   Level: advanced
5442e5e52638SMatthew G. Knepley 
5443db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5444e5e52638SMatthew G. Knepley @*/
5445083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5446e5e52638SMatthew G. Knepley {
5447083401c6SMatthew G. Knepley   PetscInt       Nds;
5448e5e52638SMatthew G. Knepley 
5449e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5450e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5451083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
54529566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
545363a3b9bcSJacob 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);
54549566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
54559566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5456083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5457083401c6SMatthew G. Knepley   if (fields) {
5458083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
54599566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) fields));
54609566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5461083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5462e5e52638SMatthew G. Knepley   }
5463083401c6SMatthew G. Knepley   if (ds) {
5464083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
54659566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) ds));
54669566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5467083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5468083401c6SMatthew G. Knepley   }
5469e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5470e5e52638SMatthew G. Knepley }
5471e5e52638SMatthew G. Knepley 
5472e5e52638SMatthew G. Knepley /*@
54731d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
54741d3af9e0SMatthew G. Knepley 
54751d3af9e0SMatthew G. Knepley   Not collective
54761d3af9e0SMatthew G. Knepley 
54771d3af9e0SMatthew G. Knepley   Input Parameters:
54781d3af9e0SMatthew G. Knepley + dm  - The DM
54791d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
54801d3af9e0SMatthew G. Knepley 
54811d3af9e0SMatthew G. Knepley   Output Parameter:
54821d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
54831d3af9e0SMatthew G. Knepley 
54841d3af9e0SMatthew G. Knepley   Level: advanced
54851d3af9e0SMatthew G. Knepley 
5486db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
54871d3af9e0SMatthew G. Knepley @*/
54881d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
54891d3af9e0SMatthew G. Knepley {
54901d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
54911d3af9e0SMatthew G. Knepley 
54921d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
54931d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54941d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
5495dadcf809SJacob Faibussowitsch   PetscValidIntPointer(num, 3);
54969566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
54971d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
54981d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
54991d3af9e0SMatthew G. Knepley   else          *num = n;
55001d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
55011d3af9e0SMatthew G. Knepley }
55021d3af9e0SMatthew G. Knepley 
55032df84da0SMatthew G. Knepley /*@C
55042df84da0SMatthew G. Knepley   DMCreateFEDefault - Create a PetscFE based on the celltype for the mesh
55052df84da0SMatthew G. Knepley 
55062df84da0SMatthew G. Knepley   Not collective
55072df84da0SMatthew G. Knepley 
5508f1a722f8SMatthew G. Knepley   Input Parameters:
55092df84da0SMatthew G. Knepley + dm     - The DM
55102df84da0SMatthew G. Knepley . Nc     - The number of components for the field
55112df84da0SMatthew G. Knepley . prefix - The options prefix for the output PetscFE, or NULL
55122df84da0SMatthew G. Knepley - qorder - The quadrature order or PETSC_DETERMINE to use PetscSpace polynomial degree
55132df84da0SMatthew G. Knepley 
55142df84da0SMatthew G. Knepley   Output Parameter:
55152df84da0SMatthew G. Knepley . fem - The PetscFE
55162df84da0SMatthew G. Knepley 
55172df84da0SMatthew G. Knepley   Note: This is a convenience method that just calls PetscFECreateByCell() underneath.
55182df84da0SMatthew G. Knepley 
55192df84da0SMatthew G. Knepley   Level: intermediate
55202df84da0SMatthew G. Knepley 
5521db781477SPatrick Sanan .seealso: `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
55222df84da0SMatthew G. Knepley @*/
55232df84da0SMatthew G. Knepley PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
55242df84da0SMatthew G. Knepley {
55252df84da0SMatthew G. Knepley   DMPolytopeType ct;
55262df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
55272df84da0SMatthew G. Knepley 
55282df84da0SMatthew G. Knepley   PetscFunctionBegin;
55292df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55302df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
55312df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
55322df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
55332df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
55349566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
55359566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
55369566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
55379566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
55382df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
55392df84da0SMatthew G. Knepley }
55402df84da0SMatthew G. Knepley 
55411d3af9e0SMatthew G. Knepley /*@
5542e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5543e5e52638SMatthew G. Knepley 
5544d083f849SBarry Smith   Collective on dm
5545e5e52638SMatthew G. Knepley 
5546e5e52638SMatthew G. Knepley   Input Parameter:
5547e5e52638SMatthew G. Knepley . dm - The DM
5548e5e52638SMatthew G. Knepley 
554945480ffeSMatthew G. Knepley   Options Database Keys:
555045480ffeSMatthew G. Knepley . -dm_petscds_view - View all the PetscDS objects in this DM
555145480ffeSMatthew G. Knepley 
5552e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5553e5e52638SMatthew G. Knepley 
5554e5e52638SMatthew G. Knepley   Level: intermediate
5555e5e52638SMatthew G. Knepley 
5556db781477SPatrick Sanan .seealso: `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5557e5e52638SMatthew G. Knepley @*/
5558e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5559e5e52638SMatthew G. Knepley {
5560e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5561083401c6SMatthew G. Knepley   PetscDS        dsDef;
5562083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5563f9244615SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5564f9244615SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE, flg;
5565e5e52638SMatthew G. Knepley 
5566e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5567e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5568e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
55699566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject) dm, &comm));
55709566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5571083401c6SMatthew G. Knepley   /* Determine how many regions we have */
55729566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5573083401c6SMatthew G. Knepley   Nl   = 0;
5574083401c6SMatthew G. Knepley   Ndef = 0;
5575083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5576083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5577083401c6SMatthew G. Knepley     PetscInt l;
5578083401c6SMatthew G. Knepley 
5579f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5580f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5581f918ec44SMatthew G. Knepley     {
5582f918ec44SMatthew G. Knepley       PetscClassId id;
5583f918ec44SMatthew G. Knepley 
55849566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5585f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5586f918ec44SMatthew G. Knepley         Ceed ceed;
5587f918ec44SMatthew G. Knepley 
55889566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
55899566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE) dm->fields[f].disc, ceed));
5590f918ec44SMatthew G. Knepley       }
5591f918ec44SMatthew G. Knepley     }
5592f918ec44SMatthew G. Knepley #endif
5593083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5594083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5595083401c6SMatthew G. Knepley     if (l < Nl) continue;
5596083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5597083401c6SMatthew G. Knepley   }
5598083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
55999566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
5600083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5601b3cf3223SMatthew G. Knepley     IS        fields;
5602b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5603b3cf3223SMatthew G. Knepley 
5604b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
56057a8be351SBarry Smith     PetscCheck(nf,comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
56069566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
5607b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
56089566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
56099566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_"));
56109566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
56119566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
561288f0c812SMatthew G. Knepley 
56139566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
56149566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef));
56159566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
56169566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
56172df9ee95SMatthew G. Knepley   }
56189566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
56199566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5620083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5621083401c6SMatthew G. Knepley   if (Ndef && Nl) {
56220122748bSMatthew G. Knepley     DM              plex;
5623083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5624083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5625083401c6SMatthew G. Knepley     PetscInt       *fields;
5626083401c6SMatthew G. Knepley     const PetscInt *cells;
5627083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
56280122748bSMatthew G. Knepley 
56299566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
56309566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
56319566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
56329566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
56335fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5634083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5635083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5636083401c6SMatthew G. Knepley       IS      pointIS;
5637083401c6SMatthew G. Knepley 
56389566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
56399566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
56409566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
56419566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5642083401c6SMatthew G. Knepley     }
56439566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5644083401c6SMatthew G. Knepley 
56459566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
56469566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
56479566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
56489566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
56499566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
56509566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
56519566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5652083401c6SMatthew G. Knepley 
56539566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
5654083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
56559566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
56569566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_"));
56579566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
56589566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5659083401c6SMatthew G. Knepley 
56609566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
56619566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef));
56629566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
56639566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
56649566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
56659566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
56669566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5667083401c6SMatthew G. Knepley   }
5668083401c6SMatthew G. Knepley   /* Create label DSes
5669083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5670083401c6SMatthew G. Knepley   */
5671083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5672083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5673083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5674083401c6SMatthew G. Knepley     PetscDS   ds;
5675083401c6SMatthew G. Knepley     IS        fields;
5676083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5677083401c6SMatthew G. Knepley 
56789566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
5679083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
56809566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
5681083401c6SMatthew G. Knepley     for (f = 0, nf  = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
56829566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
56839566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_"));
56849566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
56859566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
56869566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, label, fields, ds));
56879566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
56889566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5689083401c6SMatthew G. Knepley     {
5690083401c6SMatthew G. Knepley       DMPolytopeType ct;
5691083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
56925fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
56930122748bSMatthew G. Knepley 
56949566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5695665f567fSMatthew G. Knepley       if (lStart >= 0) {
56969566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5697412e9a14SMatthew G. Knepley         switch (ct) {
5698412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5699412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5700412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5701412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
57025fedec97SMatthew G. Knepley             isCohesiveLocal = PETSC_TRUE;break;
5703083401c6SMatthew G. Knepley           default: break;
5704412e9a14SMatthew G. Knepley         }
5705665f567fSMatthew G. Knepley       }
57069566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
57075fedec97SMatthew G. Knepley       for (f = 0, nf  = 0; f < Nf; ++f) {
57085fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
57095fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
57109566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
57119566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
57125fedec97SMatthew G. Knepley           }
57135fedec97SMatthew G. Knepley           ++nf;
57145fedec97SMatthew G. Knepley         }
57155fedec97SMatthew G. Knepley       }
5716e5e52638SMatthew G. Knepley     }
57179566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5718e5e52638SMatthew G. Knepley   }
57199566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5720e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5721083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5722083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5723083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5724083401c6SMatthew G. Knepley     const PetscInt *fld;
57255fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
57265fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5727e5e52638SMatthew G. Knepley 
57289566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
57299566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
57309566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
57319566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5732083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5733083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
57345fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5735e5e52638SMatthew G. Knepley       PetscClassId id;
5736e5e52638SMatthew G. Knepley 
57375fedec97SMatthew G. Knepley       /* Handle DS with no fields */
57389566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
57395fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
57409566063dSJacob Faibussowitsch       if (isCohesive && !isCohesiveField) PetscCall(PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc));
57419566063dSJacob Faibussowitsch       PetscCall(PetscDSSetDiscretization(ds, f, disc));
5742083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
57439566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5744e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5745e5e52638SMatthew G. Knepley     }
57469566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
5747e5e52638SMatthew G. Knepley   }
5748f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
57499566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject) dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
5750f9244615SMatthew G. Knepley   if (flg) {
57513b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
57523b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
57533b4aee56SMatthew G. Knepley       PetscInt Nf, f;
57543b4aee56SMatthew G. Knepley 
57559566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
57569566063dSJacob Faibussowitsch       for (f = 0; f < Nf; ++f) PetscCall(PetscDSSetJetDegree(ds, f, k));
57573b4aee56SMatthew G. Knepley     }
5758f9244615SMatthew G. Knepley   }
5759e5e52638SMatthew G. Knepley   /* Setup DSes */
5760e5e52638SMatthew G. Knepley   if (doSetup) {
57619566063dSJacob Faibussowitsch     for (s = 0; s < dm->Nds; ++s) PetscCall(PetscDSSetUp(dm->probs[s].ds));
5762e5e52638SMatthew G. Knepley   }
5763e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5764e5e52638SMatthew G. Knepley }
5765e5e52638SMatthew G. Knepley 
5766e5e52638SMatthew G. Knepley /*@
57677f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
57687f96f943SMatthew G. Knepley 
5769f2cacb80SMatthew G. Knepley   Collective on DM
5770f2cacb80SMatthew G. Knepley 
57717f96f943SMatthew G. Knepley   Input Parameters:
57727f96f943SMatthew G. Knepley + dm   - The DM
57737f96f943SMatthew G. Knepley - time - The time
57747f96f943SMatthew G. Knepley 
57757f96f943SMatthew G. Knepley   Output Parameters:
5776f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5777f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
57787f96f943SMatthew G. Knepley 
57797f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
57807f96f943SMatthew G. Knepley 
57817f96f943SMatthew G. Knepley   Level: developer
57827f96f943SMatthew G. Knepley 
5783db781477SPatrick Sanan .seealso: `PetscDSSetExactSolution()`
57847f96f943SMatthew G. Knepley @*/
5785f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
57867f96f943SMatthew G. Knepley {
57877f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
57887f96f943SMatthew G. Knepley   void            **ectxs;
57897f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
57907f96f943SMatthew G. Knepley 
57917f96f943SMatthew G. Knepley   PetscFunctionBegin;
5792f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5793f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5794f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
57959566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
57969566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
57979566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
57987f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
57997f96f943SMatthew G. Knepley     PetscDS         ds;
58007f96f943SMatthew G. Knepley     DMLabel         label;
58017f96f943SMatthew G. Knepley     IS              fieldIS;
58027f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
58037f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
58047f96f943SMatthew G. Knepley 
58059566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
58069566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
58079566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
58089566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
58099566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
5810f2cacb80SMatthew G. Knepley     if (u) {
58117f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
58127f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
58139566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]));
58147f96f943SMatthew G. Knepley       }
58159566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
58167f96f943SMatthew G. Knepley       if (label) {
58179566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u));
58187f96f943SMatthew G. Knepley       } else {
58199566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u));
58207f96f943SMatthew G. Knepley       }
58217f96f943SMatthew G. Knepley     }
5822f2cacb80SMatthew G. Knepley     if (u_t) {
58239566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
58249566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
5825f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5826f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
58279566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]));
5828f2cacb80SMatthew G. Knepley       }
58299566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
5830f2cacb80SMatthew G. Knepley       if (label) {
58319566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t));
5832f2cacb80SMatthew G. Knepley       } else {
58339566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t));
5834f2cacb80SMatthew G. Knepley       }
5835f2cacb80SMatthew G. Knepley     }
5836f2cacb80SMatthew G. Knepley   }
5837f2cacb80SMatthew G. Knepley   if (u) {
58389566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) u, "Exact Solution"));
58399566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) u, "exact_"));
5840f2cacb80SMatthew G. Knepley   }
5841f2cacb80SMatthew G. Knepley   if (u_t) {
58429566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative"));
58439566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_"));
5844f2cacb80SMatthew G. Knepley   }
58459566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
58467f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
58477f96f943SMatthew G. Knepley }
58487f96f943SMatthew G. Knepley 
584945480ffeSMatthew G. Knepley PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
585045480ffeSMatthew G. Knepley {
585145480ffeSMatthew G. Knepley   PetscDS        dsNew;
585245480ffeSMatthew G. Knepley   DSBoundary     b;
58536a02485aSMatthew G. Knepley   PetscInt       cdim, Nf, f, d;
58545fedec97SMatthew G. Knepley   PetscBool      isCohesive;
585545480ffeSMatthew G. Knepley   void          *ctx;
585645480ffeSMatthew G. Knepley 
585745480ffeSMatthew G. Knepley   PetscFunctionBegin;
58589566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject) ds), &dsNew));
58599566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyConstants(ds, dsNew));
58609566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyExactSolutions(ds, dsNew));
58619566063dSJacob Faibussowitsch   PetscCall(PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew));
58629566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyEquations(ds, dsNew));
58639566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
586445480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
58659566063dSJacob Faibussowitsch     PetscCall(PetscDSGetContext(ds, f, &ctx));
58669566063dSJacob Faibussowitsch     PetscCall(PetscDSSetContext(dsNew, f, ctx));
58679566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCohesive(ds, f, &isCohesive));
58689566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCohesive(dsNew, f, isCohesive));
58696a02485aSMatthew G. Knepley     PetscCall(PetscDSGetJetDegree(ds, f, &d));
58706a02485aSMatthew G. Knepley     PetscCall(PetscDSSetJetDegree(dsNew, f, d));
587145480ffeSMatthew G. Knepley   }
587245480ffeSMatthew G. Knepley   if (Nf) {
58739566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCoordinateDimension(ds, &cdim));
58749566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsNew, cdim));
587545480ffeSMatthew G. Knepley   }
58769566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew));
587745480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
58789566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, b->lname, &b->label));
587945480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
58807a8be351SBarry Smith     //PetscCheck(b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
588145480ffeSMatthew G. Knepley   }
588245480ffeSMatthew G. Knepley 
58839566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(dm, label, fields, dsNew));
58849566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
588545480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
588645480ffeSMatthew G. Knepley }
588745480ffeSMatthew G. Knepley 
58887f96f943SMatthew G. Knepley /*@
5889e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5890e5e52638SMatthew G. Knepley 
5891d083f849SBarry Smith   Collective on dm
5892e5e52638SMatthew G. Knepley 
5893e5e52638SMatthew G. Knepley   Input Parameter:
5894e5e52638SMatthew G. Knepley . dm - The DM
5895e5e52638SMatthew G. Knepley 
5896e5e52638SMatthew G. Knepley   Output Parameter:
5897e5e52638SMatthew G. Knepley . newdm - The DM
5898e5e52638SMatthew G. Knepley 
5899e5e52638SMatthew G. Knepley   Level: advanced
5900e5e52638SMatthew G. Knepley 
5901db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5902e5e52638SMatthew G. Knepley @*/
5903e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5904e5e52638SMatthew G. Knepley {
5905e5e52638SMatthew G. Knepley   PetscInt Nds, s;
5906e5e52638SMatthew G. Knepley 
5907e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5908e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
59099566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
59109566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
5911e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5912e5e52638SMatthew G. Knepley     DMLabel  label;
5913b3cf3223SMatthew G. Knepley     IS       fields;
591445480ffeSMatthew G. Knepley     PetscDS  ds, newds;
5915783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5916e5e52638SMatthew G. Knepley 
59179566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds));
5918b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
59199566063dSJacob Faibussowitsch     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds));
592045480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
59219566063dSJacob Faibussowitsch     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds));
59229566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
5923783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5924b8025e53SMatthew G. Knepley       PetscWeakForm wf;
592545480ffeSMatthew G. Knepley       DMLabel       label;
5926783e2ec8SMatthew G. Knepley       PetscInt      field;
5927783e2ec8SMatthew G. Knepley 
59289566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
59299566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
5930783e2ec8SMatthew G. Knepley     }
5931e5e52638SMatthew G. Knepley   }
5932799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
5933e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5934e5e52638SMatthew G. Knepley }
5935e5e52638SMatthew G. Knepley 
5936e5e52638SMatthew G. Knepley /*@
5937e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5938e5e52638SMatthew G. Knepley 
5939d083f849SBarry Smith   Collective on dm
5940e5e52638SMatthew G. Knepley 
5941e5e52638SMatthew G. Knepley   Input Parameter:
5942e5e52638SMatthew G. Knepley . dm - The DM
5943e5e52638SMatthew G. Knepley 
5944e5e52638SMatthew G. Knepley   Output Parameter:
5945e5e52638SMatthew G. Knepley . newdm - The DM
5946e5e52638SMatthew G. Knepley 
5947e5e52638SMatthew G. Knepley   Level: advanced
5948e5e52638SMatthew G. Knepley 
5949db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMCopyDS()`
5950e5e52638SMatthew G. Knepley @*/
5951e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5952e5e52638SMatthew G. Knepley {
5953e5e52638SMatthew G. Knepley   PetscFunctionBegin;
59549566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
59559566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
5956e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5957e5e52638SMatthew G. Knepley }
5958e5e52638SMatthew G. Knepley 
5959c73cfb54SMatthew G. Knepley /*@
5960c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5961c73cfb54SMatthew G. Knepley 
5962c73cfb54SMatthew G. Knepley   Not collective
5963c73cfb54SMatthew G. Knepley 
5964c73cfb54SMatthew G. Knepley   Input Parameter:
5965c73cfb54SMatthew G. Knepley . dm - The DM
5966c73cfb54SMatthew G. Knepley 
5967c73cfb54SMatthew G. Knepley   Output Parameter:
5968c73cfb54SMatthew G. Knepley . dim - The topological dimension
5969c73cfb54SMatthew G. Knepley 
5970c73cfb54SMatthew G. Knepley   Level: beginner
5971c73cfb54SMatthew G. Knepley 
5972db781477SPatrick Sanan .seealso: `DMSetDimension()`, `DMCreate()`
5973c73cfb54SMatthew G. Knepley @*/
5974c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5975c73cfb54SMatthew G. Knepley {
5976c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5977c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5978534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5979c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5980c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5981c73cfb54SMatthew G. Knepley }
5982c73cfb54SMatthew G. Knepley 
5983c73cfb54SMatthew G. Knepley /*@
5984c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5985c73cfb54SMatthew G. Knepley 
5986c73cfb54SMatthew G. Knepley   Collective on dm
5987c73cfb54SMatthew G. Knepley 
5988c73cfb54SMatthew G. Knepley   Input Parameters:
5989c73cfb54SMatthew G. Knepley + dm - The DM
5990c73cfb54SMatthew G. Knepley - dim - The topological dimension
5991c73cfb54SMatthew G. Knepley 
5992c73cfb54SMatthew G. Knepley   Level: beginner
5993c73cfb54SMatthew G. Knepley 
5994db781477SPatrick Sanan .seealso: `DMGetDimension()`, `DMCreate()`
5995c73cfb54SMatthew G. Knepley @*/
5996c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5997c73cfb54SMatthew G. Knepley {
5998e5e52638SMatthew G. Knepley   PetscDS        ds;
599945480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6000f17e8794SMatthew G. Knepley 
6001c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6002c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6003c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6004c73cfb54SMatthew G. Knepley   dm->dim = dim;
6005d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
60069566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
600745480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
60089566063dSJacob Faibussowitsch       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
60099566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
601045480ffeSMatthew G. Knepley     }
6011d17bd122SMatthew G. Knepley   }
6012c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6013c73cfb54SMatthew G. Knepley }
6014c73cfb54SMatthew G. Knepley 
6015793f3fe5SMatthew G. Knepley /*@
6016793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6017793f3fe5SMatthew G. Knepley 
6018d083f849SBarry Smith   Collective on dm
6019793f3fe5SMatthew G. Knepley 
6020793f3fe5SMatthew G. Knepley   Input Parameters:
6021793f3fe5SMatthew G. Knepley + dm - the DM
6022793f3fe5SMatthew G. Knepley - dim - the dimension
6023793f3fe5SMatthew G. Knepley 
6024793f3fe5SMatthew G. Knepley   Output Parameters:
6025793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6026aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6027793f3fe5SMatthew G. Knepley 
6028793f3fe5SMatthew G. Knepley   Note:
6029793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6030a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6031793f3fe5SMatthew G. Knepley   then the interval is empty.
6032793f3fe5SMatthew G. Knepley 
6033793f3fe5SMatthew G. Knepley   Level: intermediate
6034793f3fe5SMatthew G. Knepley 
6035db781477SPatrick Sanan .seealso: `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6036793f3fe5SMatthew G. Knepley @*/
6037793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6038793f3fe5SMatthew G. Knepley {
6039793f3fe5SMatthew G. Knepley   PetscInt       d;
6040793f3fe5SMatthew G. Knepley 
6041793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6042793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60439566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
60447a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
60457a8be351SBarry Smith   PetscCheck(dm->ops->getdimpoints,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
60469566063dSJacob Faibussowitsch   PetscCall((*dm->ops->getdimpoints)(dm, dim, pStart, pEnd));
6047793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6048793f3fe5SMatthew G. Knepley }
6049793f3fe5SMatthew G. Knepley 
60506636e97aSMatthew G Knepley /*@
6051f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6052f4d763aaSMatthew G. Knepley 
60538f700142SStefano Zampini   Collective on dm
60548f700142SStefano Zampini 
6055f4d763aaSMatthew G. Knepley   Input Parameter:
6056f4d763aaSMatthew G. Knepley . dm - The original DM
6057f4d763aaSMatthew G. Knepley 
6058f4d763aaSMatthew G. Knepley   Output Parameter:
6059f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6060f4d763aaSMatthew G. Knepley 
6061f4d763aaSMatthew G. Knepley   Level: intermediate
6062f4d763aaSMatthew G. Knepley 
6063db781477SPatrick Sanan .seealso: `VecView()`, `DMGetGlobalSection()`
6064f4d763aaSMatthew G. Knepley @*/
606514f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
606614f150ffSMatthew G. Knepley {
6067c26acbdeSMatthew G. Knepley   PetscSection   section;
60682d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
606914f150ffSMatthew G. Knepley 
607014f150ffSMatthew G. Knepley   PetscFunctionBegin;
607114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
607214f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
60739566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
60749566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
60759566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm)));
60762d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6077c26acbdeSMatthew G. Knepley     *odm = dm;
6078c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6079c26acbdeSMatthew G. Knepley   }
608014f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6081c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
608214f150ffSMatthew G. Knepley     PetscSF      sf;
608314f150ffSMatthew G. Knepley 
60849566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
60859566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
60869566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
60879566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
60889566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
60899566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
60909566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
60919566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
60929566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
609314f150ffSMatthew G. Knepley   }
609414f150ffSMatthew G. Knepley   *odm = dm->dmBC;
609514f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
609614f150ffSMatthew G. Knepley }
6097f4d763aaSMatthew G. Knepley 
6098f4d763aaSMatthew G. Knepley /*@
6099cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6100f4d763aaSMatthew G. Knepley 
6101f4d763aaSMatthew G. Knepley   Input Parameter:
6102f4d763aaSMatthew G. Knepley . dm - The original DM
6103f4d763aaSMatthew G. Knepley 
6104cdb7a50dSMatthew G. Knepley   Output Parameters:
6105cdb7a50dSMatthew G. Knepley + num - The output sequence number
6106cdb7a50dSMatthew G. Knepley - val - The output sequence value
6107f4d763aaSMatthew G. Knepley 
6108f4d763aaSMatthew G. Knepley   Level: intermediate
6109f4d763aaSMatthew G. Knepley 
6110f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6111f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6112f4d763aaSMatthew G. Knepley 
6113db781477SPatrick Sanan .seealso: `VecView()`
6114f4d763aaSMatthew G. Knepley @*/
6115cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6116f4d763aaSMatthew G. Knepley {
6117f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6118f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6119534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6120534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6121f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6122f4d763aaSMatthew G. Knepley }
6123f4d763aaSMatthew G. Knepley 
6124f4d763aaSMatthew G. Knepley /*@
6125cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6126f4d763aaSMatthew G. Knepley 
6127f4d763aaSMatthew G. Knepley   Input Parameters:
6128f4d763aaSMatthew G. Knepley + dm - The original DM
6129cdb7a50dSMatthew G. Knepley . num - The output sequence number
6130cdb7a50dSMatthew G. Knepley - val - The output sequence value
6131f4d763aaSMatthew G. Knepley 
6132f4d763aaSMatthew G. Knepley   Level: intermediate
6133f4d763aaSMatthew G. Knepley 
6134f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6135f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6136f4d763aaSMatthew G. Knepley 
6137db781477SPatrick Sanan .seealso: `VecView()`
6138f4d763aaSMatthew G. Knepley @*/
6139cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6140f4d763aaSMatthew G. Knepley {
6141f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6142f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6143f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6144cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6145cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6146cdb7a50dSMatthew G. Knepley }
6147cdb7a50dSMatthew G. Knepley 
6148cdb7a50dSMatthew G. Knepley /*@C
6149cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6150cdb7a50dSMatthew G. Knepley 
6151cdb7a50dSMatthew G. Knepley   Input Parameters:
6152cdb7a50dSMatthew G. Knepley + dm   - The original DM
6153cdb7a50dSMatthew G. Knepley . name - The sequence name
6154cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6155cdb7a50dSMatthew G. Knepley 
6156cdb7a50dSMatthew G. Knepley   Output Parameter:
6157cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6158cdb7a50dSMatthew G. Knepley 
6159cdb7a50dSMatthew G. Knepley   Level: intermediate
6160cdb7a50dSMatthew G. Knepley 
6161cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6162cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6163cdb7a50dSMatthew G. Knepley 
6164db781477SPatrick Sanan .seealso: `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6165cdb7a50dSMatthew G. Knepley @*/
6166cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6167cdb7a50dSMatthew G. Knepley {
6168cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6169cdb7a50dSMatthew G. Knepley 
6170cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6171cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6172cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6173064a246eSJacob Faibussowitsch   PetscValidRealPointer(val,5);
61749566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5));
6175cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6176cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6177cdb7a50dSMatthew G. Knepley     PetscScalar value;
6178cdb7a50dSMatthew G. Knepley 
61799566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
61804aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6181cdb7a50dSMatthew G. Knepley #endif
6182cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6183f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6184f4d763aaSMatthew G. Knepley }
61858e4ac7eaSMatthew G. Knepley 
61868e4ac7eaSMatthew G. Knepley /*@
61878e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
61888e4ac7eaSMatthew G. Knepley 
61898e4ac7eaSMatthew G. Knepley   Not collective
61908e4ac7eaSMatthew G. Knepley 
61918e4ac7eaSMatthew G. Knepley   Input Parameter:
61928e4ac7eaSMatthew G. Knepley . dm - The DM
61938e4ac7eaSMatthew G. Knepley 
61948e4ac7eaSMatthew G. Knepley   Output Parameter:
61958e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
61968e4ac7eaSMatthew G. Knepley 
61978e4ac7eaSMatthew G. Knepley   Level: beginner
61988e4ac7eaSMatthew G. Knepley 
6199db781477SPatrick Sanan .seealso: `DMSetUseNatural()`, `DMCreate()`
62008e4ac7eaSMatthew G. Knepley @*/
62018e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
62028e4ac7eaSMatthew G. Knepley {
62038e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
62048e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6205534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
62068e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
62078e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
62088e4ac7eaSMatthew G. Knepley }
62098e4ac7eaSMatthew G. Knepley 
62108e4ac7eaSMatthew G. Knepley /*@
62115d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
62128e4ac7eaSMatthew G. Knepley 
62138e4ac7eaSMatthew G. Knepley   Collective on dm
62148e4ac7eaSMatthew G. Knepley 
62158e4ac7eaSMatthew G. Knepley   Input Parameters:
62168e4ac7eaSMatthew G. Knepley + dm - The DM
62178e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
62188e4ac7eaSMatthew G. Knepley 
62195d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
62205d3b26e6SMatthew G. Knepley 
62218e4ac7eaSMatthew G. Knepley   Level: beginner
62228e4ac7eaSMatthew G. Knepley 
6223db781477SPatrick Sanan .seealso: `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
62248e4ac7eaSMatthew G. Knepley @*/
62258e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
62268e4ac7eaSMatthew G. Knepley {
62278e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
62288e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62298833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
62308e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
62318e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
62328e4ac7eaSMatthew G. Knepley }
6233c58f1c22SToby Isaac 
6234c58f1c22SToby Isaac /*@C
6235c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6236c58f1c22SToby Isaac 
6237c58f1c22SToby Isaac   Not Collective
6238c58f1c22SToby Isaac 
6239c58f1c22SToby Isaac   Input Parameters:
6240c58f1c22SToby Isaac + dm   - The DM object
6241c58f1c22SToby Isaac - name - The label name
6242c58f1c22SToby Isaac 
6243c58f1c22SToby Isaac   Level: intermediate
6244c58f1c22SToby Isaac 
6245db781477SPatrick Sanan .seealso: `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6246c58f1c22SToby Isaac @*/
6247c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6248c58f1c22SToby Isaac {
62495d80c0bfSVaclav Hapla   PetscBool      flg;
62505d80c0bfSVaclav Hapla   DMLabel        label;
6251c58f1c22SToby Isaac 
6252c58f1c22SToby Isaac   PetscFunctionBegin;
6253c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6254c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
62559566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6256c58f1c22SToby Isaac   if (!flg) {
62579566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
62589566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
62599566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6260c58f1c22SToby Isaac   }
6261c58f1c22SToby Isaac   PetscFunctionReturn(0);
6262c58f1c22SToby Isaac }
6263c58f1c22SToby Isaac 
6264c58f1c22SToby Isaac /*@C
62657cf350d0SPierre Jolivet   DMCreateLabelAtIndex - Create a label of the given name at the given index. If it already exists, move it to this index.
62660fdc7489SMatthew Knepley 
62670fdc7489SMatthew Knepley   Not Collective
62680fdc7489SMatthew Knepley 
62690fdc7489SMatthew Knepley   Input Parameters:
62700fdc7489SMatthew Knepley + dm   - The DM object
62710fdc7489SMatthew Knepley . l    - The index for the label
62720fdc7489SMatthew Knepley - name - The label name
62730fdc7489SMatthew Knepley 
62740fdc7489SMatthew Knepley   Level: intermediate
62750fdc7489SMatthew Knepley 
6276db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
62770fdc7489SMatthew Knepley @*/
62780fdc7489SMatthew Knepley PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
62790fdc7489SMatthew Knepley {
62800fdc7489SMatthew Knepley   DMLabelLink    orig, prev = NULL;
62810fdc7489SMatthew Knepley   DMLabel        label;
62820fdc7489SMatthew Knepley   PetscInt       Nl, m;
62830fdc7489SMatthew Knepley   PetscBool      flg, match;
62840fdc7489SMatthew Knepley   const char    *lname;
62850fdc7489SMatthew Knepley 
62860fdc7489SMatthew Knepley   PetscFunctionBegin;
62870fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6288064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
62899566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
62900fdc7489SMatthew Knepley   if (!flg) {
62919566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
62929566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
62939566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
62940fdc7489SMatthew Knepley   }
62959566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
629663a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
62970fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
62989566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) orig->label, &lname));
62999566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
63000fdc7489SMatthew Knepley     if (match) break;
63010fdc7489SMatthew Knepley   }
63020fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
63030fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
63040fdc7489SMatthew Knepley   else    prev->next = orig->next;
63050fdc7489SMatthew Knepley   if (!l) {
63060fdc7489SMatthew Knepley     orig->next = dm->labels;
63070fdc7489SMatthew Knepley     dm->labels = orig;
63080fdc7489SMatthew Knepley   } else {
63090fdc7489SMatthew Knepley     for (m = 0, prev = dm->labels; m < l-1; ++m, prev = prev->next);
63100fdc7489SMatthew Knepley     orig->next = prev->next;
63110fdc7489SMatthew Knepley     prev->next = orig;
63120fdc7489SMatthew Knepley   }
63130fdc7489SMatthew Knepley   PetscFunctionReturn(0);
63140fdc7489SMatthew Knepley }
63150fdc7489SMatthew Knepley 
63160fdc7489SMatthew Knepley /*@C
63177cf350d0SPierre Jolivet   DMGetLabelValue - Get the value in a DMLabel for the given point, with -1 as the default
6318c58f1c22SToby Isaac 
6319c58f1c22SToby Isaac   Not Collective
6320c58f1c22SToby Isaac 
6321c58f1c22SToby Isaac   Input Parameters:
6322c58f1c22SToby Isaac + dm   - The DM object
6323c58f1c22SToby Isaac . name - The label name
6324c58f1c22SToby Isaac - point - The mesh point
6325c58f1c22SToby Isaac 
6326c58f1c22SToby Isaac   Output Parameter:
6327c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6328c58f1c22SToby Isaac 
6329c58f1c22SToby Isaac   Level: beginner
6330c58f1c22SToby Isaac 
6331db781477SPatrick Sanan .seealso: `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6332c58f1c22SToby Isaac @*/
6333c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6334c58f1c22SToby Isaac {
6335c58f1c22SToby Isaac   DMLabel        label;
6336c58f1c22SToby Isaac 
6337c58f1c22SToby Isaac   PetscFunctionBegin;
6338c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6339c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
63409566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
63417a8be351SBarry Smith   PetscCheck(label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
63429566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
6343c58f1c22SToby Isaac   PetscFunctionReturn(0);
6344c58f1c22SToby Isaac }
6345c58f1c22SToby Isaac 
6346c58f1c22SToby Isaac /*@C
63477cf350d0SPierre Jolivet   DMSetLabelValue - Add a point to a DMLabel with given value
6348c58f1c22SToby Isaac 
6349c58f1c22SToby Isaac   Not Collective
6350c58f1c22SToby Isaac 
6351c58f1c22SToby Isaac   Input Parameters:
6352c58f1c22SToby Isaac + dm   - The DM object
6353c58f1c22SToby Isaac . name - The label name
6354c58f1c22SToby Isaac . point - The mesh point
6355c58f1c22SToby Isaac - value - The label value for this point
6356c58f1c22SToby Isaac 
6357c58f1c22SToby Isaac   Output Parameter:
6358c58f1c22SToby Isaac 
6359c58f1c22SToby Isaac   Level: beginner
6360c58f1c22SToby Isaac 
6361db781477SPatrick Sanan .seealso: `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6362c58f1c22SToby Isaac @*/
6363c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6364c58f1c22SToby Isaac {
6365c58f1c22SToby Isaac   DMLabel        label;
6366c58f1c22SToby Isaac 
6367c58f1c22SToby Isaac   PetscFunctionBegin;
6368c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6369c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
63709566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6371c58f1c22SToby Isaac   if (!label) {
63729566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
63739566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6374c58f1c22SToby Isaac   }
63759566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
6376c58f1c22SToby Isaac   PetscFunctionReturn(0);
6377c58f1c22SToby Isaac }
6378c58f1c22SToby Isaac 
6379c58f1c22SToby Isaac /*@C
63807cf350d0SPierre Jolivet   DMClearLabelValue - Remove a point from a DMLabel with given value
6381c58f1c22SToby Isaac 
6382c58f1c22SToby Isaac   Not Collective
6383c58f1c22SToby Isaac 
6384c58f1c22SToby Isaac   Input Parameters:
6385c58f1c22SToby Isaac + dm   - The DM object
6386c58f1c22SToby Isaac . name - The label name
6387c58f1c22SToby Isaac . point - The mesh point
6388c58f1c22SToby Isaac - value - The label value for this point
6389c58f1c22SToby Isaac 
6390c58f1c22SToby Isaac   Output Parameter:
6391c58f1c22SToby Isaac 
6392c58f1c22SToby Isaac   Level: beginner
6393c58f1c22SToby Isaac 
6394db781477SPatrick Sanan .seealso: `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6395c58f1c22SToby Isaac @*/
6396c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6397c58f1c22SToby Isaac {
6398c58f1c22SToby Isaac   DMLabel        label;
6399c58f1c22SToby Isaac 
6400c58f1c22SToby Isaac   PetscFunctionBegin;
6401c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6402c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
64039566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6404c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
64059566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
6406c58f1c22SToby Isaac   PetscFunctionReturn(0);
6407c58f1c22SToby Isaac }
6408c58f1c22SToby Isaac 
6409c58f1c22SToby Isaac /*@C
6410c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6411c58f1c22SToby Isaac 
6412c58f1c22SToby Isaac   Not Collective
6413c58f1c22SToby Isaac 
6414c58f1c22SToby Isaac   Input Parameters:
6415c58f1c22SToby Isaac + dm   - The DM object
6416c58f1c22SToby Isaac - name - The label name
6417c58f1c22SToby Isaac 
6418c58f1c22SToby Isaac   Output Parameter:
6419c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6420c58f1c22SToby Isaac 
6421c58f1c22SToby Isaac   Level: beginner
6422c58f1c22SToby Isaac 
6423db781477SPatrick Sanan .seealso: `DMLabelGetNumValues()`, `DMSetLabelValue()`
6424c58f1c22SToby Isaac @*/
6425c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6426c58f1c22SToby Isaac {
6427c58f1c22SToby Isaac   DMLabel        label;
6428c58f1c22SToby Isaac 
6429c58f1c22SToby Isaac   PetscFunctionBegin;
6430c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6431c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6432534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
64339566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6434c58f1c22SToby Isaac   *size = 0;
6435c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
64369566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
6437c58f1c22SToby Isaac   PetscFunctionReturn(0);
6438c58f1c22SToby Isaac }
6439c58f1c22SToby Isaac 
6440c58f1c22SToby Isaac /*@C
6441c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
6442c58f1c22SToby Isaac 
6443c58f1c22SToby Isaac   Not Collective
6444c58f1c22SToby Isaac 
6445c58f1c22SToby Isaac   Input Parameters:
6446c58f1c22SToby Isaac + mesh - The DM object
6447c58f1c22SToby Isaac - name - The label name
6448c58f1c22SToby Isaac 
6449c58f1c22SToby Isaac   Output Parameter:
6450c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6451c58f1c22SToby Isaac 
6452c58f1c22SToby Isaac   Level: beginner
6453c58f1c22SToby Isaac 
6454db781477SPatrick Sanan .seealso: `DMLabelGetValueIS()`, `DMGetLabelSize()`
6455c58f1c22SToby Isaac @*/
6456c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6457c58f1c22SToby Isaac {
6458c58f1c22SToby Isaac   DMLabel        label;
6459c58f1c22SToby Isaac 
6460c58f1c22SToby Isaac   PetscFunctionBegin;
6461c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6462c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6463c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
64649566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6465c58f1c22SToby Isaac   *ids = NULL;
6466dab2e251SBlaise Bourdin   if (label) {
64679566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6468dab2e251SBlaise Bourdin   } else {
6469dab2e251SBlaise Bourdin     /* returning an empty IS */
64709566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids));
6471dab2e251SBlaise Bourdin   }
6472c58f1c22SToby Isaac   PetscFunctionReturn(0);
6473c58f1c22SToby Isaac }
6474c58f1c22SToby Isaac 
6475c58f1c22SToby Isaac /*@C
6476c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6477c58f1c22SToby Isaac 
6478c58f1c22SToby Isaac   Not Collective
6479c58f1c22SToby Isaac 
6480c58f1c22SToby Isaac   Input Parameters:
6481c58f1c22SToby Isaac + dm - The DM object
6482c58f1c22SToby Isaac . name - The label name
6483c58f1c22SToby Isaac - value - The stratum value
6484c58f1c22SToby Isaac 
6485c58f1c22SToby Isaac   Output Parameter:
6486c58f1c22SToby Isaac . size - The stratum size
6487c58f1c22SToby Isaac 
6488c58f1c22SToby Isaac   Level: beginner
6489c58f1c22SToby Isaac 
6490db781477SPatrick Sanan .seealso: `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6491c58f1c22SToby Isaac @*/
6492c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6493c58f1c22SToby Isaac {
6494c58f1c22SToby Isaac   DMLabel        label;
6495c58f1c22SToby Isaac 
6496c58f1c22SToby Isaac   PetscFunctionBegin;
6497c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6498c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6499534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
65009566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6501c58f1c22SToby Isaac   *size = 0;
6502c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
65039566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
6504c58f1c22SToby Isaac   PetscFunctionReturn(0);
6505c58f1c22SToby Isaac }
6506c58f1c22SToby Isaac 
6507c58f1c22SToby Isaac /*@C
6508c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6509c58f1c22SToby Isaac 
6510c58f1c22SToby Isaac   Not Collective
6511c58f1c22SToby Isaac 
6512c58f1c22SToby Isaac   Input Parameters:
6513c58f1c22SToby Isaac + dm - The DM object
6514c58f1c22SToby Isaac . name - The label name
6515c58f1c22SToby Isaac - value - The stratum value
6516c58f1c22SToby Isaac 
6517c58f1c22SToby Isaac   Output Parameter:
6518c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6519c58f1c22SToby Isaac 
6520c58f1c22SToby Isaac   Level: beginner
6521c58f1c22SToby Isaac 
6522db781477SPatrick Sanan .seealso: `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6523c58f1c22SToby Isaac @*/
6524c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6525c58f1c22SToby Isaac {
6526c58f1c22SToby Isaac   DMLabel        label;
6527c58f1c22SToby Isaac 
6528c58f1c22SToby Isaac   PetscFunctionBegin;
6529c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6530c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6531c58f1c22SToby Isaac   PetscValidPointer(points, 4);
65329566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6533c58f1c22SToby Isaac   *points = NULL;
6534c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
65359566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
6536c58f1c22SToby Isaac   PetscFunctionReturn(0);
6537c58f1c22SToby Isaac }
6538c58f1c22SToby Isaac 
65394de306b1SToby Isaac /*@C
65409044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
65414de306b1SToby Isaac 
65424de306b1SToby Isaac   Not Collective
65434de306b1SToby Isaac 
65444de306b1SToby Isaac   Input Parameters:
65454de306b1SToby Isaac + dm - The DM object
65464de306b1SToby Isaac . name - The label name
65474de306b1SToby Isaac . value - The stratum value
65484de306b1SToby Isaac - points - The stratum points
65494de306b1SToby Isaac 
65504de306b1SToby Isaac   Level: beginner
65514de306b1SToby Isaac 
6552db781477SPatrick Sanan .seealso: `DMLabelSetStratumIS()`, `DMGetStratumSize()`
65534de306b1SToby Isaac @*/
65544de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
65554de306b1SToby Isaac {
65564de306b1SToby Isaac   DMLabel        label;
65574de306b1SToby Isaac 
65584de306b1SToby Isaac   PetscFunctionBegin;
65594de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65604de306b1SToby Isaac   PetscValidCharPointer(name, 2);
65614de306b1SToby Isaac   PetscValidPointer(points, 4);
65629566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
65634de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
65649566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
65654de306b1SToby Isaac   PetscFunctionReturn(0);
65664de306b1SToby Isaac }
65674de306b1SToby Isaac 
6568c58f1c22SToby Isaac /*@C
65697cf350d0SPierre Jolivet   DMClearLabelStratum - Remove all points from a stratum from a DMLabel
6570c58f1c22SToby Isaac 
6571c58f1c22SToby Isaac   Not Collective
6572c58f1c22SToby Isaac 
6573c58f1c22SToby Isaac   Input Parameters:
6574c58f1c22SToby Isaac + dm   - The DM object
6575c58f1c22SToby Isaac . name - The label name
6576c58f1c22SToby Isaac - value - The label value for this point
6577c58f1c22SToby Isaac 
6578c58f1c22SToby Isaac   Output Parameter:
6579c58f1c22SToby Isaac 
6580c58f1c22SToby Isaac   Level: beginner
6581c58f1c22SToby Isaac 
6582db781477SPatrick Sanan .seealso: `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6583c58f1c22SToby Isaac @*/
6584c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6585c58f1c22SToby Isaac {
6586c58f1c22SToby Isaac   DMLabel        label;
6587c58f1c22SToby Isaac 
6588c58f1c22SToby Isaac   PetscFunctionBegin;
6589c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6590c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
65919566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6592c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
65939566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
6594c58f1c22SToby Isaac   PetscFunctionReturn(0);
6595c58f1c22SToby Isaac }
6596c58f1c22SToby Isaac 
6597c58f1c22SToby Isaac /*@
6598c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
6599c58f1c22SToby Isaac 
6600c58f1c22SToby Isaac   Not Collective
6601c58f1c22SToby Isaac 
6602c58f1c22SToby Isaac   Input Parameter:
6603c58f1c22SToby Isaac . dm   - The DM object
6604c58f1c22SToby Isaac 
6605c58f1c22SToby Isaac   Output Parameter:
6606c58f1c22SToby Isaac . numLabels - the number of Labels
6607c58f1c22SToby Isaac 
6608c58f1c22SToby Isaac   Level: intermediate
6609c58f1c22SToby Isaac 
6610db781477SPatrick Sanan .seealso: `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6611c58f1c22SToby Isaac @*/
6612c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6613c58f1c22SToby Isaac {
66145d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6615c58f1c22SToby Isaac   PetscInt  n    = 0;
6616c58f1c22SToby Isaac 
6617c58f1c22SToby Isaac   PetscFunctionBegin;
6618c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6619534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
6620c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
6621c58f1c22SToby Isaac   *numLabels = n;
6622c58f1c22SToby Isaac   PetscFunctionReturn(0);
6623c58f1c22SToby Isaac }
6624c58f1c22SToby Isaac 
6625c58f1c22SToby Isaac /*@C
6626c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6627c58f1c22SToby Isaac 
6628c58f1c22SToby Isaac   Not Collective
6629c58f1c22SToby Isaac 
6630c58f1c22SToby Isaac   Input Parameters:
6631c58f1c22SToby Isaac + dm - The DM object
6632c58f1c22SToby Isaac - n  - the label number
6633c58f1c22SToby Isaac 
6634c58f1c22SToby Isaac   Output Parameter:
6635c58f1c22SToby Isaac . name - the label name
6636c58f1c22SToby Isaac 
6637c58f1c22SToby Isaac   Level: intermediate
6638c58f1c22SToby Isaac 
6639db781477SPatrick Sanan .seealso: `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6640c58f1c22SToby Isaac @*/
6641c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6642c58f1c22SToby Isaac {
66435d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
6644c58f1c22SToby Isaac   PetscInt       l    = 0;
6645c58f1c22SToby Isaac 
6646c58f1c22SToby Isaac   PetscFunctionBegin;
6647c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6648c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6649c58f1c22SToby Isaac   while (next) {
6650c58f1c22SToby Isaac     if (l == n) {
66519566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject) next->label, name));
6652c58f1c22SToby Isaac       PetscFunctionReturn(0);
6653c58f1c22SToby Isaac     }
6654c58f1c22SToby Isaac     ++l;
6655c58f1c22SToby Isaac     next = next->next;
6656c58f1c22SToby Isaac   }
665763a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6658c58f1c22SToby Isaac }
6659c58f1c22SToby Isaac 
6660c58f1c22SToby Isaac /*@C
6661c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
6662c58f1c22SToby Isaac 
6663c58f1c22SToby Isaac   Not Collective
6664c58f1c22SToby Isaac 
6665c58f1c22SToby Isaac   Input Parameters:
6666c58f1c22SToby Isaac + dm   - The DM object
6667c58f1c22SToby Isaac - name - The label name
6668c58f1c22SToby Isaac 
6669c58f1c22SToby Isaac   Output Parameter:
6670c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
6671c58f1c22SToby Isaac 
6672c58f1c22SToby Isaac   Level: intermediate
6673c58f1c22SToby Isaac 
6674db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6675c58f1c22SToby Isaac @*/
6676c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6677c58f1c22SToby Isaac {
66785d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
6679d67d17b1SMatthew G. Knepley   const char    *lname;
6680c58f1c22SToby Isaac 
6681c58f1c22SToby Isaac   PetscFunctionBegin;
6682c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6683c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6684534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
6685c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6686c58f1c22SToby Isaac   while (next) {
66879566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
66889566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
6689c58f1c22SToby Isaac     if (*hasLabel) break;
6690c58f1c22SToby Isaac     next = next->next;
6691c58f1c22SToby Isaac   }
6692c58f1c22SToby Isaac   PetscFunctionReturn(0);
6693c58f1c22SToby Isaac }
6694c58f1c22SToby Isaac 
6695c58f1c22SToby Isaac /*@C
6696c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
6697c58f1c22SToby Isaac 
6698c58f1c22SToby Isaac   Not Collective
6699c58f1c22SToby Isaac 
6700c58f1c22SToby Isaac   Input Parameters:
6701c58f1c22SToby Isaac + dm   - The DM object
6702c58f1c22SToby Isaac - name - The label name
6703c58f1c22SToby Isaac 
6704c58f1c22SToby Isaac   Output Parameter:
6705c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6706c58f1c22SToby Isaac 
67076d7c9049SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
67086d7c9049SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
67096d7c9049SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
67106d7c9049SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
67116d7c9049SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
67126d7c9049SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
67136d7c9049SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
67146d7c9049SMatthew G. Knepley 
6715c58f1c22SToby Isaac   Level: intermediate
6716c58f1c22SToby Isaac 
6717db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
6718c58f1c22SToby Isaac @*/
6719c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6720c58f1c22SToby Isaac {
67215d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
6722c58f1c22SToby Isaac   PetscBool      hasLabel;
6723d67d17b1SMatthew G. Knepley   const char    *lname;
6724c58f1c22SToby Isaac 
6725c58f1c22SToby Isaac   PetscFunctionBegin;
6726c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6727c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6728c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6729c58f1c22SToby Isaac   *label = NULL;
6730c58f1c22SToby Isaac   while (next) {
67319566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
67329566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
6733c58f1c22SToby Isaac     if (hasLabel) {
6734c58f1c22SToby Isaac       *label = next->label;
6735c58f1c22SToby Isaac       break;
6736c58f1c22SToby Isaac     }
6737c58f1c22SToby Isaac     next = next->next;
6738c58f1c22SToby Isaac   }
6739c58f1c22SToby Isaac   PetscFunctionReturn(0);
6740c58f1c22SToby Isaac }
6741c58f1c22SToby Isaac 
6742c58f1c22SToby Isaac /*@C
6743c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
6744c58f1c22SToby Isaac 
6745c58f1c22SToby Isaac   Not Collective
6746c58f1c22SToby Isaac 
6747c58f1c22SToby Isaac   Input Parameters:
6748c58f1c22SToby Isaac + dm - The DM object
6749c58f1c22SToby Isaac - n  - the label number
6750c58f1c22SToby Isaac 
6751c58f1c22SToby Isaac   Output Parameter:
6752c58f1c22SToby Isaac . label - the label
6753c58f1c22SToby Isaac 
6754c58f1c22SToby Isaac   Level: intermediate
6755c58f1c22SToby Isaac 
6756db781477SPatrick Sanan .seealso: `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6757c58f1c22SToby Isaac @*/
6758c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
6759c58f1c22SToby Isaac {
67605d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6761c58f1c22SToby Isaac   PetscInt    l    = 0;
6762c58f1c22SToby Isaac 
6763c58f1c22SToby Isaac   PetscFunctionBegin;
6764c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6765c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6766c58f1c22SToby Isaac   while (next) {
6767c58f1c22SToby Isaac     if (l == n) {
6768c58f1c22SToby Isaac       *label = next->label;
6769c58f1c22SToby Isaac       PetscFunctionReturn(0);
6770c58f1c22SToby Isaac     }
6771c58f1c22SToby Isaac     ++l;
6772c58f1c22SToby Isaac     next = next->next;
6773c58f1c22SToby Isaac   }
677463a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6775c58f1c22SToby Isaac }
6776c58f1c22SToby Isaac 
6777c58f1c22SToby Isaac /*@C
6778c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
6779c58f1c22SToby Isaac 
6780c58f1c22SToby Isaac   Not Collective
6781c58f1c22SToby Isaac 
6782c58f1c22SToby Isaac   Input Parameters:
6783c58f1c22SToby Isaac + dm   - The DM object
6784c58f1c22SToby Isaac - label - The DMLabel
6785c58f1c22SToby Isaac 
6786c58f1c22SToby Isaac   Level: developer
6787c58f1c22SToby Isaac 
6788db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6789c58f1c22SToby Isaac @*/
6790c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
6791c58f1c22SToby Isaac {
67925d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
6793c58f1c22SToby Isaac   PetscBool      hasLabel;
6794d67d17b1SMatthew G. Knepley   const char    *lname;
67955d80c0bfSVaclav Hapla   PetscBool      flg;
6796c58f1c22SToby Isaac 
6797c58f1c22SToby Isaac   PetscFunctionBegin;
6798c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67999566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject) label, &lname));
68009566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
68017a8be351SBarry Smith   PetscCheck(!hasLabel,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
68029566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
6803c58f1c22SToby Isaac   tmpLabel->label  = label;
6804c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
68055d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
68065d80c0bfSVaclav Hapla   *p = tmpLabel;
68079566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
68089566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
68095d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
68109566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
6811ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
6812c58f1c22SToby Isaac   PetscFunctionReturn(0);
6813c58f1c22SToby Isaac }
6814c58f1c22SToby Isaac 
6815c58f1c22SToby Isaac /*@C
68164a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
68174a7ee7d0SMatthew G. Knepley 
68184a7ee7d0SMatthew G. Knepley   Not Collective
68194a7ee7d0SMatthew G. Knepley 
68204a7ee7d0SMatthew G. Knepley   Input Parameters:
68214a7ee7d0SMatthew G. Knepley + dm    - The DM object
68224a7ee7d0SMatthew G. Knepley - label - The DMLabel, having the same name, to substitute
68234a7ee7d0SMatthew G. Knepley 
68244a7ee7d0SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
68254a7ee7d0SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
68264a7ee7d0SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
68274a7ee7d0SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
68284a7ee7d0SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
68294a7ee7d0SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
68304a7ee7d0SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
68314a7ee7d0SMatthew G. Knepley 
68324a7ee7d0SMatthew G. Knepley   Level: intermediate
68334a7ee7d0SMatthew G. Knepley 
6834db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
68354a7ee7d0SMatthew G. Knepley @*/
68364a7ee7d0SMatthew G. Knepley PetscErrorCode DMSetLabel(DM dm, DMLabel label)
68374a7ee7d0SMatthew G. Knepley {
68384a7ee7d0SMatthew G. Knepley   DMLabelLink    next = dm->labels;
68394a7ee7d0SMatthew G. Knepley   PetscBool      hasLabel, flg;
68404a7ee7d0SMatthew G. Knepley   const char    *name, *lname;
68414a7ee7d0SMatthew G. Knepley 
68424a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
68434a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68444a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
68459566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject) label, &name));
68464a7ee7d0SMatthew G. Knepley   while (next) {
68479566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
68489566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
68494a7ee7d0SMatthew G. Knepley     if (hasLabel) {
68509566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject) label));
68519566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
68524a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
68539566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
68544a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
68559566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
68564a7ee7d0SMatthew G. Knepley       next->label = label;
68574a7ee7d0SMatthew G. Knepley       break;
68584a7ee7d0SMatthew G. Knepley     }
68594a7ee7d0SMatthew G. Knepley     next = next->next;
68604a7ee7d0SMatthew G. Knepley   }
68614a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
68624a7ee7d0SMatthew G. Knepley }
68634a7ee7d0SMatthew G. Knepley 
68644a7ee7d0SMatthew G. Knepley /*@C
6865e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
6866c58f1c22SToby Isaac 
6867c58f1c22SToby Isaac   Not Collective
6868c58f1c22SToby Isaac 
6869c58f1c22SToby Isaac   Input Parameters:
6870c58f1c22SToby Isaac + dm   - The DM object
6871c58f1c22SToby Isaac - name - The label name
6872c58f1c22SToby Isaac 
6873c58f1c22SToby Isaac   Output Parameter:
6874c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6875c58f1c22SToby Isaac 
6876c58f1c22SToby Isaac   Level: developer
6877c58f1c22SToby Isaac 
6878e5472504SVaclav Hapla   Notes:
6879e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
6880e5472504SVaclav Hapla   DMLabelDestroy() on the label.
6881e5472504SVaclav Hapla 
6882e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
6883e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
6884e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
6885e5472504SVaclav Hapla 
6886db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
6887c58f1c22SToby Isaac @*/
6888c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
6889c58f1c22SToby Isaac {
689095d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
6891c58f1c22SToby Isaac   PetscBool      hasLabel;
6892d67d17b1SMatthew G. Knepley   const char    *lname;
6893c58f1c22SToby Isaac 
6894c58f1c22SToby Isaac   PetscFunctionBegin;
6895c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6896e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
6897e5472504SVaclav Hapla   if (label) {
6898e5472504SVaclav Hapla     PetscValidPointer(label, 3);
6899c58f1c22SToby Isaac     *label = NULL;
6900e5472504SVaclav Hapla   }
69015d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
69029566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) link->label, &lname));
69039566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
6904c58f1c22SToby Isaac     if (hasLabel) {
690595d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
69069566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
690795d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
69089566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
6909ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
691095d578d6SVaclav Hapla       if (label) *label = link->label;
69119566063dSJacob Faibussowitsch       else       PetscCall(DMLabelDestroy(&link->label));
69129566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
6913c58f1c22SToby Isaac       break;
6914c58f1c22SToby Isaac     }
6915c58f1c22SToby Isaac   }
6916c58f1c22SToby Isaac   PetscFunctionReturn(0);
6917c58f1c22SToby Isaac }
6918c58f1c22SToby Isaac 
6919306894acSVaclav Hapla /*@
6920306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
6921306894acSVaclav Hapla 
6922306894acSVaclav Hapla   Not Collective
6923306894acSVaclav Hapla 
6924306894acSVaclav Hapla   Input Parameters:
6925306894acSVaclav Hapla + dm   - The DM object
6926876aa926SVaclav Hapla . label - The DMLabel to be removed from the DM
6927306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
6928306894acSVaclav Hapla 
6929306894acSVaclav Hapla   Level: developer
6930306894acSVaclav Hapla 
6931306894acSVaclav Hapla   Notes:
6932306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
6933306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
6934306894acSVaclav Hapla   *label nullified.
6935306894acSVaclav Hapla 
6936db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
6937306894acSVaclav Hapla @*/
6938306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
6939306894acSVaclav Hapla {
694043e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
6941306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
6942306894acSVaclav Hapla 
6943306894acSVaclav Hapla   PetscFunctionBegin;
6944306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6945306894acSVaclav Hapla   PetscValidPointer(label, 2);
6946f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
6947306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
6948306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
69495d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
695043e45a93SVaclav Hapla     if (*label == link->label) {
6951306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
695243e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
6953306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
6954ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
695543e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
69569566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
69579566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
6958306894acSVaclav Hapla       break;
6959306894acSVaclav Hapla     }
6960306894acSVaclav Hapla   }
69617a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
6962306894acSVaclav Hapla   PetscFunctionReturn(0);
6963306894acSVaclav Hapla }
6964306894acSVaclav Hapla 
6965c58f1c22SToby Isaac /*@C
6966c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
6967c58f1c22SToby Isaac 
6968c58f1c22SToby Isaac   Not Collective
6969c58f1c22SToby Isaac 
6970c58f1c22SToby Isaac   Input Parameters:
6971c58f1c22SToby Isaac + dm   - The DM object
6972c58f1c22SToby Isaac - name - The label name
6973c58f1c22SToby Isaac 
6974c58f1c22SToby Isaac   Output Parameter:
6975c58f1c22SToby Isaac . output - The flag for output
6976c58f1c22SToby Isaac 
6977c58f1c22SToby Isaac   Level: developer
6978c58f1c22SToby Isaac 
6979db781477SPatrick Sanan .seealso: `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6980c58f1c22SToby Isaac @*/
6981c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
6982c58f1c22SToby Isaac {
69835d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
6984d67d17b1SMatthew G. Knepley   const char    *lname;
6985c58f1c22SToby Isaac 
6986c58f1c22SToby Isaac   PetscFunctionBegin;
6987c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6988dadcf809SJacob Faibussowitsch   PetscValidCharPointer(name, 2);
6989dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(output, 3);
6990c58f1c22SToby Isaac   while (next) {
6991c58f1c22SToby Isaac     PetscBool flg;
6992c58f1c22SToby Isaac 
69939566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
69949566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
6995c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
6996c58f1c22SToby Isaac     next = next->next;
6997c58f1c22SToby Isaac   }
699898921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6999c58f1c22SToby Isaac }
7000c58f1c22SToby Isaac 
7001c58f1c22SToby Isaac /*@C
7002c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7003c58f1c22SToby Isaac 
7004c58f1c22SToby Isaac   Not Collective
7005c58f1c22SToby Isaac 
7006c58f1c22SToby Isaac   Input Parameters:
7007c58f1c22SToby Isaac + dm     - The DM object
7008c58f1c22SToby Isaac . name   - The label name
7009c58f1c22SToby Isaac - output - The flag for output
7010c58f1c22SToby Isaac 
7011c58f1c22SToby Isaac   Level: developer
7012c58f1c22SToby Isaac 
7013db781477SPatrick Sanan .seealso: `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7014c58f1c22SToby Isaac @*/
7015c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7016c58f1c22SToby Isaac {
70175d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7018d67d17b1SMatthew G. Knepley   const char    *lname;
7019c58f1c22SToby Isaac 
7020c58f1c22SToby Isaac   PetscFunctionBegin;
7021c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7022534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7023c58f1c22SToby Isaac   while (next) {
7024c58f1c22SToby Isaac     PetscBool flg;
7025c58f1c22SToby Isaac 
70269566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
70279566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
7028c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7029c58f1c22SToby Isaac     next = next->next;
7030c58f1c22SToby Isaac   }
703198921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7032c58f1c22SToby Isaac }
7033c58f1c22SToby Isaac 
7034c58f1c22SToby Isaac /*@
7035c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7036c58f1c22SToby Isaac 
7037d083f849SBarry Smith   Collective on dmA
7038c58f1c22SToby Isaac 
7039d8d19677SJose E. Roman   Input Parameters:
70405d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
70412cbb9b06SVaclav Hapla . dmB - The DM object to which labels are copied
70425d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
70432cbb9b06SVaclav Hapla . all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
70442cbb9b06SVaclav Hapla - emode - How to behave when a DMLabel in the source and destination DMs with the same name is encountered (see DMCopyLabelsMode)
7045c58f1c22SToby Isaac 
7046c58f1c22SToby Isaac   Level: intermediate
7047c58f1c22SToby Isaac 
70482cbb9b06SVaclav Hapla   Notes:
70492cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7050c58f1c22SToby Isaac 
7051db781477SPatrick Sanan .seealso: `DMAddLabel()`, `DMCopyLabelsMode`
7052c58f1c22SToby Isaac @*/
70532cbb9b06SVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7054c58f1c22SToby Isaac {
70552cbb9b06SVaclav Hapla   DMLabel        label, labelNew, labelOld;
7056c58f1c22SToby Isaac   const char    *name;
7057c58f1c22SToby Isaac   PetscBool      flg;
70585d80c0bfSVaclav Hapla   DMLabelLink    link;
7059c58f1c22SToby Isaac 
70605d80c0bfSVaclav Hapla   PetscFunctionBegin;
70615d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
70625d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
70635d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
70645d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
70657a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER,PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
70665d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
70675d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
70685d80c0bfSVaclav Hapla     label=link->label;
70699566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
70705d80c0bfSVaclav Hapla     if (!all) {
70719566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7072c58f1c22SToby Isaac       if (flg) continue;
70739566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
70747d5acc75SStefano Zampini       if (flg) continue;
70759566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7076ba2698f1SMatthew G. Knepley       if (flg) continue;
70775d80c0bfSVaclav Hapla     }
70789566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
70792cbb9b06SVaclav Hapla     if (labelOld) {
70802cbb9b06SVaclav Hapla       switch (emode) {
70812cbb9b06SVaclav Hapla         case DM_COPY_LABELS_KEEP:
70822cbb9b06SVaclav Hapla           continue;
70832cbb9b06SVaclav Hapla         case DM_COPY_LABELS_REPLACE:
70849566063dSJacob Faibussowitsch           PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
70852cbb9b06SVaclav Hapla           break;
70862cbb9b06SVaclav Hapla         case DM_COPY_LABELS_FAIL:
708798921bdaSJacob Faibussowitsch           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
70882cbb9b06SVaclav Hapla         default:
70897a8be351SBarry Smith           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
70902cbb9b06SVaclav Hapla       }
70912cbb9b06SVaclav Hapla     }
70925d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
70939566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
70945d80c0bfSVaclav Hapla     } else {
70955d80c0bfSVaclav Hapla       labelNew = label;
70965d80c0bfSVaclav Hapla     }
70979566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
70989566063dSJacob Faibussowitsch     if (mode==PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7099c58f1c22SToby Isaac   }
7100c58f1c22SToby Isaac   PetscFunctionReturn(0);
7101c58f1c22SToby Isaac }
7102461a15a0SLisandro Dalcin 
7103609dae6eSVaclav Hapla /*@C
7104609dae6eSVaclav Hapla   DMCompareLabels - Compare labels of two DMPlex meshes
7105609dae6eSVaclav Hapla 
71065efe38ccSVaclav Hapla   Collective
7107609dae6eSVaclav Hapla 
7108609dae6eSVaclav Hapla   Input Parameters:
7109609dae6eSVaclav Hapla + dm0 - First DM object
7110609dae6eSVaclav Hapla - dm1 - Second DM object
7111609dae6eSVaclav Hapla 
7112609dae6eSVaclav Hapla   Output Parameters
71135efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
7114609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
7115609dae6eSVaclav Hapla 
7116609dae6eSVaclav Hapla   Level: intermediate
7117609dae6eSVaclav Hapla 
7118609dae6eSVaclav Hapla   Notes:
71195efe38ccSVaclav Hapla   The output flag equal is the same on all processes.
71205efe38ccSVaclav Hapla   If it is passed as NULL and difference is found, an error is thrown on all processes.
71215efe38ccSVaclav Hapla   Make sure to pass NULL on all processes.
7122609dae6eSVaclav Hapla 
71235efe38ccSVaclav Hapla   The output message is set independently on each rank.
71245efe38ccSVaclav Hapla   It is set to NULL if no difference was found on the current rank. It must be freed by user.
71255efe38ccSVaclav Hapla   If message is passed as NULL and difference is found, the difference description is printed to stderr in synchronized manner.
71265efe38ccSVaclav Hapla   Make sure to pass NULL on all processes.
7127609dae6eSVaclav Hapla 
7128609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7129609dae6eSVaclav Hapla   DMLabelCompare() is used to compare each pair of labels with the same name.
7130609dae6eSVaclav Hapla 
7131609dae6eSVaclav Hapla   Fortran Notes:
7132609dae6eSVaclav Hapla   This function is currently not available from Fortran.
7133609dae6eSVaclav Hapla 
7134db781477SPatrick Sanan .seealso: `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7135609dae6eSVaclav Hapla @*/
71365efe38ccSVaclav Hapla PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7137609dae6eSVaclav Hapla {
71385efe38ccSVaclav Hapla   PetscInt        n, i;
7139609dae6eSVaclav Hapla   char            msg[PETSC_MAX_PATH_LEN] = "";
71405efe38ccSVaclav Hapla   PetscBool       eq;
7141609dae6eSVaclav Hapla   MPI_Comm        comm;
71425efe38ccSVaclav Hapla   PetscMPIInt     rank;
7143609dae6eSVaclav Hapla 
7144609dae6eSVaclav Hapla   PetscFunctionBegin;
7145609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0,DM_CLASSID,1);
7146609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1,DM_CLASSID,2);
7147609dae6eSVaclav Hapla   PetscCheckSameComm(dm0,1,dm1,2);
71485efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal,3);
7149609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
71509566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
71519566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
71525efe38ccSVaclav Hapla   {
71535efe38ccSVaclav Hapla     PetscInt n1;
71545efe38ccSVaclav Hapla 
71559566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
71569566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
71575efe38ccSVaclav Hapla     eq = (PetscBool) (n == n1);
71585efe38ccSVaclav Hapla     if (!eq) {
715963a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7160609dae6eSVaclav Hapla     }
71619566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
71625efe38ccSVaclav Hapla     if (!eq) goto finish;
71635efe38ccSVaclav Hapla   }
71645efe38ccSVaclav Hapla   for (i=0; i<n; i++) {
7165609dae6eSVaclav Hapla     DMLabel     l0, l1;
7166609dae6eSVaclav Hapla     const char *name;
7167609dae6eSVaclav Hapla     char       *msgInner;
7168609dae6eSVaclav Hapla 
7169609dae6eSVaclav Hapla     /* Ignore label order */
71709566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
71719566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
71729566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7173609dae6eSVaclav Hapla     if (!l1) {
717463a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
71755efe38ccSVaclav Hapla       eq = PETSC_FALSE;
71765efe38ccSVaclav Hapla       break;
7177609dae6eSVaclav Hapla     }
71789566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
71799566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
71809566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
71815efe38ccSVaclav Hapla     if (!eq) break;
7182609dae6eSVaclav Hapla   }
71839566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7184609dae6eSVaclav Hapla finish:
71855efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7186609dae6eSVaclav Hapla   if (message) {
7187609dae6eSVaclav Hapla     *message = NULL;
7188609dae6eSVaclav Hapla     if (msg[0]) {
71899566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(msg, message));
7190609dae6eSVaclav Hapla     }
71915efe38ccSVaclav Hapla   } else {
71925efe38ccSVaclav Hapla     if (msg[0]) {
71939566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
7194609dae6eSVaclav Hapla     }
71959566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
71965efe38ccSVaclav Hapla   }
71975efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
71985efe38ccSVaclav Hapla   if (equal) *equal = eq;
71997a8be351SBarry Smith   else PetscCheck(eq,comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
7200609dae6eSVaclav Hapla   PetscFunctionReturn(0);
7201609dae6eSVaclav Hapla }
7202609dae6eSVaclav Hapla 
7203461a15a0SLisandro Dalcin PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7204461a15a0SLisandro Dalcin {
7205461a15a0SLisandro Dalcin   PetscFunctionBegin;
7206461a15a0SLisandro Dalcin   PetscValidPointer(label,2);
7207461a15a0SLisandro Dalcin   if (!*label) {
72089566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
72099566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7210461a15a0SLisandro Dalcin   }
72119566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
7212461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
7213461a15a0SLisandro Dalcin }
7214461a15a0SLisandro Dalcin 
72150fdc7489SMatthew Knepley /*
72160fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
72170fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
72180fdc7489SMatthew Knepley   (label, id) pair in the DM.
72190fdc7489SMatthew Knepley 
72200fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
72210fdc7489SMatthew Knepley   each label.
72220fdc7489SMatthew Knepley */
72230fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
72240fdc7489SMatthew Knepley {
72250fdc7489SMatthew Knepley   DMUniversalLabel ul;
72260fdc7489SMatthew Knepley   PetscBool       *active;
72270fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
72280fdc7489SMatthew Knepley 
72290fdc7489SMatthew Knepley   PetscFunctionBegin;
72309566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
72319566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
72329566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
72339566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
72340fdc7489SMatthew Knepley   ul->Nl = 0;
72350fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
72360fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
72370fdc7489SMatthew Knepley     const char *name;
72380fdc7489SMatthew Knepley 
72399566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
72409566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
72419566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
72420fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
72430fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
72440fdc7489SMatthew Knepley   }
72459566063dSJacob 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));
72460fdc7489SMatthew Knepley   ul->Nv = 0;
72470fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
72480fdc7489SMatthew Knepley     DMLabel     label;
72490fdc7489SMatthew Knepley     PetscInt    nv;
72500fdc7489SMatthew Knepley     const char *name;
72510fdc7489SMatthew Knepley 
72520fdc7489SMatthew Knepley     if (!active[l]) continue;
72539566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
72549566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
72559566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
72569566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
72570fdc7489SMatthew Knepley     ul->indices[m]   = l;
72580fdc7489SMatthew Knepley     ul->Nv          += nv;
72590fdc7489SMatthew Knepley     ul->offsets[m+1] = nv;
72600fdc7489SMatthew Knepley     ul->bits[m+1]    = PetscCeilReal(PetscLog2Real(nv+1));
72610fdc7489SMatthew Knepley     ++m;
72620fdc7489SMatthew Knepley   }
72630fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
72640fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l-1] + ul->offsets[l];
72650fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l-1]    + ul->bits[l];
72660fdc7489SMatthew Knepley   }
72670fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
72680fdc7489SMatthew Knepley     PetscInt b;
72690fdc7489SMatthew Knepley 
72700fdc7489SMatthew Knepley     ul->masks[l] = 0;
72710fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l+1]; ++b) ul->masks[l] |= 1 << b;
72720fdc7489SMatthew Knepley   }
72739566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
72740fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
72750fdc7489SMatthew Knepley     DMLabel         label;
72760fdc7489SMatthew Knepley     IS              valueIS;
72770fdc7489SMatthew Knepley     const PetscInt *varr;
72780fdc7489SMatthew Knepley     PetscInt        nv, v;
72790fdc7489SMatthew Knepley 
72800fdc7489SMatthew Knepley     if (!active[l]) continue;
72819566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
72829566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
72839566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
72849566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
72850fdc7489SMatthew Knepley     for (v = 0; v < nv; ++v) {
72860fdc7489SMatthew Knepley       ul->values[ul->offsets[m]+v] = varr[v];
72870fdc7489SMatthew Knepley     }
72889566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
72899566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
72909566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
72910fdc7489SMatthew Knepley     ++m;
72920fdc7489SMatthew Knepley   }
72939566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
72940fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
72950fdc7489SMatthew Knepley     PetscInt  uval = 0;
72960fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
72970fdc7489SMatthew Knepley 
72980fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
72990fdc7489SMatthew Knepley       DMLabel  label;
73000649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
73010fdc7489SMatthew Knepley 
73020fdc7489SMatthew Knepley       if (!active[l]) continue;
73039566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
73049566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
73059566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
73060fdc7489SMatthew Knepley       if (val == defval) {++m; continue;}
73070649b39aSStefano Zampini       nv = ul->offsets[m+1]-ul->offsets[m];
73080fdc7489SMatthew Knepley       marked = PETSC_TRUE;
73099566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
731063a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
73110fdc7489SMatthew Knepley       uval += (loc+1) << ul->bits[m];
73120fdc7489SMatthew Knepley       ++m;
73130fdc7489SMatthew Knepley     }
73149566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
73150fdc7489SMatthew Knepley   }
73169566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
73170fdc7489SMatthew Knepley   *universal = ul;
73180fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73190fdc7489SMatthew Knepley }
73200fdc7489SMatthew Knepley 
73210fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
73220fdc7489SMatthew Knepley {
73230fdc7489SMatthew Knepley   PetscInt       l;
73240fdc7489SMatthew Knepley 
73250fdc7489SMatthew Knepley   PetscFunctionBegin;
73269566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
73279566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
73289566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
73299566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
73309566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
73310fdc7489SMatthew Knepley   *universal = NULL;
73320fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73330fdc7489SMatthew Knepley }
73340fdc7489SMatthew Knepley 
73350fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
73360fdc7489SMatthew Knepley {
73370fdc7489SMatthew Knepley   PetscFunctionBegin;
73380fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
73390fdc7489SMatthew Knepley   *ulabel = ul->label;
73400fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73410fdc7489SMatthew Knepley }
73420fdc7489SMatthew Knepley 
73430fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
73440fdc7489SMatthew Knepley {
73450fdc7489SMatthew Knepley   PetscInt       Nl = ul->Nl, l;
73460fdc7489SMatthew Knepley 
73470fdc7489SMatthew Knepley   PetscFunctionBegin;
7348064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
73490fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
73509566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
73519566063dSJacob Faibussowitsch     else               PetscCall(DMCreateLabel(dm, ul->names[l]));
73520fdc7489SMatthew Knepley   }
73530fdc7489SMatthew Knepley   if (preserveOrder) {
73540fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
73550fdc7489SMatthew Knepley       const char *name;
73560fdc7489SMatthew Knepley       PetscBool   match;
73570fdc7489SMatthew Knepley 
73589566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
73599566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
736063a3b9bcSJacob 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]);
73610fdc7489SMatthew Knepley     }
73620fdc7489SMatthew Knepley   }
73630fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73640fdc7489SMatthew Knepley }
73650fdc7489SMatthew Knepley 
73660fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
73670fdc7489SMatthew Knepley {
73680fdc7489SMatthew Knepley   PetscInt       l;
73690fdc7489SMatthew Knepley 
73700fdc7489SMatthew Knepley   PetscFunctionBegin;
73710fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
73720fdc7489SMatthew Knepley     DMLabel  label;
73730fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
73740fdc7489SMatthew Knepley 
73750fdc7489SMatthew Knepley     if (lval) {
73769566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
73779566063dSJacob Faibussowitsch       else          PetscCall(DMGetLabel(dm, ul->names[l], &label));
73789566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l]+lval-1]));
73790fdc7489SMatthew Knepley     }
73800fdc7489SMatthew Knepley   }
73810fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73820fdc7489SMatthew Knepley }
7383a8fb8f29SToby Isaac 
7384a8fb8f29SToby Isaac /*@
7385a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7386a8fb8f29SToby Isaac 
7387a8fb8f29SToby Isaac   Input Parameter:
7388a8fb8f29SToby Isaac . dm - The DM object
7389a8fb8f29SToby Isaac 
7390a8fb8f29SToby Isaac   Output Parameter:
7391a8fb8f29SToby Isaac . cdm - The coarse DM
7392a8fb8f29SToby Isaac 
7393a8fb8f29SToby Isaac   Level: intermediate
7394a8fb8f29SToby Isaac 
7395db781477SPatrick Sanan .seealso: `DMSetCoarseDM()`
7396a8fb8f29SToby Isaac @*/
7397a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7398a8fb8f29SToby Isaac {
7399a8fb8f29SToby Isaac   PetscFunctionBegin;
7400a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7401a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7402a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7403a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7404a8fb8f29SToby Isaac }
7405a8fb8f29SToby Isaac 
7406a8fb8f29SToby Isaac /*@
7407a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7408a8fb8f29SToby Isaac 
7409a8fb8f29SToby Isaac   Input Parameters:
7410a8fb8f29SToby Isaac + dm - The DM object
7411a8fb8f29SToby Isaac - cdm - The coarse DM
7412a8fb8f29SToby Isaac 
7413a8fb8f29SToby Isaac   Level: intermediate
7414a8fb8f29SToby Isaac 
7415db781477SPatrick Sanan .seealso: `DMGetCoarseDM()`
7416a8fb8f29SToby Isaac @*/
7417a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7418a8fb8f29SToby Isaac {
7419a8fb8f29SToby Isaac   PetscFunctionBegin;
7420a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7421a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
74229566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
74239566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7424a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7425a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7426a8fb8f29SToby Isaac }
7427a8fb8f29SToby Isaac 
742888bdff64SToby Isaac /*@
742988bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
743088bdff64SToby Isaac 
743188bdff64SToby Isaac   Input Parameter:
743288bdff64SToby Isaac . dm - The DM object
743388bdff64SToby Isaac 
743488bdff64SToby Isaac   Output Parameter:
743588bdff64SToby Isaac . fdm - The fine DM
743688bdff64SToby Isaac 
743788bdff64SToby Isaac   Level: intermediate
743888bdff64SToby Isaac 
7439db781477SPatrick Sanan .seealso: `DMSetFineDM()`
744088bdff64SToby Isaac @*/
744188bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
744288bdff64SToby Isaac {
744388bdff64SToby Isaac   PetscFunctionBegin;
744488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
744588bdff64SToby Isaac   PetscValidPointer(fdm, 2);
744688bdff64SToby Isaac   *fdm = dm->fineMesh;
744788bdff64SToby Isaac   PetscFunctionReturn(0);
744888bdff64SToby Isaac }
744988bdff64SToby Isaac 
745088bdff64SToby Isaac /*@
745188bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
745288bdff64SToby Isaac 
745388bdff64SToby Isaac   Input Parameters:
745488bdff64SToby Isaac + dm - The DM object
745588bdff64SToby Isaac - fdm - The fine DM
745688bdff64SToby Isaac 
745788bdff64SToby Isaac   Level: intermediate
745888bdff64SToby Isaac 
7459db781477SPatrick Sanan .seealso: `DMGetFineDM()`
746088bdff64SToby Isaac @*/
746188bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
746288bdff64SToby Isaac {
746388bdff64SToby Isaac   PetscFunctionBegin;
746488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
746588bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
74669566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
74679566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
746888bdff64SToby Isaac   dm->fineMesh = fdm;
746988bdff64SToby Isaac   PetscFunctionReturn(0);
747088bdff64SToby Isaac }
747188bdff64SToby Isaac 
7472a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7473a6ba4734SToby Isaac 
7474a6ba4734SToby Isaac /*@C
7475a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7476a6ba4734SToby Isaac 
7477783e2ec8SMatthew G. Knepley   Collective on dm
7478783e2ec8SMatthew G. Knepley 
7479a6ba4734SToby Isaac   Input Parameters:
74804c258f51SMatthew G. Knepley + dm       - The DM, with a PetscDS that matches the problem being constrained
7481f971fd6bSMatthew G. Knepley . type     - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7482a6ba4734SToby Isaac . name     - The BC name
748345480ffeSMatthew G. Knepley . label    - The label defining constrained points
748445480ffeSMatthew G. Knepley . Nv       - The number of DMLabel values for constrained points
748545480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7486a6ba4734SToby Isaac . field    - The field to constrain
748745480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7488a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7489a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
749056cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7491a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7492a6ba4734SToby Isaac 
749345480ffeSMatthew G. Knepley   Output Parameter:
749445480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
749545480ffeSMatthew G. Knepley 
7496a6ba4734SToby Isaac   Options Database Keys:
7497a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7498a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7499a6ba4734SToby Isaac 
750056cf3b9cSMatthew G. Knepley   Note:
750156cf3b9cSMatthew G. Knepley   Both bcFunc abd bcFunc_t will depend on the boundary condition type. If the type if DM_BC_ESSENTIAL, Then the calling sequence is:
750256cf3b9cSMatthew G. Knepley 
750356cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
750456cf3b9cSMatthew G. Knepley 
750556cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
750656cf3b9cSMatthew G. Knepley 
750756cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
750856cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
750956cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
751056cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
751156cf3b9cSMatthew G. Knepley 
751256cf3b9cSMatthew G. Knepley + dim - the spatial dimension
751356cf3b9cSMatthew G. Knepley . Nf - the number of fields
751456cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
751556cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
751656cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
751756cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
751856cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
751956cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
752056cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
752156cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
752256cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
752356cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
752456cf3b9cSMatthew G. Knepley . t - current time
752556cf3b9cSMatthew G. Knepley . x - coordinates of the current point
752656cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
752756cf3b9cSMatthew G. Knepley . constants - constant parameters
752856cf3b9cSMatthew G. Knepley - bcval - output values at the current point
752956cf3b9cSMatthew G. Knepley 
7530ed808b8fSJed Brown   Level: intermediate
7531a6ba4734SToby Isaac 
7532db781477SPatrick Sanan .seealso: `DSGetBoundary()`, `PetscDSAddBoundary()`
7533a6ba4734SToby Isaac @*/
753445480ffeSMatthew 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)
7535a6ba4734SToby Isaac {
7536e5e52638SMatthew G. Knepley   PetscDS        ds;
7537a6ba4734SToby Isaac 
7538a6ba4734SToby Isaac   PetscFunctionBegin;
7539a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7540783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
754145480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
754245480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
754345480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
754445480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
754501a5d20dSJed Brown   PetscCheck(!dm->localSection,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot add boundary to DM after creating local section");
75469566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7547799db056SMatthew G. Knepley   /* Complete label */
7548799db056SMatthew G. Knepley   if (label) {
7549799db056SMatthew G. Knepley     PetscObject  obj;
7550799db056SMatthew G. Knepley     PetscClassId id;
7551799db056SMatthew G. Knepley 
7552799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7553799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7554799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7555799db056SMatthew G. Knepley       DM plex;
7556799db056SMatthew G. Knepley 
7557799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7558799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7559799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7560799db056SMatthew G. Knepley     }
7561799db056SMatthew G. Knepley   }
75629566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
7563a6ba4734SToby Isaac   PetscFunctionReturn(0);
7564a6ba4734SToby Isaac }
7565a6ba4734SToby Isaac 
756645480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7567e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7568e6f8dbb6SToby Isaac {
7569e5e52638SMatthew G. Knepley   PetscDS        ds;
7570dff059c6SToby Isaac   DMBoundary    *lastnext;
7571e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7572e6f8dbb6SToby Isaac 
7573e6f8dbb6SToby Isaac   PetscFunctionBegin;
75749566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7575e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
757647a1f5adSToby Isaac   if (dm->boundary) {
757747a1f5adSToby Isaac     DMBoundary next = dm->boundary;
757847a1f5adSToby Isaac 
757947a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
758047a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
758147a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
758247a1f5adSToby Isaac     while (next) {
758347a1f5adSToby Isaac       DMBoundary b = next;
758447a1f5adSToby Isaac 
758547a1f5adSToby Isaac       next = b->next;
75869566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7587a6ba4734SToby Isaac     }
758847a1f5adSToby Isaac     dm->boundary = NULL;
7589a6ba4734SToby Isaac   }
759047a1f5adSToby Isaac 
7591dff059c6SToby Isaac   lastnext = &(dm->boundary);
7592e6f8dbb6SToby Isaac   while (dsbound) {
7593e6f8dbb6SToby Isaac     DMBoundary dmbound;
7594e6f8dbb6SToby Isaac 
75959566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7596e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
759745480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
759847a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7599dff059c6SToby Isaac     *lastnext = dmbound;
7600dff059c6SToby Isaac     lastnext = &(dmbound->next);
7601dff059c6SToby Isaac     dsbound = dsbound->next;
7602a6ba4734SToby Isaac   }
7603a6ba4734SToby Isaac   PetscFunctionReturn(0);
7604a6ba4734SToby Isaac }
7605a6ba4734SToby Isaac 
7606a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7607a6ba4734SToby Isaac {
7608b95f2879SToby Isaac   DMBoundary     b;
7609a6ba4734SToby Isaac 
7610a6ba4734SToby Isaac   PetscFunctionBegin;
7611a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7612534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7613a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
76149566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7615b95f2879SToby Isaac   b = dm->boundary;
7616a6ba4734SToby Isaac   while (b && !(*isBd)) {
7617e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7618e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7619a6ba4734SToby Isaac     PetscInt   i;
7620a6ba4734SToby Isaac 
762145480ffeSMatthew G. Knepley     if (label) {
76229566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7623a6ba4734SToby Isaac     }
7624a6ba4734SToby Isaac     b = b->next;
7625a6ba4734SToby Isaac   }
7626a6ba4734SToby Isaac   PetscFunctionReturn(0);
7627a6ba4734SToby Isaac }
76284d6f44ffSToby Isaac 
76294d6f44ffSToby Isaac /*@C
7630a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
7631a6e0b375SMatthew G. Knepley 
7632a6e0b375SMatthew G. Knepley   Collective on DM
76334d6f44ffSToby Isaac 
76344d6f44ffSToby Isaac   Input Parameters:
76354d6f44ffSToby Isaac + dm      - The DM
76360709b2feSToby Isaac . time    - The time
76374d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
76384d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
76394d6f44ffSToby Isaac - mode    - The insertion mode for values
76404d6f44ffSToby Isaac 
76414d6f44ffSToby Isaac   Output Parameter:
76424d6f44ffSToby Isaac . X - vector
76434d6f44ffSToby Isaac 
76444d6f44ffSToby Isaac    Calling sequence of func:
764577b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
76464d6f44ffSToby Isaac 
76474d6f44ffSToby Isaac +  dim - The spatial dimension
76488ec8862eSJed Brown .  time - The time at which to sample
76494d6f44ffSToby Isaac .  x   - The coordinates
765077b739a6SMatthew Knepley .  Nc  - The number of components
76514d6f44ffSToby Isaac .  u   - The output field values
76524d6f44ffSToby Isaac -  ctx - optional user-defined function context
76534d6f44ffSToby Isaac 
76544d6f44ffSToby Isaac   Level: developer
76554d6f44ffSToby Isaac 
7656db781477SPatrick Sanan .seealso: `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
76574d6f44ffSToby Isaac @*/
76580709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
76594d6f44ffSToby Isaac {
76604d6f44ffSToby Isaac   Vec            localX;
76614d6f44ffSToby Isaac 
76624d6f44ffSToby Isaac   PetscFunctionBegin;
76634d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
76649566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
76659566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
76669566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
76679566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
76689566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
76694d6f44ffSToby Isaac   PetscFunctionReturn(0);
76704d6f44ffSToby Isaac }
76714d6f44ffSToby Isaac 
7672a6e0b375SMatthew G. Knepley /*@C
7673a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
7674a6e0b375SMatthew G. Knepley 
7675a6e0b375SMatthew G. Knepley   Not collective
7676a6e0b375SMatthew G. Knepley 
7677a6e0b375SMatthew G. Knepley   Input Parameters:
7678a6e0b375SMatthew G. Knepley + dm      - The DM
7679a6e0b375SMatthew G. Knepley . time    - The time
7680a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7681a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7682a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7683a6e0b375SMatthew G. Knepley 
7684a6e0b375SMatthew G. Knepley   Output Parameter:
7685a6e0b375SMatthew G. Knepley . localX - vector
7686a6e0b375SMatthew G. Knepley 
7687a6e0b375SMatthew G. Knepley    Calling sequence of func:
768877b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7689a6e0b375SMatthew G. Knepley 
7690a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7691a6e0b375SMatthew G. Knepley .  x   - The coordinates
769277b739a6SMatthew Knepley .  Nc  - The number of components
7693a6e0b375SMatthew G. Knepley .  u   - The output field values
7694a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7695a6e0b375SMatthew G. Knepley 
7696a6e0b375SMatthew G. Knepley   Level: developer
7697a6e0b375SMatthew G. Knepley 
7698db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7699a6e0b375SMatthew G. Knepley @*/
77000709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
77014d6f44ffSToby Isaac {
77024d6f44ffSToby Isaac   PetscFunctionBegin;
77034d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7704064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
77057a8be351SBarry Smith   PetscCheck(dm->ops->projectfunctionlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
77069566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX));
77074d6f44ffSToby Isaac   PetscFunctionReturn(0);
77084d6f44ffSToby Isaac }
77094d6f44ffSToby Isaac 
7710a6e0b375SMatthew G. Knepley /*@C
7711a6e0b375SMatthew G. Knepley   DMProjectFunctionLabel - This projects the given function into the function space provided, putting the coefficients in a global vector, setting values only for points in the given label.
7712a6e0b375SMatthew G. Knepley 
7713a6e0b375SMatthew G. Knepley   Collective on DM
7714a6e0b375SMatthew G. Knepley 
7715a6e0b375SMatthew G. Knepley   Input Parameters:
7716a6e0b375SMatthew G. Knepley + dm      - The DM
7717a6e0b375SMatthew G. Knepley . time    - The time
7718a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
7719a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7720a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7721a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7722a6e0b375SMatthew G. Knepley 
7723a6e0b375SMatthew G. Knepley   Output Parameter:
7724a6e0b375SMatthew G. Knepley . X - vector
7725a6e0b375SMatthew G. Knepley 
7726a6e0b375SMatthew G. Knepley    Calling sequence of func:
772777b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7728a6e0b375SMatthew G. Knepley 
7729a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7730a6e0b375SMatthew G. Knepley .  x   - The coordinates
773177b739a6SMatthew Knepley .  Nc  - The number of components
7732a6e0b375SMatthew G. Knepley .  u   - The output field values
7733a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7734a6e0b375SMatthew G. Knepley 
7735a6e0b375SMatthew G. Knepley   Level: developer
7736a6e0b375SMatthew G. Knepley 
7737db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
7738a6e0b375SMatthew G. Knepley @*/
77392c53366bSMatthew 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)
77402c53366bSMatthew G. Knepley {
77412c53366bSMatthew G. Knepley   Vec            localX;
77422c53366bSMatthew G. Knepley 
77432c53366bSMatthew G. Knepley   PetscFunctionBegin;
77442c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77459566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
77469566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
77479566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
77489566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
77499566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
77502c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
77512c53366bSMatthew G. Knepley }
77522c53366bSMatthew G. Knepley 
7753a6e0b375SMatthew G. Knepley /*@C
7754a6e0b375SMatthew G. Knepley   DMProjectFunctionLabelLocal - This projects the given function into the function space provided, putting the coefficients in a local vector, setting values only for points in the given label.
7755a6e0b375SMatthew G. Knepley 
7756a6e0b375SMatthew G. Knepley   Not collective
7757a6e0b375SMatthew G. Knepley 
7758a6e0b375SMatthew G. Knepley   Input Parameters:
7759a6e0b375SMatthew G. Knepley + dm      - The DM
7760a6e0b375SMatthew G. Knepley . time    - The time
7761a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
7762a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7763a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7764a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7765a6e0b375SMatthew G. Knepley 
7766a6e0b375SMatthew G. Knepley   Output Parameter:
7767a6e0b375SMatthew G. Knepley . localX - vector
7768a6e0b375SMatthew G. Knepley 
7769a6e0b375SMatthew G. Knepley    Calling sequence of func:
777077b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7771a6e0b375SMatthew G. Knepley 
7772a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7773a6e0b375SMatthew G. Knepley .  x   - The coordinates
777477b739a6SMatthew Knepley .  Nc  - The number of components
7775a6e0b375SMatthew G. Knepley .  u   - The output field values
7776a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7777a6e0b375SMatthew G. Knepley 
7778a6e0b375SMatthew G. Knepley   Level: developer
7779a6e0b375SMatthew G. Knepley 
7780db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7781a6e0b375SMatthew G. Knepley @*/
77821c531cf8SMatthew 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)
77834d6f44ffSToby Isaac {
77844d6f44ffSToby Isaac   PetscFunctionBegin;
77854d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7786064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
77877a8be351SBarry Smith   PetscCheck(dm->ops->projectfunctionlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
77889566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
77894d6f44ffSToby Isaac   PetscFunctionReturn(0);
77904d6f44ffSToby Isaac }
77912716604bSToby Isaac 
7792a6e0b375SMatthew G. Knepley /*@C
7793a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
7794a6e0b375SMatthew G. Knepley 
7795a6e0b375SMatthew G. Knepley   Not collective
7796a6e0b375SMatthew G. Knepley 
7797a6e0b375SMatthew G. Knepley   Input Parameters:
7798a6e0b375SMatthew G. Knepley + dm      - The DM
7799a6e0b375SMatthew G. Knepley . time    - The time
7800a6e0b375SMatthew G. Knepley . localU  - The input field vector
7801a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7802a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7803a6e0b375SMatthew G. Knepley 
7804a6e0b375SMatthew G. Knepley   Output Parameter:
7805a6e0b375SMatthew G. Knepley . localX  - The output vector
7806a6e0b375SMatthew G. Knepley 
7807a6e0b375SMatthew G. Knepley    Calling sequence of func:
7808a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7809a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7810a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7811a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7812a6e0b375SMatthew G. Knepley 
7813a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7814a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7815a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7816a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7817a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7818a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7819a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7820a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7821a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7822a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7823a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7824a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7825a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7826a6e0b375SMatthew G. Knepley .  t            - The current time
7827a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
7828a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
7829a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
7830a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
7831a6e0b375SMatthew G. Knepley 
7832a6e0b375SMatthew G. Knepley   Note: There are three different DMs that potentially interact in this function. The output DM, dm, specifies the layout of the values calculates by funcs.
7833a6e0b375SMatthew G. Knepley   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
7834a6e0b375SMatthew G. Knepley   a subdomain. You can also output a different number of fields than the input, with different discretizations. Last the auxiliary DM, attached to the
7835a6e0b375SMatthew 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.
7836a6e0b375SMatthew G. Knepley 
7837a6e0b375SMatthew G. Knepley   Level: intermediate
7838a6e0b375SMatthew G. Knepley 
7839db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
7840a6e0b375SMatthew G. Knepley @*/
78418c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
78428c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
78438c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
78448c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7845191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
78468c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
78478c6c5593SMatthew G. Knepley {
78488c6c5593SMatthew G. Knepley   PetscFunctionBegin;
78498c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
78508c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
78518c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
78527a8be351SBarry Smith   PetscCheck(dm->ops->projectfieldlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
78539566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX));
78548c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
78558c6c5593SMatthew G. Knepley }
78568c6c5593SMatthew G. Knepley 
7857a6e0b375SMatthew G. Knepley /*@C
7858a6e0b375SMatthew 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.
7859a6e0b375SMatthew G. Knepley 
7860a6e0b375SMatthew G. Knepley   Not collective
7861a6e0b375SMatthew G. Knepley 
7862a6e0b375SMatthew G. Knepley   Input Parameters:
7863a6e0b375SMatthew G. Knepley + dm      - The DM
7864a6e0b375SMatthew G. Knepley . time    - The time
7865a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
7866a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
7867a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
7868a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
7869a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
7870a6e0b375SMatthew G. Knepley . localU  - The input field vector
7871a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7872a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7873a6e0b375SMatthew G. Knepley 
7874a6e0b375SMatthew G. Knepley   Output Parameter:
7875a6e0b375SMatthew G. Knepley . localX  - The output vector
7876a6e0b375SMatthew G. Knepley 
7877a6e0b375SMatthew G. Knepley    Calling sequence of func:
7878a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7879a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7880a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7881a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7882a6e0b375SMatthew G. Knepley 
7883a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7884a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7885a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7886a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7887a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7888a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7889a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7890a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7891a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7892a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7893a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7894a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7895a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7896a6e0b375SMatthew G. Knepley .  t            - The current time
7897a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
7898a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
7899a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
7900a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
7901a6e0b375SMatthew G. Knepley 
7902a6e0b375SMatthew G. Knepley   Note: There are three different DMs that potentially interact in this function. The output DM, dm, specifies the layout of the values calculates by funcs.
7903a6e0b375SMatthew G. Knepley   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
7904a6e0b375SMatthew G. Knepley   a subdomain. You can also output a different number of fields than the input, with different discretizations. Last the auxiliary DM, attached to the
7905a6e0b375SMatthew 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.
7906a6e0b375SMatthew G. Knepley 
7907a6e0b375SMatthew G. Knepley   Level: intermediate
7908a6e0b375SMatthew G. Knepley 
7909db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
7910a6e0b375SMatthew G. Knepley @*/
79111c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
79128c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
79138c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
79148c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7915191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
79168c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
79178c6c5593SMatthew G. Knepley {
79188c6c5593SMatthew G. Knepley   PetscFunctionBegin;
79198c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7920064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
7921064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
79227a8be351SBarry Smith   PetscCheck(dm->ops->projectfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
79239566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
79248c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
79258c6c5593SMatthew G. Knepley }
79268c6c5593SMatthew G. Knepley 
79272716604bSToby Isaac /*@C
7928ece3a9fcSMatthew 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.
7929ece3a9fcSMatthew G. Knepley 
7930ece3a9fcSMatthew G. Knepley   Not collective
7931ece3a9fcSMatthew G. Knepley 
7932ece3a9fcSMatthew G. Knepley   Input Parameters:
7933ece3a9fcSMatthew G. Knepley + dm      - The DM
7934ece3a9fcSMatthew G. Knepley . time    - The time
7935ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
7936ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
7937ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
7938ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
7939ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
7940ece3a9fcSMatthew G. Knepley . localU  - The input field vector
7941ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7942ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
7943ece3a9fcSMatthew G. Knepley 
7944ece3a9fcSMatthew G. Knepley   Output Parameter:
7945ece3a9fcSMatthew G. Knepley . localX  - The output vector
7946ece3a9fcSMatthew G. Knepley 
7947ece3a9fcSMatthew G. Knepley    Calling sequence of func:
7948ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7949ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7950ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7951ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7952ece3a9fcSMatthew G. Knepley 
7953ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
7954ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
7955ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7956ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
7957ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7958ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
7959ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7960ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7961ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7962ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7963ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7964ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7965ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7966ece3a9fcSMatthew G. Knepley .  t            - The current time
7967ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
7968ece3a9fcSMatthew G. Knepley .  n            - The face normal
7969ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
7970ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
7971ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
7972ece3a9fcSMatthew G. Knepley 
7973ece3a9fcSMatthew G. Knepley   Note:
7974ece3a9fcSMatthew G. Knepley   There are three different DMs that potentially interact in this function. The output DM, dm, specifies the layout of the values calculates by funcs.
7975ece3a9fcSMatthew G. Knepley   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
7976ece3a9fcSMatthew G. Knepley   a subdomain. You can also output a different number of fields than the input, with different discretizations. Last the auxiliary DM, attached to the
7977ece3a9fcSMatthew 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.
7978ece3a9fcSMatthew G. Knepley 
7979ece3a9fcSMatthew G. Knepley   Level: intermediate
7980ece3a9fcSMatthew G. Knepley 
7981db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
7982ece3a9fcSMatthew G. Knepley @*/
7983ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
7984ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
7985ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7986ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7987ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
7988ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
7989ece3a9fcSMatthew G. Knepley {
7990ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
7991ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7992064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
7993064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
79947a8be351SBarry Smith   PetscCheck(dm->ops->projectbdfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
79959566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
7996ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
7997ece3a9fcSMatthew G. Knepley }
7998ece3a9fcSMatthew G. Knepley 
7999ece3a9fcSMatthew G. Knepley /*@C
80002716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
80012716604bSToby Isaac 
80022716604bSToby Isaac   Input Parameters:
80032716604bSToby Isaac + dm    - The DM
80040709b2feSToby Isaac . time  - The time
80052716604bSToby Isaac . funcs - The functions to evaluate for each field component
80062716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8007574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
80082716604bSToby Isaac 
80092716604bSToby Isaac   Output Parameter:
80102716604bSToby Isaac . diff - The diff ||u - u_h||_2
80112716604bSToby Isaac 
80122716604bSToby Isaac   Level: developer
80132716604bSToby Isaac 
8014db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
80152716604bSToby Isaac @*/
80160709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
80172716604bSToby Isaac {
80182716604bSToby Isaac   PetscFunctionBegin;
80192716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8020b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
80217a8be351SBarry Smith   PetscCheck(dm->ops->computel2diff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
80229566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff));
80232716604bSToby Isaac   PetscFunctionReturn(0);
80242716604bSToby Isaac }
8025b698f381SToby Isaac 
8026b698f381SToby Isaac /*@C
8027b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8028b698f381SToby Isaac 
8029d083f849SBarry Smith   Collective on dm
8030d083f849SBarry Smith 
8031b698f381SToby Isaac   Input Parameters:
8032b698f381SToby Isaac + dm    - The DM
8033b698f381SToby Isaac , time  - The time
8034b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8035b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8036574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8037b698f381SToby Isaac - n     - The vector to project along
8038b698f381SToby Isaac 
8039b698f381SToby Isaac   Output Parameter:
8040b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8041b698f381SToby Isaac 
8042b698f381SToby Isaac   Level: developer
8043b698f381SToby Isaac 
8044db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2Diff()`
8045b698f381SToby Isaac @*/
8046b698f381SToby 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)
8047b698f381SToby Isaac {
8048b698f381SToby Isaac   PetscFunctionBegin;
8049b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8050b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
80517a8be351SBarry Smith   PetscCheck(dm->ops->computel2gradientdiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
80529566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff));
8053b698f381SToby Isaac   PetscFunctionReturn(0);
8054b698f381SToby Isaac }
8055b698f381SToby Isaac 
80562a16baeaSToby Isaac /*@C
80572a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
80582a16baeaSToby Isaac 
8059d083f849SBarry Smith   Collective on dm
8060d083f849SBarry Smith 
80612a16baeaSToby Isaac   Input Parameters:
80622a16baeaSToby Isaac + dm    - The DM
80632a16baeaSToby Isaac . time  - The time
80642a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
80652a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8066574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
80672a16baeaSToby Isaac 
80682a16baeaSToby Isaac   Output Parameter:
80692a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
80702a16baeaSToby Isaac 
80712a16baeaSToby Isaac   Level: developer
80722a16baeaSToby Isaac 
8073db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
80742a16baeaSToby Isaac @*/
80751189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
80762a16baeaSToby Isaac {
80772a16baeaSToby Isaac   PetscFunctionBegin;
80782a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
80792a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
80807a8be351SBarry Smith   PetscCheck(dm->ops->computel2fielddiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
80819566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff));
80822a16baeaSToby Isaac   PetscFunctionReturn(0);
80832a16baeaSToby Isaac }
80842a16baeaSToby Isaac 
8085df0b854cSToby Isaac /*@C
8086502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8087502a2867SDave May 
8088502a2867SDave May  Not Collective
8089502a2867SDave May 
8090502a2867SDave May  Input Parameter:
8091502a2867SDave May .  dm    - The DM
8092502a2867SDave May 
80930a19bb7dSprj-  Output Parameters:
80940a19bb7dSprj- +  nranks - the number of neighbours
80950a19bb7dSprj- -  ranks - the neighbors ranks
8096502a2867SDave May 
8097502a2867SDave May  Notes:
8098502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8099502a2867SDave May 
8100502a2867SDave May  Level: beginner
8101502a2867SDave May 
8102db781477SPatrick Sanan  .seealso: `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8103502a2867SDave May @*/
8104502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8105502a2867SDave May {
8106502a2867SDave May   PetscFunctionBegin;
8107502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81087a8be351SBarry Smith   PetscCheck(dm->ops->getneighbors,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
81099566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm,nranks,ranks));
8110502a2867SDave May   PetscFunctionReturn(0);
8111502a2867SDave May }
8112502a2867SDave May 
8113531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8114531c7667SBarry Smith 
8115531c7667SBarry Smith /*
8116531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8117531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8118531c7667SBarry Smith */
8119531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8120531c7667SBarry Smith {
8121531c7667SBarry Smith   PetscFunctionBegin;
8122531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8123531c7667SBarry Smith     Vec x1local;
8124531c7667SBarry Smith     DM  dm;
81259566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J,&dm));
81267a8be351SBarry Smith     PetscCheck(dm,PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
81279566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm,&x1local));
81289566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local));
81299566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local));
8130531c7667SBarry Smith     x1   = x1local;
8131531c7667SBarry Smith   }
81329566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J,coloring,x1,sctx));
8133531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8134531c7667SBarry Smith     DM  dm;
81359566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J,&dm));
81369566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm,&x1));
8137531c7667SBarry Smith   }
8138531c7667SBarry Smith   PetscFunctionReturn(0);
8139531c7667SBarry Smith }
8140531c7667SBarry Smith 
8141531c7667SBarry Smith /*@
8142531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8143531c7667SBarry Smith 
8144531c7667SBarry Smith     Input Parameter:
8145531c7667SBarry Smith .    coloring - the MatFDColoring object
8146531c7667SBarry Smith 
814795452b02SPatrick Sanan     Developer Notes:
814895452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8149531c7667SBarry Smith 
81501b266c99SBarry Smith     Level: advanced
81511b266c99SBarry Smith 
8152db781477SPatrick Sanan .seealso: `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8153531c7667SBarry Smith @*/
8154531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8155531c7667SBarry Smith {
8156531c7667SBarry Smith   PetscFunctionBegin;
8157531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8158531c7667SBarry Smith   PetscFunctionReturn(0);
8159531c7667SBarry Smith }
81608320bc6fSPatrick Sanan 
81618320bc6fSPatrick Sanan /*@
81628320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
81638320bc6fSPatrick Sanan 
81648320bc6fSPatrick Sanan     Collective
81658320bc6fSPatrick Sanan 
81668320bc6fSPatrick Sanan     Input Parameters:
8167a5bc1bf3SBarry Smith +    dm1 - the first DM
81688320bc6fSPatrick Sanan -    dm2 - the second DM
81698320bc6fSPatrick Sanan 
81708320bc6fSPatrick Sanan     Output Parameters:
81718320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
81728320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
81738320bc6fSPatrick Sanan 
81748320bc6fSPatrick Sanan     Notes:
81758320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
81763d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
81778320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
81783d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
81793d862458SPatrick Sanan     decomposition, but hold different data.
81808320bc6fSPatrick Sanan 
81818320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
81828320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
81838320bc6fSPatrick Sanan 
81848320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
81858320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
81868320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
81878320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
81888320bc6fSPatrick Sanan 
81898320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
81908320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
81918320bc6fSPatrick Sanan .vb
81928320bc6fSPatrick Sanan   ...
81939566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
81948320bc6fSPatrick Sanan   if (set && compatible)  {
81959566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
81969566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
81979566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
81988320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
81998320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
82008320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
82018320bc6fSPatrick Sanan       }
82028320bc6fSPatrick Sanan     }
82039566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
82049566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
82058320bc6fSPatrick Sanan   } else {
82068320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
82078320bc6fSPatrick Sanan   }
82088320bc6fSPatrick Sanan   ...
82098320bc6fSPatrick Sanan .ve
82108320bc6fSPatrick Sanan 
82118320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
82128320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
82138320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
82148320bc6fSPatrick Sanan     always check the "set" output parameter.
82158320bc6fSPatrick Sanan 
82168320bc6fSPatrick Sanan     A DM is always compatible with itself.
82178320bc6fSPatrick Sanan 
82188320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
82198320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
82208320bc6fSPatrick Sanan     incompatible.
82218320bc6fSPatrick Sanan 
82228320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
82238320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
82248320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
82258320bc6fSPatrick Sanan 
82268320bc6fSPatrick Sanan     Developer Notes:
82273d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
82283d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8229a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
82308320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
82318320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
82323d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
82333d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
82348320bc6fSPatrick Sanan 
82358320bc6fSPatrick Sanan     Level: advanced
82368320bc6fSPatrick Sanan 
8237db781477SPatrick Sanan .seealso: `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
82388320bc6fSPatrick Sanan @*/
82398320bc6fSPatrick Sanan 
8240a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
82418320bc6fSPatrick Sanan {
82428320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
82438320bc6fSPatrick Sanan   DMType         type,type2;
82448320bc6fSPatrick Sanan   PetscBool      sameType;
82458320bc6fSPatrick Sanan 
82468320bc6fSPatrick Sanan   PetscFunctionBegin;
8247a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
82488320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
82498320bc6fSPatrick Sanan 
82508320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8251a5bc1bf3SBarry Smith   if (dm1 == dm2) {
82528320bc6fSPatrick Sanan     *set = PETSC_TRUE;
82538320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
82548320bc6fSPatrick Sanan     PetscFunctionReturn(0);
82558320bc6fSPatrick Sanan   }
82568320bc6fSPatrick Sanan 
82578320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
82588320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
82598320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
82608320bc6fSPatrick Sanan      determined by the implementation-specific logic */
82619566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult));
82628320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
82638320bc6fSPatrick Sanan     *set = PETSC_TRUE;
82648320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
82658320bc6fSPatrick Sanan     PetscFunctionReturn(0);
82668320bc6fSPatrick Sanan   }
82678320bc6fSPatrick Sanan 
82688320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8269a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
82709566063dSJacob Faibussowitsch     PetscCall((*dm1->ops->getcompatibility)(dm1,dm2,compatible,set));
8271b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
82728320bc6fSPatrick Sanan   }
82738320bc6fSPatrick Sanan 
8274a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
82758320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
82769566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1,&type));
82779566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2,&type2));
82789566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type,type2,&sameType));
82798320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
82809566063dSJacob Faibussowitsch     PetscCall((*dm2->ops->getcompatibility)(dm2,dm1,compatible,set)); /* Note argument order */
82818320bc6fSPatrick Sanan   } else {
82828320bc6fSPatrick Sanan     *set = PETSC_FALSE;
82838320bc6fSPatrick Sanan   }
82848320bc6fSPatrick Sanan   PetscFunctionReturn(0);
82858320bc6fSPatrick Sanan }
8286c0f0dcc3SMatthew G. Knepley 
8287c0f0dcc3SMatthew G. Knepley /*@C
8288c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8289c0f0dcc3SMatthew G. Knepley 
8290c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8291c0f0dcc3SMatthew G. Knepley 
8292c0f0dcc3SMatthew G. Knepley   Input Parameters:
8293c0f0dcc3SMatthew G. Knepley + DM - the DM
8294c0f0dcc3SMatthew G. Knepley . f - the monitor function
8295c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8296c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8297c0f0dcc3SMatthew G. Knepley 
8298c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8299c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
8300c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8301c0f0dcc3SMatthew G. Knepley 
8302c0f0dcc3SMatthew G. Knepley   Notes:
8303c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8304c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
8305c0f0dcc3SMatthew G. Knepley   order in which they were set.
8306c0f0dcc3SMatthew G. Knepley 
8307c0f0dcc3SMatthew G. Knepley   Fortran Notes:
8308c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
8309c0f0dcc3SMatthew G. Knepley 
8310c0f0dcc3SMatthew G. Knepley   Level: intermediate
8311c0f0dcc3SMatthew G. Knepley 
8312db781477SPatrick Sanan .seealso: `DMMonitorCancel()`
8313c0f0dcc3SMatthew G. Knepley @*/
8314c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
8315c0f0dcc3SMatthew G. Knepley {
8316c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8317c0f0dcc3SMatthew G. Knepley 
8318c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8319c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8320c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8321c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8322c0f0dcc3SMatthew G. Knepley 
83239566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
8324c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8325c0f0dcc3SMatthew G. Knepley   }
83267a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8327c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8328c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8329c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
8330c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8331c0f0dcc3SMatthew G. Knepley }
8332c0f0dcc3SMatthew G. Knepley 
8333c0f0dcc3SMatthew G. Knepley /*@
8334c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
8335c0f0dcc3SMatthew G. Knepley 
8336c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8337c0f0dcc3SMatthew G. Knepley 
8338c0f0dcc3SMatthew G. Knepley   Input Parameter:
8339c0f0dcc3SMatthew G. Knepley . dm - the DM
8340c0f0dcc3SMatthew G. Knepley 
8341c0f0dcc3SMatthew G. Knepley   Options Database Key:
8342c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8343c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
8344c0f0dcc3SMatthew G. Knepley   set via the options database
8345c0f0dcc3SMatthew G. Knepley 
8346c0f0dcc3SMatthew G. Knepley   Notes:
8347c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
8348c0f0dcc3SMatthew G. Knepley 
8349c0f0dcc3SMatthew G. Knepley   Level: intermediate
8350c0f0dcc3SMatthew G. Knepley 
8351db781477SPatrick Sanan .seealso: `DMMonitorSet()`
8352c0f0dcc3SMatthew G. Knepley @*/
8353c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
8354c0f0dcc3SMatthew G. Knepley {
8355c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8356c0f0dcc3SMatthew G. Knepley 
8357c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8358c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8359c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
83609566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8361c0f0dcc3SMatthew G. Knepley   }
8362c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8363c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8364c0f0dcc3SMatthew G. Knepley }
8365c0f0dcc3SMatthew G. Knepley 
8366c0f0dcc3SMatthew G. Knepley /*@C
8367c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8368c0f0dcc3SMatthew G. Knepley 
8369c0f0dcc3SMatthew G. Knepley   Collective on DM
8370c0f0dcc3SMatthew G. Knepley 
8371c0f0dcc3SMatthew G. Knepley   Input Parameters:
8372c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
8373c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8374c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8375c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8376c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8377c0f0dcc3SMatthew G. Knepley - 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
8378c0f0dcc3SMatthew G. Knepley 
8379c0f0dcc3SMatthew G. Knepley   Output Parameter:
8380c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8381c0f0dcc3SMatthew G. Knepley 
8382c0f0dcc3SMatthew G. Knepley   Level: developer
8383c0f0dcc3SMatthew G. Knepley 
8384db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8385db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
8386db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
8387db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8388c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8389db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8390db781477SPatrick Sanan           `PetscOptionsFList()`, `PetscOptionsEList()`
8391c0f0dcc3SMatthew G. Knepley @*/
8392c0f0dcc3SMatthew 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)
8393c0f0dcc3SMatthew G. Knepley {
8394c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8395c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8396c0f0dcc3SMatthew G. Knepley 
8397c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8398c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
83999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg));
8400c0f0dcc3SMatthew G. Knepley   if (*flg) {
8401c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8402c0f0dcc3SMatthew G. Knepley 
84039566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
84049566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject) viewer));
84059566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
84069566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy));
8407c0f0dcc3SMatthew G. Knepley   }
8408c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8409c0f0dcc3SMatthew G. Knepley }
8410c0f0dcc3SMatthew G. Knepley 
8411c0f0dcc3SMatthew G. Knepley /*@
8412c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8413c0f0dcc3SMatthew G. Knepley 
8414c0f0dcc3SMatthew G. Knepley    Collective on DM
8415c0f0dcc3SMatthew G. Knepley 
8416c0f0dcc3SMatthew G. Knepley    Input Parameters:
8417c0f0dcc3SMatthew G. Knepley .  dm - The DM
8418c0f0dcc3SMatthew G. Knepley 
8419c0f0dcc3SMatthew G. Knepley    Level: developer
8420c0f0dcc3SMatthew G. Knepley 
8421db781477SPatrick Sanan .seealso: `DMMonitorSet()`
8422c0f0dcc3SMatthew G. Knepley @*/
8423c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
8424c0f0dcc3SMatthew G. Knepley {
8425c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8426c0f0dcc3SMatthew G. Knepley 
8427c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8428c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8429c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8430c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
84319566063dSJacob Faibussowitsch     PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
8432c0f0dcc3SMatthew G. Knepley   }
8433c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8434c0f0dcc3SMatthew G. Knepley }
84352e4af2aeSMatthew G. Knepley 
84362e4af2aeSMatthew G. Knepley /*@
84372e4af2aeSMatthew G. Knepley   DMComputeError - Computes the error assuming the user has given exact solution functions
84382e4af2aeSMatthew G. Knepley 
84392e4af2aeSMatthew G. Knepley   Collective on DM
84402e4af2aeSMatthew G. Knepley 
84412e4af2aeSMatthew G. Knepley   Input Parameters:
84422e4af2aeSMatthew G. Knepley + dm     - The DM
84436b867d5aSJose E. Roman - sol    - The solution vector
84442e4af2aeSMatthew G. Knepley 
84456b867d5aSJose E. Roman   Input/Output Parameter:
84466b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
84476b867d5aSJose E. Roman            contains the error in each field
84486b867d5aSJose E. Roman 
84496b867d5aSJose E. Roman   Output Parameter:
84506b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
84512e4af2aeSMatthew G. Knepley 
84522e4af2aeSMatthew G. Knepley   Note: The exact solutions come from the PetscDS object, and the time comes from DMGetOutputSequenceNumber().
84532e4af2aeSMatthew G. Knepley 
84542e4af2aeSMatthew G. Knepley   Level: developer
84552e4af2aeSMatthew G. Knepley 
8456db781477SPatrick Sanan .seealso: `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
84572e4af2aeSMatthew G. Knepley @*/
84582e4af2aeSMatthew G. Knepley PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
84592e4af2aeSMatthew G. Knepley {
84602e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
84612e4af2aeSMatthew G. Knepley   void            **ctxs;
84622e4af2aeSMatthew G. Knepley   PetscReal         time;
84632e4af2aeSMatthew G. Knepley   PetscInt          Nf, f, Nds, s;
84642e4af2aeSMatthew G. Knepley 
84652e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
84669566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
84679566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
84689566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
84692e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
84702e4af2aeSMatthew G. Knepley     PetscDS         ds;
84712e4af2aeSMatthew G. Knepley     DMLabel         label;
84722e4af2aeSMatthew G. Knepley     IS              fieldIS;
84732e4af2aeSMatthew G. Knepley     const PetscInt *fields;
84742e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
84752e4af2aeSMatthew G. Knepley 
84769566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
84779566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
84789566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
84792e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
84802e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
84819566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
84822e4af2aeSMatthew G. Knepley     }
84839566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
84842e4af2aeSMatthew G. Knepley   }
84852e4af2aeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
848663a3b9bcSJacob 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);
84872e4af2aeSMatthew G. Knepley   }
84889566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
84899566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
84902e4af2aeSMatthew G. Knepley   if (errorVec) {
84912e4af2aeSMatthew G. Knepley     DM             edm;
84922e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
84932e4af2aeSMatthew G. Knepley     PetscBool      simplex;
84942e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
84952e4af2aeSMatthew G. Knepley 
84969566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
84979566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
84989566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
84999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
85002e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
85019566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
85022e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
85032e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
85042e4af2aeSMatthew G. Knepley       PetscQuadrature q;
85052e4af2aeSMatthew G. Knepley       const char     *name;
85062e4af2aeSMatthew G. Knepley 
85079566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *) &fe));
85089566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
85099566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject) fe, &name));
85109566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject) efe, name));
85119566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
85129566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
85139566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject) efe));
85149566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
85152e4af2aeSMatthew G. Knepley     }
85169566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
85172e4af2aeSMatthew G. Knepley 
85189566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
85199566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) *errorVec, "Error"));
85209566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
85219566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
85222e4af2aeSMatthew G. Knepley   }
85239566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
85242e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
85252e4af2aeSMatthew G. Knepley }
85269a2a23afSMatthew G. Knepley 
85279a2a23afSMatthew G. Knepley /*@
85289a2a23afSMatthew G. Knepley   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this DM
85299a2a23afSMatthew G. Knepley 
85309a2a23afSMatthew G. Knepley   Not collective
85319a2a23afSMatthew G. Knepley 
85329a2a23afSMatthew G. Knepley   Input Parameter:
85339a2a23afSMatthew G. Knepley . dm     - The DM
85349a2a23afSMatthew G. Knepley 
85359a2a23afSMatthew G. Knepley   Output Parameter:
8536a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
85379a2a23afSMatthew G. Knepley 
85389a2a23afSMatthew G. Knepley   Level: advanced
85399a2a23afSMatthew G. Knepley 
8540db781477SPatrick Sanan .seealso: `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
85419a2a23afSMatthew G. Knepley @*/
85429a2a23afSMatthew G. Knepley PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
85439a2a23afSMatthew G. Knepley {
85449a2a23afSMatthew G. Knepley   PetscFunctionBegin;
85459a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85469566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
85479a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
85489a2a23afSMatthew G. Knepley }
85499a2a23afSMatthew G. Knepley 
85509a2a23afSMatthew G. Knepley /*@
8551ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
85529a2a23afSMatthew G. Knepley 
85539a2a23afSMatthew G. Knepley   Not collective
85549a2a23afSMatthew G. Knepley 
85559a2a23afSMatthew G. Knepley   Input Parameters:
85569a2a23afSMatthew G. Knepley + dm     - The DM
85579a2a23afSMatthew G. Knepley . label  - The DMLabel
8558ac17215fSMatthew G. Knepley . value  - The label value indicating the region
8559ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
85609a2a23afSMatthew G. Knepley 
85619a2a23afSMatthew G. Knepley   Output Parameter:
85629a2a23afSMatthew G. Knepley . aux    - The Vec holding auxiliary field data
85639a2a23afSMatthew G. Knepley 
8564ac17215fSMatthew G. Knepley   Note: If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
856504c51a94SMatthew G. Knepley 
85669a2a23afSMatthew G. Knepley   Level: advanced
85679a2a23afSMatthew G. Knepley 
8568db781477SPatrick Sanan .seealso: `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`
85699a2a23afSMatthew G. Knepley @*/
8570ac17215fSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
85719a2a23afSMatthew G. Knepley {
8572ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
857304c51a94SMatthew G. Knepley   PetscBool       has;
85749a2a23afSMatthew G. Knepley 
85759a2a23afSMatthew G. Knepley   PetscFunctionBegin;
85769a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85779a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
85789a2a23afSMatthew G. Knepley   key.label = label;
85799a2a23afSMatthew G. Knepley   key.value = value;
8580ac17215fSMatthew G. Knepley   key.part  = part;
85819566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
85829566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key,  aux));
85839566063dSJacob Faibussowitsch   else     PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
85849a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
85859a2a23afSMatthew G. Knepley }
85869a2a23afSMatthew G. Knepley 
85879a2a23afSMatthew G. Knepley /*@
8588ac17215fSMatthew G. Knepley   DMSetAuxiliaryVec - Set the auxiliary vector for region specified by the given label and value, and equation part
85899a2a23afSMatthew G. Knepley 
85909a2a23afSMatthew G. Knepley   Not collective
85919a2a23afSMatthew G. Knepley 
85929a2a23afSMatthew G. Knepley   Input Parameters:
85939a2a23afSMatthew G. Knepley + dm     - The DM
85949a2a23afSMatthew G. Knepley . label  - The DMLabel
85959a2a23afSMatthew G. Knepley . value  - The label value indicating the region
8596ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
85979a2a23afSMatthew G. Knepley - aux    - The Vec holding auxiliary field data
85989a2a23afSMatthew G. Knepley 
85999a2a23afSMatthew G. Knepley   Level: advanced
86009a2a23afSMatthew G. Knepley 
8601db781477SPatrick Sanan .seealso: `DMGetAuxiliaryVec()`
86029a2a23afSMatthew G. Knepley @*/
8603ac17215fSMatthew G. Knepley PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
86049a2a23afSMatthew G. Knepley {
86059a2a23afSMatthew G. Knepley   Vec             old;
86069a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
86079a2a23afSMatthew G. Knepley 
86089a2a23afSMatthew G. Knepley   PetscFunctionBegin;
86099a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86109a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
86119a2a23afSMatthew G. Knepley   key.label = label;
86129a2a23afSMatthew G. Knepley   key.value = value;
8613ac17215fSMatthew G. Knepley   key.part  = part;
86149566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
86159566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) aux));
86169566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject) old));
86179566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
86189566063dSJacob Faibussowitsch   else      PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
86199a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
86209a2a23afSMatthew G. Knepley }
86219a2a23afSMatthew G. Knepley 
86229a2a23afSMatthew G. Knepley /*@C
8623ac17215fSMatthew G. Knepley   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this DM
86249a2a23afSMatthew G. Knepley 
86259a2a23afSMatthew G. Knepley   Not collective
86269a2a23afSMatthew G. Knepley 
86279a2a23afSMatthew G. Knepley   Input Parameter:
86289a2a23afSMatthew G. Knepley . dm      - The DM
86299a2a23afSMatthew G. Knepley 
86309a2a23afSMatthew G. Knepley   Output Parameters:
86319a2a23afSMatthew G. Knepley + labels  - The DMLabels for each Vec
8632ac17215fSMatthew G. Knepley . values  - The label values for each Vec
8633ac17215fSMatthew G. Knepley - parts   - The equation parts for each Vec
86349a2a23afSMatthew G. Knepley 
86359a2a23afSMatthew G. Knepley   Note: The arrays passed in must be at least as large as DMGetNumAuxiliaryVec().
86369a2a23afSMatthew G. Knepley 
86379a2a23afSMatthew G. Knepley   Level: advanced
86389a2a23afSMatthew G. Knepley 
8639db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
86409a2a23afSMatthew G. Knepley @*/
8641ac17215fSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
86429a2a23afSMatthew G. Knepley {
86439a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
86449a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
86459a2a23afSMatthew G. Knepley 
86469a2a23afSMatthew G. Knepley   PetscFunctionBegin;
86479a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86489a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
8649dadcf809SJacob Faibussowitsch   PetscValidIntPointer(values, 3);
8650dadcf809SJacob Faibussowitsch   PetscValidIntPointer(parts,  4);
86519566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
86529566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
86539566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
8654ac17215fSMatthew G. Knepley   for (i = 0; i < n; ++i) {labels[i] = keys[i].label; values[i] = keys[i].value; parts[i] = keys[i].part;}
86559566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
86569a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
86579a2a23afSMatthew G. Knepley }
86589a2a23afSMatthew G. Knepley 
86599a2a23afSMatthew G. Knepley /*@
86609a2a23afSMatthew G. Knepley   DMCopyAuxiliaryVec - Copy the auxiliary data to a new DM
86619a2a23afSMatthew G. Knepley 
86629a2a23afSMatthew G. Knepley   Not collective
86639a2a23afSMatthew G. Knepley 
86649a2a23afSMatthew G. Knepley   Input Parameter:
86659a2a23afSMatthew G. Knepley . dm    - The DM
86669a2a23afSMatthew G. Knepley 
86679a2a23afSMatthew G. Knepley   Output Parameter:
86689a2a23afSMatthew G. Knepley . dmNew - The new DM, now with the same auxiliary data
86699a2a23afSMatthew G. Knepley 
86709a2a23afSMatthew G. Knepley   Level: advanced
86719a2a23afSMatthew G. Knepley 
8672db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
86739a2a23afSMatthew G. Knepley @*/
86749a2a23afSMatthew G. Knepley PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
86759a2a23afSMatthew G. Knepley {
86769a2a23afSMatthew G. Knepley   PetscFunctionBegin;
86779a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86789566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
86799566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
86809a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
86819a2a23afSMatthew G. Knepley }
8682b5a892a1SMatthew G. Knepley 
8683b5a892a1SMatthew G. Knepley /*@C
8684b5a892a1SMatthew G. Knepley   DMPolytopeMatchOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
8685b5a892a1SMatthew G. Knepley 
8686b5a892a1SMatthew G. Knepley   Not collective
8687b5a892a1SMatthew G. Knepley 
8688b5a892a1SMatthew G. Knepley   Input Parameters:
8689b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
8690b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
8691b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
8692b5a892a1SMatthew G. Knepley 
8693b5a892a1SMatthew G. Knepley   Output Parameters:
8694b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
8695b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
8696b5a892a1SMatthew G. Knepley 
8697b5a892a1SMatthew G. Knepley   Level: advanced
8698b5a892a1SMatthew G. Knepley 
8699db781477SPatrick Sanan .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`
8700b5a892a1SMatthew G. Knepley @*/
8701b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
8702b5a892a1SMatthew G. Knepley {
8703b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
8704b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
8705b5a892a1SMatthew G. Knepley   PetscInt       o, c;
8706b5a892a1SMatthew G. Knepley 
8707b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
8708b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
8709b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
8710b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
8711b5a892a1SMatthew G. Knepley 
8712b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceCone[arr[c*2]] != targetCone[c]) break;
8713b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
8714b5a892a1SMatthew G. Knepley   }
8715b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
8716b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8717b5a892a1SMatthew G. Knepley }
8718b5a892a1SMatthew G. Knepley 
8719b5a892a1SMatthew G. Knepley /*@C
8720b5a892a1SMatthew G. Knepley   DMPolytopeGetOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
8721b5a892a1SMatthew G. Knepley 
8722b5a892a1SMatthew G. Knepley   Not collective
8723b5a892a1SMatthew G. Knepley 
8724b5a892a1SMatthew G. Knepley   Input Parameters:
8725b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
8726b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
8727b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
8728b5a892a1SMatthew G. Knepley 
8729b5a892a1SMatthew G. Knepley   Output Parameters:
8730b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
8731b5a892a1SMatthew G. Knepley 
8732b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
8733b5a892a1SMatthew G. Knepley 
8734b5a892a1SMatthew G. Knepley   Level: advanced
8735b5a892a1SMatthew G. Knepley 
8736db781477SPatrick Sanan .seealso: `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`
8737b5a892a1SMatthew G. Knepley @*/
8738b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
8739b5a892a1SMatthew G. Knepley {
8740b5a892a1SMatthew G. Knepley   PetscBool      found;
8741b5a892a1SMatthew G. Knepley 
8742b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
87439566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
87447a8be351SBarry Smith   PetscCheck(found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
8745b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8746b5a892a1SMatthew G. Knepley }
8747b5a892a1SMatthew G. Knepley 
8748b5a892a1SMatthew G. Knepley /*@C
8749b5a892a1SMatthew G. Knepley   DMPolytopeMatchVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
8750b5a892a1SMatthew G. Knepley 
8751b5a892a1SMatthew G. Knepley   Not collective
8752b5a892a1SMatthew G. Knepley 
8753b5a892a1SMatthew G. Knepley   Input Parameters:
8754b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
8755b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
8756b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
8757b5a892a1SMatthew G. Knepley 
8758b5a892a1SMatthew G. Knepley   Output Parameters:
8759b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
8760b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
8761b5a892a1SMatthew G. Knepley 
8762b5a892a1SMatthew G. Knepley   Level: advanced
8763b5a892a1SMatthew G. Knepley 
8764db781477SPatrick Sanan .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`
8765b5a892a1SMatthew G. Knepley @*/
8766b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
8767b5a892a1SMatthew G. Knepley {
8768b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
8769b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
8770b5a892a1SMatthew G. Knepley   PetscInt       o, c;
8771b5a892a1SMatthew G. Knepley 
8772b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
8773b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
8774b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
8775b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
8776b5a892a1SMatthew G. Knepley 
8777b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceVert[arr[c]] != targetVert[c]) break;
8778b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
8779b5a892a1SMatthew G. Knepley   }
8780b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
8781b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8782b5a892a1SMatthew G. Knepley }
8783b5a892a1SMatthew G. Knepley 
8784b5a892a1SMatthew G. Knepley /*@C
8785b5a892a1SMatthew G. Knepley   DMPolytopeGetVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
8786b5a892a1SMatthew G. Knepley 
8787b5a892a1SMatthew G. Knepley   Not collective
8788b5a892a1SMatthew G. Knepley 
8789b5a892a1SMatthew G. Knepley   Input Parameters:
8790b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
8791b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
8792b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
8793b5a892a1SMatthew G. Knepley 
8794b5a892a1SMatthew G. Knepley   Output Parameters:
8795b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
8796b5a892a1SMatthew G. Knepley 
8797b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
8798b5a892a1SMatthew G. Knepley 
8799b5a892a1SMatthew G. Knepley   Level: advanced
8800b5a892a1SMatthew G. Knepley 
8801db781477SPatrick Sanan .seealso: `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
8802b5a892a1SMatthew G. Knepley @*/
8803b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
8804b5a892a1SMatthew G. Knepley {
8805b5a892a1SMatthew G. Knepley   PetscBool      found;
8806b5a892a1SMatthew G. Knepley 
8807b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
88089566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
88097a8be351SBarry Smith   PetscCheck(found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
8810b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8811b5a892a1SMatthew G. Knepley }
8812012bc364SMatthew G. Knepley 
8813012bc364SMatthew G. Knepley /*@C
8814012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
8815012bc364SMatthew G. Knepley 
8816012bc364SMatthew G. Knepley   Not collective
8817012bc364SMatthew G. Knepley 
8818012bc364SMatthew G. Knepley   Input Parameters:
8819012bc364SMatthew G. Knepley + ct    - The DMPolytopeType
8820012bc364SMatthew G. Knepley - point - Coordinates of the point
8821012bc364SMatthew G. Knepley 
8822012bc364SMatthew G. Knepley   Output Parameters:
8823012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
8824012bc364SMatthew G. Knepley 
8825012bc364SMatthew G. Knepley   Level: advanced
8826012bc364SMatthew G. Knepley 
8827db781477SPatrick Sanan .seealso: `DMLocatePoints()`
8828012bc364SMatthew G. Knepley @*/
8829012bc364SMatthew G. Knepley PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
8830012bc364SMatthew G. Knepley {
8831012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
8832012bc364SMatthew G. Knepley   PetscInt  d;
8833012bc364SMatthew G. Knepley 
8834012bc364SMatthew G. Knepley   PetscFunctionBegin;
8835012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
8836012bc364SMatthew G. Knepley   switch (ct) {
8837012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
8838012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
8839012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
8840012bc364SMatthew G. Knepley       if (point[d] < -1.0) {*inside = PETSC_FALSE; break;}
8841012bc364SMatthew G. Knepley       sum += point[d];
8842012bc364SMatthew G. Knepley     }
8843012bc364SMatthew G. Knepley     if (sum > PETSC_SMALL) {*inside = PETSC_FALSE; break;}
8844012bc364SMatthew G. Knepley     break;
8845012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
8846012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
8847012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
8848012bc364SMatthew G. Knepley       if (PetscAbsReal(point[d]) > 1.+PETSC_SMALL) {*inside = PETSC_FALSE; break;}
8849012bc364SMatthew G. Knepley     break;
8850012bc364SMatthew G. Knepley   default:
885198921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
8852012bc364SMatthew G. Knepley   }
8853012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
8854012bc364SMatthew G. Knepley }
8855