xref: /petsc/src/dm/interface/dm.c (revision 6858538e710279fe46cd8279ab34c98b10293bbd)
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;
75*6858538eSMatthew G. Knepley   v->coordinates[0].dim       = PETSC_DEFAULT;
76*6858538eSMatthew G. Knepley   v->coordinates[1].dim       = PETSC_DEFAULT;
77*6858538eSMatthew 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;
129*6858538eSMatthew 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));
151*6858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
152*6858538eSMatthew 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 
157*6858538eSMatthew 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) {
161*6858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
162*6858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1639566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
164*6858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
165*6858538eSMatthew G. Knepley         else   PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1669566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
167be4c1c3eSMatthew G. Knepley       }
168be4c1c3eSMatthew G. Knepley     }
169*6858538eSMatthew 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   }
179*6858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
180*6858538eSMatthew G. Knepley   if (coords) {
181*6858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
182*6858538eSMatthew G. Knepley   } else {
183*6858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
184*6858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
185*6858538eSMatthew G. Knepley   }
18690b157c4SStefano Zampini   {
187c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
188*6858538eSMatthew G. Knepley 
189*6858538eSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm,    &maxCell, &L));
190*6858538eSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell,  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 
581*6858538eSMatthew G. Knepley PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
582*6858538eSMatthew G. Knepley {
583*6858538eSMatthew G. Knepley   PetscFunctionBegin;
584*6858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
585*6858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
586*6858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
587*6858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
588*6858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
589*6858538eSMatthew G. Knepley   PetscFunctionReturn(0);
590*6858538eSMatthew G. Knepley }
591*6858538eSMatthew 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));
7559566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7569566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
757*6858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
758*6858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7599566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7609566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7619566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7626636e97aSMatthew G Knepley 
7639566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7649566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
765e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7669566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
767732e2eb9SMatthew G Knepley 
768ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7699566063dSJacob Faibussowitsch     PetscCall((*(*dm)->ops->destroy)(*dm));
770ed3c66a1SDave May   }
7719566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
772f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7739566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7749566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
775f918ec44SMatthew G. Knepley #endif
776435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7779566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
77847c6ae99SBarry Smith   PetscFunctionReturn(0);
77947c6ae99SBarry Smith }
78047c6ae99SBarry Smith 
781d7bf68aeSBarry Smith /*@
782d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
783d7bf68aeSBarry Smith 
784d083f849SBarry Smith     Collective on dm
785d7bf68aeSBarry Smith 
786d7bf68aeSBarry Smith     Input Parameter:
787d7bf68aeSBarry Smith .   dm - the DM object to setup
788d7bf68aeSBarry Smith 
789d7bf68aeSBarry Smith     Level: developer
790d7bf68aeSBarry Smith 
791db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
792d7bf68aeSBarry Smith 
793d7bf68aeSBarry Smith @*/
7947087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
795d7bf68aeSBarry Smith {
796d7bf68aeSBarry Smith   PetscFunctionBegin;
797171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7988387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
7991baa6e33SBarry Smith   if (dm->ops->setup) PetscCall((*dm->ops->setup)(dm));
8008387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
801d7bf68aeSBarry Smith   PetscFunctionReturn(0);
802d7bf68aeSBarry Smith }
803d7bf68aeSBarry Smith 
804d7bf68aeSBarry Smith /*@
805d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
806d7bf68aeSBarry Smith 
807d083f849SBarry Smith     Collective on dm
808d7bf68aeSBarry Smith 
809d7bf68aeSBarry Smith     Input Parameter:
810d7bf68aeSBarry Smith .   dm - the DM object to set options for
811d7bf68aeSBarry Smith 
812732e2eb9SMatthew G Knepley     Options Database:
8135b8ffe73SMark Adams +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix() and DMCreateMassMatrix(), but do not fill it with zeros
8144164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8154164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
816a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
817a4ea9b21SRichard 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
818732e2eb9SMatthew G Knepley 
8199318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8209318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8219318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
822cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
8239318fe57SMatthew G. Knepley . -dm_plex_shape <shape>            - The domain shape, such as DM_SHAPE_BOX, DM_SHAPE_SPHERE, etc.
8249318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8259318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8269318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
8279318fe57SMatthew G. Knepley . -dm_plex_simplex <bool>           - PETSC_TRUE for simplex elements, PETSC_FALSE for tensor elements
8289318fe57SMatthew G. Knepley . -dm_plex_interpolate <bool>       - PETSC_TRUE turns on topological interpolation (creating edges and faces)
8299318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8309318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8319318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8329318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
8339318fe57SMatthew G. Knepley . -dm_plex_box_bd <bx,by,bz>        - Specify the DMBoundaryType for each direction
8349318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8359318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8369318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8379318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
838bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8399318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8409318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8419318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8429318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
843bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
844d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
845d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
846d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
847d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
848d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
849909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
850909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8519318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8529318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8539318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8549318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8559318fe57SMatthew G. Knepley 
856384a6580SVaclav Hapla     DMPLEX Specific Checks
857384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
858384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
859384a6580SVaclav 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()
860384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
861384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
862384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
863384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
864d7bf68aeSBarry Smith 
86595eb5ee5SVaclav Hapla     Level: intermediate
86695eb5ee5SVaclav Hapla 
867db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
868db781477SPatrick Sanan          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`
869d7bf68aeSBarry Smith 
870d7bf68aeSBarry Smith @*/
8717087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
872d7bf68aeSBarry Smith {
8737781c08eSBarry Smith   char           typeName[256];
874ca266f36SBarry Smith   PetscBool      flg;
875d7bf68aeSBarry Smith 
876d7bf68aeSBarry Smith   PetscFunctionBegin;
877171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87849be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
8799566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
8809566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
881d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
8829566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,NULL));
8839566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg));
8841baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm,typeName));
8859566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg));
8861baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm,typeName));
8879566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL));
8889566063dSJacob 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));
8891baa6e33SBarry Smith   if (dm->ops->setfromoptions) PetscCall((*dm->ops->setfromoptions)(PetscOptionsObject,dm));
890f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8919566063dSJacob Faibussowitsch   PetscCall(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm));
892d0609cedSBarry Smith   PetscOptionsEnd();
893d7bf68aeSBarry Smith   PetscFunctionReturn(0);
894d7bf68aeSBarry Smith }
895d7bf68aeSBarry Smith 
896fc9bc008SSatish Balay /*@C
897fe2efc57SMark    DMViewFromOptions - View from Options
898fe2efc57SMark 
899fe2efc57SMark    Collective on DM
900fe2efc57SMark 
901fe2efc57SMark    Input Parameters:
902fe2efc57SMark +  dm - the DM object
903736c3998SJose E. Roman .  obj - Optional object
904736c3998SJose E. Roman -  name - command line option
905fe2efc57SMark 
906fe2efc57SMark    Level: intermediate
907db781477SPatrick Sanan .seealso: `DM`, `DMView`, `PetscObjectViewFromOptions()`, `DMCreate()`
908fe2efc57SMark @*/
909fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
910fe2efc57SMark {
911fe2efc57SMark   PetscFunctionBegin;
912fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9139566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm,obj,name));
914fe2efc57SMark   PetscFunctionReturn(0);
915fe2efc57SMark }
916fe2efc57SMark 
917fe2efc57SMark /*@C
918224748a4SBarry Smith     DMView - Views a DM
91947c6ae99SBarry Smith 
920d083f849SBarry Smith     Collective on dm
92147c6ae99SBarry Smith 
922d8d19677SJose E. Roman     Input Parameters:
92347c6ae99SBarry Smith +   dm - the DM object to view
92447c6ae99SBarry Smith -   v - the viewer
92547c6ae99SBarry Smith 
926cd7e8a5eSksagiyam     Notes:
927cd7e8a5eSksagiyam     Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
928cd7e8a5eSksagiyam     meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
929cd7e8a5eSksagiyam     before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
930cd7e8a5eSksagiyam 
931224748a4SBarry Smith     Level: beginner
93247c6ae99SBarry Smith 
933db781477SPatrick Sanan .seealso `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
93447c6ae99SBarry Smith 
93547c6ae99SBarry Smith @*/
9367087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
93747c6ae99SBarry Smith {
93832c0f0efSBarry Smith   PetscBool         isbinary;
93976a8abe0SBarry Smith   PetscMPIInt       size;
94076a8abe0SBarry Smith   PetscViewerFormat format;
94147c6ae99SBarry Smith 
94247c6ae99SBarry Smith   PetscFunctionBegin;
943171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9443014e516SBarry Smith   if (!v) {
9459566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v));
9463014e516SBarry Smith   }
947b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
94874903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
94974903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
95074903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
95174903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
95274903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
95374903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
95474903a4fSStefano Zampini      in an error here */
95574903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9569566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
957b1b135c8SBarry Smith 
9589566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v,&format));
9599566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size));
96076a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
9619566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm,v));
9629566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary));
96332c0f0efSBarry Smith   if (isbinary) {
96455849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
96532c0f0efSBarry Smith     char     type[256];
96632c0f0efSBarry Smith 
9679566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v,&classid,1,PETSC_INT));
9689566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(type,((PetscObject)dm)->type_name,256));
9699566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v,type,256,PETSC_CHAR));
97032c0f0efSBarry Smith   }
9711baa6e33SBarry Smith   if (dm->ops->view) PetscCall((*dm->ops->view)(dm,v));
97247c6ae99SBarry Smith   PetscFunctionReturn(0);
97347c6ae99SBarry Smith }
97447c6ae99SBarry Smith 
97547c6ae99SBarry Smith /*@
9768472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
97747c6ae99SBarry Smith 
978d083f849SBarry Smith     Collective on dm
97947c6ae99SBarry Smith 
98047c6ae99SBarry Smith     Input Parameter:
98147c6ae99SBarry Smith .   dm - the DM object
98247c6ae99SBarry Smith 
98347c6ae99SBarry Smith     Output Parameter:
98447c6ae99SBarry Smith .   vec - the global vector
98547c6ae99SBarry Smith 
986073dac72SJed Brown     Level: beginner
98747c6ae99SBarry Smith 
988db781477SPatrick Sanan .seealso `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
98947c6ae99SBarry Smith 
99047c6ae99SBarry Smith @*/
9917087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
99247c6ae99SBarry Smith {
99347c6ae99SBarry Smith   PetscFunctionBegin;
994171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
995b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
9967a8be351SBarry Smith   PetscCheck(dm->ops->createglobalvector,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
9979566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createglobalvector)(dm,vec));
99876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
999c6b011d8SStefano Zampini     DM vdm;
1000c6b011d8SStefano Zampini 
10019566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec,&vdm));
10027a8be351SBarry Smith     PetscCheck(vdm,PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the vector",((PetscObject)dm)->type_name);
1003c6b011d8SStefano Zampini   }
100447c6ae99SBarry Smith   PetscFunctionReturn(0);
100547c6ae99SBarry Smith }
100647c6ae99SBarry Smith 
100747c6ae99SBarry Smith /*@
10088472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
100947c6ae99SBarry Smith 
101047c6ae99SBarry Smith     Not Collective
101147c6ae99SBarry Smith 
101247c6ae99SBarry Smith     Input Parameter:
101347c6ae99SBarry Smith .   dm - the DM object
101447c6ae99SBarry Smith 
101547c6ae99SBarry Smith     Output Parameter:
101647c6ae99SBarry Smith .   vec - the local vector
101747c6ae99SBarry Smith 
1018073dac72SJed Brown     Level: beginner
101947c6ae99SBarry Smith 
1020db781477SPatrick Sanan .seealso `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
102147c6ae99SBarry Smith 
102247c6ae99SBarry Smith @*/
10237087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
102447c6ae99SBarry Smith {
102547c6ae99SBarry Smith   PetscFunctionBegin;
1026171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1027b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
10287a8be351SBarry Smith   PetscCheck(dm->ops->createlocalvector,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
10299566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createlocalvector)(dm,vec));
103076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1031c6b011d8SStefano Zampini     DM vdm;
1032c6b011d8SStefano Zampini 
10339566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec,&vdm));
10347a8be351SBarry Smith     PetscCheck(vdm,PETSC_COMM_SELF,PETSC_ERR_LIB,"DM type '%s' did not attach the DM to the vector",((PetscObject)dm)->type_name);
1035c6b011d8SStefano Zampini   }
103647c6ae99SBarry Smith   PetscFunctionReturn(0);
103747c6ae99SBarry Smith }
103847c6ae99SBarry Smith 
10391411c6eeSJed Brown /*@
10401411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10411411c6eeSJed Brown 
1042d083f849SBarry Smith    Collective on dm
10431411c6eeSJed Brown 
10441411c6eeSJed Brown    Input Parameter:
10451411c6eeSJed Brown .  dm - the DM that provides the mapping
10461411c6eeSJed Brown 
10471411c6eeSJed Brown    Output Parameter:
10481411c6eeSJed Brown .  ltog - the mapping
10491411c6eeSJed Brown 
10501411c6eeSJed Brown    Level: intermediate
10511411c6eeSJed Brown 
10521411c6eeSJed Brown    Notes:
10531411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10541411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10551411c6eeSJed Brown 
1056db781477SPatrick Sanan .seealso: `DMCreateLocalVector()`
10571411c6eeSJed Brown @*/
10587087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10591411c6eeSJed Brown {
10600be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10611411c6eeSJed Brown 
10621411c6eeSJed Brown   PetscFunctionBegin;
10631411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10641411c6eeSJed Brown   PetscValidPointer(ltog,2);
10651411c6eeSJed Brown   if (!dm->ltogmap) {
106637d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
106737d0c07bSMatthew G Knepley 
10689566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
106937d0c07bSMatthew G Knepley     if (section) {
1070a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
107137d0c07bSMatthew G Knepley       PetscInt       *ltog;
1072ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
107337d0c07bSMatthew G Knepley 
10749566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
10759566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
10769566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
10779566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
107837d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1079e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
108037d0c07bSMatthew G Knepley 
108137d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
10829566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
10839566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
10849566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
10859566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
10861a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10871a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10881a7dc684SMatthew G. Knepley         if (dof) {
10895b8243e1SJed Brown           bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
10901a7dc684SMatthew G. Knepley         }
10915227eafbSStefano Zampini 
1092e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
10935227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1094e6befd46SJed Brown             ltog[l] = off < 0 ? off-c : -(off+c+1);
1095e6befd46SJed Brown             cind++;
1096e6befd46SJed Brown           } else {
10975227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off+1) : off) + c - cind;
1098e6befd46SJed Brown           }
109937d0c07bSMatthew G Knepley         }
110037d0c07bSMatthew G Knepley       }
1101bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11020be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11039566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax));
11040be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11050be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
11067591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11077591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1108ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1109ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1110ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1111ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l]/bs : -(-(ltog[l]+1)/bs + 1);
1112ca469d19SJed Brown         }
1113ccf3bd66SMatthew G. Knepley         n /= bs;
1114ccf3bd66SMatthew G. Knepley       }
11159566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
11169566063dSJacob Faibussowitsch       PetscCall(PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap));
111737d0c07bSMatthew G Knepley     } else {
11187a8be351SBarry Smith       PetscCheck(dm->ops->getlocaltoglobalmapping,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
11199566063dSJacob Faibussowitsch       PetscCall((*dm->ops->getlocaltoglobalmapping)(dm));
11201411c6eeSJed Brown     }
112137d0c07bSMatthew G Knepley   }
11221411c6eeSJed Brown   *ltog = dm->ltogmap;
11231411c6eeSJed Brown   PetscFunctionReturn(0);
11241411c6eeSJed Brown }
11251411c6eeSJed Brown 
11261411c6eeSJed Brown /*@
11271411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
11281411c6eeSJed Brown 
11291411c6eeSJed Brown    Not Collective
11301411c6eeSJed Brown 
11311411c6eeSJed Brown    Input Parameter:
11321411c6eeSJed Brown .  dm - the DM with block structure
11331411c6eeSJed Brown 
11341411c6eeSJed Brown    Output Parameter:
11351411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11361411c6eeSJed Brown 
11371411c6eeSJed Brown    Level: intermediate
11381411c6eeSJed Brown 
1139db781477SPatrick Sanan .seealso: `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11401411c6eeSJed Brown @*/
11417087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
11421411c6eeSJed Brown {
11431411c6eeSJed Brown   PetscFunctionBegin;
11441411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1145534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11467a8be351SBarry Smith   PetscCheck(dm->bs >= 1,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
11471411c6eeSJed Brown   *bs = dm->bs;
11481411c6eeSJed Brown   PetscFunctionReturn(0);
11491411c6eeSJed Brown }
11501411c6eeSJed Brown 
115148eeb7c8SBarry Smith /*@C
11528472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
115347c6ae99SBarry Smith 
1154a5bc1bf3SBarry Smith     Collective on dmc
115547c6ae99SBarry Smith 
1156d8d19677SJose E. Roman     Input Parameters:
1157a5bc1bf3SBarry Smith +   dmc - the DM object
1158a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
115947c6ae99SBarry Smith 
1160d8d19677SJose E. Roman     Output Parameters:
116147c6ae99SBarry Smith +  mat - the interpolation
116247c6ae99SBarry Smith -  vec - the scaling (optional)
116347c6ae99SBarry Smith 
116447c6ae99SBarry Smith     Level: developer
116547c6ae99SBarry Smith 
116695452b02SPatrick Sanan     Notes:
116795452b02SPatrick 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
116885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1169d52bd9f3SBarry Smith 
11701f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1171d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
117285afcc9aSBarry Smith 
1173db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
117447c6ae99SBarry Smith 
117547c6ae99SBarry Smith @*/
1176a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
117747c6ae99SBarry Smith {
117847c6ae99SBarry Smith   PetscFunctionBegin;
1179a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1180a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1181c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
11827a8be351SBarry Smith   PetscCheck(dmc->ops->createinterpolation,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
11839566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0));
11849566063dSJacob Faibussowitsch   PetscCall((*dmc->ops->createinterpolation)(dmc,dmf,mat,vec));
11859566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0));
118647c6ae99SBarry Smith   PetscFunctionReturn(0);
118747c6ae99SBarry Smith }
118847c6ae99SBarry Smith 
11893ad4599aSBarry Smith /*@
1190d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
11912ed6491fSPatrick Sanan 
11922ed6491fSPatrick Sanan   Input Parameters:
11932ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
11942ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
11952ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
11962ed6491fSPatrick Sanan 
11972ed6491fSPatrick Sanan   Output Parameter:
11982ed6491fSPatrick Sanan .    scale - the scaled vector
11992ed6491fSPatrick Sanan 
12002ed6491fSPatrick Sanan   Level: developer
12012ed6491fSPatrick Sanan 
1202e9c74fd6SRichard Tran Mills   Developer Notes:
1203e9c74fd6SRichard Tran Mills   If the fine-scale DMDA has the -dm_bind_below option set to true, then DMCreateInterpolationScale() calls MatSetBindingPropagates()
1204e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1205e9c74fd6SRichard Tran Mills 
1206db781477SPatrick Sanan .seealso: `DMCreateInterpolation()`
12072ed6491fSPatrick Sanan 
12082ed6491fSPatrick Sanan @*/
12092ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
12102ed6491fSPatrick Sanan {
12112ed6491fSPatrick Sanan   Vec            fine;
12122ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
12139704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1214e9c74fd6SRichard Tran Mills   PetscBool      bindingpropagates,isbound;
12159704db99SRichard Tran Mills #endif
12162ed6491fSPatrick Sanan 
12172ed6491fSPatrick Sanan   PetscFunctionBegin;
12189566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf,&fine));
12199566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac,scale));
12209566063dSJacob Faibussowitsch   PetscCall(VecSet(fine,one));
12219704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12229704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12239704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12249704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12259566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine,&bindingpropagates));
1226e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12279566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat,PETSC_TRUE));
12289566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine,&isbound));
12299566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat,isbound));
123083aa49f4SRichard Tran Mills   }
12319704db99SRichard Tran Mills #endif
12329566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat,fine,*scale));
12339566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12349566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12352ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12362ed6491fSPatrick Sanan }
12372ed6491fSPatrick Sanan 
12382ed6491fSPatrick Sanan /*@
12393ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
12403ad4599aSBarry Smith 
1241a5bc1bf3SBarry Smith     Collective on dmc
12423ad4599aSBarry Smith 
1243d8d19677SJose E. Roman     Input Parameters:
1244a5bc1bf3SBarry Smith +   dmc - the DM object
1245a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
12463ad4599aSBarry Smith 
12473ad4599aSBarry Smith     Output Parameter:
12483ad4599aSBarry Smith .  mat - the restriction
12493ad4599aSBarry Smith 
12503ad4599aSBarry Smith     Level: developer
12513ad4599aSBarry Smith 
125295452b02SPatrick Sanan     Notes:
125395452b02SPatrick 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
12543ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
12553ad4599aSBarry Smith 
1256db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12573ad4599aSBarry Smith 
12583ad4599aSBarry Smith @*/
1259a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12603ad4599aSBarry Smith {
12613ad4599aSBarry Smith   PetscFunctionBegin;
1262a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1263a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12645a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
12657a8be351SBarry Smith   PetscCheck(dmc->ops->createrestriction,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
12669566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0));
12679566063dSJacob Faibussowitsch   PetscCall((*dmc->ops->createrestriction)(dmc,dmf,mat));
12689566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0));
12693ad4599aSBarry Smith   PetscFunctionReturn(0);
12703ad4599aSBarry Smith }
12713ad4599aSBarry Smith 
127247c6ae99SBarry Smith /*@
12738472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
127447c6ae99SBarry Smith 
1275a5bc1bf3SBarry Smith     Collective on dac
127647c6ae99SBarry Smith 
1277d8d19677SJose E. Roman     Input Parameters:
1278a5bc1bf3SBarry Smith +   dac - the DM object
1279a5bc1bf3SBarry Smith -   daf - the second, finer DM object
128047c6ae99SBarry Smith 
128147c6ae99SBarry Smith     Output Parameter:
12826dbf9973SLawrence Mitchell .   mat - the injection
128347c6ae99SBarry Smith 
128447c6ae99SBarry Smith     Level: developer
128547c6ae99SBarry Smith 
128695452b02SPatrick Sanan    Notes:
128795452b02SPatrick 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
128885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
128985afcc9aSBarry Smith 
1290db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`
129147c6ae99SBarry Smith 
129247c6ae99SBarry Smith @*/
1293a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
129447c6ae99SBarry Smith {
129547c6ae99SBarry Smith   PetscFunctionBegin;
1296a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1297a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
12985a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
12997a8be351SBarry Smith   PetscCheck(dac->ops->createinjection,PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
13009566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0));
13019566063dSJacob Faibussowitsch   PetscCall((*dac->ops->createinjection)(dac,daf,mat));
13029566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0));
130347c6ae99SBarry Smith   PetscFunctionReturn(0);
130447c6ae99SBarry Smith }
130547c6ae99SBarry Smith 
1306b412c318SBarry Smith /*@
1307bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1308bd041c0cSMatthew G. Knepley 
1309a5bc1bf3SBarry Smith   Collective on dac
1310bd041c0cSMatthew G. Knepley 
1311d8d19677SJose E. Roman   Input Parameters:
1312b4937a87SMatthew G. Knepley + dmc - the target DM object
1313b4937a87SMatthew G. Knepley - dmf - the source DM object
1314bd041c0cSMatthew G. Knepley 
1315bd041c0cSMatthew G. Knepley   Output Parameter:
1316b4937a87SMatthew G. Knepley . mat - the mass matrix
1317bd041c0cSMatthew G. Knepley 
1318bd041c0cSMatthew G. Knepley   Level: developer
1319bd041c0cSMatthew G. Knepley 
1320db781477SPatrick Sanan .seealso `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1321bd041c0cSMatthew G. Knepley @*/
1322b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1323bd041c0cSMatthew G. Knepley {
1324bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1325b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1326b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13275a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
13282c71b3e2SJacob Faibussowitsch   PetscCheck(dmc->ops->createmassmatrix,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dmc)->type_name);
13295b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix,0,0,0,0));
13309566063dSJacob Faibussowitsch   PetscCall((*dmc->ops->createmassmatrix)(dmc, dmf, mat));
13315b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix,0,0,0,0));
1332b4937a87SMatthew G. Knepley   PetscFunctionReturn(0);
1333b4937a87SMatthew G. Knepley }
1334b4937a87SMatthew G. Knepley 
1335b4937a87SMatthew G. Knepley /*@
1336b4937a87SMatthew G. Knepley   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given DM
1337b4937a87SMatthew G. Knepley 
1338b4937a87SMatthew G. Knepley   Collective on dm
1339b4937a87SMatthew G. Knepley 
1340b4937a87SMatthew G. Knepley   Input Parameter:
1341b4937a87SMatthew G. Knepley . dm - the DM object
1342b4937a87SMatthew G. Knepley 
1343b4937a87SMatthew G. Knepley   Output Parameter:
1344b4937a87SMatthew G. Knepley . lm - the lumped mass matrix
1345b4937a87SMatthew G. Knepley 
1346b4937a87SMatthew G. Knepley   Level: developer
1347b4937a87SMatthew G. Knepley 
1348db781477SPatrick Sanan .seealso `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1349b4937a87SMatthew G. Knepley @*/
1350b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1351b4937a87SMatthew G. Knepley {
1352b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1353b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1354b4937a87SMatthew G. Knepley   PetscValidPointer(lm,2);
13552c71b3e2SJacob Faibussowitsch   PetscCheck(dm->ops->createmassmatrixlumped,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrixLumped",((PetscObject)dm)->type_name);
13569566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createmassmatrixlumped)(dm, lm));
1357bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1358bd041c0cSMatthew G. Knepley }
1359bd041c0cSMatthew G. Knepley 
1360bd041c0cSMatthew G. Knepley /*@
1361b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
136247c6ae99SBarry Smith 
1363d083f849SBarry Smith     Collective on dm
136447c6ae99SBarry Smith 
1365d8d19677SJose E. Roman     Input Parameters:
136647c6ae99SBarry Smith +   dm - the DM object
13675bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
136847c6ae99SBarry Smith 
136947c6ae99SBarry Smith     Output Parameter:
137047c6ae99SBarry Smith .   coloring - the coloring
137147c6ae99SBarry Smith 
1372ec5066bdSBarry Smith     Notes:
1373ec5066bdSBarry 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
1374ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1375ec5066bdSBarry Smith 
1376ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1377ec5066bdSBarry Smith 
137847c6ae99SBarry Smith     Level: developer
137947c6ae99SBarry Smith 
1380db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
138147c6ae99SBarry Smith 
1382aab9d709SJed Brown @*/
1383b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
138447c6ae99SBarry Smith {
138547c6ae99SBarry Smith   PetscFunctionBegin;
1386171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13875a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
13887a8be351SBarry Smith   PetscCheck(dm->ops->getcoloring,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
13899566063dSJacob Faibussowitsch   PetscCall((*dm->ops->getcoloring)(dm,ctype,coloring));
139047c6ae99SBarry Smith   PetscFunctionReturn(0);
139147c6ae99SBarry Smith }
139247c6ae99SBarry Smith 
1393b412c318SBarry Smith /*@
13948472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
139547c6ae99SBarry Smith 
1396d083f849SBarry Smith     Collective on dm
139747c6ae99SBarry Smith 
139847c6ae99SBarry Smith     Input Parameter:
1399b412c318SBarry Smith .   dm - the DM object
140047c6ae99SBarry Smith 
140147c6ae99SBarry Smith     Output Parameter:
140247c6ae99SBarry Smith .   mat - the empty Jacobian
140347c6ae99SBarry Smith 
1404073dac72SJed Brown     Level: beginner
140547c6ae99SBarry Smith 
1406f27dd7c6SMatthew G. Knepley     Options Database Keys:
14075b8ffe73SMark Adams . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix() and DMCreateMassMatrix(), but do not fill it with zeros
1408f27dd7c6SMatthew G. Knepley 
140995452b02SPatrick Sanan     Notes:
141095452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
141194013140SBarry Smith        do not need to do it yourself.
141294013140SBarry Smith 
141394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
14147889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
141594013140SBarry Smith 
141694013140SBarry 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
141794013140SBarry Smith        internally by PETSc.
141894013140SBarry Smith 
141994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1420aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
142194013140SBarry Smith 
1422db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
142347c6ae99SBarry Smith 
1424aab9d709SJed Brown @*/
1425b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
142647c6ae99SBarry Smith {
142747c6ae99SBarry Smith   PetscFunctionBegin;
1428171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1429064a246eSJacob Faibussowitsch   PetscValidPointer(mat,2);
14307a8be351SBarry Smith   PetscCheck(dm->ops->creatematrix,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
14319566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14329566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix,0,0,0,0));
14339566063dSJacob Faibussowitsch   PetscCall((*dm->ops->creatematrix)(dm,mat));
143476bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1435c6b011d8SStefano Zampini     DM mdm;
1436c6b011d8SStefano Zampini 
14379566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat,&mdm));
14387a8be351SBarry Smith     PetscCheck(mdm,PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the matrix",((PetscObject)dm)->type_name);
1439c6b011d8SStefano Zampini   }
1440e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1441e5e52638SMatthew G. Knepley   if (dm->Nf) {
1442e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1443649ef022SMatthew Knepley     PetscInt     Nf, f;
1444e571a35bSMatthew G. Knepley 
14459566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1446649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1447649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14489566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14499566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14509566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1451649ef022SMatthew Knepley         break;
1452e571a35bSMatthew G. Knepley       }
1453649ef022SMatthew Knepley     }
1454649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1455649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
14569566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
14579566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
14589566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1459e571a35bSMatthew G. Knepley       }
1460e571a35bSMatthew G. Knepley     }
1461e571a35bSMatthew G. Knepley   }
14629566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix,0,0,0,0));
146347c6ae99SBarry Smith   PetscFunctionReturn(0);
146447c6ae99SBarry Smith }
146547c6ae99SBarry Smith 
1466732e2eb9SMatthew G Knepley /*@
1467aa0f6e3cSJed Brown   DMSetMatrixPreallocateSkip - When DMCreateMatrix() is called the matrix sizes and ISLocalToGlobalMapping will be
1468aa0f6e3cSJed Brown   properly set, but the entries will not be preallocated. This is most useful to reduce initialization costs when
1469aa0f6e3cSJed Brown   MatSetPreallocationCOO() and MatSetValuesCOO() will be used.
1470aa0f6e3cSJed Brown 
1471aa0f6e3cSJed Brown   Logically Collective on dm
1472aa0f6e3cSJed Brown 
1473aa0f6e3cSJed Brown   Input Parameters:
1474aa0f6e3cSJed Brown + dm - the DM
1475aa0f6e3cSJed Brown - skip - PETSC_TRUE to skip preallocation
1476aa0f6e3cSJed Brown 
1477aa0f6e3cSJed Brown   Level: developer
1478aa0f6e3cSJed Brown 
1479db781477SPatrick Sanan .seealso `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`
1480aa0f6e3cSJed Brown @*/
1481aa0f6e3cSJed Brown PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1482aa0f6e3cSJed Brown {
1483aa0f6e3cSJed Brown   PetscFunctionBegin;
1484aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1485aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
1486aa0f6e3cSJed Brown   PetscFunctionReturn(0);
1487aa0f6e3cSJed Brown }
1488aa0f6e3cSJed Brown 
1489aa0f6e3cSJed Brown /*@
1490950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1491732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1492732e2eb9SMatthew G Knepley 
1493d083f849SBarry Smith   Logically Collective on dm
1494732e2eb9SMatthew G Knepley 
1495d8d19677SJose E. Roman   Input Parameters:
1496732e2eb9SMatthew G Knepley + dm - the DM
1497732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1498732e2eb9SMatthew G Knepley 
1499732e2eb9SMatthew G Knepley   Level: developer
1500f27dd7c6SMatthew G. Knepley 
1501f27dd7c6SMatthew G. Knepley   Options Database Keys:
15025b8ffe73SMark Adams . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), DMCreateMassMatrix(), but do not fill it with zeros
1503f27dd7c6SMatthew G. Knepley 
1504db781477SPatrick Sanan .seealso `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`
1505732e2eb9SMatthew G Knepley @*/
1506732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1507732e2eb9SMatthew G Knepley {
1508732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1509732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1510732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1511732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1512732e2eb9SMatthew G Knepley }
1513732e2eb9SMatthew G Knepley 
1514b06ff27eSHong Zhang /*@
1515b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1516b06ff27eSHong Zhang     but the array for values will not be allocated.
1517b06ff27eSHong Zhang 
1518d083f849SBarry Smith   Logically Collective on dm
1519b06ff27eSHong Zhang 
1520d8d19677SJose E. Roman   Input Parameters:
1521b06ff27eSHong Zhang + dm - the DM
1522b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1523b06ff27eSHong Zhang 
1524b06ff27eSHong Zhang   Level: developer
1525db781477SPatrick Sanan .seealso `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`
1526b06ff27eSHong Zhang @*/
1527b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1528b06ff27eSHong Zhang {
1529b06ff27eSHong Zhang   PetscFunctionBegin;
1530b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1531b06ff27eSHong Zhang   dm->structure_only = only;
1532b06ff27eSHong Zhang   PetscFunctionReturn(0);
1533b06ff27eSHong Zhang }
1534b06ff27eSHong Zhang 
1535a89ea682SMatthew G Knepley /*@C
1536aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1537a89ea682SMatthew G Knepley 
1538a89ea682SMatthew G Knepley   Not Collective
1539a89ea682SMatthew G Knepley 
1540a89ea682SMatthew G Knepley   Input Parameters:
1541a89ea682SMatthew G Knepley + dm - the DM object
1542a5b23f4aSJose E. Roman . count - The minimum size
154369291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1544a89ea682SMatthew G Knepley 
1545a89ea682SMatthew G Knepley   Output Parameter:
1546a89ea682SMatthew G Knepley . array - the work array
1547a89ea682SMatthew G Knepley 
1548a89ea682SMatthew G Knepley   Level: developer
1549a89ea682SMatthew G Knepley 
1550db781477SPatrick Sanan .seealso `DMDestroy()`, `DMCreate()`
1551a89ea682SMatthew G Knepley @*/
155269291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1553a89ea682SMatthew G Knepley {
1554aa1993deSMatthew G Knepley   DMWorkLink     link;
155569291d52SBarry Smith   PetscMPIInt    dsize;
1556a89ea682SMatthew G Knepley 
1557a89ea682SMatthew G Knepley   PetscFunctionBegin;
1558a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1559aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1560aa1993deSMatthew G Knepley   if (dm->workin) {
1561aa1993deSMatthew G Knepley     link       = dm->workin;
1562aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1563aa1993deSMatthew G Knepley   } else {
15649566063dSJacob Faibussowitsch     PetscCall(PetscNewLog(dm,&link));
1565a89ea682SMatthew G Knepley   }
15669566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dtype,&dsize));
15675056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
15689566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
15699566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize*count,&link->mem));
1570854ce69bSBarry Smith     link->bytes = dsize*count;
1571aa1993deSMatthew G Knepley   }
1572aa1993deSMatthew G Knepley   link->next   = dm->workout;
1573aa1993deSMatthew G Knepley   dm->workout  = link;
1574cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
157500d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
157600d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
157700d952a4SJed Brown #endif
1578aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1579a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1580a89ea682SMatthew G Knepley }
1581a89ea682SMatthew G Knepley 
1582aa1993deSMatthew G Knepley /*@C
1583aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1584aa1993deSMatthew G Knepley 
1585aa1993deSMatthew G Knepley   Not Collective
1586aa1993deSMatthew G Knepley 
1587aa1993deSMatthew G Knepley   Input Parameters:
1588aa1993deSMatthew G Knepley + dm - the DM object
1589a5b23f4aSJose E. Roman . count - The minimum size
159069291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1591aa1993deSMatthew G Knepley 
1592aa1993deSMatthew G Knepley   Output Parameter:
1593aa1993deSMatthew G Knepley . array - the work array
1594aa1993deSMatthew G Knepley 
1595aa1993deSMatthew G Knepley   Level: developer
1596aa1993deSMatthew G Knepley 
159795452b02SPatrick Sanan   Developer Notes:
159895452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1599147403d9SBarry Smith 
1600db781477SPatrick Sanan .seealso `DMDestroy()`, `DMCreate()`
1601aa1993deSMatthew G Knepley @*/
160269291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1603aa1993deSMatthew G Knepley {
1604aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1605aa1993deSMatthew G Knepley 
1606aa1993deSMatthew G Knepley   PetscFunctionBegin;
1607aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1608aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1609aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1610aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1611aa1993deSMatthew G Knepley       *p           = link->next;
1612aa1993deSMatthew G Knepley       link->next   = dm->workin;
1613aa1993deSMatthew G Knepley       dm->workin   = link;
16140298fd71SBarry Smith       *(void**)mem = NULL;
1615aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1616aa1993deSMatthew G Knepley     }
1617aa1993deSMatthew G Knepley   }
1618aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1619aa1993deSMatthew G Knepley }
1620e7c4fc90SDmitry Karpeev 
16218cda7954SMatthew G. Knepley /*@C
1622147403d9SBarry 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()
16238cda7954SMatthew G. Knepley 
16248cda7954SMatthew G. Knepley   Logically collective on DM
16258cda7954SMatthew G. Knepley 
16268cda7954SMatthew G. Knepley   Input Parameters:
16278cda7954SMatthew G. Knepley + dm     - The DM
16288cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16298cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
16308cda7954SMatthew G. Knepley 
1631147403d9SBarry Smith   Calling sequence of nullsp:
1632147403d9SBarry Smith .vb
1633147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1634147403d9SBarry Smith .ve
1635147403d9SBarry Smith +  dm        - The present DM
1636147403d9SBarry Smith .  origField - The field number given above, in the original DM
1637147403d9SBarry Smith .  field     - The field number in dm
1638147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16398cda7954SMatthew G. Knepley 
16408cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16418cda7954SMatthew G. Knepley 
164249762cbcSSatish Balay    Level: intermediate
164349762cbcSSatish Balay 
1644db781477SPatrick Sanan .seealso: `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1645147403d9SBarry Smith @*/
1646147403d9SBarry Smith PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace*))
1647435a35e8SMatthew G Knepley {
1648435a35e8SMatthew G Knepley   PetscFunctionBegin;
1649435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16507a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1651435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1652435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1653435a35e8SMatthew G Knepley }
1654435a35e8SMatthew G Knepley 
16558cda7954SMatthew G. Knepley /*@C
16568cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
16578cda7954SMatthew G. Knepley 
16588cda7954SMatthew G. Knepley   Not collective
16598cda7954SMatthew G. Knepley 
16608cda7954SMatthew G. Knepley   Input Parameters:
16618cda7954SMatthew G. Knepley + dm     - The DM
16628cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16638cda7954SMatthew G. Knepley 
16648cda7954SMatthew G. Knepley   Output Parameter:
16658cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
16668cda7954SMatthew G. Knepley 
1667147403d9SBarry Smith   Calling sequence of nullsp:
1668147403d9SBarry Smith .vb
1669147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1670147403d9SBarry Smith .ve
1671147403d9SBarry Smith +  dm        - The present DM
1672147403d9SBarry Smith .  origField - The field number given above, in the original DM
1673147403d9SBarry Smith .  field     - The field number in dm
1674147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16758cda7954SMatthew G. Knepley 
16768cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16778cda7954SMatthew G. Knepley 
167849762cbcSSatish Balay    Level: intermediate
167949762cbcSSatish Balay 
1680db781477SPatrick Sanan .seealso: `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1681147403d9SBarry Smith @*/
1682147403d9SBarry Smith PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
16830a50eb56SMatthew G. Knepley {
16840a50eb56SMatthew G. Knepley   PetscFunctionBegin;
16850a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1686f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
16877a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
16880a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
16890a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
16900a50eb56SMatthew G. Knepley }
16910a50eb56SMatthew G. Knepley 
16928cda7954SMatthew G. Knepley /*@C
16938cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
16948cda7954SMatthew G. Knepley 
16958cda7954SMatthew G. Knepley   Logically collective on DM
16968cda7954SMatthew G. Knepley 
16978cda7954SMatthew G. Knepley   Input Parameters:
16988cda7954SMatthew G. Knepley + dm     - The DM
16998cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17008cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
17018cda7954SMatthew G. Knepley 
1702147403d9SBarry Smith   Calling sequence of nullsp:
1703147403d9SBarry Smith .vb
1704147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1705147403d9SBarry Smith .ve
1706147403d9SBarry Smith +  dm        - The present DM
1707147403d9SBarry Smith .  origField - The field number given above, in the original DM
1708147403d9SBarry Smith .  field     - The field number in dm
1709147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17108cda7954SMatthew G. Knepley 
17118cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
17128cda7954SMatthew G. Knepley 
171349762cbcSSatish Balay    Level: intermediate
171449762cbcSSatish Balay 
1715db781477SPatrick Sanan .seealso: `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1716147403d9SBarry Smith @*/
1717147403d9SBarry Smith PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1718f9d4088aSMatthew G. Knepley {
1719f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1720f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17217a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1722f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1723f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1724f9d4088aSMatthew G. Knepley }
1725f9d4088aSMatthew G. Knepley 
17268cda7954SMatthew G. Knepley /*@C
17278cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
17288cda7954SMatthew G. Knepley 
17298cda7954SMatthew G. Knepley   Not collective
17308cda7954SMatthew G. Knepley 
17318cda7954SMatthew G. Knepley   Input Parameters:
17328cda7954SMatthew G. Knepley + dm     - The DM
17338cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17348cda7954SMatthew G. Knepley 
17358cda7954SMatthew G. Knepley   Output Parameter:
17368cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
17378cda7954SMatthew G. Knepley 
1738147403d9SBarry Smith   Calling sequence of nullsp:
1739147403d9SBarry Smith .vb
1740147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1741147403d9SBarry Smith .ve
1742147403d9SBarry Smith +  dm        - The present DM
1743147403d9SBarry Smith .  origField - The field number given above, in the original DM
1744147403d9SBarry Smith .  field     - The field number in dm
1745147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17468cda7954SMatthew G. Knepley 
17478cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
17488cda7954SMatthew G. Knepley 
174949762cbcSSatish Balay    Level: intermediate
175049762cbcSSatish Balay 
1751db781477SPatrick Sanan .seealso: `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1752147403d9SBarry Smith @*/
1753147403d9SBarry Smith PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1754f9d4088aSMatthew G. Knepley {
1755f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1756f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1757f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
17587a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1759f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1760f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1761f9d4088aSMatthew G. Knepley }
1762f9d4088aSMatthew G. Knepley 
17634f3b5142SJed Brown /*@C
17644d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
17654d343eeaSMatthew G Knepley 
17664d343eeaSMatthew G Knepley   Not collective
17674d343eeaSMatthew G Knepley 
17684d343eeaSMatthew G Knepley   Input Parameter:
17694d343eeaSMatthew G Knepley . dm - the DM object
17704d343eeaSMatthew G Knepley 
17714d343eeaSMatthew G Knepley   Output Parameters:
17720298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
17730298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
17740298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
17754d343eeaSMatthew G Knepley 
17764d343eeaSMatthew G Knepley   Level: intermediate
17774d343eeaSMatthew G Knepley 
177821c9b008SJed Brown   Notes:
177921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
178021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
178121c9b008SJed Brown   PetscFree().
178221c9b008SJed Brown 
1783db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
17844d343eeaSMatthew G Knepley @*/
178537d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
17864d343eeaSMatthew G Knepley {
178737d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
17884d343eeaSMatthew G Knepley 
17894d343eeaSMatthew G Knepley   PetscFunctionBegin;
17904d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
179169ca1f37SDmitry Karpeev   if (numFields) {
1792534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
179369ca1f37SDmitry Karpeev     *numFields = 0;
179469ca1f37SDmitry Karpeev   }
179537d0c07bSMatthew G Knepley   if (fieldNames) {
179637d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
17970298fd71SBarry Smith     *fieldNames = NULL;
179869ca1f37SDmitry Karpeev   }
179969ca1f37SDmitry Karpeev   if (fields) {
180069ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
18010298fd71SBarry Smith     *fields = NULL;
180269ca1f37SDmitry Karpeev   }
18039566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
180437d0c07bSMatthew G Knepley   if (section) {
18053a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
180637d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
180737d0c07bSMatthew G Knepley 
18089566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
18099566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
18109566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices));
18119566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
181237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
181337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
18149566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
181537d0c07bSMatthew G Knepley     }
181637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
181737d0c07bSMatthew G Knepley       PetscInt gdof;
181837d0c07bSMatthew G Knepley 
18199566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
182037d0c07bSMatthew G Knepley       if (gdof > 0) {
182137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
18223a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
182337d0c07bSMatthew G Knepley 
18249566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
18259566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
18263a544194SStefano Zampini           fpdof = fdof-fcdof;
18273a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
18283a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
18293a544194SStefano Zampini             fieldNc[f] = 1;
18303a544194SStefano Zampini           }
18313a544194SStefano Zampini           fieldSizes[f] += fpdof;
183237d0c07bSMatthew G Knepley         }
183337d0c07bSMatthew G Knepley       }
183437d0c07bSMatthew G Knepley     }
183537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
18369566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
183737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
183837d0c07bSMatthew G Knepley     }
183937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
184037d0c07bSMatthew G Knepley       PetscInt gdof, goff;
184137d0c07bSMatthew G Knepley 
18429566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
184337d0c07bSMatthew G Knepley       if (gdof > 0) {
18449566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
184537d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
184637d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
184737d0c07bSMatthew G Knepley 
18489566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
18499566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
185037d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
185137d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
185237d0c07bSMatthew G Knepley           }
185337d0c07bSMatthew G Knepley         }
185437d0c07bSMatthew G Knepley       }
185537d0c07bSMatthew G Knepley     }
18568865f1eaSKarl Rupp     if (numFields) *numFields = nF;
185737d0c07bSMatthew G Knepley     if (fieldNames) {
18589566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
185937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
186037d0c07bSMatthew G Knepley         const char *fieldName;
186137d0c07bSMatthew G Knepley 
18629566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
18639566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]));
186437d0c07bSMatthew G Knepley       }
186537d0c07bSMatthew G Knepley     }
186637d0c07bSMatthew G Knepley     if (fields) {
18679566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
186837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
18693a544194SStefano Zampini         PetscInt bs, in[2], out[2];
18703a544194SStefano Zampini 
18719566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
18723a544194SStefano Zampini         in[0] = -fieldNc[f];
18733a544194SStefano Zampini         in[1] = fieldNc[f];
18741c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
18753a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
18769566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
187737d0c07bSMatthew G Knepley       }
187837d0c07bSMatthew G Knepley     }
18799566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes,fieldNc,fieldIndices));
18801baa6e33SBarry Smith   } else if (dm->ops->createfieldis) PetscCall((*dm->ops->createfieldis)(dm, numFields, fieldNames, fields));
18814d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
18824d343eeaSMatthew G Knepley }
18834d343eeaSMatthew G Knepley 
188416621825SDmitry Karpeev /*@C
188516621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
188616621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
188716621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1888e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1889e7c4fc90SDmitry Karpeev 
1890e7c4fc90SDmitry Karpeev   Not collective
1891e7c4fc90SDmitry Karpeev 
1892e7c4fc90SDmitry Karpeev   Input Parameter:
1893e7c4fc90SDmitry Karpeev . dm - the DM object
1894e7c4fc90SDmitry Karpeev 
1895e7c4fc90SDmitry Karpeev   Output Parameters:
18960298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
18970298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
18980298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
18990298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1900e7c4fc90SDmitry Karpeev 
1901e7c4fc90SDmitry Karpeev   Level: intermediate
1902e7c4fc90SDmitry Karpeev 
1903e7c4fc90SDmitry Karpeev   Notes:
1904e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1905e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1906e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1907e7c4fc90SDmitry Karpeev 
1908db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
1909e7c4fc90SDmitry Karpeev @*/
191016621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1911e7c4fc90SDmitry Karpeev {
1912e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1913e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19148865f1eaSKarl Rupp   if (len) {
1915534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
19168865f1eaSKarl Rupp     *len = 0;
19178865f1eaSKarl Rupp   }
19188865f1eaSKarl Rupp   if (namelist) {
19198865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1920ea78f98cSLisandro Dalcin     *namelist = NULL;
19218865f1eaSKarl Rupp   }
19228865f1eaSKarl Rupp   if (islist) {
19238865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1924ea78f98cSLisandro Dalcin     *islist = NULL;
19258865f1eaSKarl Rupp   }
19268865f1eaSKarl Rupp   if (dmlist) {
19278865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1928ea78f98cSLisandro Dalcin     *dmlist = NULL;
19298865f1eaSKarl Rupp   }
1930f3f0edfdSDmitry Karpeev   /*
1931f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1932f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1933f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1934f3f0edfdSDmitry Karpeev    */
19357a8be351SBarry Smith   PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
193616621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1937435a35e8SMatthew G Knepley     PetscSection section;
1938435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1939435a35e8SMatthew G Knepley 
19409566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
19419566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
1942435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1943f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
19449566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields,namelist));
19459566063dSJacob Faibussowitsch       if (islist)   PetscCall(PetscMalloc1(numFields,islist));
19469566063dSJacob Faibussowitsch       if (dmlist)   PetscCall(PetscMalloc1(numFields,dmlist));
1947435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1948435a35e8SMatthew G Knepley         const char *fieldName;
1949435a35e8SMatthew G Knepley 
19509566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
195103dc3394SMatthew G. Knepley         if (namelist) {
19529566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19539566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char**) &(*namelist)[f]));
1954435a35e8SMatthew G Knepley         }
195503dc3394SMatthew G. Knepley       }
1956435a35e8SMatthew G Knepley     } else {
19579566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
1958e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
19590298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1960e7c4fc90SDmitry Karpeev     }
19618865f1eaSKarl Rupp   } else {
19629566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist));
196316621825SDmitry Karpeev   }
196416621825SDmitry Karpeev   PetscFunctionReturn(0);
196516621825SDmitry Karpeev }
196616621825SDmitry Karpeev 
1967564cec59SMatthew G. Knepley /*@
1968435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1969435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1970435a35e8SMatthew G Knepley 
1971435a35e8SMatthew G Knepley   Not collective
1972435a35e8SMatthew G Knepley 
1973435a35e8SMatthew G Knepley   Input Parameters:
19742adcc780SMatthew G. Knepley + dm        - The DM object
19752adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
19762adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1977435a35e8SMatthew G Knepley 
1978435a35e8SMatthew G Knepley   Output Parameters:
19792adcc780SMatthew G. Knepley + is - The global indices for the subproblem
19802adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1981435a35e8SMatthew G Knepley 
19825d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19835d3b26e6SMatthew G. Knepley 
1984435a35e8SMatthew G Knepley   Level: intermediate
1985435a35e8SMatthew G Knepley 
1986db781477SPatrick Sanan .seealso `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
1987435a35e8SMatthew G Knepley @*/
198837bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1989435a35e8SMatthew G Knepley {
1990435a35e8SMatthew G Knepley   PetscFunctionBegin;
1991435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1992dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields,3);
19938865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
19948865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
19957a8be351SBarry Smith   PetscCheck(dm->ops->createsubdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
19969566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createsubdm)(dm, numFields, fields, is, subdm));
1997435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1998435a35e8SMatthew G Knepley }
1999435a35e8SMatthew G Knepley 
20002adcc780SMatthew G. Knepley /*@C
20012adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
20022adcc780SMatthew G. Knepley 
20032adcc780SMatthew G. Knepley   Not collective
20042adcc780SMatthew G. Knepley 
2005d8d19677SJose E. Roman   Input Parameters:
20062adcc780SMatthew G. Knepley + dms - The DM objects
20072adcc780SMatthew G. Knepley - len - The number of DMs
20082adcc780SMatthew G. Knepley 
20092adcc780SMatthew G. Knepley   Output Parameters:
2010a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
20112adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
20122adcc780SMatthew G. Knepley 
20135d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
20145d3b26e6SMatthew G. Knepley 
20152adcc780SMatthew G. Knepley   Level: intermediate
20162adcc780SMatthew G. Knepley 
2017db781477SPatrick Sanan .seealso `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
20182adcc780SMatthew G. Knepley @*/
20192adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
20202adcc780SMatthew G. Knepley {
20212adcc780SMatthew G. Knepley   PetscInt       i;
20222adcc780SMatthew G. Knepley 
20232adcc780SMatthew G. Knepley   PetscFunctionBegin;
20242adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
20252adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
20262adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
2027a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
202863a3b9bcSJacob Faibussowitsch   PetscCheck(len >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, len);
20292adcc780SMatthew G. Knepley   if (len) {
2030b9d85ea2SLisandro Dalcin     DM dm = dms[0];
20317a8be351SBarry Smith     PetscCheck(dm->ops->createsuperdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
20329566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createsuperdm)(dms, len, is, superdm));
20332adcc780SMatthew G. Knepley   }
20342adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
20352adcc780SMatthew G. Knepley }
20362adcc780SMatthew G. Knepley 
203716621825SDmitry Karpeev /*@C
20388d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
20398d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
20408d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
20418d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
20428d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
204316621825SDmitry Karpeev 
204416621825SDmitry Karpeev   Not collective
204516621825SDmitry Karpeev 
204616621825SDmitry Karpeev   Input Parameter:
204716621825SDmitry Karpeev . dm - the DM object
204816621825SDmitry Karpeev 
204916621825SDmitry Karpeev   Output Parameters:
20500298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
20510298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
20520298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
20530298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
20540298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
205516621825SDmitry Karpeev 
205616621825SDmitry Karpeev   Level: intermediate
205716621825SDmitry Karpeev 
205816621825SDmitry Karpeev   Notes:
205916621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
206016621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
206116621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
206216621825SDmitry Karpeev 
2063db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldDecomposition()`
206416621825SDmitry Karpeev @*/
20658d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
206616621825SDmitry Karpeev {
2067be081cd6SPeter Brune   DMSubDomainHookLink link;
2068be081cd6SPeter Brune   PetscInt            i,l;
206916621825SDmitry Karpeev 
207016621825SDmitry Karpeev   PetscFunctionBegin;
207116621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2072dadcf809SJacob Faibussowitsch   if (len)           {PetscValidIntPointer(len,2);            *len         = 0;}
20730298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
20740298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
20750298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
20760298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
2077f3f0edfdSDmitry Karpeev   /*
2078f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2079f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2080f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2081f3f0edfdSDmitry Karpeev    */
20827a8be351SBarry Smith   PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
208316621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
20849566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist));
208514a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2086f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2087be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2088be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
20899566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm,(*dmlist)[i],link->ctx));
2090be081cd6SPeter Brune         }
2091648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2092e7c4fc90SDmitry Karpeev       }
209314a18fd3SPeter Brune     }
209414a18fd3SPeter Brune     if (len) *len = l;
209514a18fd3SPeter Brune   }
2096e30e807fSPeter Brune   PetscFunctionReturn(0);
2097e30e807fSPeter Brune }
2098e30e807fSPeter Brune 
2099e30e807fSPeter Brune /*@C
2100e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2101e30e807fSPeter Brune 
2102e30e807fSPeter Brune   Not collective
2103e30e807fSPeter Brune 
2104e30e807fSPeter Brune   Input Parameters:
2105e30e807fSPeter Brune + dm - the DM object
2106e30e807fSPeter Brune . n  - the number of subdomain scatters
2107e30e807fSPeter Brune - subdms - the local subdomains
2108e30e807fSPeter Brune 
2109e30e807fSPeter Brune   Output Parameters:
21106b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2111e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2112e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2113e30e807fSPeter Brune 
211495452b02SPatrick Sanan   Notes:
211595452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2116e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2117e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2118e30e807fSPeter Brune   solution and residual data.
2119e30e807fSPeter Brune 
2120e30e807fSPeter Brune   Level: developer
2121e30e807fSPeter Brune 
2122db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2123e30e807fSPeter Brune @*/
2124e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2125e30e807fSPeter Brune {
2126e30e807fSPeter Brune   PetscFunctionBegin;
2127e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2128e30e807fSPeter Brune   PetscValidPointer(subdms,3);
21297a8be351SBarry Smith   PetscCheck(dm->ops->createddscatters,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
21309566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat));
2131e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2132e7c4fc90SDmitry Karpeev }
2133e7c4fc90SDmitry Karpeev 
213447c6ae99SBarry Smith /*@
213547c6ae99SBarry Smith   DMRefine - Refines a DM object
213647c6ae99SBarry Smith 
2137d083f849SBarry Smith   Collective on dm
213847c6ae99SBarry Smith 
2139d8d19677SJose E. Roman   Input Parameters:
214047c6ae99SBarry Smith + dm   - the DM object
214191d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
214247c6ae99SBarry Smith 
214347c6ae99SBarry Smith   Output Parameter:
21440298fd71SBarry Smith . dmf - the refined DM, or NULL
2145ae0a1c52SMatthew G Knepley 
2146f27dd7c6SMatthew G. Knepley   Options Database Keys:
2147412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2148412e9a14SMatthew G. Knepley 
21490298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
215047c6ae99SBarry Smith 
215147c6ae99SBarry Smith   Level: developer
215247c6ae99SBarry Smith 
2153db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
215447c6ae99SBarry Smith @*/
21557087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
215647c6ae99SBarry Smith {
2157c833c3b5SJed Brown   DMRefineHookLink link;
215847c6ae99SBarry Smith 
215947c6ae99SBarry Smith   PetscFunctionBegin;
2160732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21617a8be351SBarry Smith   PetscCheck(dm->ops->refine,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
21629566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine,dm,0,0,0));
21639566063dSJacob Faibussowitsch   PetscCall((*dm->ops->refine)(dm,comm,dmf));
21644057135bSMatthew G Knepley   if (*dmf) {
216543842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
21668865f1eaSKarl Rupp 
21679566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf));
21688865f1eaSKarl Rupp 
2169644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
21700598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2171656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
21728865f1eaSKarl Rupp 
21739566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf,dm->mattype));
2174c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
21751baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm,*dmf,link->ctx));
2176c833c3b5SJed Brown     }
2177c833c3b5SJed Brown   }
21789566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine,dm,0,0,0));
2179c833c3b5SJed Brown   PetscFunctionReturn(0);
2180c833c3b5SJed Brown }
2181c833c3b5SJed Brown 
2182bb9467b5SJed Brown /*@C
2183c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2184c833c3b5SJed Brown 
2185c833c3b5SJed Brown    Logically Collective
2186c833c3b5SJed Brown 
21874165533cSJose E. Roman    Input Parameters:
2188c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2189c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2190c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21910298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2192c833c3b5SJed Brown 
2193c833c3b5SJed Brown    Calling sequence of refinehook:
2194c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2195c833c3b5SJed Brown 
2196c833c3b5SJed Brown +  coarse - coarse level DM
2197c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2198c833c3b5SJed Brown -  ctx - optional user-defined function context
2199c833c3b5SJed Brown 
2200c833c3b5SJed Brown    Calling sequence for interphook:
2201c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2202c833c3b5SJed Brown 
2203c833c3b5SJed Brown +  coarse - coarse level DM
2204c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2205c833c3b5SJed Brown .  fine - fine level DM to update
2206c833c3b5SJed Brown -  ctx - optional user-defined function context
2207c833c3b5SJed Brown 
2208c833c3b5SJed Brown    Level: advanced
2209c833c3b5SJed Brown 
2210c833c3b5SJed Brown    Notes:
2211c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2212c833c3b5SJed Brown 
2213c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2214c833c3b5SJed Brown 
2215bb9467b5SJed Brown    This function is currently not available from Fortran.
2216bb9467b5SJed Brown 
2217db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2218c833c3b5SJed Brown @*/
2219c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2220c833c3b5SJed Brown {
2221c833c3b5SJed Brown   DMRefineHookLink link,*p;
2222c833c3b5SJed Brown 
2223c833c3b5SJed Brown   PetscFunctionBegin;
2224c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22253d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
22263d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
22273d8e3701SJed Brown   }
22289566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2229c833c3b5SJed Brown   link->refinehook = refinehook;
2230c833c3b5SJed Brown   link->interphook = interphook;
2231c833c3b5SJed Brown   link->ctx        = ctx;
22320298fd71SBarry Smith   link->next       = NULL;
2233c833c3b5SJed Brown   *p               = link;
2234c833c3b5SJed Brown   PetscFunctionReturn(0);
2235c833c3b5SJed Brown }
2236c833c3b5SJed Brown 
22373d8e3701SJed Brown /*@C
22383d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
22393d8e3701SJed Brown 
22403d8e3701SJed Brown    Logically Collective
22413d8e3701SJed Brown 
22424165533cSJose E. Roman    Input Parameters:
22433d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
22443d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
22453d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
22463d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
22473d8e3701SJed Brown 
22483d8e3701SJed Brown    Level: advanced
22493d8e3701SJed Brown 
22503d8e3701SJed Brown    Notes:
22513d8e3701SJed Brown    This function does nothing if the hook is not in the list.
22523d8e3701SJed Brown 
22533d8e3701SJed Brown    This function is currently not available from Fortran.
22543d8e3701SJed Brown 
2255db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
22563d8e3701SJed Brown @*/
22573d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
22583d8e3701SJed Brown {
22593d8e3701SJed Brown   DMRefineHookLink link,*p;
22603d8e3701SJed Brown 
22613d8e3701SJed Brown   PetscFunctionBegin;
22623d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22633d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
22643d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
22653d8e3701SJed Brown       link = *p;
22663d8e3701SJed Brown       *p = link->next;
22679566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
22683d8e3701SJed Brown       break;
22693d8e3701SJed Brown     }
22703d8e3701SJed Brown   }
22713d8e3701SJed Brown   PetscFunctionReturn(0);
22723d8e3701SJed Brown }
22733d8e3701SJed Brown 
2274c833c3b5SJed Brown /*@
2275c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2276c833c3b5SJed Brown 
2277c833c3b5SJed Brown    Collective if any hooks are
2278c833c3b5SJed Brown 
22794165533cSJose E. Roman    Input Parameters:
2280c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2281e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2282c833c3b5SJed Brown -  fine - finer DM to update
2283c833c3b5SJed Brown 
2284c833c3b5SJed Brown    Level: developer
2285c833c3b5SJed Brown 
2286db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `MatInterpolate()`
2287c833c3b5SJed Brown @*/
2288c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2289c833c3b5SJed Brown {
2290c833c3b5SJed Brown   DMRefineHookLink link;
2291c833c3b5SJed Brown 
2292c833c3b5SJed Brown   PetscFunctionBegin;
2293c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
22941baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse,interp,fine,link->ctx));
22954057135bSMatthew G Knepley   }
229647c6ae99SBarry Smith   PetscFunctionReturn(0);
229747c6ae99SBarry Smith }
229847c6ae99SBarry Smith 
2299eb3f98d2SBarry Smith /*@
23001f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
23011f3379b2SToby Isaac 
23021f3379b2SToby Isaac    Collective on DM
23031f3379b2SToby Isaac 
23044165533cSJose E. Roman    Input Parameters:
23051f3379b2SToby Isaac +  coarse - coarse DM
23061f3379b2SToby Isaac .  fine   - fine DM
23071f3379b2SToby Isaac .  interp - (optional) the matrix computed by DMCreateInterpolation().  Implementations may not need this, but if it
23081f3379b2SToby Isaac             is available it can avoid some recomputation.  If it is provided, MatInterpolate() will be used if
23091f3379b2SToby Isaac             the coarse DM does not have a specialized implementation.
23101f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
23111f3379b2SToby Isaac 
23124165533cSJose E. Roman    Output Parameter:
23131f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
23141f3379b2SToby Isaac 
23151f3379b2SToby Isaac    Level: developer
23161f3379b2SToby Isaac 
23171f3379b2SToby Isaac    Note: This function exists because the interpolation of a solution vector between meshes is not always a linear
23181f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
23191f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
23201f3379b2SToby Isaac    slope-limiting reconstruction.
23211f3379b2SToby Isaac 
2322db781477SPatrick Sanan .seealso `DMInterpolate()`, `DMCreateInterpolation()`
23231f3379b2SToby Isaac @*/
23241f3379b2SToby Isaac PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
23251f3379b2SToby Isaac {
23261f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM,DM,Mat,Vec,Vec) = NULL;
23271f3379b2SToby Isaac 
23281f3379b2SToby Isaac   PetscFunctionBegin;
23291f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
23301f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp,MAT_CLASSID,3);
23311f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol,VEC_CLASSID,4);
23321f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol,VEC_CLASSID,5);
23331f3379b2SToby Isaac 
23349566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse,"DMInterpolateSolution_C", &interpsol));
23351f3379b2SToby Isaac   if (interpsol) {
23369566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
23371f3379b2SToby Isaac   } else if (interp) {
23389566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
233998921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
23401f3379b2SToby Isaac   PetscFunctionReturn(0);
23411f3379b2SToby Isaac }
23421f3379b2SToby Isaac 
23431f3379b2SToby Isaac /*@
2344aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2345eb3f98d2SBarry Smith 
2346eb3f98d2SBarry Smith     Not Collective
2347eb3f98d2SBarry Smith 
2348eb3f98d2SBarry Smith     Input Parameter:
2349eb3f98d2SBarry Smith .   dm - the DM object
2350eb3f98d2SBarry Smith 
2351eb3f98d2SBarry Smith     Output Parameter:
2352eb3f98d2SBarry Smith .   level - number of refinements
2353eb3f98d2SBarry Smith 
2354eb3f98d2SBarry Smith     Level: developer
2355eb3f98d2SBarry Smith 
2356db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2357eb3f98d2SBarry Smith 
2358eb3f98d2SBarry Smith @*/
2359eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2360eb3f98d2SBarry Smith {
2361eb3f98d2SBarry Smith   PetscFunctionBegin;
2362eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2363eb3f98d2SBarry Smith   *level = dm->levelup;
2364eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2365eb3f98d2SBarry Smith }
2366eb3f98d2SBarry Smith 
2367fef3a512SBarry Smith /*@
2368aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2369fef3a512SBarry Smith 
2370fef3a512SBarry Smith     Not Collective
2371fef3a512SBarry Smith 
2372d8d19677SJose E. Roman     Input Parameters:
2373fef3a512SBarry Smith +   dm - the DM object
2374fef3a512SBarry Smith -   level - number of refinements
2375fef3a512SBarry Smith 
2376fef3a512SBarry Smith     Level: advanced
2377fef3a512SBarry Smith 
237895452b02SPatrick Sanan     Notes:
237995452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2380fef3a512SBarry Smith 
2381db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2382fef3a512SBarry Smith 
2383fef3a512SBarry Smith @*/
2384fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2385fef3a512SBarry Smith {
2386fef3a512SBarry Smith   PetscFunctionBegin;
2387fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2388fef3a512SBarry Smith   dm->levelup = level;
2389fef3a512SBarry Smith   PetscFunctionReturn(0);
2390fef3a512SBarry Smith }
2391fef3a512SBarry Smith 
2392d410b0cfSMatthew G. Knepley /*@
2393d410b0cfSMatthew G. Knepley   DMExtrude - Extrude a DM object from a surface
2394d410b0cfSMatthew G. Knepley 
2395d410b0cfSMatthew G. Knepley   Collective on dm
2396d410b0cfSMatthew G. Knepley 
2397f1a722f8SMatthew G. Knepley   Input Parameters:
2398d410b0cfSMatthew G. Knepley + dm     - the DM object
2399d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2400d410b0cfSMatthew G. Knepley 
2401d410b0cfSMatthew G. Knepley   Output Parameter:
2402d410b0cfSMatthew G. Knepley . dme - the extruded DM, or NULL
2403d410b0cfSMatthew G. Knepley 
2404d410b0cfSMatthew G. Knepley   Note: If no extrusion was done, the return value is NULL
2405d410b0cfSMatthew G. Knepley 
2406d410b0cfSMatthew G. Knepley   Level: developer
2407d410b0cfSMatthew G. Knepley 
2408db781477SPatrick Sanan .seealso `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2409d410b0cfSMatthew G. Knepley @*/
2410d410b0cfSMatthew G. Knepley PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2411d410b0cfSMatthew G. Knepley {
2412d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2413d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24147a8be351SBarry Smith   PetscCheck(dm->ops->extrude,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "DM type %s does not implement DMExtrude", ((PetscObject) dm)->type_name);
24159566063dSJacob Faibussowitsch   PetscCall((*dm->ops->extrude)(dm, layers, dme));
2416d410b0cfSMatthew G. Knepley   if (*dme) {
2417d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
24189566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject) dm, (PetscObject) *dme));
2419d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
24209566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2421d410b0cfSMatthew G. Knepley   }
2422d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2423d410b0cfSMatthew G. Knepley }
2424d410b0cfSMatthew G. Knepley 
2425ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2426ca3d3a14SMatthew G. Knepley {
2427ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2428ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2429ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2430ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2431ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2432ca3d3a14SMatthew G. Knepley }
2433ca3d3a14SMatthew G. Knepley 
2434ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2435ca3d3a14SMatthew G. Knepley {
2436ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2437ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2438ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2439ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2440ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2441ca3d3a14SMatthew G. Knepley }
2442ca3d3a14SMatthew G. Knepley 
2443ca3d3a14SMatthew G. Knepley /*@
2444c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2445ca3d3a14SMatthew G. Knepley 
2446ca3d3a14SMatthew G. Knepley   Input Parameter:
2447ca3d3a14SMatthew G. Knepley . dm - The DM
2448ca3d3a14SMatthew G. Knepley 
2449ca3d3a14SMatthew G. Knepley   Output Parameter:
2450ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2451ca3d3a14SMatthew G. Knepley 
2452ca3d3a14SMatthew G. Knepley   Level: developer
2453ca3d3a14SMatthew G. Knepley 
2454db781477SPatrick Sanan .seealso: `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2455ca3d3a14SMatthew G. Knepley @*/
2456ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2457ca3d3a14SMatthew G. Knepley {
2458ca3d3a14SMatthew G. Knepley   Vec            tv;
2459ca3d3a14SMatthew G. Knepley 
2460ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2461ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2462534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
24639566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2464ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2465ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2466ca3d3a14SMatthew G. Knepley }
2467ca3d3a14SMatthew G. Knepley 
2468ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2469ca3d3a14SMatthew G. Knepley {
2470ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2471ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2472ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2473ca3d3a14SMatthew G. Knepley 
2474ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
24759566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
24769566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
24779566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
24789566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
24799566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
24809566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
24819566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
24829566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2483ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
24849566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2485ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2486ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2487ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
24889566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2489ca3d3a14SMatthew G. Knepley       if (!dof) continue;
24909566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
24919566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2492ca3d3a14SMatthew G. Knepley     }
2493ca3d3a14SMatthew G. Knepley   }
24949566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
24959566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
24969566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2497ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2498ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
24999566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2500ca3d3a14SMatthew G. Knepley       if (dof) {
2501ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2502ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2503ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2504ca3d3a14SMatthew G. Knepley 
2505ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
25069566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
25079566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva));
25089566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2509ca3d3a14SMatthew G. Knepley       }
2510ca3d3a14SMatthew G. Knepley     }
2511ca3d3a14SMatthew G. Knepley   }
25129566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
2513ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2514ca3d3a14SMatthew G. Knepley }
2515ca3d3a14SMatthew G. Knepley 
2516ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2517ca3d3a14SMatthew G. Knepley {
2518ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2519ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2520ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2521ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2522ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2523ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2524ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
25259566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
2526ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2527ca3d3a14SMatthew G. Knepley }
2528ca3d3a14SMatthew G. Knepley 
2529bb9467b5SJed Brown /*@C
2530baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2531baf369e7SPeter Brune 
2532baf369e7SPeter Brune    Logically Collective
2533baf369e7SPeter Brune 
25344165533cSJose E. Roman    Input Parameters:
2535baf369e7SPeter Brune +  dm - the DM
2536baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2537baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
25380298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2539baf369e7SPeter Brune 
2540baf369e7SPeter Brune    Calling sequence for beginhook:
2541baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2542baf369e7SPeter Brune 
2543baf369e7SPeter Brune +  dm - global DM
2544baf369e7SPeter Brune .  g - global vector
2545baf369e7SPeter Brune .  mode - mode
2546baf369e7SPeter Brune .  l - local vector
2547baf369e7SPeter Brune -  ctx - optional user-defined function context
2548baf369e7SPeter Brune 
2549baf369e7SPeter Brune    Calling sequence for endhook:
2550ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2551baf369e7SPeter Brune 
2552baf369e7SPeter Brune +  global - global DM
2553baf369e7SPeter Brune -  ctx - optional user-defined function context
2554baf369e7SPeter Brune 
2555baf369e7SPeter Brune    Level: advanced
2556baf369e7SPeter Brune 
2557db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2558baf369e7SPeter Brune @*/
2559baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2560baf369e7SPeter Brune {
2561baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2562baf369e7SPeter Brune 
2563baf369e7SPeter Brune   PetscFunctionBegin;
2564baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2565baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
25669566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2567baf369e7SPeter Brune   link->beginhook = beginhook;
2568baf369e7SPeter Brune   link->endhook   = endhook;
2569baf369e7SPeter Brune   link->ctx       = ctx;
25700298fd71SBarry Smith   link->next      = NULL;
2571baf369e7SPeter Brune   *p              = link;
2572baf369e7SPeter Brune   PetscFunctionReturn(0);
2573baf369e7SPeter Brune }
2574baf369e7SPeter Brune 
25754c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
25764c274da1SToby Isaac {
25774c274da1SToby Isaac   Mat cMat;
257879769bd5SJed Brown   Vec cVec, cBias;
25794c274da1SToby Isaac   PetscSection section, cSec;
25804c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25814c274da1SToby Isaac 
25824c274da1SToby Isaac   PetscFunctionBegin;
25834c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25849566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm,&cSec,&cMat,&cBias));
25854c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
25865db9a05bSToby Isaac     PetscInt nRows;
25875db9a05bSToby Isaac 
25889566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat,&nRows,NULL));
25895db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
25909566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm,&section));
25919566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat,NULL,&cVec));
25929566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat,l,cVec));
25939566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec,1.,cBias));
25949566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec,&pStart,&pEnd));
25954c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
25969566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec,p,&dof));
25974c274da1SToby Isaac       if (dof) {
25984c274da1SToby Isaac         PetscScalar *vals;
25999566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec,cSec,p,&vals));
26009566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES));
26014c274da1SToby Isaac       }
26024c274da1SToby Isaac     }
26039566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
26044c274da1SToby Isaac   }
26054c274da1SToby Isaac   PetscFunctionReturn(0);
26064c274da1SToby Isaac }
26074c274da1SToby Isaac 
260847c6ae99SBarry Smith /*@
260901729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
261001729b5cSPatrick Sanan 
2611d083f849SBarry Smith     Neighbor-wise Collective on dm
261201729b5cSPatrick Sanan 
261301729b5cSPatrick Sanan     Input Parameters:
261401729b5cSPatrick Sanan +   dm - the DM object
261501729b5cSPatrick Sanan .   g - the global vector
261601729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
261701729b5cSPatrick Sanan -   l - the local vector
261801729b5cSPatrick Sanan 
261901729b5cSPatrick Sanan     Notes:
262001729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
262101729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
262201729b5cSPatrick Sanan 
262301729b5cSPatrick Sanan     Level: beginner
262401729b5cSPatrick Sanan 
2625db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
262601729b5cSPatrick Sanan 
262701729b5cSPatrick Sanan @*/
262801729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
262901729b5cSPatrick Sanan {
263001729b5cSPatrick Sanan   PetscFunctionBegin;
26319566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm,g,mode,l));
26329566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm,g,mode,l));
263301729b5cSPatrick Sanan   PetscFunctionReturn(0);
263401729b5cSPatrick Sanan }
263501729b5cSPatrick Sanan 
263601729b5cSPatrick Sanan /*@
263747c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
263847c6ae99SBarry Smith 
2639d083f849SBarry Smith     Neighbor-wise Collective on dm
264047c6ae99SBarry Smith 
264147c6ae99SBarry Smith     Input Parameters:
264247c6ae99SBarry Smith +   dm - the DM object
264347c6ae99SBarry Smith .   g - the global vector
264447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
264547c6ae99SBarry Smith -   l - the local vector
264647c6ae99SBarry Smith 
264701729b5cSPatrick Sanan     Level: intermediate
264847c6ae99SBarry Smith 
2649db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
265047c6ae99SBarry Smith 
265147c6ae99SBarry Smith @*/
26527087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
265347c6ae99SBarry Smith {
26547128ae9fSMatthew G Knepley   PetscSF                 sf;
2655baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
265647c6ae99SBarry Smith 
265747c6ae99SBarry Smith   PetscFunctionBegin;
2658171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2659baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
26601baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm,g,mode,l,link->ctx));
2661baf369e7SPeter Brune   }
26629566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
26637128ae9fSMatthew G Knepley   if (sf) {
2664ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2665ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2666d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
26677128ae9fSMatthew G Knepley 
26687a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
26699566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
26709566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
26719566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
26729566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
26739566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
26747128ae9fSMatthew G Knepley   } else {
26757a8be351SBarry Smith     PetscCheck(dm->ops->globaltolocalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
26769566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
26777128ae9fSMatthew G Knepley   }
267847c6ae99SBarry Smith   PetscFunctionReturn(0);
267947c6ae99SBarry Smith }
268047c6ae99SBarry Smith 
268147c6ae99SBarry Smith /*@
268247c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
268347c6ae99SBarry Smith 
2684d083f849SBarry Smith     Neighbor-wise Collective on dm
268547c6ae99SBarry Smith 
268647c6ae99SBarry Smith     Input Parameters:
268747c6ae99SBarry Smith +   dm - the DM object
268847c6ae99SBarry Smith .   g - the global vector
268947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
269047c6ae99SBarry Smith -   l - the local vector
269147c6ae99SBarry Smith 
269201729b5cSPatrick Sanan     Level: intermediate
269347c6ae99SBarry Smith 
2694db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
269547c6ae99SBarry Smith 
269647c6ae99SBarry Smith @*/
26977087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
269847c6ae99SBarry Smith {
26997128ae9fSMatthew G Knepley   PetscSF                 sf;
2700ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2701ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2702ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2703baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2704d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
270547c6ae99SBarry Smith 
270647c6ae99SBarry Smith   PetscFunctionBegin;
2707171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27089566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
27099566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
27107128ae9fSMatthew G Knepley   if (sf) {
27117a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
27127128ae9fSMatthew G Knepley 
27139566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
27149566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
27159566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray,MPI_REPLACE));
27169566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
27179566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
27189566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
27197128ae9fSMatthew G Knepley   } else {
27207a8be351SBarry Smith     PetscCheck(dm->ops->globaltolocalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
27219566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
27227128ae9fSMatthew G Knepley   }
27239566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL));
2724baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
27259566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm,g,mode,l,link->ctx));
2726baf369e7SPeter Brune   }
272747c6ae99SBarry Smith   PetscFunctionReturn(0);
272847c6ae99SBarry Smith }
272947c6ae99SBarry Smith 
2730d4d07f1eSToby Isaac /*@C
2731d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2732d4d07f1eSToby Isaac 
2733d4d07f1eSToby Isaac    Logically Collective
2734d4d07f1eSToby Isaac 
27354165533cSJose E. Roman    Input Parameters:
2736d4d07f1eSToby Isaac +  dm - the DM
2737d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2738d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2739d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2740d4d07f1eSToby Isaac 
2741d4d07f1eSToby Isaac    Calling sequence for beginhook:
2742d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2743d4d07f1eSToby Isaac 
2744d4d07f1eSToby Isaac +  dm - global DM
2745d4d07f1eSToby Isaac .  l - local vector
2746d4d07f1eSToby Isaac .  mode - mode
2747d4d07f1eSToby Isaac .  g - global vector
2748d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2749d4d07f1eSToby Isaac 
2750d4d07f1eSToby Isaac    Calling sequence for endhook:
2751d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2752d4d07f1eSToby Isaac 
2753d4d07f1eSToby Isaac +  global - global DM
2754d4d07f1eSToby Isaac .  l - local vector
2755d4d07f1eSToby Isaac .  mode - mode
2756d4d07f1eSToby Isaac .  g - global vector
2757d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2758d4d07f1eSToby Isaac 
2759d4d07f1eSToby Isaac    Level: advanced
2760d4d07f1eSToby Isaac 
2761db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2762d4d07f1eSToby Isaac @*/
2763d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2764d4d07f1eSToby Isaac {
2765d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2766d4d07f1eSToby Isaac 
2767d4d07f1eSToby Isaac   PetscFunctionBegin;
2768d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2769d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
27709566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2771d4d07f1eSToby Isaac   link->beginhook = beginhook;
2772d4d07f1eSToby Isaac   link->endhook   = endhook;
2773d4d07f1eSToby Isaac   link->ctx       = ctx;
2774d4d07f1eSToby Isaac   link->next      = NULL;
2775d4d07f1eSToby Isaac   *p              = link;
2776d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2777d4d07f1eSToby Isaac }
2778d4d07f1eSToby Isaac 
27794c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
27804c274da1SToby Isaac {
27814c274da1SToby Isaac   Mat cMat;
27824c274da1SToby Isaac   Vec cVec;
27834c274da1SToby Isaac   PetscSection section, cSec;
27844c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
27854c274da1SToby Isaac 
27864c274da1SToby Isaac   PetscFunctionBegin;
27874c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27889566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm,&cSec,&cMat,NULL));
27894c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
27905db9a05bSToby Isaac     PetscInt nRows;
27915db9a05bSToby Isaac 
27929566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat,&nRows,NULL));
27935db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
27949566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm,&section));
27959566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat,NULL,&cVec));
27969566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec,&pStart,&pEnd));
27974c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27989566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec,p,&dof));
27994c274da1SToby Isaac       if (dof) {
28004c274da1SToby Isaac         PetscInt d;
28014c274da1SToby Isaac         PetscScalar *vals;
28029566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l,section,p,&vals));
28039566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec,cSec,p,vals,mode));
28044c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
28054c274da1SToby Isaac          * we just extracted */
28064c274da1SToby Isaac         for (d = 0; d < dof; d++) {
28074c274da1SToby Isaac           vals[d] = 0.;
28084c274da1SToby Isaac         }
28094c274da1SToby Isaac       }
28104c274da1SToby Isaac     }
28119566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat,cVec,l,l));
28129566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
28134c274da1SToby Isaac   }
28144c274da1SToby Isaac   PetscFunctionReturn(0);
28154c274da1SToby Isaac }
281601729b5cSPatrick Sanan /*@
281701729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
281801729b5cSPatrick Sanan 
2819d083f849SBarry Smith     Neighbor-wise Collective on dm
282001729b5cSPatrick Sanan 
282101729b5cSPatrick Sanan     Input Parameters:
282201729b5cSPatrick Sanan +   dm - the DM object
282301729b5cSPatrick Sanan .   l - the local vector
282401729b5cSPatrick 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.
282501729b5cSPatrick Sanan -   g - the global vector
282601729b5cSPatrick Sanan 
282701729b5cSPatrick Sanan     Notes:
282801729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
282901729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
283001729b5cSPatrick Sanan 
283101729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
283201729b5cSPatrick 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.
283301729b5cSPatrick Sanan 
283401729b5cSPatrick Sanan     Level: beginner
283501729b5cSPatrick Sanan 
2836db781477SPatrick Sanan .seealso `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
283701729b5cSPatrick Sanan 
283801729b5cSPatrick Sanan @*/
283901729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
284001729b5cSPatrick Sanan {
284101729b5cSPatrick Sanan   PetscFunctionBegin;
28429566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm,l,mode,g));
28439566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm,l,mode,g));
284401729b5cSPatrick Sanan   PetscFunctionReturn(0);
284501729b5cSPatrick Sanan }
28464c274da1SToby Isaac 
284747c6ae99SBarry Smith /*@
284801729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
28499a42bb27SBarry Smith 
2850d083f849SBarry Smith     Neighbor-wise Collective on dm
28519a42bb27SBarry Smith 
28529a42bb27SBarry Smith     Input Parameters:
28539a42bb27SBarry Smith +   dm - the DM object
2854f6813fd5SJed Brown .   l - the local vector
28551eb28f2eSBarry 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.
28561eb28f2eSBarry Smith -   g - the global vector
28579a42bb27SBarry Smith 
285895452b02SPatrick Sanan     Notes:
285995452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
286084330215SMatthew 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.
28619a42bb27SBarry Smith 
286201729b5cSPatrick Sanan     Level: intermediate
28639a42bb27SBarry Smith 
2864db781477SPatrick Sanan .seealso `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
28659a42bb27SBarry Smith 
28669a42bb27SBarry Smith @*/
28677087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
28689a42bb27SBarry Smith {
28697128ae9fSMatthew G Knepley   PetscSF                 sf;
287084330215SMatthew G. Knepley   PetscSection            s, gs;
2871d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2872ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2873ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2874ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2875fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
2876d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
28779a42bb27SBarry Smith 
28789a42bb27SBarry Smith   PetscFunctionBegin;
2879171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2880d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
28811baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm,l,mode,g,link->ctx));
2882d4d07f1eSToby Isaac   }
28839566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL));
28849566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28859566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
28867128ae9fSMatthew G Knepley   switch (mode) {
28877128ae9fSMatthew G Knepley   case INSERT_VALUES:
28887128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2889304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
289084330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
28917128ae9fSMatthew G Knepley   case ADD_VALUES:
28927128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2893304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
289484330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
28957128ae9fSMatthew G Knepley   default:
289663a3b9bcSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
28977128ae9fSMatthew G Knepley   }
2898ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
28999566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
2900ca3d3a14SMatthew G. Knepley     if (transform) {
29019566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
29029566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
29039566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
29049566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
2905fa88e482SJed Brown     } else if (isInsert) {
29069566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
2907fa88e482SJed Brown     } else {
29089566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
2909fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2910ca3d3a14SMatthew G. Knepley     }
2911fa88e482SJed Brown     if (s && isInsert) {
29129566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
2913fa88e482SJed Brown     } else {
29149566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
2915fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2916fa88e482SJed Brown     }
2917ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
29189566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
291984330215SMatthew G. Knepley     } else if (s && isInsert) {
292084330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
292184330215SMatthew G. Knepley 
29229566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
29239566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
29249566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
292584330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2926b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
292784330215SMatthew G. Knepley 
29289566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
29299566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
29309566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
29319566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
29329566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
29339566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
2934b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
293503442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
29367a8be351SBarry 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);
2937b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2938b3b16f48SMatthew G. Knepley         if (!gcdof) {
293984330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2940b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2941b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
294284330215SMatthew G. Knepley           const PetscInt *cdofs;
294384330215SMatthew G. Knepley           PetscInt        cind = 0;
294484330215SMatthew G. Knepley 
29459566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
2946b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
294784330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2948b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
294984330215SMatthew G. Knepley           }
29507a8be351SBarry 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);
295184330215SMatthew G. Knepley       }
2952ca3d3a14SMatthew G. Knepley     }
2953fa88e482SJed Brown     if (g_inplace) {
29549566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
2955fa88e482SJed Brown     } else {
29569566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
2957fa88e482SJed Brown     }
2958ca3d3a14SMatthew G. Knepley     if (transform) {
29599566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
29609566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
2961fa88e482SJed Brown     } else if (l_inplace) {
29629566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
2963ca3d3a14SMatthew G. Knepley     } else {
29649566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
2965ca3d3a14SMatthew G. Knepley     }
29667128ae9fSMatthew G Knepley   } else {
29677a8be351SBarry Smith     PetscCheck(dm->ops->localtoglobalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
29689566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g));
29697128ae9fSMatthew G Knepley   }
29709a42bb27SBarry Smith   PetscFunctionReturn(0);
29719a42bb27SBarry Smith }
29729a42bb27SBarry Smith 
29739a42bb27SBarry Smith /*@
29749a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
297547c6ae99SBarry Smith 
2976d083f849SBarry Smith     Neighbor-wise Collective on dm
297747c6ae99SBarry Smith 
297847c6ae99SBarry Smith     Input Parameters:
297947c6ae99SBarry Smith +   dm - the DM object
2980f6813fd5SJed Brown .   l - the local vector
298147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2982f6813fd5SJed Brown -   g - the global vector
298347c6ae99SBarry Smith 
298401729b5cSPatrick Sanan     Level: intermediate
298547c6ae99SBarry Smith 
2986db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalEnd()`
298747c6ae99SBarry Smith 
298847c6ae99SBarry Smith @*/
29897087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
299047c6ae99SBarry Smith {
29917128ae9fSMatthew G Knepley   PetscSF                 sf;
299284330215SMatthew G. Knepley   PetscSection            s;
2993d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2994ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
299547c6ae99SBarry Smith 
299647c6ae99SBarry Smith   PetscFunctionBegin;
2997171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29989566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
29999566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30007128ae9fSMatthew G Knepley   switch (mode) {
30017128ae9fSMatthew G Knepley   case INSERT_VALUES:
30027128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
300384330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
30047128ae9fSMatthew G Knepley   case ADD_VALUES:
30057128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
300684330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
30077128ae9fSMatthew G Knepley   default:
300863a3b9bcSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30097128ae9fSMatthew G Knepley   }
301084330215SMatthew G. Knepley   if (sf && !isInsert) {
3011ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3012ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3013ca3d3a14SMatthew G. Knepley     Vec                tmpl;
301484330215SMatthew G. Knepley 
30159566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3016ca3d3a14SMatthew G. Knepley     if (transform) {
30179566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30189566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3019ca3d3a14SMatthew G. Knepley     } else {
30209566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3021ca3d3a14SMatthew G. Knepley     }
30229566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
30239566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3024ca3d3a14SMatthew G. Knepley     if (transform) {
30259566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
30269566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3027ca3d3a14SMatthew G. Knepley     } else {
30289566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3029ca3d3a14SMatthew G. Knepley     }
30309566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
303184330215SMatthew G. Knepley   } else if (s && isInsert) {
30327128ae9fSMatthew G Knepley   } else {
30337a8be351SBarry Smith     PetscCheck(dm->ops->localtoglobalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
30349566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g));
30357128ae9fSMatthew G Knepley   }
3036d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
30379566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm,g,mode,l,link->ctx));
3038d4d07f1eSToby Isaac   }
303947c6ae99SBarry Smith   PetscFunctionReturn(0);
304047c6ae99SBarry Smith }
304147c6ae99SBarry Smith 
3042f089877aSRichard Tran Mills /*@
3043bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
3044bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3045d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
3046f089877aSRichard Tran Mills 
3047d083f849SBarry Smith    Neighbor-wise Collective on dm
3048f089877aSRichard Tran Mills 
3049f089877aSRichard Tran Mills    Input Parameters:
3050f089877aSRichard Tran Mills +  dm - the DM object
3051bc0a1609SRichard Tran Mills .  g - the original local vector
3052bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3053f089877aSRichard Tran Mills 
3054bc0a1609SRichard Tran Mills    Output Parameter:
3055bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3056f089877aSRichard Tran Mills 
3057f089877aSRichard Tran Mills    Level: intermediate
3058f089877aSRichard Tran Mills 
3059bc0a1609SRichard Tran Mills    Notes:
3060bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3061bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3062bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3063bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3064bc0a1609SRichard Tran Mills 
3065db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalEnd()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3066f089877aSRichard Tran Mills 
3067f089877aSRichard Tran Mills @*/
3068f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
3069f089877aSRichard Tran Mills {
3070f089877aSRichard Tran Mills   PetscFunctionBegin;
3071f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30727a8be351SBarry Smith   PetscCheck(dm->ops->localtolocalbegin,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
30739566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
3074f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3075f089877aSRichard Tran Mills }
3076f089877aSRichard Tran Mills 
3077f089877aSRichard Tran Mills /*@
3078bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
3079bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3080d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
3081f089877aSRichard Tran Mills 
3082d083f849SBarry Smith    Neighbor-wise Collective on dm
3083f089877aSRichard Tran Mills 
3084f089877aSRichard Tran Mills    Input Parameters:
3085bc0a1609SRichard Tran Mills +  da - the DM object
3086bc0a1609SRichard Tran Mills .  g - the original local vector
3087bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3088f089877aSRichard Tran Mills 
3089bc0a1609SRichard Tran Mills    Output Parameter:
3090bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3091f089877aSRichard Tran Mills 
3092f089877aSRichard Tran Mills    Level: intermediate
3093f089877aSRichard Tran Mills 
3094bc0a1609SRichard Tran Mills    Notes:
3095bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3096bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3097bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3098bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3099bc0a1609SRichard Tran Mills 
3100db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalBegin()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3101f089877aSRichard Tran Mills 
3102f089877aSRichard Tran Mills @*/
3103f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
3104f089877aSRichard Tran Mills {
3105f089877aSRichard Tran Mills   PetscFunctionBegin;
3106f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31077a8be351SBarry Smith   PetscCheck(dm->ops->localtolocalend,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
31089566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
3109f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3110f089877aSRichard Tran Mills }
3111f089877aSRichard Tran Mills 
311247c6ae99SBarry Smith /*@
311347c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
311447c6ae99SBarry Smith 
3115d083f849SBarry Smith     Collective on dm
311647c6ae99SBarry Smith 
3117d8d19677SJose E. Roman     Input Parameters:
311847c6ae99SBarry Smith +   dm - the DM object
311991d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
312047c6ae99SBarry Smith 
312147c6ae99SBarry Smith     Output Parameter:
312247c6ae99SBarry Smith .   dmc - the coarsened DM
312347c6ae99SBarry Smith 
312447c6ae99SBarry Smith     Level: developer
312547c6ae99SBarry Smith 
3126db781477SPatrick Sanan .seealso `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
312747c6ae99SBarry Smith 
312847c6ae99SBarry Smith @*/
31297087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
313047c6ae99SBarry Smith {
3131b17ce1afSJed Brown   DMCoarsenHookLink link;
313247c6ae99SBarry Smith 
313347c6ae99SBarry Smith   PetscFunctionBegin;
3134171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31357a8be351SBarry Smith   PetscCheck(dm->ops->coarsen,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
31369566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen,dm,0,0,0));
31379566063dSJacob Faibussowitsch   PetscCall((*dm->ops->coarsen)(dm, comm, dmc));
3138b9d85ea2SLisandro Dalcin   if (*dmc) {
3139a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
31409566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm,*dmc));
314143842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
31429566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc));
3143644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
31440598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3145656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
31469566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc,dm->mattype));
3147b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
31489566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm,*dmc,link->ctx));
3149b17ce1afSJed Brown     }
3150b9d85ea2SLisandro Dalcin   }
31519566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen,dm,0,0,0));
31527a8be351SBarry Smith   PetscCheck(*dmc,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3153b17ce1afSJed Brown   PetscFunctionReturn(0);
3154b17ce1afSJed Brown }
3155b17ce1afSJed Brown 
3156bb9467b5SJed Brown /*@C
3157b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3158b17ce1afSJed Brown 
3159b17ce1afSJed Brown    Logically Collective
3160b17ce1afSJed Brown 
31614165533cSJose E. Roman    Input Parameters:
3162b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3163b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3164b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
31650298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3166b17ce1afSJed Brown 
3167b17ce1afSJed Brown    Calling sequence of coarsenhook:
3168b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3169b17ce1afSJed Brown 
3170b17ce1afSJed Brown +  fine - fine level DM
3171b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3172b17ce1afSJed Brown -  ctx - optional user-defined function context
3173b17ce1afSJed Brown 
3174b17ce1afSJed Brown    Calling sequence for restricthook:
3175c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3176b17ce1afSJed Brown 
3177b17ce1afSJed Brown +  fine - fine level DM
3178b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3179c833c3b5SJed Brown .  rscale - scaling vector for restriction
3180c833c3b5SJed Brown .  inject - matrix restricting by injection
3181b17ce1afSJed Brown .  coarse - coarse level DM to update
3182b17ce1afSJed Brown -  ctx - optional user-defined function context
3183b17ce1afSJed Brown 
3184b17ce1afSJed Brown    Level: advanced
3185b17ce1afSJed Brown 
3186b17ce1afSJed Brown    Notes:
3187b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3188b17ce1afSJed Brown 
3189b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3190b17ce1afSJed Brown 
3191b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3192b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3193b17ce1afSJed Brown 
3194bb9467b5SJed Brown    This function is currently not available from Fortran.
3195bb9467b5SJed Brown 
3196db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3197b17ce1afSJed Brown @*/
3198b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3199b17ce1afSJed Brown {
3200b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3201b17ce1afSJed Brown 
3202b17ce1afSJed Brown   PetscFunctionBegin;
3203b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
32041e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
32051e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
32061e3d8eccSJed Brown   }
32079566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3208b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3209b17ce1afSJed Brown   link->restricthook = restricthook;
3210b17ce1afSJed Brown   link->ctx          = ctx;
32110298fd71SBarry Smith   link->next         = NULL;
3212b17ce1afSJed Brown   *p                 = link;
3213b17ce1afSJed Brown   PetscFunctionReturn(0);
3214b17ce1afSJed Brown }
3215b17ce1afSJed Brown 
3216dc822a44SJed Brown /*@C
3217dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3218dc822a44SJed Brown 
3219dc822a44SJed Brown    Logically Collective
3220dc822a44SJed Brown 
32214165533cSJose E. Roman    Input Parameters:
3222dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3223dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3224dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3225dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3226dc822a44SJed Brown 
3227dc822a44SJed Brown    Level: advanced
3228dc822a44SJed Brown 
3229dc822a44SJed Brown    Notes:
3230dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3231dc822a44SJed Brown 
3232dc822a44SJed Brown    This function is currently not available from Fortran.
3233dc822a44SJed Brown 
3234db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3235dc822a44SJed Brown @*/
3236dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3237dc822a44SJed Brown {
3238dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3239dc822a44SJed Brown 
3240dc822a44SJed Brown   PetscFunctionBegin;
3241dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3242dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3243dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3244dc822a44SJed Brown       link = *p;
3245dc822a44SJed Brown       *p = link->next;
32469566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3247dc822a44SJed Brown       break;
3248dc822a44SJed Brown     }
3249dc822a44SJed Brown   }
3250dc822a44SJed Brown   PetscFunctionReturn(0);
3251dc822a44SJed Brown }
3252dc822a44SJed Brown 
3253b17ce1afSJed Brown /*@
3254b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3255b17ce1afSJed Brown 
3256b17ce1afSJed Brown    Collective if any hooks are
3257b17ce1afSJed Brown 
32584165533cSJose E. Roman    Input Parameters:
3259b17ce1afSJed Brown +  fine - finer DM to use as a base
3260b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3261e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3262b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3263e91eccc2SStefano Zampini -  coarse - coarser DM to update
3264b17ce1afSJed Brown 
3265b17ce1afSJed Brown    Level: developer
3266b17ce1afSJed Brown 
3267db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
3268b17ce1afSJed Brown @*/
3269b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3270b17ce1afSJed Brown {
3271b17ce1afSJed Brown   DMCoarsenHookLink link;
3272b17ce1afSJed Brown 
3273b17ce1afSJed Brown   PetscFunctionBegin;
3274b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
32751baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx));
3276b17ce1afSJed Brown   }
327747c6ae99SBarry Smith   PetscFunctionReturn(0);
327847c6ae99SBarry Smith }
327947c6ae99SBarry Smith 
3280bb9467b5SJed Brown /*@C
3281be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
32825dbd56e3SPeter Brune 
3283d083f849SBarry Smith    Logically Collective on global
32845dbd56e3SPeter Brune 
32854165533cSJose E. Roman    Input Parameters:
32865dbd56e3SPeter Brune +  global - global DM
3287ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
32885dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
32890298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
32905dbd56e3SPeter Brune 
3291ec4806b8SPeter Brune    Calling sequence for ddhook:
3292ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3293ec4806b8SPeter Brune 
3294ec4806b8SPeter Brune +  global - global DM
3295ec4806b8SPeter Brune .  block  - block DM
3296ec4806b8SPeter Brune -  ctx - optional user-defined function context
3297ec4806b8SPeter Brune 
32985dbd56e3SPeter Brune    Calling sequence for restricthook:
3299ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
33005dbd56e3SPeter Brune 
33015dbd56e3SPeter Brune +  global - global DM
33025dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
33035dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3304ec4806b8SPeter Brune .  block  - block DM
33055dbd56e3SPeter Brune -  ctx - optional user-defined function context
33065dbd56e3SPeter Brune 
33075dbd56e3SPeter Brune    Level: advanced
33085dbd56e3SPeter Brune 
33095dbd56e3SPeter Brune    Notes:
3310ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
33115dbd56e3SPeter Brune 
33125dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
33135dbd56e3SPeter Brune 
33145dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3315ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
33165dbd56e3SPeter Brune 
3317bb9467b5SJed Brown    This function is currently not available from Fortran.
3318bb9467b5SJed Brown 
3319db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
33205dbd56e3SPeter Brune @*/
3321be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
33225dbd56e3SPeter Brune {
3323be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
33245dbd56e3SPeter Brune 
33255dbd56e3SPeter Brune   PetscFunctionBegin;
33265dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3327b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3328b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3329b3a6b972SJed Brown   }
33309566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
33315dbd56e3SPeter Brune   link->restricthook = restricthook;
3332be081cd6SPeter Brune   link->ddhook       = ddhook;
33335dbd56e3SPeter Brune   link->ctx          = ctx;
33340298fd71SBarry Smith   link->next         = NULL;
33355dbd56e3SPeter Brune   *p                 = link;
33365dbd56e3SPeter Brune   PetscFunctionReturn(0);
33375dbd56e3SPeter Brune }
33385dbd56e3SPeter Brune 
3339b3a6b972SJed Brown /*@C
3340b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3341b3a6b972SJed Brown 
3342b3a6b972SJed Brown    Logically Collective
3343b3a6b972SJed Brown 
33444165533cSJose E. Roman    Input Parameters:
3345b3a6b972SJed Brown +  global - global DM
3346b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3347b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3348b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3349b3a6b972SJed Brown 
3350b3a6b972SJed Brown    Level: advanced
3351b3a6b972SJed Brown 
3352b3a6b972SJed Brown    Notes:
3353b3a6b972SJed Brown 
3354b3a6b972SJed Brown    This function is currently not available from Fortran.
3355b3a6b972SJed Brown 
3356db781477SPatrick Sanan .seealso: `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3357b3a6b972SJed Brown @*/
3358b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3359b3a6b972SJed Brown {
3360b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3361b3a6b972SJed Brown 
3362b3a6b972SJed Brown   PetscFunctionBegin;
3363b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3364b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3365b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3366b3a6b972SJed Brown       link = *p;
3367b3a6b972SJed Brown       *p = link->next;
33689566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3369b3a6b972SJed Brown       break;
3370b3a6b972SJed Brown     }
3371b3a6b972SJed Brown   }
3372b3a6b972SJed Brown   PetscFunctionReturn(0);
3373b3a6b972SJed Brown }
3374b3a6b972SJed Brown 
33755dbd56e3SPeter Brune /*@
3376be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
33775dbd56e3SPeter Brune 
33785dbd56e3SPeter Brune    Collective if any hooks are
33795dbd56e3SPeter Brune 
33804165533cSJose E. Roman    Input Parameters:
33815dbd56e3SPeter Brune +  fine - finer DM to use as a base
3382be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3383be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
33845dbd56e3SPeter Brune -  coarse - coarer DM to update
33855dbd56e3SPeter Brune 
33865dbd56e3SPeter Brune    Level: developer
33875dbd56e3SPeter Brune 
3388db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
33895dbd56e3SPeter Brune @*/
3390be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
33915dbd56e3SPeter Brune {
3392be081cd6SPeter Brune   DMSubDomainHookLink link;
33935dbd56e3SPeter Brune 
33945dbd56e3SPeter Brune   PetscFunctionBegin;
3395be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
33961baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx));
33975dbd56e3SPeter Brune   }
33985dbd56e3SPeter Brune   PetscFunctionReturn(0);
33995dbd56e3SPeter Brune }
34005dbd56e3SPeter Brune 
34015fe1f584SPeter Brune /*@
34026a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
34035fe1f584SPeter Brune 
34045fe1f584SPeter Brune     Not Collective
34055fe1f584SPeter Brune 
34065fe1f584SPeter Brune     Input Parameter:
34075fe1f584SPeter Brune .   dm - the DM object
34085fe1f584SPeter Brune 
34095fe1f584SPeter Brune     Output Parameter:
34106a7d9d85SPeter Brune .   level - number of coarsenings
34115fe1f584SPeter Brune 
34125fe1f584SPeter Brune     Level: developer
34135fe1f584SPeter Brune 
3414db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
34155fe1f584SPeter Brune 
34165fe1f584SPeter Brune @*/
34175fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
34185fe1f584SPeter Brune {
34195fe1f584SPeter Brune   PetscFunctionBegin;
34205fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3421b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
34225fe1f584SPeter Brune   *level = dm->leveldown;
34235fe1f584SPeter Brune   PetscFunctionReturn(0);
34245fe1f584SPeter Brune }
34255fe1f584SPeter Brune 
34269a64c4a8SMatthew G. Knepley /*@
34279a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
34289a64c4a8SMatthew G. Knepley 
34299a64c4a8SMatthew G. Knepley     Not Collective
34309a64c4a8SMatthew G. Knepley 
34319a64c4a8SMatthew G. Knepley     Input Parameters:
34329a64c4a8SMatthew G. Knepley +   dm - the DM object
34339a64c4a8SMatthew G. Knepley -   level - number of coarsenings
34349a64c4a8SMatthew G. Knepley 
34359a64c4a8SMatthew G. Knepley     Level: developer
34369a64c4a8SMatthew G. Knepley 
3437db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
34389a64c4a8SMatthew G. Knepley @*/
34399a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
34409a64c4a8SMatthew G. Knepley {
34419a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
34429a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34439a64c4a8SMatthew G. Knepley   dm->leveldown = level;
34449a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
34459a64c4a8SMatthew G. Knepley }
34469a64c4a8SMatthew G. Knepley 
344747c6ae99SBarry Smith /*@C
344847c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
344947c6ae99SBarry Smith 
3450d083f849SBarry Smith     Collective on dm
345147c6ae99SBarry Smith 
3452d8d19677SJose E. Roman     Input Parameters:
345347c6ae99SBarry Smith +   dm - the DM object
345447c6ae99SBarry Smith -   nlevels - the number of levels of refinement
345547c6ae99SBarry Smith 
345647c6ae99SBarry Smith     Output Parameter:
345747c6ae99SBarry Smith .   dmf - the refined DM hierarchy
345847c6ae99SBarry Smith 
345947c6ae99SBarry Smith     Level: developer
346047c6ae99SBarry Smith 
3461db781477SPatrick Sanan .seealso `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
346247c6ae99SBarry Smith 
346347c6ae99SBarry Smith @*/
34647087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
346547c6ae99SBarry Smith {
346647c6ae99SBarry Smith   PetscFunctionBegin;
3467171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34687a8be351SBarry Smith   PetscCheck(nlevels >= 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
346947c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3470b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
347147c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
34729566063dSJacob Faibussowitsch     PetscCall((*dm->ops->refinehierarchy)(dm,nlevels,dmf));
347347c6ae99SBarry Smith   } else if (dm->ops->refine) {
347447c6ae99SBarry Smith     PetscInt i;
347547c6ae99SBarry Smith 
34769566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]));
347747c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
34789566063dSJacob Faibussowitsch       PetscCall(DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]));
347947c6ae99SBarry Smith     }
3480ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
348147c6ae99SBarry Smith   PetscFunctionReturn(0);
348247c6ae99SBarry Smith }
348347c6ae99SBarry Smith 
348447c6ae99SBarry Smith /*@C
348547c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
348647c6ae99SBarry Smith 
3487d083f849SBarry Smith     Collective on dm
348847c6ae99SBarry Smith 
3489d8d19677SJose E. Roman     Input Parameters:
349047c6ae99SBarry Smith +   dm - the DM object
349147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
349247c6ae99SBarry Smith 
349347c6ae99SBarry Smith     Output Parameter:
349447c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
349547c6ae99SBarry Smith 
349647c6ae99SBarry Smith     Level: developer
349747c6ae99SBarry Smith 
3498db781477SPatrick Sanan .seealso `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
349947c6ae99SBarry Smith 
350047c6ae99SBarry Smith @*/
35017087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
350247c6ae99SBarry Smith {
350347c6ae99SBarry Smith   PetscFunctionBegin;
3504171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35057a8be351SBarry Smith   PetscCheck(nlevels >= 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
350647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
350747c6ae99SBarry Smith   PetscValidPointer(dmc,3);
350847c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
35099566063dSJacob Faibussowitsch     PetscCall((*dm->ops->coarsenhierarchy)(dm, nlevels, dmc));
351047c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
351147c6ae99SBarry Smith     PetscInt i;
351247c6ae99SBarry Smith 
35139566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]));
351447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
35159566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]));
351647c6ae99SBarry Smith     }
3517ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
351847c6ae99SBarry Smith   PetscFunctionReturn(0);
351947c6ae99SBarry Smith }
352047c6ae99SBarry Smith 
35211a266240SBarry Smith /*@C
35221a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
35231a266240SBarry Smith 
35241a266240SBarry Smith     Not Collective
35251a266240SBarry Smith 
35261a266240SBarry Smith     Input Parameters:
35271a266240SBarry Smith +   dm - the DM object
35281a266240SBarry Smith -   destroy - the destroy function
35291a266240SBarry Smith 
35301a266240SBarry Smith     Level: intermediate
35311a266240SBarry Smith 
3532db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
35331a266240SBarry Smith 
3534f07f9ceaSJed Brown @*/
35351a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
35361a266240SBarry Smith {
35371a266240SBarry Smith   PetscFunctionBegin;
3538171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35391a266240SBarry Smith   dm->ctxdestroy = destroy;
35401a266240SBarry Smith   PetscFunctionReturn(0);
35411a266240SBarry Smith }
35421a266240SBarry Smith 
3543b07ff414SBarry Smith /*@
35441b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
354547c6ae99SBarry Smith 
354647c6ae99SBarry Smith     Not Collective
354747c6ae99SBarry Smith 
354847c6ae99SBarry Smith     Input Parameters:
354947c6ae99SBarry Smith +   dm - the DM object
355047c6ae99SBarry Smith -   ctx - the user context
355147c6ae99SBarry Smith 
355247c6ae99SBarry Smith     Level: intermediate
355347c6ae99SBarry Smith 
3554db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
355547c6ae99SBarry Smith 
355647c6ae99SBarry Smith @*/
35571b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
355847c6ae99SBarry Smith {
355947c6ae99SBarry Smith   PetscFunctionBegin;
3560171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
356147c6ae99SBarry Smith   dm->ctx = ctx;
356247c6ae99SBarry Smith   PetscFunctionReturn(0);
356347c6ae99SBarry Smith }
356447c6ae99SBarry Smith 
356547c6ae99SBarry Smith /*@
35661b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
356747c6ae99SBarry Smith 
356847c6ae99SBarry Smith     Not Collective
356947c6ae99SBarry Smith 
357047c6ae99SBarry Smith     Input Parameter:
357147c6ae99SBarry Smith .   dm - the DM object
357247c6ae99SBarry Smith 
357347c6ae99SBarry Smith     Output Parameter:
357447c6ae99SBarry Smith .   ctx - the user context
357547c6ae99SBarry Smith 
357647c6ae99SBarry Smith     Level: intermediate
357747c6ae99SBarry Smith 
3578db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
357947c6ae99SBarry Smith 
358047c6ae99SBarry Smith @*/
35811b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
358247c6ae99SBarry Smith {
358347c6ae99SBarry Smith   PetscFunctionBegin;
3584171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35851b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
358647c6ae99SBarry Smith   PetscFunctionReturn(0);
358747c6ae99SBarry Smith }
358847c6ae99SBarry Smith 
358908da532bSDmitry Karpeev /*@C
3590df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
359108da532bSDmitry Karpeev 
3592d083f849SBarry Smith     Logically Collective on dm
359308da532bSDmitry Karpeev 
3594d8d19677SJose E. Roman     Input Parameters:
359508da532bSDmitry Karpeev +   dm - the DM object
35960298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
359708da532bSDmitry Karpeev 
359808da532bSDmitry Karpeev     Level: intermediate
359908da532bSDmitry Karpeev 
3600db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3601db781477SPatrick Sanan          `DMSetJacobian()`
360208da532bSDmitry Karpeev 
360308da532bSDmitry Karpeev @*/
360408da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
360508da532bSDmitry Karpeev {
360608da532bSDmitry Karpeev   PetscFunctionBegin;
36075a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
360808da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
360908da532bSDmitry Karpeev   PetscFunctionReturn(0);
361008da532bSDmitry Karpeev }
361108da532bSDmitry Karpeev 
361208da532bSDmitry Karpeev /*@
361308da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
361408da532bSDmitry Karpeev 
361508da532bSDmitry Karpeev     Not Collective
361608da532bSDmitry Karpeev 
361708da532bSDmitry Karpeev     Input Parameter:
361808da532bSDmitry Karpeev .   dm - the DM object to destroy
361908da532bSDmitry Karpeev 
362008da532bSDmitry Karpeev     Output Parameter:
362108da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
362208da532bSDmitry Karpeev 
362308da532bSDmitry Karpeev     Level: developer
362408da532bSDmitry Karpeev 
3625db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
362608da532bSDmitry Karpeev 
362708da532bSDmitry Karpeev @*/
362808da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
362908da532bSDmitry Karpeev {
363008da532bSDmitry Karpeev   PetscFunctionBegin;
36315a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3632534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
363308da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
363408da532bSDmitry Karpeev   PetscFunctionReturn(0);
363508da532bSDmitry Karpeev }
363608da532bSDmitry Karpeev 
363708da532bSDmitry Karpeev /*@C
363808da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
363908da532bSDmitry Karpeev 
3640d083f849SBarry Smith     Logically Collective on dm
364108da532bSDmitry Karpeev 
3642f899ff85SJose E. Roman     Input Parameter:
3643907376e6SBarry Smith .   dm - the DM object
364408da532bSDmitry Karpeev 
364508da532bSDmitry Karpeev     Output parameters:
364608da532bSDmitry Karpeev +   xl - lower bound
364708da532bSDmitry Karpeev -   xu - upper bound
364808da532bSDmitry Karpeev 
3649907376e6SBarry Smith     Level: advanced
3650907376e6SBarry Smith 
365195452b02SPatrick Sanan     Notes:
365295452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
365308da532bSDmitry Karpeev 
3654db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
365508da532bSDmitry Karpeev 
365608da532bSDmitry Karpeev @*/
365708da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
365808da532bSDmitry Karpeev {
365908da532bSDmitry Karpeev   PetscFunctionBegin;
36605a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
366108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
36625a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
36637a8be351SBarry Smith   PetscCheck(dm->ops->computevariablebounds,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
36649566063dSJacob Faibussowitsch   PetscCall((*dm->ops->computevariablebounds)(dm, xl,xu));
366508da532bSDmitry Karpeev   PetscFunctionReturn(0);
366608da532bSDmitry Karpeev }
366708da532bSDmitry Karpeev 
3668b0ae01b7SPeter Brune /*@
3669b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3670b0ae01b7SPeter Brune 
3671b0ae01b7SPeter Brune     Not Collective
3672b0ae01b7SPeter Brune 
3673b0ae01b7SPeter Brune     Input Parameter:
3674b0ae01b7SPeter Brune .   dm - the DM object
3675b0ae01b7SPeter Brune 
3676b0ae01b7SPeter Brune     Output Parameter:
3677b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3678b0ae01b7SPeter Brune 
3679b0ae01b7SPeter Brune     Level: developer
3680b0ae01b7SPeter Brune 
3681db781477SPatrick Sanan .seealso `DMCreateColoring()`
3682b0ae01b7SPeter Brune 
3683b0ae01b7SPeter Brune @*/
3684b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3685b0ae01b7SPeter Brune {
3686b0ae01b7SPeter Brune   PetscFunctionBegin;
36875a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3688534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3689b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3690b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3691b0ae01b7SPeter Brune }
3692b0ae01b7SPeter Brune 
36933ad4599aSBarry Smith /*@
36943ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
36953ad4599aSBarry Smith 
36963ad4599aSBarry Smith     Not Collective
36973ad4599aSBarry Smith 
36983ad4599aSBarry Smith     Input Parameter:
36993ad4599aSBarry Smith .   dm - the DM object
37003ad4599aSBarry Smith 
37013ad4599aSBarry Smith     Output Parameter:
37023ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
37033ad4599aSBarry Smith 
37043ad4599aSBarry Smith     Level: developer
37053ad4599aSBarry Smith 
3706db781477SPatrick Sanan .seealso `DMCreateRestriction()`
37073ad4599aSBarry Smith 
37083ad4599aSBarry Smith @*/
37093ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
37103ad4599aSBarry Smith {
37113ad4599aSBarry Smith   PetscFunctionBegin;
37125a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3713534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37143ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
37153ad4599aSBarry Smith   PetscFunctionReturn(0);
37163ad4599aSBarry Smith }
37173ad4599aSBarry Smith 
3718a7058e45SLawrence Mitchell /*@
3719a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3720a7058e45SLawrence Mitchell 
3721a7058e45SLawrence Mitchell     Not Collective
3722a7058e45SLawrence Mitchell 
3723a7058e45SLawrence Mitchell     Input Parameter:
3724a7058e45SLawrence Mitchell .   dm - the DM object
3725a7058e45SLawrence Mitchell 
3726a7058e45SLawrence Mitchell     Output Parameter:
3727a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3728a7058e45SLawrence Mitchell 
3729a7058e45SLawrence Mitchell     Level: developer
3730a7058e45SLawrence Mitchell 
3731db781477SPatrick Sanan .seealso `DMCreateInjection()`
3732a7058e45SLawrence Mitchell 
3733a7058e45SLawrence Mitchell @*/
3734a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3735a7058e45SLawrence Mitchell {
3736a7058e45SLawrence Mitchell   PetscFunctionBegin;
37375a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3738534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37395a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
37409566063dSJacob Faibussowitsch     PetscCall((*dm->ops->hascreateinjection)(dm,flg));
37415a84ad33SLisandro Dalcin   } else {
37425a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
37435a84ad33SLisandro Dalcin   }
3744a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3745a7058e45SLawrence Mitchell }
3746a7058e45SLawrence Mitchell 
37470298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3748264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3749264ace61SBarry Smith 
3750264ace61SBarry Smith /*@C
3751264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3752264ace61SBarry Smith 
3753d083f849SBarry Smith   Collective on dm
3754264ace61SBarry Smith 
3755264ace61SBarry Smith   Input Parameters:
3756264ace61SBarry Smith + dm     - The DM object
3757264ace61SBarry Smith - method - The name of the DM type
3758264ace61SBarry Smith 
3759264ace61SBarry Smith   Options Database Key:
3760264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3761264ace61SBarry Smith 
3762264ace61SBarry Smith   Notes:
3763e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3764264ace61SBarry Smith 
3765264ace61SBarry Smith   Level: intermediate
3766264ace61SBarry Smith 
3767db781477SPatrick Sanan .seealso: `DMGetType()`, `DMCreate()`
3768264ace61SBarry Smith @*/
376919fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3770264ace61SBarry Smith {
3771264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3772264ace61SBarry Smith   PetscBool      match;
3773264ace61SBarry Smith 
3774264ace61SBarry Smith   PetscFunctionBegin;
3775264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
37769566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) dm, method, &match));
3777264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3778264ace61SBarry Smith 
37799566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
37809566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList,method,&r));
37817a8be351SBarry Smith   PetscCheck(r,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3782264ace61SBarry Smith 
37831baa6e33SBarry Smith   if (dm->ops->destroy) PetscCall((*dm->ops->destroy)(dm));
37849566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops,sizeof(*dm->ops)));
37859566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm,method));
37869566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
3787264ace61SBarry Smith   PetscFunctionReturn(0);
3788264ace61SBarry Smith }
3789264ace61SBarry Smith 
3790264ace61SBarry Smith /*@C
3791264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3792264ace61SBarry Smith 
3793264ace61SBarry Smith   Not Collective
3794264ace61SBarry Smith 
3795264ace61SBarry Smith   Input Parameter:
3796264ace61SBarry Smith . dm  - The DM
3797264ace61SBarry Smith 
3798264ace61SBarry Smith   Output Parameter:
3799264ace61SBarry Smith . type - The DM type name
3800264ace61SBarry Smith 
3801264ace61SBarry Smith   Level: intermediate
3802264ace61SBarry Smith 
3803db781477SPatrick Sanan .seealso: `DMSetType()`, `DMCreate()`
3804264ace61SBarry Smith @*/
380519fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3806264ace61SBarry Smith {
3807264ace61SBarry Smith   PetscFunctionBegin;
3808264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3809c959eef4SJed Brown   PetscValidPointer(type,2);
38109566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3811264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3812264ace61SBarry Smith   PetscFunctionReturn(0);
3813264ace61SBarry Smith }
3814264ace61SBarry Smith 
381567a56275SMatthew G Knepley /*@C
381667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
381767a56275SMatthew G Knepley 
3818d083f849SBarry Smith   Collective on dm
381967a56275SMatthew G Knepley 
382067a56275SMatthew G Knepley   Input Parameters:
382167a56275SMatthew G Knepley + dm - the DM
382267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
382367a56275SMatthew G Knepley 
382467a56275SMatthew G Knepley   Output Parameter:
382567a56275SMatthew G Knepley . M - pointer to new DM
382667a56275SMatthew G Knepley 
382767a56275SMatthew G Knepley   Notes:
382867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
382967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
383067a56275SMatthew G Knepley   of the input DM.
383167a56275SMatthew G Knepley 
383267a56275SMatthew G Knepley   Level: intermediate
383367a56275SMatthew G Knepley 
3834db781477SPatrick Sanan .seealso: `DMCreate()`
383567a56275SMatthew G Knepley @*/
383619fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
383767a56275SMatthew G Knepley {
383867a56275SMatthew G Knepley   DM             B;
383967a56275SMatthew G Knepley   char           convname[256];
3840c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
384167a56275SMatthew G Knepley 
384267a56275SMatthew G Knepley   PetscFunctionBegin;
384367a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
384467a56275SMatthew G Knepley   PetscValidType(dm,1);
384567a56275SMatthew G Knepley   PetscValidPointer(M,3);
38469566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype));
38479566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3848c067b6caSMatthew G. Knepley   if (sametype) {
3849c067b6caSMatthew G. Knepley     *M   = dm;
38509566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) dm));
3851c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3852c067b6caSMatthew G. Knepley   } else {
38530298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
385467a56275SMatthew G Knepley 
385567a56275SMatthew G Knepley     /*
385667a56275SMatthew G Knepley        Order of precedence:
385767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
385867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
385967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
386067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
386167a56275SMatthew G Knepley        5) Use a really basic converter.
386267a56275SMatthew G Knepley     */
386367a56275SMatthew G Knepley 
386467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
38659566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname,"DMConvert_",sizeof(convname)));
38669566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname)));
38679566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_",sizeof(convname)));
38689566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,newtype,sizeof(convname)));
38699566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_C",sizeof(convname)));
38709566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm,convname,&conv));
387167a56275SMatthew G Knepley     if (conv) goto foundconv;
387267a56275SMatthew G Knepley 
387367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
38749566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
38759566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
38769566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname,"DMConvert_",sizeof(convname)));
38779566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname)));
38789566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_",sizeof(convname)));
38799566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,newtype,sizeof(convname)));
38809566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_C",sizeof(convname)));
38819566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B,convname,&conv));
388267a56275SMatthew G Knepley     if (conv) {
38839566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
388467a56275SMatthew G Knepley       goto foundconv;
388567a56275SMatthew G Knepley     }
388667a56275SMatthew G Knepley 
388767a56275SMatthew G Knepley #if 0
388867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
388967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
38909566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
389167a56275SMatthew G Knepley     if (conv) goto foundconv;
389267a56275SMatthew G Knepley 
389367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
389467a56275SMatthew G Knepley     if (dm->ops->convert) {
389567a56275SMatthew G Knepley       conv = dm->ops->convert;
389667a56275SMatthew G Knepley     }
389767a56275SMatthew G Knepley     if (conv) goto foundconv;
389867a56275SMatthew G Knepley #endif
389967a56275SMatthew G Knepley 
390067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
390198921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
390267a56275SMatthew G Knepley 
390367a56275SMatthew G Knepley foundconv:
39049566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert,dm,0,0,0));
39059566063dSJacob Faibussowitsch     PetscCall((*conv)(dm,newtype,M));
390612fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
390790b157c4SStefano Zampini     {
390812fa691eSMatthew G. Knepley       const PetscReal *maxCell, *L;
3909*6858538eSMatthew G. Knepley 
3910*6858538eSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &L));
3911*6858538eSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M,  maxCell,  L));
3912c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
39139566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
39149566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype,(char**)&(*M)->vectype));
39159566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
39169566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype,(char**)&(*M)->mattype));
391712fa691eSMatthew G. Knepley     }
39189566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert,dm,0,0,0));
391967a56275SMatthew G Knepley   }
39209566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject) *M));
392167a56275SMatthew G Knepley   PetscFunctionReturn(0);
392267a56275SMatthew G Knepley }
3923264ace61SBarry Smith 
3924264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3925264ace61SBarry Smith 
3926264ace61SBarry Smith /*@C
39271c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
39281c84c290SBarry Smith 
39291c84c290SBarry Smith   Not Collective
39301c84c290SBarry Smith 
39311c84c290SBarry Smith   Input Parameters:
39321c84c290SBarry Smith + name        - The name of a new user-defined creation routine
39331c84c290SBarry Smith - create_func - The creation routine itself
39341c84c290SBarry Smith 
39351c84c290SBarry Smith   Notes:
39361c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
39371c84c290SBarry Smith 
39381c84c290SBarry Smith   Sample usage:
39391c84c290SBarry Smith .vb
3940bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
39411c84c290SBarry Smith .ve
39421c84c290SBarry Smith 
39431c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
39441c84c290SBarry Smith .vb
39451c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
39461c84c290SBarry Smith     DMSetType(DM,"my_da");
39471c84c290SBarry Smith .ve
39481c84c290SBarry Smith    or at runtime via the option
39491c84c290SBarry Smith .vb
39501c84c290SBarry Smith     -da_type my_da
39511c84c290SBarry Smith .ve
3952264ace61SBarry Smith 
3953264ace61SBarry Smith   Level: advanced
39541c84c290SBarry Smith 
3955db781477SPatrick Sanan .seealso: `DMRegisterAll()`, `DMRegisterDestroy()`
39561c84c290SBarry Smith 
3957264ace61SBarry Smith @*/
3958bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3959264ace61SBarry Smith {
3960264ace61SBarry Smith   PetscFunctionBegin;
39619566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
39629566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList,sname,function));
3963264ace61SBarry Smith   PetscFunctionReturn(0);
3964264ace61SBarry Smith }
3965264ace61SBarry Smith 
3966b859378eSBarry Smith /*@C
396755849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3968b859378eSBarry Smith 
3969d083f849SBarry Smith   Collective on viewer
3970b859378eSBarry Smith 
3971b859378eSBarry Smith   Input Parameters:
3972b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3973b859378eSBarry Smith            some related function before a call to DMLoad().
3974b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3975b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3976b859378eSBarry Smith 
3977b859378eSBarry Smith    Level: intermediate
3978b859378eSBarry Smith 
3979b859378eSBarry Smith   Notes:
398055849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
3981b859378eSBarry Smith 
3982cd7e8a5eSksagiyam   Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
3983cd7e8a5eSksagiyam   meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
3984cd7e8a5eSksagiyam   before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
3985cd7e8a5eSksagiyam 
3986b859378eSBarry Smith   Notes for advanced users:
3987b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3988b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3989b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3990b859378eSBarry Smith   format is
3991b859378eSBarry Smith .vb
3992b859378eSBarry Smith      has not yet been determined
3993b859378eSBarry Smith .ve
3994b859378eSBarry Smith 
3995db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
3996b859378eSBarry Smith @*/
3997b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3998b859378eSBarry Smith {
39999331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
4000b859378eSBarry Smith 
4001b859378eSBarry Smith   PetscFunctionBegin;
4002b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
4003b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
40049566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
40059566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary));
40069566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5));
40079566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load,viewer,0,0,0));
40089331c7a4SMatthew G. Knepley   if (isbinary) {
40099331c7a4SMatthew G. Knepley     PetscInt classid;
40109331c7a4SMatthew G. Knepley     char     type[256];
4011b859378eSBarry Smith 
40129566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT));
40137a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID,PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
40149566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR));
40159566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
40169566063dSJacob Faibussowitsch     if (newdm->ops->load) PetscCall((*newdm->ops->load)(newdm,viewer));
40179331c7a4SMatthew G. Knepley   } else if (ishdf5) {
40189566063dSJacob Faibussowitsch     if (newdm->ops->load) PetscCall((*newdm->ops->load)(newdm,viewer));
40199331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
40209566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load,viewer,0,0,0));
4021b859378eSBarry Smith   PetscFunctionReturn(0);
4022b859378eSBarry Smith }
4023b859378eSBarry Smith 
40247da65231SMatthew G Knepley /******************************** FEM Support **********************************/
40257da65231SMatthew G Knepley 
4026a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4027a6dfd86eSKarl Rupp {
40281d47ebbbSSatish Balay   PetscInt       f;
40291b30c384SMatthew G Knepley 
40307da65231SMatthew G Knepley   PetscFunctionBegin;
403163a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
40321d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
40339566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
40347da65231SMatthew G Knepley   }
40357da65231SMatthew G Knepley   PetscFunctionReturn(0);
40367da65231SMatthew G Knepley }
40377da65231SMatthew G Knepley 
4038a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4039a6dfd86eSKarl Rupp {
40401b30c384SMatthew G Knepley   PetscInt       f, g;
40417da65231SMatthew G Knepley 
40427da65231SMatthew G Knepley   PetscFunctionBegin;
404363a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
40441d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
40459566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
40461d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
404763a3b9bcSJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f*cols+g])));
40487da65231SMatthew G Knepley     }
40499566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
40507da65231SMatthew G Knepley   }
40517da65231SMatthew G Knepley   PetscFunctionReturn(0);
40527da65231SMatthew G Knepley }
4053e7c4fc90SDmitry Karpeev 
40546113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4055e759306cSMatthew G. Knepley {
40560c5b8624SToby Isaac   PetscInt          localSize, bs;
40570c5b8624SToby Isaac   PetscMPIInt       size;
40580c5b8624SToby Isaac   Vec               x, xglob;
40590c5b8624SToby Isaac   const PetscScalar *xarray;
4060e759306cSMatthew G. Knepley 
4061e759306cSMatthew G. Knepley   PetscFunctionBegin;
40629566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size));
40639566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
40649566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
40659566063dSJacob Faibussowitsch   PetscCall(VecChop(x, tol));
40669566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name));
40670c5b8624SToby Isaac   if (size > 1) {
40689566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x,&localSize));
40699566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x,&xarray));
40709566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x,&bs));
40719566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob));
40720c5b8624SToby Isaac   } else {
40730c5b8624SToby Isaac     xglob = x;
40740c5b8624SToby Isaac   }
40759566063dSJacob Faibussowitsch   PetscCall(VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm))));
40760c5b8624SToby Isaac   if (size > 1) {
40779566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
40789566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x,&xarray));
40790c5b8624SToby Isaac   }
40809566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
4081e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4082e759306cSMatthew G. Knepley }
4083e759306cSMatthew G. Knepley 
408488ed4aceSMatthew G Knepley /*@
40851bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4086061576a5SJed Brown 
4087061576a5SJed Brown   Input Parameter:
4088061576a5SJed Brown . dm - The DM
4089061576a5SJed Brown 
4090061576a5SJed Brown   Output Parameter:
4091061576a5SJed Brown . section - The PetscSection
4092061576a5SJed Brown 
4093061576a5SJed Brown   Options Database Keys:
4094061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4095061576a5SJed Brown 
4096061576a5SJed Brown   Level: advanced
4097061576a5SJed Brown 
4098061576a5SJed Brown   Notes:
4099061576a5SJed Brown   Use DMGetLocalSection() in new code.
4100061576a5SJed Brown 
4101061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4102061576a5SJed Brown 
4103db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4104061576a5SJed Brown @*/
4105061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4106061576a5SJed Brown {
4107061576a5SJed Brown   PetscFunctionBegin;
41089566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm,section));
4109061576a5SJed Brown   PetscFunctionReturn(0);
4110061576a5SJed Brown }
4111061576a5SJed Brown 
4112061576a5SJed Brown /*@
4113061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
411488ed4aceSMatthew G Knepley 
411588ed4aceSMatthew G Knepley   Input Parameter:
411688ed4aceSMatthew G Knepley . dm - The DM
411788ed4aceSMatthew G Knepley 
411888ed4aceSMatthew G Knepley   Output Parameter:
411988ed4aceSMatthew G Knepley . section - The PetscSection
412088ed4aceSMatthew G Knepley 
4121e5893cccSMatthew G. Knepley   Options Database Keys:
4122e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4123e5893cccSMatthew G. Knepley 
412488ed4aceSMatthew G Knepley   Level: intermediate
412588ed4aceSMatthew G Knepley 
412688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
412788ed4aceSMatthew G Knepley 
4128db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetGlobalSection()`
412988ed4aceSMatthew G Knepley @*/
4130061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
41310adebc6cSBarry Smith {
413288ed4aceSMatthew G Knepley   PetscFunctionBegin;
413388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
413488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
41351bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4136e5e52638SMatthew G. Knepley     PetscInt d;
4137e5e52638SMatthew G. Knepley 
413845480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
413945480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject) dm;
414045480ffeSMatthew G. Knepley       PetscViewer       viewer;
414145480ffeSMatthew G. Knepley       PetscViewerFormat format;
414245480ffeSMatthew G. Knepley       PetscBool         flg;
414345480ffeSMatthew G. Knepley 
41449566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
41459566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
414645480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
41479566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
41489566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
414945480ffeSMatthew G. Knepley       }
415045480ffeSMatthew G. Knepley       if (flg) {
41519566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
41529566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
41539566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
415445480ffeSMatthew G. Knepley       }
415545480ffeSMatthew G. Knepley     }
41569566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createlocalsection)(dm));
41579566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view"));
41582f0f8703SMatthew G. Knepley   }
41591bb6d2a8SBarry Smith   *section = dm->localSection;
416088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
416188ed4aceSMatthew G Knepley }
416288ed4aceSMatthew G Knepley 
416388ed4aceSMatthew G Knepley /*@
41641bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4165061576a5SJed Brown 
4166061576a5SJed Brown   Input Parameters:
4167061576a5SJed Brown + dm - The DM
4168061576a5SJed Brown - section - The PetscSection
4169061576a5SJed Brown 
4170061576a5SJed Brown   Level: advanced
4171061576a5SJed Brown 
4172061576a5SJed Brown   Notes:
4173061576a5SJed Brown   Use DMSetLocalSection() in new code.
4174061576a5SJed Brown 
4175061576a5SJed Brown   Any existing Section will be destroyed
4176061576a5SJed Brown 
4177db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4178061576a5SJed Brown @*/
4179061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4180061576a5SJed Brown {
4181061576a5SJed Brown   PetscFunctionBegin;
41829566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm,section));
4183061576a5SJed Brown   PetscFunctionReturn(0);
4184061576a5SJed Brown }
4185061576a5SJed Brown 
4186061576a5SJed Brown /*@
4187061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
418888ed4aceSMatthew G Knepley 
418988ed4aceSMatthew G Knepley   Input Parameters:
419088ed4aceSMatthew G Knepley + dm - The DM
419188ed4aceSMatthew G Knepley - section - The PetscSection
419288ed4aceSMatthew G Knepley 
419388ed4aceSMatthew G Knepley   Level: intermediate
419488ed4aceSMatthew G Knepley 
419588ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
419688ed4aceSMatthew G Knepley 
4197db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetGlobalSection()`
419888ed4aceSMatthew G Knepley @*/
4199061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
42000adebc6cSBarry Smith {
4201c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4202af122d2aSMatthew G Knepley   PetscInt       f;
420388ed4aceSMatthew G Knepley 
420488ed4aceSMatthew G Knepley   PetscFunctionBegin;
420588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4206b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42079566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
42089566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
42091bb6d2a8SBarry Smith   dm->localSection = section;
42109566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4211af122d2aSMatthew G Knepley   if (numFields) {
42129566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4213af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
42140f21e855SMatthew G. Knepley       PetscObject disc;
4215af122d2aSMatthew G Knepley       const char *name;
4216af122d2aSMatthew G Knepley 
42179566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
42189566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
42199566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4220af122d2aSMatthew G Knepley     }
4221af122d2aSMatthew G Knepley   }
4222e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
42239566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
422488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
422588ed4aceSMatthew G Knepley }
422688ed4aceSMatthew G Knepley 
42279435951eSToby Isaac /*@
4228b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
42299435951eSToby Isaac 
4230e228b242SToby Isaac   not collective
4231e228b242SToby Isaac 
42329435951eSToby Isaac   Input Parameter:
42339435951eSToby Isaac . dm - The DM
42349435951eSToby Isaac 
4235d8d19677SJose E. Roman   Output Parameters:
42369435951eSToby 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.
423779769bd5SJed 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.
423879769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
42399435951eSToby Isaac 
42409435951eSToby Isaac   Level: advanced
42419435951eSToby Isaac 
424279769bd5SJed Brown   Note: This gets borrowed references, so the user should not destroy the PetscSection, Mat, or Vec.
42439435951eSToby Isaac 
4244db781477SPatrick Sanan .seealso: `DMSetDefaultConstraints()`
42459435951eSToby Isaac @*/
424679769bd5SJed Brown PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
42479435951eSToby Isaac {
42489435951eSToby Isaac   PetscFunctionBegin;
42499435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42509566063dSJacob Faibussowitsch   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscCall((*dm->ops->createdefaultconstraints)(dm));
42513b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
42523b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
425379769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
42549435951eSToby Isaac   PetscFunctionReturn(0);
42559435951eSToby Isaac }
42569435951eSToby Isaac 
42579435951eSToby Isaac /*@
4258b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
42599435951eSToby Isaac 
426079769bd5SJed 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().
42619435951eSToby Isaac 
426279769bd5SJed 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.
42639435951eSToby Isaac 
4264e228b242SToby Isaac   collective on dm
4265e228b242SToby Isaac 
42669435951eSToby Isaac   Input Parameters:
42679435951eSToby Isaac + dm - The DM
426879769bd5SJed 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).
426979769bd5SJed 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).
427079769bd5SJed 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).
42719435951eSToby Isaac 
42729435951eSToby Isaac   Level: advanced
42739435951eSToby Isaac 
427479769bd5SJed Brown   Note: This increments the references of the PetscSection, Mat, and Vec, so they user can destroy them.
42759435951eSToby Isaac 
4276db781477SPatrick Sanan .seealso: `DMGetDefaultConstraints()`
42779435951eSToby Isaac @*/
427879769bd5SJed Brown PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
42799435951eSToby Isaac {
4280e228b242SToby Isaac   PetscMPIInt result;
42819435951eSToby Isaac 
42829435951eSToby Isaac   PetscFunctionBegin;
42839435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4284e228b242SToby Isaac   if (section) {
4285e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42869566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result));
42877a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4288e228b242SToby Isaac   }
4289e228b242SToby Isaac   if (mat) {
4290e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
42919566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result));
42927a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4293e228b242SToby Isaac   }
429479769bd5SJed Brown   if (bias) {
429579769bd5SJed Brown     PetscValidHeaderSpecific(bias,VEC_CLASSID,4);
42969566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)bias),&result));
429779769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint bias must have local communicator");
429879769bd5SJed Brown   }
42999566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43009566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
43013b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
43029566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
43039566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
43043b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
43059566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
43069566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
430779769bd5SJed Brown   dm->defaultConstraint.bias = bias;
43089435951eSToby Isaac   PetscFunctionReturn(0);
43099435951eSToby Isaac }
43109435951eSToby Isaac 
4311497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4312507e4973SMatthew G. Knepley /*
4313507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4314507e4973SMatthew G. Knepley 
4315507e4973SMatthew G. Knepley   Input Parameters:
4316507e4973SMatthew G. Knepley + dm - The DM
4317507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4318507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4319507e4973SMatthew G. Knepley 
4320507e4973SMatthew G. Knepley   Level: intermediate
4321507e4973SMatthew G. Knepley 
4322db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`
4323507e4973SMatthew G. Knepley */
4324f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4325507e4973SMatthew G. Knepley {
4326507e4973SMatthew G. Knepley   MPI_Comm        comm;
4327507e4973SMatthew G. Knepley   PetscLayout     layout;
4328507e4973SMatthew G. Knepley   const PetscInt *ranges;
4329507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4330507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4331507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4332507e4973SMatthew G. Knepley 
4333507e4973SMatthew G. Knepley   PetscFunctionBegin;
43349566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
4335507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43369566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
43379566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
43389566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
43399566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
43409566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
43419566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
43429566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
43439566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
43449566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4345507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4346f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4347507e4973SMatthew G. Knepley 
43489566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
43499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
43509566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
43519566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
43529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
43539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4354507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
435563a3b9bcSJacob 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;}
435663a3b9bcSJacob 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;}
4357507e4973SMatthew G. Knepley     if (gdof < 0) {
4358507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4359507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4360507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4361507e4973SMatthew G. Knepley 
43629566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset,size+1,ranges,&r));
4363507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
436463a3b9bcSJacob 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;}
4365507e4973SMatthew G. Knepley       }
4366507e4973SMatthew G. Knepley     }
4367507e4973SMatthew G. Knepley   }
43689566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
43699566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
43701c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4371507e4973SMatthew G. Knepley   if (!gvalid) {
43729566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4373507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4374507e4973SMatthew G. Knepley   }
4375507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4376507e4973SMatthew G. Knepley }
4377f741bcd2SMatthew G. Knepley #endif
4378507e4973SMatthew G. Knepley 
437988ed4aceSMatthew G Knepley /*@
4380e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
438188ed4aceSMatthew G Knepley 
4382d083f849SBarry Smith   Collective on dm
43838b1ab98fSJed Brown 
438488ed4aceSMatthew G Knepley   Input Parameter:
438588ed4aceSMatthew G Knepley . dm - The DM
438688ed4aceSMatthew G Knepley 
438788ed4aceSMatthew G Knepley   Output Parameter:
438888ed4aceSMatthew G Knepley . section - The PetscSection
438988ed4aceSMatthew G Knepley 
439088ed4aceSMatthew G Knepley   Level: intermediate
439188ed4aceSMatthew G Knepley 
439288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
439388ed4aceSMatthew G Knepley 
4394db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`
439588ed4aceSMatthew G Knepley @*/
4396e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
43970adebc6cSBarry Smith {
439888ed4aceSMatthew G Knepley   PetscFunctionBegin;
439988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
440088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
44011bb6d2a8SBarry Smith   if (!dm->globalSection) {
4402fd59a867SMatthew G. Knepley     PetscSection s;
4403fd59a867SMatthew G. Knepley 
44049566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
44057a8be351SBarry Smith     PetscCheck(s,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
44067a8be351SBarry Smith     PetscCheck(dm->sf,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
44079566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
44089566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
44099566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
44109566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
441188ed4aceSMatthew G Knepley   }
44121bb6d2a8SBarry Smith   *section = dm->globalSection;
441388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
441488ed4aceSMatthew G Knepley }
441588ed4aceSMatthew G Knepley 
4416b21d0597SMatthew G Knepley /*@
4417e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4418b21d0597SMatthew G Knepley 
4419b21d0597SMatthew G Knepley   Input Parameters:
4420b21d0597SMatthew G Knepley + dm - The DM
44215080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4422b21d0597SMatthew G Knepley 
4423b21d0597SMatthew G Knepley   Level: intermediate
4424b21d0597SMatthew G Knepley 
4425b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4426b21d0597SMatthew G Knepley 
4427db781477SPatrick Sanan .seealso: `DMGetGlobalSection()`, `DMSetLocalSection()`
4428b21d0597SMatthew G Knepley @*/
4429e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
44300adebc6cSBarry Smith {
4431b21d0597SMatthew G Knepley   PetscFunctionBegin;
4432b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44335080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
44349566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44359566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
44361bb6d2a8SBarry Smith   dm->globalSection = section;
4437497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
44389566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4439507e4973SMatthew G. Knepley #endif
4440b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4441b21d0597SMatthew G Knepley }
4442b21d0597SMatthew G Knepley 
444388ed4aceSMatthew G Knepley /*@
44441bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
444588ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
444688ed4aceSMatthew G Knepley 
444788ed4aceSMatthew G Knepley   Input Parameter:
444888ed4aceSMatthew G Knepley . dm - The DM
444988ed4aceSMatthew G Knepley 
445088ed4aceSMatthew G Knepley   Output Parameter:
445188ed4aceSMatthew G Knepley . sf - The PetscSF
445288ed4aceSMatthew G Knepley 
445388ed4aceSMatthew G Knepley   Level: intermediate
445488ed4aceSMatthew G Knepley 
445588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
445688ed4aceSMatthew G Knepley 
4457db781477SPatrick Sanan .seealso: `DMSetSectionSF()`, `DMCreateSectionSF()`
445888ed4aceSMatthew G Knepley @*/
44591bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
44600adebc6cSBarry Smith {
446188ed4aceSMatthew G Knepley   PetscInt       nroots;
446288ed4aceSMatthew G Knepley 
446388ed4aceSMatthew G Knepley   PetscFunctionBegin;
446488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
446588ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
44661bb6d2a8SBarry Smith   if (!dm->sectionSF) {
44679566063dSJacob Faibussowitsch     PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF));
446833907cc2SStefano Zampini   }
44699566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
447088ed4aceSMatthew G Knepley   if (nroots < 0) {
447188ed4aceSMatthew G Knepley     PetscSection section, gSection;
447288ed4aceSMatthew G Knepley 
44739566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
447431ea6d37SMatthew G Knepley     if (section) {
44759566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
44769566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
447731ea6d37SMatthew G Knepley     } else {
44780298fd71SBarry Smith       *sf = NULL;
447931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
448031ea6d37SMatthew G Knepley     }
448188ed4aceSMatthew G Knepley   }
44821bb6d2a8SBarry Smith   *sf = dm->sectionSF;
448388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
448488ed4aceSMatthew G Knepley }
448588ed4aceSMatthew G Knepley 
448688ed4aceSMatthew G Knepley /*@
44871bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
448888ed4aceSMatthew G Knepley 
448988ed4aceSMatthew G Knepley   Input Parameters:
449088ed4aceSMatthew G Knepley + dm - The DM
449188ed4aceSMatthew G Knepley - sf - The PetscSF
449288ed4aceSMatthew G Knepley 
449388ed4aceSMatthew G Knepley   Level: intermediate
449488ed4aceSMatthew G Knepley 
449588ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
449688ed4aceSMatthew G Knepley 
4497db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMCreateSectionSF()`
449888ed4aceSMatthew G Knepley @*/
44991bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
45000adebc6cSBarry Smith {
450188ed4aceSMatthew G Knepley   PetscFunctionBegin;
450288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4503b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
45049566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
45059566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
45061bb6d2a8SBarry Smith   dm->sectionSF = sf;
450788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
450888ed4aceSMatthew G Knepley }
450988ed4aceSMatthew G Knepley 
451088ed4aceSMatthew G Knepley /*@C
45111bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
451288ed4aceSMatthew G Knepley   describing the data layout.
451388ed4aceSMatthew G Knepley 
451488ed4aceSMatthew G Knepley   Input Parameters:
451588ed4aceSMatthew G Knepley + dm - The DM
451688ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
451788ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
451888ed4aceSMatthew G Knepley 
45191bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
452088ed4aceSMatthew G Knepley 
45211bb6d2a8SBarry Smith   Level: developer
45221bb6d2a8SBarry Smith 
45231bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
45241bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
45251bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
45261bb6d2a8SBarry Smith 
4527db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
452888ed4aceSMatthew G Knepley @*/
45291bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
453088ed4aceSMatthew G Knepley {
453188ed4aceSMatthew G Knepley   PetscFunctionBegin;
453288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45339566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
453488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
453588ed4aceSMatthew G Knepley }
4536af122d2aSMatthew G Knepley 
4537b21d0597SMatthew G Knepley /*@
4538b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4539b21d0597SMatthew G Knepley 
4540b21d0597SMatthew G Knepley   Input Parameter:
4541b21d0597SMatthew G Knepley . dm - The DM
4542b21d0597SMatthew G Knepley 
4543b21d0597SMatthew G Knepley   Output Parameter:
4544b21d0597SMatthew G Knepley . sf - The PetscSF
4545b21d0597SMatthew G Knepley 
4546b21d0597SMatthew G Knepley   Level: intermediate
4547b21d0597SMatthew G Knepley 
4548b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4549b21d0597SMatthew G Knepley 
4550db781477SPatrick Sanan .seealso: `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4551b21d0597SMatthew G Knepley @*/
45520adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
45530adebc6cSBarry Smith {
4554b21d0597SMatthew G Knepley   PetscFunctionBegin;
4555b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4556b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4557b21d0597SMatthew G Knepley   *sf = dm->sf;
4558b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4559b21d0597SMatthew G Knepley }
4560b21d0597SMatthew G Knepley 
4561057b4bcdSMatthew G Knepley /*@
4562057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4563057b4bcdSMatthew G Knepley 
4564057b4bcdSMatthew G Knepley   Input Parameters:
4565057b4bcdSMatthew G Knepley + dm - The DM
4566057b4bcdSMatthew G Knepley - sf - The PetscSF
4567057b4bcdSMatthew G Knepley 
4568057b4bcdSMatthew G Knepley   Level: intermediate
4569057b4bcdSMatthew G Knepley 
4570db781477SPatrick Sanan .seealso: `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4571057b4bcdSMatthew G Knepley @*/
45720adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
45730adebc6cSBarry Smith {
4574057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4575057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4576b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
45779566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
45789566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4579057b4bcdSMatthew G Knepley   dm->sf = sf;
4580057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4581057b4bcdSMatthew G Knepley }
4582057b4bcdSMatthew G Knepley 
45834f37162bSMatthew G. Knepley /*@
45844f37162bSMatthew G. Knepley   DMGetNaturalSF - Get the PetscSF encoding the map back to the original mesh ordering
45854f37162bSMatthew G. Knepley 
45864f37162bSMatthew G. Knepley   Input Parameter:
45874f37162bSMatthew G. Knepley . dm - The DM
45884f37162bSMatthew G. Knepley 
45894f37162bSMatthew G. Knepley   Output Parameter:
45904f37162bSMatthew G. Knepley . sf - The PetscSF
45914f37162bSMatthew G. Knepley 
45924f37162bSMatthew G. Knepley   Level: intermediate
45934f37162bSMatthew G. Knepley 
45944f37162bSMatthew G. Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
45954f37162bSMatthew G. Knepley 
4596db781477SPatrick Sanan .seealso: `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
45974f37162bSMatthew G. Knepley @*/
45984f37162bSMatthew G. Knepley PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
45994f37162bSMatthew G. Knepley {
46004f37162bSMatthew G. Knepley   PetscFunctionBegin;
46014f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46024f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
46034f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
46044f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
46054f37162bSMatthew G. Knepley }
46064f37162bSMatthew G. Knepley 
46074f37162bSMatthew G. Knepley /*@
46084f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
46094f37162bSMatthew G. Knepley 
46104f37162bSMatthew G. Knepley   Input Parameters:
46114f37162bSMatthew G. Knepley + dm - The DM
46124f37162bSMatthew G. Knepley - sf - The PetscSF
46134f37162bSMatthew G. Knepley 
46144f37162bSMatthew G. Knepley   Level: intermediate
46154f37162bSMatthew G. Knepley 
4616db781477SPatrick Sanan .seealso: `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
46174f37162bSMatthew G. Knepley @*/
46184f37162bSMatthew G. Knepley PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
46194f37162bSMatthew G. Knepley {
46204f37162bSMatthew G. Knepley   PetscFunctionBegin;
46214f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46224f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46239566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
46249566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
46254f37162bSMatthew G. Knepley   dm->sfNatural = sf;
46264f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
46274f37162bSMatthew G. Knepley }
46284f37162bSMatthew G. Knepley 
462934aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
463034aa8a36SMatthew G. Knepley {
463134aa8a36SMatthew G. Knepley   PetscClassId   id;
463234aa8a36SMatthew G. Knepley 
463334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
46349566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
463534aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
46369566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
463734aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
46389566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
463917c1d62eSMatthew G. Knepley   } else {
46409566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
464134aa8a36SMatthew G. Knepley   }
464234aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
464334aa8a36SMatthew G. Knepley }
464434aa8a36SMatthew G. Knepley 
464544a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
464644a7f3ddSMatthew G. Knepley {
464744a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
464844a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
464944a7f3ddSMatthew G. Knepley 
465044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
465144a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
46529566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
465344a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4654e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
46559566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
465644a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
465744a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
465844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
465944a7f3ddSMatthew G. Knepley }
466044a7f3ddSMatthew G. Knepley 
466144a7f3ddSMatthew G. Knepley /*@
466244a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
466344a7f3ddSMatthew G. Knepley 
4664d083f849SBarry Smith   Logically collective on dm
466544a7f3ddSMatthew G. Knepley 
466644a7f3ddSMatthew G. Knepley   Input Parameter:
466744a7f3ddSMatthew G. Knepley . dm - The DM
466844a7f3ddSMatthew G. Knepley 
466944a7f3ddSMatthew G. Knepley   Level: intermediate
467044a7f3ddSMatthew G. Knepley 
4671db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
467244a7f3ddSMatthew G. Knepley @*/
467344a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
467444a7f3ddSMatthew G. Knepley {
467544a7f3ddSMatthew G. Knepley   PetscInt       f;
467644a7f3ddSMatthew G. Knepley 
467744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
467844a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
467944a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
46809566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
46819566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
468244a7f3ddSMatthew G. Knepley   }
46839566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
468444a7f3ddSMatthew G. Knepley   dm->fields = NULL;
468544a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
468644a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
468744a7f3ddSMatthew G. Knepley }
468844a7f3ddSMatthew G. Knepley 
4689689b5837SMatthew G. Knepley /*@
4690689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4691689b5837SMatthew G. Knepley 
4692689b5837SMatthew G. Knepley   Not collective
4693689b5837SMatthew G. Knepley 
4694689b5837SMatthew G. Knepley   Input Parameter:
4695689b5837SMatthew G. Knepley . dm - The DM
4696689b5837SMatthew G. Knepley 
4697689b5837SMatthew G. Knepley   Output Parameter:
4698689b5837SMatthew G. Knepley . Nf - The number of fields
4699689b5837SMatthew G. Knepley 
4700689b5837SMatthew G. Knepley   Level: intermediate
4701689b5837SMatthew G. Knepley 
4702db781477SPatrick Sanan .seealso: `DMSetNumFields()`, `DMSetField()`
4703689b5837SMatthew G. Knepley @*/
47040f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
47050f21e855SMatthew G. Knepley {
47060f21e855SMatthew G. Knepley   PetscFunctionBegin;
47070f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4708534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
470944a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4710af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4711af122d2aSMatthew G Knepley }
4712af122d2aSMatthew G Knepley 
4713689b5837SMatthew G. Knepley /*@
4714689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4715689b5837SMatthew G. Knepley 
4716d083f849SBarry Smith   Logically collective on dm
4717689b5837SMatthew G. Knepley 
4718689b5837SMatthew G. Knepley   Input Parameters:
4719689b5837SMatthew G. Knepley + dm - The DM
4720689b5837SMatthew G. Knepley - Nf - The number of fields
4721689b5837SMatthew G. Knepley 
4722689b5837SMatthew G. Knepley   Level: intermediate
4723689b5837SMatthew G. Knepley 
4724db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetField()`
4725689b5837SMatthew G. Knepley @*/
4726af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4727af122d2aSMatthew G Knepley {
47280f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4729af122d2aSMatthew G Knepley 
4730af122d2aSMatthew G Knepley   PetscFunctionBegin;
4731af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47329566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
47330f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
47340f21e855SMatthew G. Knepley     PetscContainer obj;
47350f21e855SMatthew G. Knepley 
47369566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj));
47379566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject) obj));
47389566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4739af122d2aSMatthew G Knepley   }
4740af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4741af122d2aSMatthew G Knepley }
4742af122d2aSMatthew G Knepley 
4743c1929be8SMatthew G. Knepley /*@
4744c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4745c1929be8SMatthew G. Knepley 
4746c1929be8SMatthew G. Knepley   Not collective
4747c1929be8SMatthew G. Knepley 
4748c1929be8SMatthew G. Knepley   Input Parameters:
4749c1929be8SMatthew G. Knepley + dm - The DM
4750c1929be8SMatthew G. Knepley - f  - The field number
4751c1929be8SMatthew G. Knepley 
475244a7f3ddSMatthew G. Knepley   Output Parameters:
475344a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
475444a7f3ddSMatthew G. Knepley - field - The discretization object
4755c1929be8SMatthew G. Knepley 
475644a7f3ddSMatthew G. Knepley   Level: intermediate
4757c1929be8SMatthew G. Knepley 
4758db781477SPatrick Sanan .seealso: `DMAddField()`, `DMSetField()`
4759c1929be8SMatthew G. Knepley @*/
476044a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4761af122d2aSMatthew G Knepley {
4762af122d2aSMatthew G Knepley   PetscFunctionBegin;
4763af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4764064a246eSJacob Faibussowitsch   PetscValidPointer(field, 4);
47657a8be351SBarry 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);
476644a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
476744a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4768decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4769decb47aaSMatthew G. Knepley }
4770decb47aaSMatthew G. Knepley 
4771083401c6SMatthew G. Knepley /* Does not clear the DS */
4772083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4773083401c6SMatthew G. Knepley {
4774083401c6SMatthew G. Knepley   PetscFunctionBegin;
47759566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f+1));
47769566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
47779566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4778083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4779083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
47809566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
47819566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) field));
4782083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4783083401c6SMatthew G. Knepley }
4784083401c6SMatthew G. Knepley 
4785c1929be8SMatthew G. Knepley /*@
4786c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4787c1929be8SMatthew G. Knepley 
4788d083f849SBarry Smith   Logically collective on dm
4789c1929be8SMatthew G. Knepley 
4790c1929be8SMatthew G. Knepley   Input Parameters:
4791c1929be8SMatthew G. Knepley + dm    - The DM
4792c1929be8SMatthew G. Knepley . f     - The field number
479344a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4794c1929be8SMatthew G. Knepley - field - The discretization object
4795c1929be8SMatthew G. Knepley 
479644a7f3ddSMatthew G. Knepley   Level: intermediate
4797c1929be8SMatthew G. Knepley 
4798db781477SPatrick Sanan .seealso: `DMAddField()`, `DMGetField()`
4799c1929be8SMatthew G. Knepley @*/
480044a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4801decb47aaSMatthew G. Knepley {
4802decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4803decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4804e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
480544a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
48067a8be351SBarry Smith   PetscCheck(f >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
48079566063dSJacob Faibussowitsch   PetscCall(DMSetField_Internal(dm, f, label, field));
48089566063dSJacob Faibussowitsch   PetscCall(DMSetDefaultAdjacency_Private(dm, f, field));
48099566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
481044a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
481144a7f3ddSMatthew G. Knepley }
481244a7f3ddSMatthew G. Knepley 
481344a7f3ddSMatthew G. Knepley /*@
481444a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
481544a7f3ddSMatthew G. Knepley 
4816d083f849SBarry Smith   Logically collective on dm
481744a7f3ddSMatthew G. Knepley 
481844a7f3ddSMatthew G. Knepley   Input Parameters:
481944a7f3ddSMatthew G. Knepley + dm    - The DM
482044a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
482144a7f3ddSMatthew G. Knepley - field - The discretization object
482244a7f3ddSMatthew G. Knepley 
482344a7f3ddSMatthew G. Knepley   Level: intermediate
482444a7f3ddSMatthew G. Knepley 
4825db781477SPatrick Sanan .seealso: `DMSetField()`, `DMGetField()`
482644a7f3ddSMatthew G. Knepley @*/
482744a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
482844a7f3ddSMatthew G. Knepley {
482944a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
483044a7f3ddSMatthew G. Knepley 
483144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
483244a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4833064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
483444a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
48359566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf+1));
483644a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
483744a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
48389566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
48399566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) field));
48409566063dSJacob Faibussowitsch   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, field));
48419566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
4842af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4843af122d2aSMatthew G Knepley }
48446636e97aSMatthew G Knepley 
4845e5e52638SMatthew G. Knepley /*@
4846e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4847e0b68406SMatthew Knepley 
4848e0b68406SMatthew Knepley   Logically collective on dm
4849e0b68406SMatthew Knepley 
4850e0b68406SMatthew Knepley   Input Parameters:
4851e0b68406SMatthew Knepley + dm          - The DM
4852e0b68406SMatthew Knepley . f           - The field index
4853e0b68406SMatthew Knepley - avoidTensor - The flag to avoid defining the field on tensor cells
4854e0b68406SMatthew Knepley 
4855e0b68406SMatthew Knepley   Level: intermediate
4856e0b68406SMatthew Knepley 
4857db781477SPatrick Sanan .seealso: `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
4858e0b68406SMatthew Knepley @*/
4859e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
4860e0b68406SMatthew Knepley {
4861e0b68406SMatthew Knepley   PetscFunctionBegin;
486263a3b9bcSJacob 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);
4863e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
4864e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4865e0b68406SMatthew Knepley }
4866e0b68406SMatthew Knepley 
4867e0b68406SMatthew Knepley /*@
4868e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
4869e0b68406SMatthew Knepley 
4870e0b68406SMatthew Knepley   Logically collective on dm
4871e0b68406SMatthew Knepley 
4872e0b68406SMatthew Knepley   Input Parameters:
4873e0b68406SMatthew Knepley + dm          - The DM
4874e0b68406SMatthew Knepley - f           - The field index
4875e0b68406SMatthew Knepley 
4876e0b68406SMatthew Knepley   Output Parameter:
4877e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
4878e0b68406SMatthew Knepley 
4879e0b68406SMatthew Knepley   Level: intermediate
4880e0b68406SMatthew Knepley 
4881db781477SPatrick Sanan .seealso: `DMSetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
4882e0b68406SMatthew Knepley @*/
4883e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
4884e0b68406SMatthew Knepley {
4885e0b68406SMatthew Knepley   PetscFunctionBegin;
488663a3b9bcSJacob 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);
4887e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
4888e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4889e0b68406SMatthew Knepley }
4890e0b68406SMatthew Knepley 
4891e0b68406SMatthew Knepley /*@
4892e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4893e5e52638SMatthew G. Knepley 
4894d083f849SBarry Smith   Collective on dm
4895e5e52638SMatthew G. Knepley 
4896e5e52638SMatthew G. Knepley   Input Parameter:
4897e5e52638SMatthew G. Knepley . dm - The DM
4898e5e52638SMatthew G. Knepley 
4899e5e52638SMatthew G. Knepley   Output Parameter:
4900e5e52638SMatthew G. Knepley . newdm - The DM
4901e5e52638SMatthew G. Knepley 
4902e5e52638SMatthew G. Knepley   Level: advanced
4903e5e52638SMatthew G. Knepley 
4904db781477SPatrick Sanan .seealso: `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
4905e5e52638SMatthew G. Knepley @*/
4906e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4907e5e52638SMatthew G. Knepley {
4908e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4909e5e52638SMatthew G. Knepley 
4910e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4911e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
49129566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49139566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
4914e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4915e5e52638SMatthew G. Knepley     DMLabel     label;
4916e5e52638SMatthew G. Knepley     PetscObject field;
491734aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4918e5e52638SMatthew G. Knepley 
49199566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
49209566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
49219566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
49229566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
492334aa8a36SMatthew G. Knepley   }
492434aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
492534aa8a36SMatthew G. Knepley }
492634aa8a36SMatthew G. Knepley 
492734aa8a36SMatthew G. Knepley /*@
492834aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
492934aa8a36SMatthew G. Knepley 
493034aa8a36SMatthew G. Knepley   Not collective
493134aa8a36SMatthew G. Knepley 
493234aa8a36SMatthew G. Knepley   Input Parameters:
493334aa8a36SMatthew G. Knepley + dm - The DM object
493434aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
493534aa8a36SMatthew G. Knepley 
4936d8d19677SJose E. Roman   Output Parameters:
493734aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
493834aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
493934aa8a36SMatthew G. Knepley 
494034aa8a36SMatthew G. Knepley   Notes:
494134aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
494234aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
494334aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4944979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
494534aa8a36SMatthew G. Knepley 
494634aa8a36SMatthew G. Knepley   Level: developer
494734aa8a36SMatthew G. Knepley 
4948db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
494934aa8a36SMatthew G. Knepley @*/
495034aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
495134aa8a36SMatthew G. Knepley {
495234aa8a36SMatthew G. Knepley   PetscFunctionBegin;
495334aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4954534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4955534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
495634aa8a36SMatthew G. Knepley   if (f < 0) {
495734aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
495834aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
495934aa8a36SMatthew G. Knepley   } else {
496034aa8a36SMatthew G. Knepley     PetscInt       Nf;
496134aa8a36SMatthew G. Knepley 
49629566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
49637a8be351SBarry Smith     PetscCheck(f < Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
496434aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
496534aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
496634aa8a36SMatthew G. Knepley   }
496734aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
496834aa8a36SMatthew G. Knepley }
496934aa8a36SMatthew G. Knepley 
497034aa8a36SMatthew G. Knepley /*@
497134aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
497234aa8a36SMatthew G. Knepley 
497334aa8a36SMatthew G. Knepley   Not collective
497434aa8a36SMatthew G. Knepley 
497534aa8a36SMatthew G. Knepley   Input Parameters:
497634aa8a36SMatthew G. Knepley + dm         - The DM object
497734aa8a36SMatthew G. Knepley . f          - The field number
497834aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
497934aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
498034aa8a36SMatthew G. Knepley 
498134aa8a36SMatthew G. Knepley   Notes:
498234aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
498334aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
498434aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4985979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
498634aa8a36SMatthew G. Knepley 
498734aa8a36SMatthew G. Knepley   Level: developer
498834aa8a36SMatthew G. Knepley 
4989db781477SPatrick Sanan .seealso: `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
499034aa8a36SMatthew G. Knepley @*/
499134aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
499234aa8a36SMatthew G. Knepley {
499334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
499434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
499534aa8a36SMatthew G. Knepley   if (f < 0) {
499634aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
499734aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
499834aa8a36SMatthew G. Knepley   } else {
499934aa8a36SMatthew G. Knepley     PetscInt       Nf;
500034aa8a36SMatthew G. Knepley 
50019566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
50027a8be351SBarry Smith     PetscCheck(f < Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
500334aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
500434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5005e5e52638SMatthew G. Knepley   }
5006e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5007e5e52638SMatthew G. Knepley }
5008e5e52638SMatthew G. Knepley 
5009b0441da4SMatthew G. Knepley /*@
5010b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5011b0441da4SMatthew G. Knepley 
5012b0441da4SMatthew G. Knepley   Not collective
5013b0441da4SMatthew G. Knepley 
5014f899ff85SJose E. Roman   Input Parameter:
5015b0441da4SMatthew G. Knepley . dm - The DM object
5016b0441da4SMatthew G. Knepley 
5017d8d19677SJose E. Roman   Output Parameters:
5018b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5019b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5020b0441da4SMatthew G. Knepley 
5021b0441da4SMatthew G. Knepley   Notes:
5022b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5023b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5024b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5025b0441da4SMatthew G. Knepley 
5026b0441da4SMatthew G. Knepley   Level: developer
5027b0441da4SMatthew G. Knepley 
5028db781477SPatrick Sanan .seealso: `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5029b0441da4SMatthew G. Knepley @*/
5030b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5031b0441da4SMatthew G. Knepley {
5032b0441da4SMatthew G. Knepley   PetscInt       Nf;
5033b0441da4SMatthew G. Knepley 
5034b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5035b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5036064a246eSJacob Faibussowitsch   if (useCone)    PetscValidBoolPointer(useCone, 2);
5037064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
50389566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5039b0441da4SMatthew G. Knepley   if (!Nf) {
50409566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5041b0441da4SMatthew G. Knepley   } else {
50429566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5043b0441da4SMatthew G. Knepley   }
5044b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5045b0441da4SMatthew G. Knepley }
5046b0441da4SMatthew G. Knepley 
5047b0441da4SMatthew G. Knepley /*@
5048b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5049b0441da4SMatthew G. Knepley 
5050b0441da4SMatthew G. Knepley   Not collective
5051b0441da4SMatthew G. Knepley 
5052b0441da4SMatthew G. Knepley   Input Parameters:
5053b0441da4SMatthew G. Knepley + dm         - The DM object
5054b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5055b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5056b0441da4SMatthew G. Knepley 
5057b0441da4SMatthew G. Knepley   Notes:
5058b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5059b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5060b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5061b0441da4SMatthew G. Knepley 
5062b0441da4SMatthew G. Knepley   Level: developer
5063b0441da4SMatthew G. Knepley 
5064db781477SPatrick Sanan .seealso: `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5065b0441da4SMatthew G. Knepley @*/
5066b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5067b0441da4SMatthew G. Knepley {
5068b0441da4SMatthew G. Knepley   PetscInt       Nf;
5069b0441da4SMatthew G. Knepley 
5070b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5071b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50729566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5073b0441da4SMatthew G. Knepley   if (!Nf) {
50749566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5075b0441da4SMatthew G. Knepley   } else {
50769566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5077e5e52638SMatthew G. Knepley   }
5078e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5079e5e52638SMatthew G. Knepley }
5080e5e52638SMatthew G. Knepley 
5081799db056SMatthew G. Knepley PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5082783e2ec8SMatthew G. Knepley {
5083799db056SMatthew G. Knepley   DM           plex;
5084799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5085799db056SMatthew G. Knepley   const char **names;
5086799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5087799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5088799db056SMatthew G. Knepley   size_t       len;
5089799db056SMatthew G. Knepley   MPI_Comm     comm;
5090799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5091783e2ec8SMatthew G. Knepley 
5092783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5093799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject) dm, &comm));
5094799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5095799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5096799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5097799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5098799db056SMatthew G. Knepley     PetscDS  dsBC;
5099799db056SMatthew G. Knepley     PetscInt numBd;
5100799db056SMatthew G. Knepley 
5101799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5102799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5103799db056SMatthew G. Knepley     maxLabels += numBd;
5104799db056SMatthew G. Knepley   }
5105799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5106799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5107799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5108799db056SMatthew G. Knepley     PetscDS  dsBC;
5109799db056SMatthew G. Knepley     PetscInt numBd, bd;
5110799db056SMatthew G. Knepley 
5111799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5112799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5113799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5114799db056SMatthew G. Knepley       DMLabel      label;
5115799db056SMatthew G. Knepley       PetscInt     field;
5116799db056SMatthew G. Knepley       PetscObject  obj;
5117799db056SMatthew G. Knepley       PetscClassId id;
5118799db056SMatthew G. Knepley 
5119799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
51209566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
51219566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5122799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
5123799db056SMatthew G. Knepley       for (l = 0; l < Nl; ++l) if (labels[l] == label) break;
5124799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5125783e2ec8SMatthew G. Knepley     }
5126799db056SMatthew G. Knepley   }
5127799db056SMatthew G. Knepley   /* Get label names */
5128799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5129799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject) labels[l], &names[l]));
5130799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {PetscCall(PetscStrlen(names[l], &len)); maxLen = PetscMax(maxLen, (PetscInt) len+2);}
5131799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5132799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5133799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5134799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscStrcpy(&sendNames[gmaxLen*l], names[l]));
5135799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5136799db056SMatthew G. Knepley   /* Put all names on all processes */
5137799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size+1, &displs));
5138799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5139799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p+1] = displs[p] + counts[p];
5140799db056SMatthew G. Knepley   gNl = displs[size];
5141799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) {counts[p] *= gmaxLen; displs[p] *= gmaxLen;}
5142799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5143799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5144799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5145799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5146799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5147799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l*gmaxLen], &glabels[gl]));
5148799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l*gmaxLen], rank);
5149799db056SMatthew G. Knepley     for (m = 0; m < gl; ++m) if (glabels[m] == glabels[gl]) continue;
51509566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5151799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
51529566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5153799db056SMatthew G. Knepley     ++gl;
5154783e2ec8SMatthew G. Knepley   }
5155799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
5156783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5157783e2ec8SMatthew G. Knepley }
5158783e2ec8SMatthew G. Knepley 
5159e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5160e5e52638SMatthew G. Knepley {
5161e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5162e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5163e5e52638SMatthew G. Knepley 
5164e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5165e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
51669566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5167e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5168b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
51699566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5170e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5171e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5172e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5173e5e52638SMatthew G. Knepley }
5174e5e52638SMatthew G. Knepley 
5175e5e52638SMatthew G. Knepley /*@
5176e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5177e5e52638SMatthew G. Knepley 
5178e5e52638SMatthew G. Knepley   Not collective
5179e5e52638SMatthew G. Knepley 
5180e5e52638SMatthew G. Knepley   Input Parameter:
5181e5e52638SMatthew G. Knepley . dm - The DM
5182e5e52638SMatthew G. Knepley 
5183e5e52638SMatthew G. Knepley   Output Parameter:
5184e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5185e5e52638SMatthew G. Knepley 
5186e5e52638SMatthew G. Knepley   Level: intermediate
5187e5e52638SMatthew G. Knepley 
5188db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMGetCellDS()`
5189e5e52638SMatthew G. Knepley @*/
5190e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5191e5e52638SMatthew G. Knepley {
5192e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5193e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5194534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5195e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5196e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5197e5e52638SMatthew G. Knepley }
5198e5e52638SMatthew G. Knepley 
5199e5e52638SMatthew G. Knepley /*@
5200e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5201e5e52638SMatthew G. Knepley 
5202d083f849SBarry Smith   Logically collective on dm
5203e5e52638SMatthew G. Knepley 
5204e5e52638SMatthew G. Knepley   Input Parameter:
5205e5e52638SMatthew G. Knepley . dm - The DM
5206e5e52638SMatthew G. Knepley 
5207e5e52638SMatthew G. Knepley   Level: intermediate
5208e5e52638SMatthew G. Knepley 
5209db781477SPatrick Sanan .seealso: `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5210e5e52638SMatthew G. Knepley @*/
5211e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5212e5e52638SMatthew G. Knepley {
5213e5e52638SMatthew G. Knepley   PetscInt       s;
5214e5e52638SMatthew G. Knepley 
5215e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5216e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5217e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
52189566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
52199566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
52209566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5221e5e52638SMatthew G. Knepley   }
52229566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5223e5e52638SMatthew G. Knepley   dm->probs = NULL;
5224e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5225e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5226e5e52638SMatthew G. Knepley }
5227e5e52638SMatthew G. Knepley 
5228e5e52638SMatthew G. Knepley /*@
5229e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5230e5e52638SMatthew G. Knepley 
5231e5e52638SMatthew G. Knepley   Not collective
5232e5e52638SMatthew G. Knepley 
5233e5e52638SMatthew G. Knepley   Input Parameter:
5234e5e52638SMatthew G. Knepley . dm    - The DM
5235e5e52638SMatthew G. Knepley 
5236e5e52638SMatthew G. Knepley   Output Parameter:
5237e5e52638SMatthew G. Knepley . prob - The default PetscDS
5238e5e52638SMatthew G. Knepley 
5239e5e52638SMatthew G. Knepley   Level: intermediate
5240e5e52638SMatthew G. Knepley 
5241db781477SPatrick Sanan .seealso: `DMGetCellDS()`, `DMGetRegionDS()`
5242e5e52638SMatthew G. Knepley @*/
5243e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5244e5e52638SMatthew G. Knepley {
5245e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5246e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5247e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5248b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5249b0143b4dSMatthew G. Knepley     PetscDS ds;
5250b0143b4dSMatthew G. Knepley 
52519566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
52529566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, NULL, ds));
52539566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5254b0143b4dSMatthew G. Knepley   }
5255b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5256e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5257e5e52638SMatthew G. Knepley }
5258e5e52638SMatthew G. Knepley 
5259e5e52638SMatthew G. Knepley /*@
5260e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5261e5e52638SMatthew G. Knepley 
5262e5e52638SMatthew G. Knepley   Not collective
5263e5e52638SMatthew G. Knepley 
5264e5e52638SMatthew G. Knepley   Input Parameters:
5265e5e52638SMatthew G. Knepley + dm    - The DM
5266e5e52638SMatthew G. Knepley - point - Cell for the DS
5267e5e52638SMatthew G. Knepley 
5268e5e52638SMatthew G. Knepley   Output Parameter:
5269e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5270e5e52638SMatthew G. Knepley 
5271e5e52638SMatthew G. Knepley   Level: developer
5272e5e52638SMatthew G. Knepley 
5273db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMSetRegionDS()`
5274e5e52638SMatthew G. Knepley @*/
5275e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5276e5e52638SMatthew G. Knepley {
5277e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5278e5e52638SMatthew G. Knepley   PetscInt       s;
5279e5e52638SMatthew G. Knepley 
5280e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5281e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5282e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
528363a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
5284e5e52638SMatthew G. Knepley   *prob = NULL;
5285e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5286e5e52638SMatthew G. Knepley     PetscInt val;
5287e5e52638SMatthew G. Knepley 
5288e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5289e5e52638SMatthew G. Knepley     else {
52909566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
5291e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5292e5e52638SMatthew G. Knepley     }
5293e5e52638SMatthew G. Knepley   }
5294e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5295e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5296e5e52638SMatthew G. Knepley }
5297e5e52638SMatthew G. Knepley 
5298e5e52638SMatthew G. Knepley /*@
5299e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5300e5e52638SMatthew G. Knepley 
5301e5e52638SMatthew G. Knepley   Not collective
5302e5e52638SMatthew G. Knepley 
5303e5e52638SMatthew G. Knepley   Input Parameters:
5304e5e52638SMatthew G. Knepley + dm    - The DM
5305e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5306e5e52638SMatthew G. Knepley 
5307b3cf3223SMatthew G. Knepley   Output Parameters:
5308b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5309b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5310e5e52638SMatthew G. Knepley 
5311154ca461SJed Brown   Note:
5312154ca461SJed Brown   If a non-NULL label is given, but there is no PetscDS on that specific label,
5313154ca461SJed Brown   the PetscDS for the full domain (if present) is returned. Returns with
5314154ca461SJed Brown   fields=NULL and prob=NULL if there is no PetscDS for the full domain.
5315e5e52638SMatthew G. Knepley 
5316e5e52638SMatthew G. Knepley   Level: advanced
5317e5e52638SMatthew G. Knepley 
5318db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5319e5e52638SMatthew G. Knepley @*/
5320b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5321e5e52638SMatthew G. Knepley {
5322e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5323e5e52638SMatthew G. Knepley 
5324e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5325e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5326e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5327b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5328b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5329e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5330154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5331b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5332b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5333154ca461SJed Brown       if (dm->probs[s].label) PetscFunctionReturn(0);
5334b3cf3223SMatthew G. Knepley     }
5335e5e52638SMatthew G. Knepley   }
53362df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5337e5e52638SMatthew G. Knepley }
5338e5e52638SMatthew G. Knepley 
5339e5e52638SMatthew G. Knepley /*@
5340083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5341083401c6SMatthew G. Knepley 
5342083401c6SMatthew G. Knepley   Collective on dm
5343083401c6SMatthew G. Knepley 
5344083401c6SMatthew G. Knepley   Input Parameters:
5345083401c6SMatthew G. Knepley + dm     - The DM
5346083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5347083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5348083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5349083401c6SMatthew G. Knepley 
5350083401c6SMatthew 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,
5351083401c6SMatthew G. Knepley   the fields argument is ignored.
5352083401c6SMatthew G. Knepley 
5353083401c6SMatthew G. Knepley   Level: advanced
5354083401c6SMatthew G. Knepley 
5355db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5356083401c6SMatthew G. Knepley @*/
5357083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5358083401c6SMatthew G. Knepley {
5359083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5360083401c6SMatthew G. Knepley 
5361083401c6SMatthew G. Knepley   PetscFunctionBegin;
5362083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5363083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5364064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5365083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5366083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
53679566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
5368083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5369083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5370083401c6SMatthew G. Knepley     }
5371083401c6SMatthew G. Knepley   }
53729566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds+1));
53739566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
53749566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) fields));
53759566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) ds));
5376083401c6SMatthew G. Knepley   if (!label) {
5377083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5378083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5379083401c6SMatthew G. Knepley     Nds = 0;
5380083401c6SMatthew G. Knepley   }
5381083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5382083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5383083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5384083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5385083401c6SMatthew G. Knepley }
5386083401c6SMatthew G. Knepley 
5387083401c6SMatthew G. Knepley /*@
5388e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5389e5e52638SMatthew G. Knepley 
5390e5e52638SMatthew G. Knepley   Not collective
5391e5e52638SMatthew G. Knepley 
5392e5e52638SMatthew G. Knepley   Input Parameters:
5393e5e52638SMatthew G. Knepley + dm  - The DM
5394e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5395e5e52638SMatthew G. Knepley 
5396e5e52638SMatthew G. Knepley   Output Parameters:
5397b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5398b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5399083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5400e5e52638SMatthew G. Knepley 
5401e5e52638SMatthew G. Knepley   Level: advanced
5402e5e52638SMatthew G. Knepley 
5403db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5404e5e52638SMatthew G. Knepley @*/
5405b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5406e5e52638SMatthew G. Knepley {
5407e5e52638SMatthew G. Knepley   PetscInt       Nds;
5408e5e52638SMatthew G. Knepley 
5409e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5410e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54119566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
541263a3b9bcSJacob 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);
5413e5e52638SMatthew G. Knepley   if (label) {
5414e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5415e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5416e5e52638SMatthew G. Knepley   }
5417b3cf3223SMatthew G. Knepley   if (fields) {
5418b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5419b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5420b3cf3223SMatthew G. Knepley   }
5421e5e52638SMatthew G. Knepley   if (ds) {
5422b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5423e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5424e5e52638SMatthew G. Knepley   }
5425e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5426e5e52638SMatthew G. Knepley }
5427e5e52638SMatthew G. Knepley 
5428e5e52638SMatthew G. Knepley /*@
5429083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5430e5e52638SMatthew G. Knepley 
5431083401c6SMatthew G. Knepley   Not collective
5432e5e52638SMatthew G. Knepley 
5433e5e52638SMatthew G. Knepley   Input Parameters:
5434e5e52638SMatthew G. Knepley + dm     - The DM
5435083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5436083401c6SMatthew G. Knepley . label  - The region label, or NULL
5437083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5438083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5439e5e52638SMatthew G. Knepley 
5440e5e52638SMatthew G. Knepley   Level: advanced
5441e5e52638SMatthew G. Knepley 
5442db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5443e5e52638SMatthew G. Knepley @*/
5444083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5445e5e52638SMatthew G. Knepley {
5446083401c6SMatthew G. Knepley   PetscInt       Nds;
5447e5e52638SMatthew G. Knepley 
5448e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5449e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5450083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
54519566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
545263a3b9bcSJacob 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);
54539566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
54549566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5455083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5456083401c6SMatthew G. Knepley   if (fields) {
5457083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
54589566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) fields));
54599566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5460083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5461e5e52638SMatthew G. Knepley   }
5462083401c6SMatthew G. Knepley   if (ds) {
5463083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
54649566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) ds));
54659566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5466083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5467083401c6SMatthew G. Knepley   }
5468e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5469e5e52638SMatthew G. Knepley }
5470e5e52638SMatthew G. Knepley 
5471e5e52638SMatthew G. Knepley /*@
54721d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
54731d3af9e0SMatthew G. Knepley 
54741d3af9e0SMatthew G. Knepley   Not collective
54751d3af9e0SMatthew G. Knepley 
54761d3af9e0SMatthew G. Knepley   Input Parameters:
54771d3af9e0SMatthew G. Knepley + dm  - The DM
54781d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
54791d3af9e0SMatthew G. Knepley 
54801d3af9e0SMatthew G. Knepley   Output Parameter:
54811d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
54821d3af9e0SMatthew G. Knepley 
54831d3af9e0SMatthew G. Knepley   Level: advanced
54841d3af9e0SMatthew G. Knepley 
5485db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
54861d3af9e0SMatthew G. Knepley @*/
54871d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
54881d3af9e0SMatthew G. Knepley {
54891d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
54901d3af9e0SMatthew G. Knepley 
54911d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
54921d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54931d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
5494dadcf809SJacob Faibussowitsch   PetscValidIntPointer(num, 3);
54959566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
54961d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
54971d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
54981d3af9e0SMatthew G. Knepley   else          *num = n;
54991d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
55001d3af9e0SMatthew G. Knepley }
55011d3af9e0SMatthew G. Knepley 
55022df84da0SMatthew G. Knepley /*@C
55032df84da0SMatthew G. Knepley   DMCreateFEDefault - Create a PetscFE based on the celltype for the mesh
55042df84da0SMatthew G. Knepley 
55052df84da0SMatthew G. Knepley   Not collective
55062df84da0SMatthew G. Knepley 
5507f1a722f8SMatthew G. Knepley   Input Parameters:
55082df84da0SMatthew G. Knepley + dm     - The DM
55092df84da0SMatthew G. Knepley . Nc     - The number of components for the field
55102df84da0SMatthew G. Knepley . prefix - The options prefix for the output PetscFE, or NULL
55112df84da0SMatthew G. Knepley - qorder - The quadrature order or PETSC_DETERMINE to use PetscSpace polynomial degree
55122df84da0SMatthew G. Knepley 
55132df84da0SMatthew G. Knepley   Output Parameter:
55142df84da0SMatthew G. Knepley . fem - The PetscFE
55152df84da0SMatthew G. Knepley 
55162df84da0SMatthew G. Knepley   Note: This is a convenience method that just calls PetscFECreateByCell() underneath.
55172df84da0SMatthew G. Knepley 
55182df84da0SMatthew G. Knepley   Level: intermediate
55192df84da0SMatthew G. Knepley 
5520db781477SPatrick Sanan .seealso: `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
55212df84da0SMatthew G. Knepley @*/
55222df84da0SMatthew G. Knepley PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
55232df84da0SMatthew G. Knepley {
55242df84da0SMatthew G. Knepley   DMPolytopeType ct;
55252df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
55262df84da0SMatthew G. Knepley 
55272df84da0SMatthew G. Knepley   PetscFunctionBegin;
55282df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55292df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
55302df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
55312df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
55322df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
55339566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
55349566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
55359566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
55369566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
55372df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
55382df84da0SMatthew G. Knepley }
55392df84da0SMatthew G. Knepley 
55401d3af9e0SMatthew G. Knepley /*@
5541e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5542e5e52638SMatthew G. Knepley 
5543d083f849SBarry Smith   Collective on dm
5544e5e52638SMatthew G. Knepley 
5545e5e52638SMatthew G. Knepley   Input Parameter:
5546e5e52638SMatthew G. Knepley . dm - The DM
5547e5e52638SMatthew G. Knepley 
554845480ffeSMatthew G. Knepley   Options Database Keys:
554945480ffeSMatthew G. Knepley . -dm_petscds_view - View all the PetscDS objects in this DM
555045480ffeSMatthew G. Knepley 
5551e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5552e5e52638SMatthew G. Knepley 
5553e5e52638SMatthew G. Knepley   Level: intermediate
5554e5e52638SMatthew G. Knepley 
5555db781477SPatrick Sanan .seealso: `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5556e5e52638SMatthew G. Knepley @*/
5557e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5558e5e52638SMatthew G. Knepley {
5559e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5560083401c6SMatthew G. Knepley   PetscDS        dsDef;
5561083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5562f9244615SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5563f9244615SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE, flg;
5564e5e52638SMatthew G. Knepley 
5565e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5566e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5567e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
55689566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject) dm, &comm));
55699566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5570083401c6SMatthew G. Knepley   /* Determine how many regions we have */
55719566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5572083401c6SMatthew G. Knepley   Nl   = 0;
5573083401c6SMatthew G. Knepley   Ndef = 0;
5574083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5575083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5576083401c6SMatthew G. Knepley     PetscInt l;
5577083401c6SMatthew G. Knepley 
5578f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5579f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5580f918ec44SMatthew G. Knepley     {
5581f918ec44SMatthew G. Knepley       PetscClassId id;
5582f918ec44SMatthew G. Knepley 
55839566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5584f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5585f918ec44SMatthew G. Knepley         Ceed ceed;
5586f918ec44SMatthew G. Knepley 
55879566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
55889566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE) dm->fields[f].disc, ceed));
5589f918ec44SMatthew G. Knepley       }
5590f918ec44SMatthew G. Knepley     }
5591f918ec44SMatthew G. Knepley #endif
5592083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5593083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5594083401c6SMatthew G. Knepley     if (l < Nl) continue;
5595083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5596083401c6SMatthew G. Knepley   }
5597083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
55989566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
5599083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5600b3cf3223SMatthew G. Knepley     IS        fields;
5601b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5602b3cf3223SMatthew G. Knepley 
5603b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
56047a8be351SBarry Smith     PetscCheck(nf,comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
56059566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
5606b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
56079566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
56089566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_"));
56099566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
56109566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
561188f0c812SMatthew G. Knepley 
56129566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
56139566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef));
56149566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
56159566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
56162df9ee95SMatthew G. Knepley   }
56179566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
56189566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5619083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5620083401c6SMatthew G. Knepley   if (Ndef && Nl) {
56210122748bSMatthew G. Knepley     DM              plex;
5622083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5623083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5624083401c6SMatthew G. Knepley     PetscInt       *fields;
5625083401c6SMatthew G. Knepley     const PetscInt *cells;
5626083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
56270122748bSMatthew G. Knepley 
56289566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
56299566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
56309566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
56319566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
56325fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5633083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5634083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5635083401c6SMatthew G. Knepley       IS      pointIS;
5636083401c6SMatthew G. Knepley 
56379566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
56389566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
56399566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
56409566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5641083401c6SMatthew G. Knepley     }
56429566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5643083401c6SMatthew G. Knepley 
56449566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
56459566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
56469566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
56479566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
56489566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
56499566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
56509566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5651083401c6SMatthew G. Knepley 
56529566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
5653083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
56549566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
56559566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_"));
56569566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
56579566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5658083401c6SMatthew G. Knepley 
56599566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
56609566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef));
56619566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
56629566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
56639566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
56649566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
56659566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5666083401c6SMatthew G. Knepley   }
5667083401c6SMatthew G. Knepley   /* Create label DSes
5668083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5669083401c6SMatthew G. Knepley   */
5670083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5671083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5672083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5673083401c6SMatthew G. Knepley     PetscDS   ds;
5674083401c6SMatthew G. Knepley     IS        fields;
5675083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5676083401c6SMatthew G. Knepley 
56779566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
5678083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
56799566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
5680083401c6SMatthew G. Knepley     for (f = 0, nf  = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
56819566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
56829566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_"));
56839566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
56849566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
56859566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, label, fields, ds));
56869566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
56879566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5688083401c6SMatthew G. Knepley     {
5689083401c6SMatthew G. Knepley       DMPolytopeType ct;
5690083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
56915fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
56920122748bSMatthew G. Knepley 
56939566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5694665f567fSMatthew G. Knepley       if (lStart >= 0) {
56959566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5696412e9a14SMatthew G. Knepley         switch (ct) {
5697412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5698412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5699412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5700412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
57015fedec97SMatthew G. Knepley             isCohesiveLocal = PETSC_TRUE;break;
5702083401c6SMatthew G. Knepley           default: break;
5703412e9a14SMatthew G. Knepley         }
5704665f567fSMatthew G. Knepley       }
57059566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
57065fedec97SMatthew G. Knepley       for (f = 0, nf  = 0; f < Nf; ++f) {
57075fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
57085fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
57099566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
57109566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
57115fedec97SMatthew G. Knepley           }
57125fedec97SMatthew G. Knepley           ++nf;
57135fedec97SMatthew G. Knepley         }
57145fedec97SMatthew G. Knepley       }
5715e5e52638SMatthew G. Knepley     }
57169566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5717e5e52638SMatthew G. Knepley   }
57189566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5719e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5720083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5721083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5722083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5723083401c6SMatthew G. Knepley     const PetscInt *fld;
57245fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
57255fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5726e5e52638SMatthew G. Knepley 
57279566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
57289566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
57299566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
57309566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5731083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5732083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
57335fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5734e5e52638SMatthew G. Knepley       PetscClassId id;
5735e5e52638SMatthew G. Knepley 
57365fedec97SMatthew G. Knepley       /* Handle DS with no fields */
57379566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
57385fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
57399566063dSJacob Faibussowitsch       if (isCohesive && !isCohesiveField) PetscCall(PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc));
57409566063dSJacob Faibussowitsch       PetscCall(PetscDSSetDiscretization(ds, f, disc));
5741083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
57429566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5743e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5744e5e52638SMatthew G. Knepley     }
57459566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
5746e5e52638SMatthew G. Knepley   }
5747f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
57489566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject) dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
5749f9244615SMatthew G. Knepley   if (flg) {
57503b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
57513b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
57523b4aee56SMatthew G. Knepley       PetscInt Nf, f;
57533b4aee56SMatthew G. Knepley 
57549566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
57559566063dSJacob Faibussowitsch       for (f = 0; f < Nf; ++f) PetscCall(PetscDSSetJetDegree(ds, f, k));
57563b4aee56SMatthew G. Knepley     }
5757f9244615SMatthew G. Knepley   }
5758e5e52638SMatthew G. Knepley   /* Setup DSes */
5759e5e52638SMatthew G. Knepley   if (doSetup) {
57609566063dSJacob Faibussowitsch     for (s = 0; s < dm->Nds; ++s) PetscCall(PetscDSSetUp(dm->probs[s].ds));
5761e5e52638SMatthew G. Knepley   }
5762e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5763e5e52638SMatthew G. Knepley }
5764e5e52638SMatthew G. Knepley 
5765e5e52638SMatthew G. Knepley /*@
57667f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
57677f96f943SMatthew G. Knepley 
5768f2cacb80SMatthew G. Knepley   Collective on DM
5769f2cacb80SMatthew G. Knepley 
57707f96f943SMatthew G. Knepley   Input Parameters:
57717f96f943SMatthew G. Knepley + dm   - The DM
57727f96f943SMatthew G. Knepley - time - The time
57737f96f943SMatthew G. Knepley 
57747f96f943SMatthew G. Knepley   Output Parameters:
5775f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5776f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
57777f96f943SMatthew G. Knepley 
57787f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
57797f96f943SMatthew G. Knepley 
57807f96f943SMatthew G. Knepley   Level: developer
57817f96f943SMatthew G. Knepley 
5782db781477SPatrick Sanan .seealso: `PetscDSSetExactSolution()`
57837f96f943SMatthew G. Knepley @*/
5784f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
57857f96f943SMatthew G. Knepley {
57867f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
57877f96f943SMatthew G. Knepley   void            **ectxs;
57887f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
57897f96f943SMatthew G. Knepley 
57907f96f943SMatthew G. Knepley   PetscFunctionBegin;
5791f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5792f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5793f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
57949566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
57959566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
57969566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
57977f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
57987f96f943SMatthew G. Knepley     PetscDS         ds;
57997f96f943SMatthew G. Knepley     DMLabel         label;
58007f96f943SMatthew G. Knepley     IS              fieldIS;
58017f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
58027f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
58037f96f943SMatthew G. Knepley 
58049566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
58059566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
58069566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
58079566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
58089566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
5809f2cacb80SMatthew G. Knepley     if (u) {
58107f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
58117f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
58129566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]));
58137f96f943SMatthew G. Knepley       }
58149566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
58157f96f943SMatthew G. Knepley       if (label) {
58169566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u));
58177f96f943SMatthew G. Knepley       } else {
58189566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u));
58197f96f943SMatthew G. Knepley       }
58207f96f943SMatthew G. Knepley     }
5821f2cacb80SMatthew G. Knepley     if (u_t) {
58229566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
58239566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
5824f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5825f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
58269566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]));
5827f2cacb80SMatthew G. Knepley       }
58289566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
5829f2cacb80SMatthew G. Knepley       if (label) {
58309566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t));
5831f2cacb80SMatthew G. Knepley       } else {
58329566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t));
5833f2cacb80SMatthew G. Knepley       }
5834f2cacb80SMatthew G. Knepley     }
5835f2cacb80SMatthew G. Knepley   }
5836f2cacb80SMatthew G. Knepley   if (u) {
58379566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) u, "Exact Solution"));
58389566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) u, "exact_"));
5839f2cacb80SMatthew G. Knepley   }
5840f2cacb80SMatthew G. Knepley   if (u_t) {
58419566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative"));
58429566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_"));
5843f2cacb80SMatthew G. Knepley   }
58449566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
58457f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
58467f96f943SMatthew G. Knepley }
58477f96f943SMatthew G. Knepley 
584845480ffeSMatthew G. Knepley PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
584945480ffeSMatthew G. Knepley {
585045480ffeSMatthew G. Knepley   PetscDS        dsNew;
585145480ffeSMatthew G. Knepley   DSBoundary     b;
58526a02485aSMatthew G. Knepley   PetscInt       cdim, Nf, f, d;
58535fedec97SMatthew G. Knepley   PetscBool      isCohesive;
585445480ffeSMatthew G. Knepley   void          *ctx;
585545480ffeSMatthew G. Knepley 
585645480ffeSMatthew G. Knepley   PetscFunctionBegin;
58579566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject) ds), &dsNew));
58589566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyConstants(ds, dsNew));
58599566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyExactSolutions(ds, dsNew));
58609566063dSJacob Faibussowitsch   PetscCall(PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew));
58619566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyEquations(ds, dsNew));
58629566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
586345480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
58649566063dSJacob Faibussowitsch     PetscCall(PetscDSGetContext(ds, f, &ctx));
58659566063dSJacob Faibussowitsch     PetscCall(PetscDSSetContext(dsNew, f, ctx));
58669566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCohesive(ds, f, &isCohesive));
58679566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCohesive(dsNew, f, isCohesive));
58686a02485aSMatthew G. Knepley     PetscCall(PetscDSGetJetDegree(ds, f, &d));
58696a02485aSMatthew G. Knepley     PetscCall(PetscDSSetJetDegree(dsNew, f, d));
587045480ffeSMatthew G. Knepley   }
587145480ffeSMatthew G. Knepley   if (Nf) {
58729566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCoordinateDimension(ds, &cdim));
58739566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsNew, cdim));
587445480ffeSMatthew G. Knepley   }
58759566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew));
587645480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
58779566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, b->lname, &b->label));
587845480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
58797a8be351SBarry Smith     //PetscCheck(b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
588045480ffeSMatthew G. Knepley   }
588145480ffeSMatthew G. Knepley 
58829566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(dm, label, fields, dsNew));
58839566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
588445480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
588545480ffeSMatthew G. Knepley }
588645480ffeSMatthew G. Knepley 
58877f96f943SMatthew G. Knepley /*@
5888e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5889e5e52638SMatthew G. Knepley 
5890d083f849SBarry Smith   Collective on dm
5891e5e52638SMatthew G. Knepley 
5892e5e52638SMatthew G. Knepley   Input Parameter:
5893e5e52638SMatthew G. Knepley . dm - The DM
5894e5e52638SMatthew G. Knepley 
5895e5e52638SMatthew G. Knepley   Output Parameter:
5896e5e52638SMatthew G. Knepley . newdm - The DM
5897e5e52638SMatthew G. Knepley 
5898e5e52638SMatthew G. Knepley   Level: advanced
5899e5e52638SMatthew G. Knepley 
5900db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5901e5e52638SMatthew G. Knepley @*/
5902e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5903e5e52638SMatthew G. Knepley {
5904e5e52638SMatthew G. Knepley   PetscInt Nds, s;
5905e5e52638SMatthew G. Knepley 
5906e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5907e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
59089566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
59099566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
5910e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5911e5e52638SMatthew G. Knepley     DMLabel  label;
5912b3cf3223SMatthew G. Knepley     IS       fields;
591345480ffeSMatthew G. Knepley     PetscDS  ds, newds;
5914783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5915e5e52638SMatthew G. Knepley 
59169566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds));
5917b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
59189566063dSJacob Faibussowitsch     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds));
591945480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
59209566063dSJacob Faibussowitsch     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds));
59219566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
5922783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5923b8025e53SMatthew G. Knepley       PetscWeakForm wf;
592445480ffeSMatthew G. Knepley       DMLabel       label;
5925783e2ec8SMatthew G. Knepley       PetscInt      field;
5926783e2ec8SMatthew G. Knepley 
59279566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
59289566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
5929783e2ec8SMatthew G. Knepley     }
5930e5e52638SMatthew G. Knepley   }
5931799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
5932e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5933e5e52638SMatthew G. Knepley }
5934e5e52638SMatthew G. Knepley 
5935e5e52638SMatthew G. Knepley /*@
5936e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5937e5e52638SMatthew G. Knepley 
5938d083f849SBarry Smith   Collective on dm
5939e5e52638SMatthew G. Knepley 
5940e5e52638SMatthew G. Knepley   Input Parameter:
5941e5e52638SMatthew G. Knepley . dm - The DM
5942e5e52638SMatthew G. Knepley 
5943e5e52638SMatthew G. Knepley   Output Parameter:
5944e5e52638SMatthew G. Knepley . newdm - The DM
5945e5e52638SMatthew G. Knepley 
5946e5e52638SMatthew G. Knepley   Level: advanced
5947e5e52638SMatthew G. Knepley 
5948db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMCopyDS()`
5949e5e52638SMatthew G. Knepley @*/
5950e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5951e5e52638SMatthew G. Knepley {
5952e5e52638SMatthew G. Knepley   PetscFunctionBegin;
59539566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
59549566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
5955e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5956e5e52638SMatthew G. Knepley }
5957e5e52638SMatthew G. Knepley 
5958c73cfb54SMatthew G. Knepley /*@
5959c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5960c73cfb54SMatthew G. Knepley 
5961c73cfb54SMatthew G. Knepley   Not collective
5962c73cfb54SMatthew G. Knepley 
5963c73cfb54SMatthew G. Knepley   Input Parameter:
5964c73cfb54SMatthew G. Knepley . dm - The DM
5965c73cfb54SMatthew G. Knepley 
5966c73cfb54SMatthew G. Knepley   Output Parameter:
5967c73cfb54SMatthew G. Knepley . dim - The topological dimension
5968c73cfb54SMatthew G. Knepley 
5969c73cfb54SMatthew G. Knepley   Level: beginner
5970c73cfb54SMatthew G. Knepley 
5971db781477SPatrick Sanan .seealso: `DMSetDimension()`, `DMCreate()`
5972c73cfb54SMatthew G. Knepley @*/
5973c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5974c73cfb54SMatthew G. Knepley {
5975c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5976c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5977534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5978c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5979c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5980c73cfb54SMatthew G. Knepley }
5981c73cfb54SMatthew G. Knepley 
5982c73cfb54SMatthew G. Knepley /*@
5983c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5984c73cfb54SMatthew G. Knepley 
5985c73cfb54SMatthew G. Knepley   Collective on dm
5986c73cfb54SMatthew G. Knepley 
5987c73cfb54SMatthew G. Knepley   Input Parameters:
5988c73cfb54SMatthew G. Knepley + dm - The DM
5989c73cfb54SMatthew G. Knepley - dim - The topological dimension
5990c73cfb54SMatthew G. Knepley 
5991c73cfb54SMatthew G. Knepley   Level: beginner
5992c73cfb54SMatthew G. Knepley 
5993db781477SPatrick Sanan .seealso: `DMGetDimension()`, `DMCreate()`
5994c73cfb54SMatthew G. Knepley @*/
5995c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5996c73cfb54SMatthew G. Knepley {
5997e5e52638SMatthew G. Knepley   PetscDS        ds;
599845480ffeSMatthew G. Knepley   PetscInt       Nds, n;
5999f17e8794SMatthew G. Knepley 
6000c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6001c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6002c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6003c73cfb54SMatthew G. Knepley   dm->dim = dim;
6004d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
60059566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
600645480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
60079566063dSJacob Faibussowitsch       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
60089566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
600945480ffeSMatthew G. Knepley     }
6010d17bd122SMatthew G. Knepley   }
6011c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6012c73cfb54SMatthew G. Knepley }
6013c73cfb54SMatthew G. Knepley 
6014793f3fe5SMatthew G. Knepley /*@
6015793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6016793f3fe5SMatthew G. Knepley 
6017d083f849SBarry Smith   Collective on dm
6018793f3fe5SMatthew G. Knepley 
6019793f3fe5SMatthew G. Knepley   Input Parameters:
6020793f3fe5SMatthew G. Knepley + dm - the DM
6021793f3fe5SMatthew G. Knepley - dim - the dimension
6022793f3fe5SMatthew G. Knepley 
6023793f3fe5SMatthew G. Knepley   Output Parameters:
6024793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6025aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6026793f3fe5SMatthew G. Knepley 
6027793f3fe5SMatthew G. Knepley   Note:
6028793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6029a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6030793f3fe5SMatthew G. Knepley   then the interval is empty.
6031793f3fe5SMatthew G. Knepley 
6032793f3fe5SMatthew G. Knepley   Level: intermediate
6033793f3fe5SMatthew G. Knepley 
6034db781477SPatrick Sanan .seealso: `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6035793f3fe5SMatthew G. Knepley @*/
6036793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6037793f3fe5SMatthew G. Knepley {
6038793f3fe5SMatthew G. Knepley   PetscInt       d;
6039793f3fe5SMatthew G. Knepley 
6040793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6041793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60429566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
60437a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
60447a8be351SBarry Smith   PetscCheck(dm->ops->getdimpoints,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
60459566063dSJacob Faibussowitsch   PetscCall((*dm->ops->getdimpoints)(dm, dim, pStart, pEnd));
6046793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6047793f3fe5SMatthew G. Knepley }
6048793f3fe5SMatthew G. Knepley 
60496636e97aSMatthew G Knepley /*@
6050f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6051f4d763aaSMatthew G. Knepley 
60528f700142SStefano Zampini   Collective on dm
60538f700142SStefano Zampini 
6054f4d763aaSMatthew G. Knepley   Input Parameter:
6055f4d763aaSMatthew G. Knepley . dm - The original DM
6056f4d763aaSMatthew G. Knepley 
6057f4d763aaSMatthew G. Knepley   Output Parameter:
6058f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6059f4d763aaSMatthew G. Knepley 
6060f4d763aaSMatthew G. Knepley   Level: intermediate
6061f4d763aaSMatthew G. Knepley 
6062db781477SPatrick Sanan .seealso: `VecView()`, `DMGetGlobalSection()`
6063f4d763aaSMatthew G. Knepley @*/
606414f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
606514f150ffSMatthew G. Knepley {
6066c26acbdeSMatthew G. Knepley   PetscSection   section;
60672d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
606814f150ffSMatthew G. Knepley 
606914f150ffSMatthew G. Knepley   PetscFunctionBegin;
607014f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
607114f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
60729566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
60739566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
60749566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm)));
60752d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6076c26acbdeSMatthew G. Knepley     *odm = dm;
6077c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6078c26acbdeSMatthew G. Knepley   }
607914f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6080c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
608114f150ffSMatthew G. Knepley     PetscSF      sf;
608214f150ffSMatthew G. Knepley 
60839566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
60849566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
60859566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
60869566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
60879566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
60889566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
60899566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
60909566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
60919566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
609214f150ffSMatthew G. Knepley   }
609314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
609414f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
609514f150ffSMatthew G. Knepley }
6096f4d763aaSMatthew G. Knepley 
6097f4d763aaSMatthew G. Knepley /*@
6098cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6099f4d763aaSMatthew G. Knepley 
6100f4d763aaSMatthew G. Knepley   Input Parameter:
6101f4d763aaSMatthew G. Knepley . dm - The original DM
6102f4d763aaSMatthew G. Knepley 
6103cdb7a50dSMatthew G. Knepley   Output Parameters:
6104cdb7a50dSMatthew G. Knepley + num - The output sequence number
6105cdb7a50dSMatthew G. Knepley - val - The output sequence value
6106f4d763aaSMatthew G. Knepley 
6107f4d763aaSMatthew G. Knepley   Level: intermediate
6108f4d763aaSMatthew G. Knepley 
6109f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6110f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6111f4d763aaSMatthew G. Knepley 
6112db781477SPatrick Sanan .seealso: `VecView()`
6113f4d763aaSMatthew G. Knepley @*/
6114cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6115f4d763aaSMatthew G. Knepley {
6116f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6117f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6118534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6119534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6120f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6121f4d763aaSMatthew G. Knepley }
6122f4d763aaSMatthew G. Knepley 
6123f4d763aaSMatthew G. Knepley /*@
6124cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6125f4d763aaSMatthew G. Knepley 
6126f4d763aaSMatthew G. Knepley   Input Parameters:
6127f4d763aaSMatthew G. Knepley + dm - The original DM
6128cdb7a50dSMatthew G. Knepley . num - The output sequence number
6129cdb7a50dSMatthew G. Knepley - val - The output sequence value
6130f4d763aaSMatthew G. Knepley 
6131f4d763aaSMatthew G. Knepley   Level: intermediate
6132f4d763aaSMatthew G. Knepley 
6133f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6134f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6135f4d763aaSMatthew G. Knepley 
6136db781477SPatrick Sanan .seealso: `VecView()`
6137f4d763aaSMatthew G. Knepley @*/
6138cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6139f4d763aaSMatthew G. Knepley {
6140f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6141f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6142f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6143cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6144cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6145cdb7a50dSMatthew G. Knepley }
6146cdb7a50dSMatthew G. Knepley 
6147cdb7a50dSMatthew G. Knepley /*@C
6148cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6149cdb7a50dSMatthew G. Knepley 
6150cdb7a50dSMatthew G. Knepley   Input Parameters:
6151cdb7a50dSMatthew G. Knepley + dm   - The original DM
6152cdb7a50dSMatthew G. Knepley . name - The sequence name
6153cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6154cdb7a50dSMatthew G. Knepley 
6155cdb7a50dSMatthew G. Knepley   Output Parameter:
6156cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6157cdb7a50dSMatthew G. Knepley 
6158cdb7a50dSMatthew G. Knepley   Level: intermediate
6159cdb7a50dSMatthew G. Knepley 
6160cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6161cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6162cdb7a50dSMatthew G. Knepley 
6163db781477SPatrick Sanan .seealso: `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6164cdb7a50dSMatthew G. Knepley @*/
6165cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6166cdb7a50dSMatthew G. Knepley {
6167cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6168cdb7a50dSMatthew G. Knepley 
6169cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6170cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6171cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6172064a246eSJacob Faibussowitsch   PetscValidRealPointer(val,5);
61739566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5));
6174cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6175cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6176cdb7a50dSMatthew G. Knepley     PetscScalar value;
6177cdb7a50dSMatthew G. Knepley 
61789566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
61794aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6180cdb7a50dSMatthew G. Knepley #endif
6181cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6182f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6183f4d763aaSMatthew G. Knepley }
61848e4ac7eaSMatthew G. Knepley 
61858e4ac7eaSMatthew G. Knepley /*@
61868e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
61878e4ac7eaSMatthew G. Knepley 
61888e4ac7eaSMatthew G. Knepley   Not collective
61898e4ac7eaSMatthew G. Knepley 
61908e4ac7eaSMatthew G. Knepley   Input Parameter:
61918e4ac7eaSMatthew G. Knepley . dm - The DM
61928e4ac7eaSMatthew G. Knepley 
61938e4ac7eaSMatthew G. Knepley   Output Parameter:
61948e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
61958e4ac7eaSMatthew G. Knepley 
61968e4ac7eaSMatthew G. Knepley   Level: beginner
61978e4ac7eaSMatthew G. Knepley 
6198db781477SPatrick Sanan .seealso: `DMSetUseNatural()`, `DMCreate()`
61998e4ac7eaSMatthew G. Knepley @*/
62008e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
62018e4ac7eaSMatthew G. Knepley {
62028e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
62038e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6204534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
62058e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
62068e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
62078e4ac7eaSMatthew G. Knepley }
62088e4ac7eaSMatthew G. Knepley 
62098e4ac7eaSMatthew G. Knepley /*@
62105d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
62118e4ac7eaSMatthew G. Knepley 
62128e4ac7eaSMatthew G. Knepley   Collective on dm
62138e4ac7eaSMatthew G. Knepley 
62148e4ac7eaSMatthew G. Knepley   Input Parameters:
62158e4ac7eaSMatthew G. Knepley + dm - The DM
62168e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
62178e4ac7eaSMatthew G. Knepley 
62185d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
62195d3b26e6SMatthew G. Knepley 
62208e4ac7eaSMatthew G. Knepley   Level: beginner
62218e4ac7eaSMatthew G. Knepley 
6222db781477SPatrick Sanan .seealso: `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
62238e4ac7eaSMatthew G. Knepley @*/
62248e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
62258e4ac7eaSMatthew G. Knepley {
62268e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
62278e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62288833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
62298e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
62308e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
62318e4ac7eaSMatthew G. Knepley }
6232c58f1c22SToby Isaac 
6233c58f1c22SToby Isaac /*@C
6234c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6235c58f1c22SToby Isaac 
6236c58f1c22SToby Isaac   Not Collective
6237c58f1c22SToby Isaac 
6238c58f1c22SToby Isaac   Input Parameters:
6239c58f1c22SToby Isaac + dm   - The DM object
6240c58f1c22SToby Isaac - name - The label name
6241c58f1c22SToby Isaac 
6242c58f1c22SToby Isaac   Level: intermediate
6243c58f1c22SToby Isaac 
6244db781477SPatrick Sanan .seealso: `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6245c58f1c22SToby Isaac @*/
6246c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6247c58f1c22SToby Isaac {
62485d80c0bfSVaclav Hapla   PetscBool      flg;
62495d80c0bfSVaclav Hapla   DMLabel        label;
6250c58f1c22SToby Isaac 
6251c58f1c22SToby Isaac   PetscFunctionBegin;
6252c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6253c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
62549566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6255c58f1c22SToby Isaac   if (!flg) {
62569566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
62579566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
62589566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6259c58f1c22SToby Isaac   }
6260c58f1c22SToby Isaac   PetscFunctionReturn(0);
6261c58f1c22SToby Isaac }
6262c58f1c22SToby Isaac 
6263c58f1c22SToby Isaac /*@C
62647cf350d0SPierre Jolivet   DMCreateLabelAtIndex - Create a label of the given name at the given index. If it already exists, move it to this index.
62650fdc7489SMatthew Knepley 
62660fdc7489SMatthew Knepley   Not Collective
62670fdc7489SMatthew Knepley 
62680fdc7489SMatthew Knepley   Input Parameters:
62690fdc7489SMatthew Knepley + dm   - The DM object
62700fdc7489SMatthew Knepley . l    - The index for the label
62710fdc7489SMatthew Knepley - name - The label name
62720fdc7489SMatthew Knepley 
62730fdc7489SMatthew Knepley   Level: intermediate
62740fdc7489SMatthew Knepley 
6275db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
62760fdc7489SMatthew Knepley @*/
62770fdc7489SMatthew Knepley PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
62780fdc7489SMatthew Knepley {
62790fdc7489SMatthew Knepley   DMLabelLink    orig, prev = NULL;
62800fdc7489SMatthew Knepley   DMLabel        label;
62810fdc7489SMatthew Knepley   PetscInt       Nl, m;
62820fdc7489SMatthew Knepley   PetscBool      flg, match;
62830fdc7489SMatthew Knepley   const char    *lname;
62840fdc7489SMatthew Knepley 
62850fdc7489SMatthew Knepley   PetscFunctionBegin;
62860fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6287064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
62889566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
62890fdc7489SMatthew Knepley   if (!flg) {
62909566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
62919566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
62929566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
62930fdc7489SMatthew Knepley   }
62949566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
629563a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
62960fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
62979566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) orig->label, &lname));
62989566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
62990fdc7489SMatthew Knepley     if (match) break;
63000fdc7489SMatthew Knepley   }
63010fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
63020fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
63030fdc7489SMatthew Knepley   else    prev->next = orig->next;
63040fdc7489SMatthew Knepley   if (!l) {
63050fdc7489SMatthew Knepley     orig->next = dm->labels;
63060fdc7489SMatthew Knepley     dm->labels = orig;
63070fdc7489SMatthew Knepley   } else {
63080fdc7489SMatthew Knepley     for (m = 0, prev = dm->labels; m < l-1; ++m, prev = prev->next);
63090fdc7489SMatthew Knepley     orig->next = prev->next;
63100fdc7489SMatthew Knepley     prev->next = orig;
63110fdc7489SMatthew Knepley   }
63120fdc7489SMatthew Knepley   PetscFunctionReturn(0);
63130fdc7489SMatthew Knepley }
63140fdc7489SMatthew Knepley 
63150fdc7489SMatthew Knepley /*@C
63167cf350d0SPierre Jolivet   DMGetLabelValue - Get the value in a DMLabel for the given point, with -1 as the default
6317c58f1c22SToby Isaac 
6318c58f1c22SToby Isaac   Not Collective
6319c58f1c22SToby Isaac 
6320c58f1c22SToby Isaac   Input Parameters:
6321c58f1c22SToby Isaac + dm   - The DM object
6322c58f1c22SToby Isaac . name - The label name
6323c58f1c22SToby Isaac - point - The mesh point
6324c58f1c22SToby Isaac 
6325c58f1c22SToby Isaac   Output Parameter:
6326c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6327c58f1c22SToby Isaac 
6328c58f1c22SToby Isaac   Level: beginner
6329c58f1c22SToby Isaac 
6330db781477SPatrick Sanan .seealso: `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6331c58f1c22SToby Isaac @*/
6332c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6333c58f1c22SToby Isaac {
6334c58f1c22SToby Isaac   DMLabel        label;
6335c58f1c22SToby Isaac 
6336c58f1c22SToby Isaac   PetscFunctionBegin;
6337c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6338c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
63399566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
63407a8be351SBarry Smith   PetscCheck(label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
63419566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
6342c58f1c22SToby Isaac   PetscFunctionReturn(0);
6343c58f1c22SToby Isaac }
6344c58f1c22SToby Isaac 
6345c58f1c22SToby Isaac /*@C
63467cf350d0SPierre Jolivet   DMSetLabelValue - Add a point to a DMLabel with given value
6347c58f1c22SToby Isaac 
6348c58f1c22SToby Isaac   Not Collective
6349c58f1c22SToby Isaac 
6350c58f1c22SToby Isaac   Input Parameters:
6351c58f1c22SToby Isaac + dm   - The DM object
6352c58f1c22SToby Isaac . name - The label name
6353c58f1c22SToby Isaac . point - The mesh point
6354c58f1c22SToby Isaac - value - The label value for this point
6355c58f1c22SToby Isaac 
6356c58f1c22SToby Isaac   Output Parameter:
6357c58f1c22SToby Isaac 
6358c58f1c22SToby Isaac   Level: beginner
6359c58f1c22SToby Isaac 
6360db781477SPatrick Sanan .seealso: `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6361c58f1c22SToby Isaac @*/
6362c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6363c58f1c22SToby Isaac {
6364c58f1c22SToby Isaac   DMLabel        label;
6365c58f1c22SToby Isaac 
6366c58f1c22SToby Isaac   PetscFunctionBegin;
6367c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6368c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
63699566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6370c58f1c22SToby Isaac   if (!label) {
63719566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
63729566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6373c58f1c22SToby Isaac   }
63749566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
6375c58f1c22SToby Isaac   PetscFunctionReturn(0);
6376c58f1c22SToby Isaac }
6377c58f1c22SToby Isaac 
6378c58f1c22SToby Isaac /*@C
63797cf350d0SPierre Jolivet   DMClearLabelValue - Remove a point from a DMLabel with given value
6380c58f1c22SToby Isaac 
6381c58f1c22SToby Isaac   Not Collective
6382c58f1c22SToby Isaac 
6383c58f1c22SToby Isaac   Input Parameters:
6384c58f1c22SToby Isaac + dm   - The DM object
6385c58f1c22SToby Isaac . name - The label name
6386c58f1c22SToby Isaac . point - The mesh point
6387c58f1c22SToby Isaac - value - The label value for this point
6388c58f1c22SToby Isaac 
6389c58f1c22SToby Isaac   Output Parameter:
6390c58f1c22SToby Isaac 
6391c58f1c22SToby Isaac   Level: beginner
6392c58f1c22SToby Isaac 
6393db781477SPatrick Sanan .seealso: `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6394c58f1c22SToby Isaac @*/
6395c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6396c58f1c22SToby Isaac {
6397c58f1c22SToby Isaac   DMLabel        label;
6398c58f1c22SToby Isaac 
6399c58f1c22SToby Isaac   PetscFunctionBegin;
6400c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6401c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
64029566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6403c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
64049566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
6405c58f1c22SToby Isaac   PetscFunctionReturn(0);
6406c58f1c22SToby Isaac }
6407c58f1c22SToby Isaac 
6408c58f1c22SToby Isaac /*@C
6409c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6410c58f1c22SToby Isaac 
6411c58f1c22SToby Isaac   Not Collective
6412c58f1c22SToby Isaac 
6413c58f1c22SToby Isaac   Input Parameters:
6414c58f1c22SToby Isaac + dm   - The DM object
6415c58f1c22SToby Isaac - name - The label name
6416c58f1c22SToby Isaac 
6417c58f1c22SToby Isaac   Output Parameter:
6418c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6419c58f1c22SToby Isaac 
6420c58f1c22SToby Isaac   Level: beginner
6421c58f1c22SToby Isaac 
6422db781477SPatrick Sanan .seealso: `DMLabelGetNumValues()`, `DMSetLabelValue()`
6423c58f1c22SToby Isaac @*/
6424c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6425c58f1c22SToby Isaac {
6426c58f1c22SToby Isaac   DMLabel        label;
6427c58f1c22SToby Isaac 
6428c58f1c22SToby Isaac   PetscFunctionBegin;
6429c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6430c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6431534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
64329566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6433c58f1c22SToby Isaac   *size = 0;
6434c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
64359566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
6436c58f1c22SToby Isaac   PetscFunctionReturn(0);
6437c58f1c22SToby Isaac }
6438c58f1c22SToby Isaac 
6439c58f1c22SToby Isaac /*@C
6440c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
6441c58f1c22SToby Isaac 
6442c58f1c22SToby Isaac   Not Collective
6443c58f1c22SToby Isaac 
6444c58f1c22SToby Isaac   Input Parameters:
6445c58f1c22SToby Isaac + mesh - The DM object
6446c58f1c22SToby Isaac - name - The label name
6447c58f1c22SToby Isaac 
6448c58f1c22SToby Isaac   Output Parameter:
6449c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6450c58f1c22SToby Isaac 
6451c58f1c22SToby Isaac   Level: beginner
6452c58f1c22SToby Isaac 
6453db781477SPatrick Sanan .seealso: `DMLabelGetValueIS()`, `DMGetLabelSize()`
6454c58f1c22SToby Isaac @*/
6455c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6456c58f1c22SToby Isaac {
6457c58f1c22SToby Isaac   DMLabel        label;
6458c58f1c22SToby Isaac 
6459c58f1c22SToby Isaac   PetscFunctionBegin;
6460c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6461c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6462c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
64639566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6464c58f1c22SToby Isaac   *ids = NULL;
6465dab2e251SBlaise Bourdin   if (label) {
64669566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6467dab2e251SBlaise Bourdin   } else {
6468dab2e251SBlaise Bourdin     /* returning an empty IS */
64699566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids));
6470dab2e251SBlaise Bourdin   }
6471c58f1c22SToby Isaac   PetscFunctionReturn(0);
6472c58f1c22SToby Isaac }
6473c58f1c22SToby Isaac 
6474c58f1c22SToby Isaac /*@C
6475c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6476c58f1c22SToby Isaac 
6477c58f1c22SToby Isaac   Not Collective
6478c58f1c22SToby Isaac 
6479c58f1c22SToby Isaac   Input Parameters:
6480c58f1c22SToby Isaac + dm - The DM object
6481c58f1c22SToby Isaac . name - The label name
6482c58f1c22SToby Isaac - value - The stratum value
6483c58f1c22SToby Isaac 
6484c58f1c22SToby Isaac   Output Parameter:
6485c58f1c22SToby Isaac . size - The stratum size
6486c58f1c22SToby Isaac 
6487c58f1c22SToby Isaac   Level: beginner
6488c58f1c22SToby Isaac 
6489db781477SPatrick Sanan .seealso: `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6490c58f1c22SToby Isaac @*/
6491c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6492c58f1c22SToby Isaac {
6493c58f1c22SToby Isaac   DMLabel        label;
6494c58f1c22SToby Isaac 
6495c58f1c22SToby Isaac   PetscFunctionBegin;
6496c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6497c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6498534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
64999566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6500c58f1c22SToby Isaac   *size = 0;
6501c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
65029566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
6503c58f1c22SToby Isaac   PetscFunctionReturn(0);
6504c58f1c22SToby Isaac }
6505c58f1c22SToby Isaac 
6506c58f1c22SToby Isaac /*@C
6507c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6508c58f1c22SToby Isaac 
6509c58f1c22SToby Isaac   Not Collective
6510c58f1c22SToby Isaac 
6511c58f1c22SToby Isaac   Input Parameters:
6512c58f1c22SToby Isaac + dm - The DM object
6513c58f1c22SToby Isaac . name - The label name
6514c58f1c22SToby Isaac - value - The stratum value
6515c58f1c22SToby Isaac 
6516c58f1c22SToby Isaac   Output Parameter:
6517c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6518c58f1c22SToby Isaac 
6519c58f1c22SToby Isaac   Level: beginner
6520c58f1c22SToby Isaac 
6521db781477SPatrick Sanan .seealso: `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6522c58f1c22SToby Isaac @*/
6523c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6524c58f1c22SToby Isaac {
6525c58f1c22SToby Isaac   DMLabel        label;
6526c58f1c22SToby Isaac 
6527c58f1c22SToby Isaac   PetscFunctionBegin;
6528c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6529c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6530c58f1c22SToby Isaac   PetscValidPointer(points, 4);
65319566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6532c58f1c22SToby Isaac   *points = NULL;
6533c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
65349566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
6535c58f1c22SToby Isaac   PetscFunctionReturn(0);
6536c58f1c22SToby Isaac }
6537c58f1c22SToby Isaac 
65384de306b1SToby Isaac /*@C
65399044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
65404de306b1SToby Isaac 
65414de306b1SToby Isaac   Not Collective
65424de306b1SToby Isaac 
65434de306b1SToby Isaac   Input Parameters:
65444de306b1SToby Isaac + dm - The DM object
65454de306b1SToby Isaac . name - The label name
65464de306b1SToby Isaac . value - The stratum value
65474de306b1SToby Isaac - points - The stratum points
65484de306b1SToby Isaac 
65494de306b1SToby Isaac   Level: beginner
65504de306b1SToby Isaac 
6551db781477SPatrick Sanan .seealso: `DMLabelSetStratumIS()`, `DMGetStratumSize()`
65524de306b1SToby Isaac @*/
65534de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
65544de306b1SToby Isaac {
65554de306b1SToby Isaac   DMLabel        label;
65564de306b1SToby Isaac 
65574de306b1SToby Isaac   PetscFunctionBegin;
65584de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65594de306b1SToby Isaac   PetscValidCharPointer(name, 2);
65604de306b1SToby Isaac   PetscValidPointer(points, 4);
65619566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
65624de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
65639566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
65644de306b1SToby Isaac   PetscFunctionReturn(0);
65654de306b1SToby Isaac }
65664de306b1SToby Isaac 
6567c58f1c22SToby Isaac /*@C
65687cf350d0SPierre Jolivet   DMClearLabelStratum - Remove all points from a stratum from a DMLabel
6569c58f1c22SToby Isaac 
6570c58f1c22SToby Isaac   Not Collective
6571c58f1c22SToby Isaac 
6572c58f1c22SToby Isaac   Input Parameters:
6573c58f1c22SToby Isaac + dm   - The DM object
6574c58f1c22SToby Isaac . name - The label name
6575c58f1c22SToby Isaac - value - The label value for this point
6576c58f1c22SToby Isaac 
6577c58f1c22SToby Isaac   Output Parameter:
6578c58f1c22SToby Isaac 
6579c58f1c22SToby Isaac   Level: beginner
6580c58f1c22SToby Isaac 
6581db781477SPatrick Sanan .seealso: `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6582c58f1c22SToby Isaac @*/
6583c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6584c58f1c22SToby Isaac {
6585c58f1c22SToby Isaac   DMLabel        label;
6586c58f1c22SToby Isaac 
6587c58f1c22SToby Isaac   PetscFunctionBegin;
6588c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6589c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
65909566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6591c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
65929566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
6593c58f1c22SToby Isaac   PetscFunctionReturn(0);
6594c58f1c22SToby Isaac }
6595c58f1c22SToby Isaac 
6596c58f1c22SToby Isaac /*@
6597c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
6598c58f1c22SToby Isaac 
6599c58f1c22SToby Isaac   Not Collective
6600c58f1c22SToby Isaac 
6601c58f1c22SToby Isaac   Input Parameter:
6602c58f1c22SToby Isaac . dm   - The DM object
6603c58f1c22SToby Isaac 
6604c58f1c22SToby Isaac   Output Parameter:
6605c58f1c22SToby Isaac . numLabels - the number of Labels
6606c58f1c22SToby Isaac 
6607c58f1c22SToby Isaac   Level: intermediate
6608c58f1c22SToby Isaac 
6609db781477SPatrick Sanan .seealso: `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6610c58f1c22SToby Isaac @*/
6611c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6612c58f1c22SToby Isaac {
66135d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6614c58f1c22SToby Isaac   PetscInt  n    = 0;
6615c58f1c22SToby Isaac 
6616c58f1c22SToby Isaac   PetscFunctionBegin;
6617c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6618534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
6619c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
6620c58f1c22SToby Isaac   *numLabels = n;
6621c58f1c22SToby Isaac   PetscFunctionReturn(0);
6622c58f1c22SToby Isaac }
6623c58f1c22SToby Isaac 
6624c58f1c22SToby Isaac /*@C
6625c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6626c58f1c22SToby Isaac 
6627c58f1c22SToby Isaac   Not Collective
6628c58f1c22SToby Isaac 
6629c58f1c22SToby Isaac   Input Parameters:
6630c58f1c22SToby Isaac + dm - The DM object
6631c58f1c22SToby Isaac - n  - the label number
6632c58f1c22SToby Isaac 
6633c58f1c22SToby Isaac   Output Parameter:
6634c58f1c22SToby Isaac . name - the label name
6635c58f1c22SToby Isaac 
6636c58f1c22SToby Isaac   Level: intermediate
6637c58f1c22SToby Isaac 
6638db781477SPatrick Sanan .seealso: `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6639c58f1c22SToby Isaac @*/
6640c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6641c58f1c22SToby Isaac {
66425d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
6643c58f1c22SToby Isaac   PetscInt       l    = 0;
6644c58f1c22SToby Isaac 
6645c58f1c22SToby Isaac   PetscFunctionBegin;
6646c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6647c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6648c58f1c22SToby Isaac   while (next) {
6649c58f1c22SToby Isaac     if (l == n) {
66509566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject) next->label, name));
6651c58f1c22SToby Isaac       PetscFunctionReturn(0);
6652c58f1c22SToby Isaac     }
6653c58f1c22SToby Isaac     ++l;
6654c58f1c22SToby Isaac     next = next->next;
6655c58f1c22SToby Isaac   }
665663a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6657c58f1c22SToby Isaac }
6658c58f1c22SToby Isaac 
6659c58f1c22SToby Isaac /*@C
6660c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
6661c58f1c22SToby Isaac 
6662c58f1c22SToby Isaac   Not Collective
6663c58f1c22SToby Isaac 
6664c58f1c22SToby Isaac   Input Parameters:
6665c58f1c22SToby Isaac + dm   - The DM object
6666c58f1c22SToby Isaac - name - The label name
6667c58f1c22SToby Isaac 
6668c58f1c22SToby Isaac   Output Parameter:
6669c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
6670c58f1c22SToby Isaac 
6671c58f1c22SToby Isaac   Level: intermediate
6672c58f1c22SToby Isaac 
6673db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6674c58f1c22SToby Isaac @*/
6675c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6676c58f1c22SToby Isaac {
66775d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
6678d67d17b1SMatthew G. Knepley   const char    *lname;
6679c58f1c22SToby Isaac 
6680c58f1c22SToby Isaac   PetscFunctionBegin;
6681c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6682c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6683534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
6684c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6685c58f1c22SToby Isaac   while (next) {
66869566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
66879566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
6688c58f1c22SToby Isaac     if (*hasLabel) break;
6689c58f1c22SToby Isaac     next = next->next;
6690c58f1c22SToby Isaac   }
6691c58f1c22SToby Isaac   PetscFunctionReturn(0);
6692c58f1c22SToby Isaac }
6693c58f1c22SToby Isaac 
6694c58f1c22SToby Isaac /*@C
6695c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
6696c58f1c22SToby Isaac 
6697c58f1c22SToby Isaac   Not Collective
6698c58f1c22SToby Isaac 
6699c58f1c22SToby Isaac   Input Parameters:
6700c58f1c22SToby Isaac + dm   - The DM object
6701c58f1c22SToby Isaac - name - The label name
6702c58f1c22SToby Isaac 
6703c58f1c22SToby Isaac   Output Parameter:
6704c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6705c58f1c22SToby Isaac 
67066d7c9049SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
67076d7c9049SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
67086d7c9049SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
67096d7c9049SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
67106d7c9049SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
67116d7c9049SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
67126d7c9049SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
67136d7c9049SMatthew G. Knepley 
6714c58f1c22SToby Isaac   Level: intermediate
6715c58f1c22SToby Isaac 
6716db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
6717c58f1c22SToby Isaac @*/
6718c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6719c58f1c22SToby Isaac {
67205d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
6721c58f1c22SToby Isaac   PetscBool      hasLabel;
6722d67d17b1SMatthew G. Knepley   const char    *lname;
6723c58f1c22SToby Isaac 
6724c58f1c22SToby Isaac   PetscFunctionBegin;
6725c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6726c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6727c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6728c58f1c22SToby Isaac   *label = NULL;
6729c58f1c22SToby Isaac   while (next) {
67309566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
67319566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
6732c58f1c22SToby Isaac     if (hasLabel) {
6733c58f1c22SToby Isaac       *label = next->label;
6734c58f1c22SToby Isaac       break;
6735c58f1c22SToby Isaac     }
6736c58f1c22SToby Isaac     next = next->next;
6737c58f1c22SToby Isaac   }
6738c58f1c22SToby Isaac   PetscFunctionReturn(0);
6739c58f1c22SToby Isaac }
6740c58f1c22SToby Isaac 
6741c58f1c22SToby Isaac /*@C
6742c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
6743c58f1c22SToby Isaac 
6744c58f1c22SToby Isaac   Not Collective
6745c58f1c22SToby Isaac 
6746c58f1c22SToby Isaac   Input Parameters:
6747c58f1c22SToby Isaac + dm - The DM object
6748c58f1c22SToby Isaac - n  - the label number
6749c58f1c22SToby Isaac 
6750c58f1c22SToby Isaac   Output Parameter:
6751c58f1c22SToby Isaac . label - the label
6752c58f1c22SToby Isaac 
6753c58f1c22SToby Isaac   Level: intermediate
6754c58f1c22SToby Isaac 
6755db781477SPatrick Sanan .seealso: `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6756c58f1c22SToby Isaac @*/
6757c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
6758c58f1c22SToby Isaac {
67595d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6760c58f1c22SToby Isaac   PetscInt    l    = 0;
6761c58f1c22SToby Isaac 
6762c58f1c22SToby Isaac   PetscFunctionBegin;
6763c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6764c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6765c58f1c22SToby Isaac   while (next) {
6766c58f1c22SToby Isaac     if (l == n) {
6767c58f1c22SToby Isaac       *label = next->label;
6768c58f1c22SToby Isaac       PetscFunctionReturn(0);
6769c58f1c22SToby Isaac     }
6770c58f1c22SToby Isaac     ++l;
6771c58f1c22SToby Isaac     next = next->next;
6772c58f1c22SToby Isaac   }
677363a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6774c58f1c22SToby Isaac }
6775c58f1c22SToby Isaac 
6776c58f1c22SToby Isaac /*@C
6777c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
6778c58f1c22SToby Isaac 
6779c58f1c22SToby Isaac   Not Collective
6780c58f1c22SToby Isaac 
6781c58f1c22SToby Isaac   Input Parameters:
6782c58f1c22SToby Isaac + dm   - The DM object
6783c58f1c22SToby Isaac - label - The DMLabel
6784c58f1c22SToby Isaac 
6785c58f1c22SToby Isaac   Level: developer
6786c58f1c22SToby Isaac 
6787db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6788c58f1c22SToby Isaac @*/
6789c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
6790c58f1c22SToby Isaac {
67915d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
6792c58f1c22SToby Isaac   PetscBool      hasLabel;
6793d67d17b1SMatthew G. Knepley   const char    *lname;
67945d80c0bfSVaclav Hapla   PetscBool      flg;
6795c58f1c22SToby Isaac 
6796c58f1c22SToby Isaac   PetscFunctionBegin;
6797c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67989566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject) label, &lname));
67999566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
68007a8be351SBarry Smith   PetscCheck(!hasLabel,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
68019566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
6802c58f1c22SToby Isaac   tmpLabel->label  = label;
6803c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
68045d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
68055d80c0bfSVaclav Hapla   *p = tmpLabel;
68069566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
68079566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
68085d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
68099566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
6810ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
6811c58f1c22SToby Isaac   PetscFunctionReturn(0);
6812c58f1c22SToby Isaac }
6813c58f1c22SToby Isaac 
6814c58f1c22SToby Isaac /*@C
68154a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
68164a7ee7d0SMatthew G. Knepley 
68174a7ee7d0SMatthew G. Knepley   Not Collective
68184a7ee7d0SMatthew G. Knepley 
68194a7ee7d0SMatthew G. Knepley   Input Parameters:
68204a7ee7d0SMatthew G. Knepley + dm    - The DM object
68214a7ee7d0SMatthew G. Knepley - label - The DMLabel, having the same name, to substitute
68224a7ee7d0SMatthew G. Knepley 
68234a7ee7d0SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
68244a7ee7d0SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
68254a7ee7d0SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
68264a7ee7d0SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
68274a7ee7d0SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
68284a7ee7d0SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
68294a7ee7d0SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
68304a7ee7d0SMatthew G. Knepley 
68314a7ee7d0SMatthew G. Knepley   Level: intermediate
68324a7ee7d0SMatthew G. Knepley 
6833db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
68344a7ee7d0SMatthew G. Knepley @*/
68354a7ee7d0SMatthew G. Knepley PetscErrorCode DMSetLabel(DM dm, DMLabel label)
68364a7ee7d0SMatthew G. Knepley {
68374a7ee7d0SMatthew G. Knepley   DMLabelLink    next = dm->labels;
68384a7ee7d0SMatthew G. Knepley   PetscBool      hasLabel, flg;
68394a7ee7d0SMatthew G. Knepley   const char    *name, *lname;
68404a7ee7d0SMatthew G. Knepley 
68414a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
68424a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68434a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
68449566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject) label, &name));
68454a7ee7d0SMatthew G. Knepley   while (next) {
68469566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
68479566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
68484a7ee7d0SMatthew G. Knepley     if (hasLabel) {
68499566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject) label));
68509566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
68514a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
68529566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
68534a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
68549566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
68554a7ee7d0SMatthew G. Knepley       next->label = label;
68564a7ee7d0SMatthew G. Knepley       break;
68574a7ee7d0SMatthew G. Knepley     }
68584a7ee7d0SMatthew G. Knepley     next = next->next;
68594a7ee7d0SMatthew G. Knepley   }
68604a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
68614a7ee7d0SMatthew G. Knepley }
68624a7ee7d0SMatthew G. Knepley 
68634a7ee7d0SMatthew G. Knepley /*@C
6864e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
6865c58f1c22SToby Isaac 
6866c58f1c22SToby Isaac   Not Collective
6867c58f1c22SToby Isaac 
6868c58f1c22SToby Isaac   Input Parameters:
6869c58f1c22SToby Isaac + dm   - The DM object
6870c58f1c22SToby Isaac - name - The label name
6871c58f1c22SToby Isaac 
6872c58f1c22SToby Isaac   Output Parameter:
6873c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6874c58f1c22SToby Isaac 
6875c58f1c22SToby Isaac   Level: developer
6876c58f1c22SToby Isaac 
6877e5472504SVaclav Hapla   Notes:
6878e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
6879e5472504SVaclav Hapla   DMLabelDestroy() on the label.
6880e5472504SVaclav Hapla 
6881e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
6882e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
6883e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
6884e5472504SVaclav Hapla 
6885db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
6886c58f1c22SToby Isaac @*/
6887c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
6888c58f1c22SToby Isaac {
688995d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
6890c58f1c22SToby Isaac   PetscBool      hasLabel;
6891d67d17b1SMatthew G. Knepley   const char    *lname;
6892c58f1c22SToby Isaac 
6893c58f1c22SToby Isaac   PetscFunctionBegin;
6894c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6895e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
6896e5472504SVaclav Hapla   if (label) {
6897e5472504SVaclav Hapla     PetscValidPointer(label, 3);
6898c58f1c22SToby Isaac     *label = NULL;
6899e5472504SVaclav Hapla   }
69005d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
69019566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) link->label, &lname));
69029566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
6903c58f1c22SToby Isaac     if (hasLabel) {
690495d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
69059566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
690695d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
69079566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
6908ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
690995d578d6SVaclav Hapla       if (label) *label = link->label;
69109566063dSJacob Faibussowitsch       else       PetscCall(DMLabelDestroy(&link->label));
69119566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
6912c58f1c22SToby Isaac       break;
6913c58f1c22SToby Isaac     }
6914c58f1c22SToby Isaac   }
6915c58f1c22SToby Isaac   PetscFunctionReturn(0);
6916c58f1c22SToby Isaac }
6917c58f1c22SToby Isaac 
6918306894acSVaclav Hapla /*@
6919306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
6920306894acSVaclav Hapla 
6921306894acSVaclav Hapla   Not Collective
6922306894acSVaclav Hapla 
6923306894acSVaclav Hapla   Input Parameters:
6924306894acSVaclav Hapla + dm   - The DM object
6925876aa926SVaclav Hapla . label - The DMLabel to be removed from the DM
6926306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
6927306894acSVaclav Hapla 
6928306894acSVaclav Hapla   Level: developer
6929306894acSVaclav Hapla 
6930306894acSVaclav Hapla   Notes:
6931306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
6932306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
6933306894acSVaclav Hapla   *label nullified.
6934306894acSVaclav Hapla 
6935db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
6936306894acSVaclav Hapla @*/
6937306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
6938306894acSVaclav Hapla {
693943e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
6940306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
6941306894acSVaclav Hapla 
6942306894acSVaclav Hapla   PetscFunctionBegin;
6943306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6944306894acSVaclav Hapla   PetscValidPointer(label, 2);
6945f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
6946306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
6947306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
69485d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
694943e45a93SVaclav Hapla     if (*label == link->label) {
6950306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
695143e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
6952306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
6953ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
695443e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
69559566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
69569566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
6957306894acSVaclav Hapla       break;
6958306894acSVaclav Hapla     }
6959306894acSVaclav Hapla   }
69607a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
6961306894acSVaclav Hapla   PetscFunctionReturn(0);
6962306894acSVaclav Hapla }
6963306894acSVaclav Hapla 
6964c58f1c22SToby Isaac /*@C
6965c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
6966c58f1c22SToby Isaac 
6967c58f1c22SToby Isaac   Not Collective
6968c58f1c22SToby Isaac 
6969c58f1c22SToby Isaac   Input Parameters:
6970c58f1c22SToby Isaac + dm   - The DM object
6971c58f1c22SToby Isaac - name - The label name
6972c58f1c22SToby Isaac 
6973c58f1c22SToby Isaac   Output Parameter:
6974c58f1c22SToby Isaac . output - The flag for output
6975c58f1c22SToby Isaac 
6976c58f1c22SToby Isaac   Level: developer
6977c58f1c22SToby Isaac 
6978db781477SPatrick Sanan .seealso: `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6979c58f1c22SToby Isaac @*/
6980c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
6981c58f1c22SToby Isaac {
69825d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
6983d67d17b1SMatthew G. Knepley   const char    *lname;
6984c58f1c22SToby Isaac 
6985c58f1c22SToby Isaac   PetscFunctionBegin;
6986c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6987dadcf809SJacob Faibussowitsch   PetscValidCharPointer(name, 2);
6988dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(output, 3);
6989c58f1c22SToby Isaac   while (next) {
6990c58f1c22SToby Isaac     PetscBool flg;
6991c58f1c22SToby Isaac 
69929566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
69939566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
6994c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
6995c58f1c22SToby Isaac     next = next->next;
6996c58f1c22SToby Isaac   }
699798921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6998c58f1c22SToby Isaac }
6999c58f1c22SToby Isaac 
7000c58f1c22SToby Isaac /*@C
7001c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7002c58f1c22SToby Isaac 
7003c58f1c22SToby Isaac   Not Collective
7004c58f1c22SToby Isaac 
7005c58f1c22SToby Isaac   Input Parameters:
7006c58f1c22SToby Isaac + dm     - The DM object
7007c58f1c22SToby Isaac . name   - The label name
7008c58f1c22SToby Isaac - output - The flag for output
7009c58f1c22SToby Isaac 
7010c58f1c22SToby Isaac   Level: developer
7011c58f1c22SToby Isaac 
7012db781477SPatrick Sanan .seealso: `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7013c58f1c22SToby Isaac @*/
7014c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7015c58f1c22SToby Isaac {
70165d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7017d67d17b1SMatthew G. Knepley   const char    *lname;
7018c58f1c22SToby Isaac 
7019c58f1c22SToby Isaac   PetscFunctionBegin;
7020c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7021534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7022c58f1c22SToby Isaac   while (next) {
7023c58f1c22SToby Isaac     PetscBool flg;
7024c58f1c22SToby Isaac 
70259566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
70269566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
7027c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7028c58f1c22SToby Isaac     next = next->next;
7029c58f1c22SToby Isaac   }
703098921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7031c58f1c22SToby Isaac }
7032c58f1c22SToby Isaac 
7033c58f1c22SToby Isaac /*@
7034c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7035c58f1c22SToby Isaac 
7036d083f849SBarry Smith   Collective on dmA
7037c58f1c22SToby Isaac 
7038d8d19677SJose E. Roman   Input Parameters:
70395d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
70402cbb9b06SVaclav Hapla . dmB - The DM object to which labels are copied
70415d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
70422cbb9b06SVaclav Hapla . all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
70432cbb9b06SVaclav Hapla - emode - How to behave when a DMLabel in the source and destination DMs with the same name is encountered (see DMCopyLabelsMode)
7044c58f1c22SToby Isaac 
7045c58f1c22SToby Isaac   Level: intermediate
7046c58f1c22SToby Isaac 
70472cbb9b06SVaclav Hapla   Notes:
70482cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7049c58f1c22SToby Isaac 
7050db781477SPatrick Sanan .seealso: `DMAddLabel()`, `DMCopyLabelsMode`
7051c58f1c22SToby Isaac @*/
70522cbb9b06SVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7053c58f1c22SToby Isaac {
70542cbb9b06SVaclav Hapla   DMLabel        label, labelNew, labelOld;
7055c58f1c22SToby Isaac   const char    *name;
7056c58f1c22SToby Isaac   PetscBool      flg;
70575d80c0bfSVaclav Hapla   DMLabelLink    link;
7058c58f1c22SToby Isaac 
70595d80c0bfSVaclav Hapla   PetscFunctionBegin;
70605d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
70615d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
70625d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
70635d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
70647a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER,PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
70655d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
70665d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
70675d80c0bfSVaclav Hapla     label=link->label;
70689566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
70695d80c0bfSVaclav Hapla     if (!all) {
70709566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7071c58f1c22SToby Isaac       if (flg) continue;
70729566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
70737d5acc75SStefano Zampini       if (flg) continue;
70749566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7075ba2698f1SMatthew G. Knepley       if (flg) continue;
70765d80c0bfSVaclav Hapla     }
70779566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
70782cbb9b06SVaclav Hapla     if (labelOld) {
70792cbb9b06SVaclav Hapla       switch (emode) {
70802cbb9b06SVaclav Hapla         case DM_COPY_LABELS_KEEP:
70812cbb9b06SVaclav Hapla           continue;
70822cbb9b06SVaclav Hapla         case DM_COPY_LABELS_REPLACE:
70839566063dSJacob Faibussowitsch           PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
70842cbb9b06SVaclav Hapla           break;
70852cbb9b06SVaclav Hapla         case DM_COPY_LABELS_FAIL:
708698921bdaSJacob Faibussowitsch           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
70872cbb9b06SVaclav Hapla         default:
70887a8be351SBarry Smith           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
70892cbb9b06SVaclav Hapla       }
70902cbb9b06SVaclav Hapla     }
70915d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
70929566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
70935d80c0bfSVaclav Hapla     } else {
70945d80c0bfSVaclav Hapla       labelNew = label;
70955d80c0bfSVaclav Hapla     }
70969566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
70979566063dSJacob Faibussowitsch     if (mode==PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7098c58f1c22SToby Isaac   }
7099c58f1c22SToby Isaac   PetscFunctionReturn(0);
7100c58f1c22SToby Isaac }
7101461a15a0SLisandro Dalcin 
7102609dae6eSVaclav Hapla /*@C
7103609dae6eSVaclav Hapla   DMCompareLabels - Compare labels of two DMPlex meshes
7104609dae6eSVaclav Hapla 
71055efe38ccSVaclav Hapla   Collective
7106609dae6eSVaclav Hapla 
7107609dae6eSVaclav Hapla   Input Parameters:
7108609dae6eSVaclav Hapla + dm0 - First DM object
7109609dae6eSVaclav Hapla - dm1 - Second DM object
7110609dae6eSVaclav Hapla 
7111609dae6eSVaclav Hapla   Output Parameters
71125efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
7113609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
7114609dae6eSVaclav Hapla 
7115609dae6eSVaclav Hapla   Level: intermediate
7116609dae6eSVaclav Hapla 
7117609dae6eSVaclav Hapla   Notes:
71185efe38ccSVaclav Hapla   The output flag equal is the same on all processes.
71195efe38ccSVaclav Hapla   If it is passed as NULL and difference is found, an error is thrown on all processes.
71205efe38ccSVaclav Hapla   Make sure to pass NULL on all processes.
7121609dae6eSVaclav Hapla 
71225efe38ccSVaclav Hapla   The output message is set independently on each rank.
71235efe38ccSVaclav Hapla   It is set to NULL if no difference was found on the current rank. It must be freed by user.
71245efe38ccSVaclav Hapla   If message is passed as NULL and difference is found, the difference description is printed to stderr in synchronized manner.
71255efe38ccSVaclav Hapla   Make sure to pass NULL on all processes.
7126609dae6eSVaclav Hapla 
7127609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7128609dae6eSVaclav Hapla   DMLabelCompare() is used to compare each pair of labels with the same name.
7129609dae6eSVaclav Hapla 
7130609dae6eSVaclav Hapla   Fortran Notes:
7131609dae6eSVaclav Hapla   This function is currently not available from Fortran.
7132609dae6eSVaclav Hapla 
7133db781477SPatrick Sanan .seealso: `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7134609dae6eSVaclav Hapla @*/
71355efe38ccSVaclav Hapla PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7136609dae6eSVaclav Hapla {
71375efe38ccSVaclav Hapla   PetscInt        n, i;
7138609dae6eSVaclav Hapla   char            msg[PETSC_MAX_PATH_LEN] = "";
71395efe38ccSVaclav Hapla   PetscBool       eq;
7140609dae6eSVaclav Hapla   MPI_Comm        comm;
71415efe38ccSVaclav Hapla   PetscMPIInt     rank;
7142609dae6eSVaclav Hapla 
7143609dae6eSVaclav Hapla   PetscFunctionBegin;
7144609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0,DM_CLASSID,1);
7145609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1,DM_CLASSID,2);
7146609dae6eSVaclav Hapla   PetscCheckSameComm(dm0,1,dm1,2);
71475efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal,3);
7148609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
71499566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
71509566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
71515efe38ccSVaclav Hapla   {
71525efe38ccSVaclav Hapla     PetscInt n1;
71535efe38ccSVaclav Hapla 
71549566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
71559566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
71565efe38ccSVaclav Hapla     eq = (PetscBool) (n == n1);
71575efe38ccSVaclav Hapla     if (!eq) {
715863a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7159609dae6eSVaclav Hapla     }
71609566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
71615efe38ccSVaclav Hapla     if (!eq) goto finish;
71625efe38ccSVaclav Hapla   }
71635efe38ccSVaclav Hapla   for (i=0; i<n; i++) {
7164609dae6eSVaclav Hapla     DMLabel     l0, l1;
7165609dae6eSVaclav Hapla     const char *name;
7166609dae6eSVaclav Hapla     char       *msgInner;
7167609dae6eSVaclav Hapla 
7168609dae6eSVaclav Hapla     /* Ignore label order */
71699566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
71709566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
71719566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7172609dae6eSVaclav Hapla     if (!l1) {
717363a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
71745efe38ccSVaclav Hapla       eq = PETSC_FALSE;
71755efe38ccSVaclav Hapla       break;
7176609dae6eSVaclav Hapla     }
71779566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
71789566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
71799566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
71805efe38ccSVaclav Hapla     if (!eq) break;
7181609dae6eSVaclav Hapla   }
71829566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7183609dae6eSVaclav Hapla finish:
71845efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7185609dae6eSVaclav Hapla   if (message) {
7186609dae6eSVaclav Hapla     *message = NULL;
7187609dae6eSVaclav Hapla     if (msg[0]) {
71889566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(msg, message));
7189609dae6eSVaclav Hapla     }
71905efe38ccSVaclav Hapla   } else {
71915efe38ccSVaclav Hapla     if (msg[0]) {
71929566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
7193609dae6eSVaclav Hapla     }
71949566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
71955efe38ccSVaclav Hapla   }
71965efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
71975efe38ccSVaclav Hapla   if (equal) *equal = eq;
71987a8be351SBarry Smith   else PetscCheck(eq,comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
7199609dae6eSVaclav Hapla   PetscFunctionReturn(0);
7200609dae6eSVaclav Hapla }
7201609dae6eSVaclav Hapla 
7202461a15a0SLisandro Dalcin PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7203461a15a0SLisandro Dalcin {
7204461a15a0SLisandro Dalcin   PetscFunctionBegin;
7205461a15a0SLisandro Dalcin   PetscValidPointer(label,2);
7206461a15a0SLisandro Dalcin   if (!*label) {
72079566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
72089566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7209461a15a0SLisandro Dalcin   }
72109566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
7211461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
7212461a15a0SLisandro Dalcin }
7213461a15a0SLisandro Dalcin 
72140fdc7489SMatthew Knepley /*
72150fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
72160fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
72170fdc7489SMatthew Knepley   (label, id) pair in the DM.
72180fdc7489SMatthew Knepley 
72190fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
72200fdc7489SMatthew Knepley   each label.
72210fdc7489SMatthew Knepley */
72220fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
72230fdc7489SMatthew Knepley {
72240fdc7489SMatthew Knepley   DMUniversalLabel ul;
72250fdc7489SMatthew Knepley   PetscBool       *active;
72260fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
72270fdc7489SMatthew Knepley 
72280fdc7489SMatthew Knepley   PetscFunctionBegin;
72299566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
72309566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
72319566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
72329566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
72330fdc7489SMatthew Knepley   ul->Nl = 0;
72340fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
72350fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
72360fdc7489SMatthew Knepley     const char *name;
72370fdc7489SMatthew Knepley 
72389566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
72399566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
72409566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
72410fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
72420fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
72430fdc7489SMatthew Knepley   }
72449566063dSJacob 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));
72450fdc7489SMatthew Knepley   ul->Nv = 0;
72460fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
72470fdc7489SMatthew Knepley     DMLabel     label;
72480fdc7489SMatthew Knepley     PetscInt    nv;
72490fdc7489SMatthew Knepley     const char *name;
72500fdc7489SMatthew Knepley 
72510fdc7489SMatthew Knepley     if (!active[l]) continue;
72529566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
72539566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
72549566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
72559566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
72560fdc7489SMatthew Knepley     ul->indices[m]   = l;
72570fdc7489SMatthew Knepley     ul->Nv          += nv;
72580fdc7489SMatthew Knepley     ul->offsets[m+1] = nv;
72590fdc7489SMatthew Knepley     ul->bits[m+1]    = PetscCeilReal(PetscLog2Real(nv+1));
72600fdc7489SMatthew Knepley     ++m;
72610fdc7489SMatthew Knepley   }
72620fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
72630fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l-1] + ul->offsets[l];
72640fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l-1]    + ul->bits[l];
72650fdc7489SMatthew Knepley   }
72660fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
72670fdc7489SMatthew Knepley     PetscInt b;
72680fdc7489SMatthew Knepley 
72690fdc7489SMatthew Knepley     ul->masks[l] = 0;
72700fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l+1]; ++b) ul->masks[l] |= 1 << b;
72710fdc7489SMatthew Knepley   }
72729566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
72730fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
72740fdc7489SMatthew Knepley     DMLabel         label;
72750fdc7489SMatthew Knepley     IS              valueIS;
72760fdc7489SMatthew Knepley     const PetscInt *varr;
72770fdc7489SMatthew Knepley     PetscInt        nv, v;
72780fdc7489SMatthew Knepley 
72790fdc7489SMatthew Knepley     if (!active[l]) continue;
72809566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
72819566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
72829566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
72839566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
72840fdc7489SMatthew Knepley     for (v = 0; v < nv; ++v) {
72850fdc7489SMatthew Knepley       ul->values[ul->offsets[m]+v] = varr[v];
72860fdc7489SMatthew Knepley     }
72879566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
72889566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
72899566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
72900fdc7489SMatthew Knepley     ++m;
72910fdc7489SMatthew Knepley   }
72929566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
72930fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
72940fdc7489SMatthew Knepley     PetscInt  uval = 0;
72950fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
72960fdc7489SMatthew Knepley 
72970fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
72980fdc7489SMatthew Knepley       DMLabel  label;
72990649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
73000fdc7489SMatthew Knepley 
73010fdc7489SMatthew Knepley       if (!active[l]) continue;
73029566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
73039566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
73049566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
73050fdc7489SMatthew Knepley       if (val == defval) {++m; continue;}
73060649b39aSStefano Zampini       nv = ul->offsets[m+1]-ul->offsets[m];
73070fdc7489SMatthew Knepley       marked = PETSC_TRUE;
73089566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
730963a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
73100fdc7489SMatthew Knepley       uval += (loc+1) << ul->bits[m];
73110fdc7489SMatthew Knepley       ++m;
73120fdc7489SMatthew Knepley     }
73139566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
73140fdc7489SMatthew Knepley   }
73159566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
73160fdc7489SMatthew Knepley   *universal = ul;
73170fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73180fdc7489SMatthew Knepley }
73190fdc7489SMatthew Knepley 
73200fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
73210fdc7489SMatthew Knepley {
73220fdc7489SMatthew Knepley   PetscInt       l;
73230fdc7489SMatthew Knepley 
73240fdc7489SMatthew Knepley   PetscFunctionBegin;
73259566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
73269566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
73279566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
73289566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
73299566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
73300fdc7489SMatthew Knepley   *universal = NULL;
73310fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73320fdc7489SMatthew Knepley }
73330fdc7489SMatthew Knepley 
73340fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
73350fdc7489SMatthew Knepley {
73360fdc7489SMatthew Knepley   PetscFunctionBegin;
73370fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
73380fdc7489SMatthew Knepley   *ulabel = ul->label;
73390fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73400fdc7489SMatthew Knepley }
73410fdc7489SMatthew Knepley 
73420fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
73430fdc7489SMatthew Knepley {
73440fdc7489SMatthew Knepley   PetscInt       Nl = ul->Nl, l;
73450fdc7489SMatthew Knepley 
73460fdc7489SMatthew Knepley   PetscFunctionBegin;
7347064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
73480fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
73499566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
73509566063dSJacob Faibussowitsch     else               PetscCall(DMCreateLabel(dm, ul->names[l]));
73510fdc7489SMatthew Knepley   }
73520fdc7489SMatthew Knepley   if (preserveOrder) {
73530fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
73540fdc7489SMatthew Knepley       const char *name;
73550fdc7489SMatthew Knepley       PetscBool   match;
73560fdc7489SMatthew Knepley 
73579566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
73589566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
735963a3b9bcSJacob 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]);
73600fdc7489SMatthew Knepley     }
73610fdc7489SMatthew Knepley   }
73620fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73630fdc7489SMatthew Knepley }
73640fdc7489SMatthew Knepley 
73650fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
73660fdc7489SMatthew Knepley {
73670fdc7489SMatthew Knepley   PetscInt       l;
73680fdc7489SMatthew Knepley 
73690fdc7489SMatthew Knepley   PetscFunctionBegin;
73700fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
73710fdc7489SMatthew Knepley     DMLabel  label;
73720fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
73730fdc7489SMatthew Knepley 
73740fdc7489SMatthew Knepley     if (lval) {
73759566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
73769566063dSJacob Faibussowitsch       else          PetscCall(DMGetLabel(dm, ul->names[l], &label));
73779566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l]+lval-1]));
73780fdc7489SMatthew Knepley     }
73790fdc7489SMatthew Knepley   }
73800fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73810fdc7489SMatthew Knepley }
7382a8fb8f29SToby Isaac 
7383a8fb8f29SToby Isaac /*@
7384a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7385a8fb8f29SToby Isaac 
7386a8fb8f29SToby Isaac   Input Parameter:
7387a8fb8f29SToby Isaac . dm - The DM object
7388a8fb8f29SToby Isaac 
7389a8fb8f29SToby Isaac   Output Parameter:
7390a8fb8f29SToby Isaac . cdm - The coarse DM
7391a8fb8f29SToby Isaac 
7392a8fb8f29SToby Isaac   Level: intermediate
7393a8fb8f29SToby Isaac 
7394db781477SPatrick Sanan .seealso: `DMSetCoarseDM()`
7395a8fb8f29SToby Isaac @*/
7396a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7397a8fb8f29SToby Isaac {
7398a8fb8f29SToby Isaac   PetscFunctionBegin;
7399a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7400a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7401a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7402a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7403a8fb8f29SToby Isaac }
7404a8fb8f29SToby Isaac 
7405a8fb8f29SToby Isaac /*@
7406a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7407a8fb8f29SToby Isaac 
7408a8fb8f29SToby Isaac   Input Parameters:
7409a8fb8f29SToby Isaac + dm - The DM object
7410a8fb8f29SToby Isaac - cdm - The coarse DM
7411a8fb8f29SToby Isaac 
7412a8fb8f29SToby Isaac   Level: intermediate
7413a8fb8f29SToby Isaac 
7414db781477SPatrick Sanan .seealso: `DMGetCoarseDM()`
7415a8fb8f29SToby Isaac @*/
7416a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7417a8fb8f29SToby Isaac {
7418a8fb8f29SToby Isaac   PetscFunctionBegin;
7419a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7420a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
74219566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
74229566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7423a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7424a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7425a8fb8f29SToby Isaac }
7426a8fb8f29SToby Isaac 
742788bdff64SToby Isaac /*@
742888bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
742988bdff64SToby Isaac 
743088bdff64SToby Isaac   Input Parameter:
743188bdff64SToby Isaac . dm - The DM object
743288bdff64SToby Isaac 
743388bdff64SToby Isaac   Output Parameter:
743488bdff64SToby Isaac . fdm - The fine DM
743588bdff64SToby Isaac 
743688bdff64SToby Isaac   Level: intermediate
743788bdff64SToby Isaac 
7438db781477SPatrick Sanan .seealso: `DMSetFineDM()`
743988bdff64SToby Isaac @*/
744088bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
744188bdff64SToby Isaac {
744288bdff64SToby Isaac   PetscFunctionBegin;
744388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
744488bdff64SToby Isaac   PetscValidPointer(fdm, 2);
744588bdff64SToby Isaac   *fdm = dm->fineMesh;
744688bdff64SToby Isaac   PetscFunctionReturn(0);
744788bdff64SToby Isaac }
744888bdff64SToby Isaac 
744988bdff64SToby Isaac /*@
745088bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
745188bdff64SToby Isaac 
745288bdff64SToby Isaac   Input Parameters:
745388bdff64SToby Isaac + dm - The DM object
745488bdff64SToby Isaac - fdm - The fine DM
745588bdff64SToby Isaac 
745688bdff64SToby Isaac   Level: intermediate
745788bdff64SToby Isaac 
7458db781477SPatrick Sanan .seealso: `DMGetFineDM()`
745988bdff64SToby Isaac @*/
746088bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
746188bdff64SToby Isaac {
746288bdff64SToby Isaac   PetscFunctionBegin;
746388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
746488bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
74659566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
74669566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
746788bdff64SToby Isaac   dm->fineMesh = fdm;
746888bdff64SToby Isaac   PetscFunctionReturn(0);
746988bdff64SToby Isaac }
747088bdff64SToby Isaac 
7471a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7472a6ba4734SToby Isaac 
7473a6ba4734SToby Isaac /*@C
7474a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7475a6ba4734SToby Isaac 
7476783e2ec8SMatthew G. Knepley   Collective on dm
7477783e2ec8SMatthew G. Knepley 
7478a6ba4734SToby Isaac   Input Parameters:
74794c258f51SMatthew G. Knepley + dm       - The DM, with a PetscDS that matches the problem being constrained
7480f971fd6bSMatthew G. Knepley . type     - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7481a6ba4734SToby Isaac . name     - The BC name
748245480ffeSMatthew G. Knepley . label    - The label defining constrained points
748345480ffeSMatthew G. Knepley . Nv       - The number of DMLabel values for constrained points
748445480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7485a6ba4734SToby Isaac . field    - The field to constrain
748645480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7487a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7488a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
748956cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7490a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7491a6ba4734SToby Isaac 
749245480ffeSMatthew G. Knepley   Output Parameter:
749345480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
749445480ffeSMatthew G. Knepley 
7495a6ba4734SToby Isaac   Options Database Keys:
7496a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7497a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7498a6ba4734SToby Isaac 
749956cf3b9cSMatthew G. Knepley   Note:
750056cf3b9cSMatthew 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:
750156cf3b9cSMatthew G. Knepley 
750256cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
750356cf3b9cSMatthew G. Knepley 
750456cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
750556cf3b9cSMatthew G. Knepley 
750656cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
750756cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
750856cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
750956cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
751056cf3b9cSMatthew G. Knepley 
751156cf3b9cSMatthew G. Knepley + dim - the spatial dimension
751256cf3b9cSMatthew G. Knepley . Nf - the number of fields
751356cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
751456cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
751556cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
751656cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
751756cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
751856cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
751956cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
752056cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
752156cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
752256cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
752356cf3b9cSMatthew G. Knepley . t - current time
752456cf3b9cSMatthew G. Knepley . x - coordinates of the current point
752556cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
752656cf3b9cSMatthew G. Knepley . constants - constant parameters
752756cf3b9cSMatthew G. Knepley - bcval - output values at the current point
752856cf3b9cSMatthew G. Knepley 
7529ed808b8fSJed Brown   Level: intermediate
7530a6ba4734SToby Isaac 
7531db781477SPatrick Sanan .seealso: `DSGetBoundary()`, `PetscDSAddBoundary()`
7532a6ba4734SToby Isaac @*/
753345480ffeSMatthew 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)
7534a6ba4734SToby Isaac {
7535e5e52638SMatthew G. Knepley   PetscDS        ds;
7536a6ba4734SToby Isaac 
7537a6ba4734SToby Isaac   PetscFunctionBegin;
7538a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7539783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
754045480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
754145480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
754245480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
754345480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
754401a5d20dSJed Brown   PetscCheck(!dm->localSection,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot add boundary to DM after creating local section");
75459566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7546799db056SMatthew G. Knepley   /* Complete label */
7547799db056SMatthew G. Knepley   if (label) {
7548799db056SMatthew G. Knepley     PetscObject  obj;
7549799db056SMatthew G. Knepley     PetscClassId id;
7550799db056SMatthew G. Knepley 
7551799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7552799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7553799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7554799db056SMatthew G. Knepley       DM plex;
7555799db056SMatthew G. Knepley 
7556799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7557799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7558799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7559799db056SMatthew G. Knepley     }
7560799db056SMatthew G. Knepley   }
75619566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
7562a6ba4734SToby Isaac   PetscFunctionReturn(0);
7563a6ba4734SToby Isaac }
7564a6ba4734SToby Isaac 
756545480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7566e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7567e6f8dbb6SToby Isaac {
7568e5e52638SMatthew G. Knepley   PetscDS        ds;
7569dff059c6SToby Isaac   DMBoundary    *lastnext;
7570e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7571e6f8dbb6SToby Isaac 
7572e6f8dbb6SToby Isaac   PetscFunctionBegin;
75739566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7574e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
757547a1f5adSToby Isaac   if (dm->boundary) {
757647a1f5adSToby Isaac     DMBoundary next = dm->boundary;
757747a1f5adSToby Isaac 
757847a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
757947a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
758047a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
758147a1f5adSToby Isaac     while (next) {
758247a1f5adSToby Isaac       DMBoundary b = next;
758347a1f5adSToby Isaac 
758447a1f5adSToby Isaac       next = b->next;
75859566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7586a6ba4734SToby Isaac     }
758747a1f5adSToby Isaac     dm->boundary = NULL;
7588a6ba4734SToby Isaac   }
758947a1f5adSToby Isaac 
7590dff059c6SToby Isaac   lastnext = &(dm->boundary);
7591e6f8dbb6SToby Isaac   while (dsbound) {
7592e6f8dbb6SToby Isaac     DMBoundary dmbound;
7593e6f8dbb6SToby Isaac 
75949566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7595e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
759645480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
759747a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7598dff059c6SToby Isaac     *lastnext = dmbound;
7599dff059c6SToby Isaac     lastnext = &(dmbound->next);
7600dff059c6SToby Isaac     dsbound = dsbound->next;
7601a6ba4734SToby Isaac   }
7602a6ba4734SToby Isaac   PetscFunctionReturn(0);
7603a6ba4734SToby Isaac }
7604a6ba4734SToby Isaac 
7605a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7606a6ba4734SToby Isaac {
7607b95f2879SToby Isaac   DMBoundary     b;
7608a6ba4734SToby Isaac 
7609a6ba4734SToby Isaac   PetscFunctionBegin;
7610a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7611534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7612a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
76139566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7614b95f2879SToby Isaac   b = dm->boundary;
7615a6ba4734SToby Isaac   while (b && !(*isBd)) {
7616e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7617e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7618a6ba4734SToby Isaac     PetscInt   i;
7619a6ba4734SToby Isaac 
762045480ffeSMatthew G. Knepley     if (label) {
76219566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7622a6ba4734SToby Isaac     }
7623a6ba4734SToby Isaac     b = b->next;
7624a6ba4734SToby Isaac   }
7625a6ba4734SToby Isaac   PetscFunctionReturn(0);
7626a6ba4734SToby Isaac }
76274d6f44ffSToby Isaac 
76284d6f44ffSToby Isaac /*@C
7629a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
7630a6e0b375SMatthew G. Knepley 
7631a6e0b375SMatthew G. Knepley   Collective on DM
76324d6f44ffSToby Isaac 
76334d6f44ffSToby Isaac   Input Parameters:
76344d6f44ffSToby Isaac + dm      - The DM
76350709b2feSToby Isaac . time    - The time
76364d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
76374d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
76384d6f44ffSToby Isaac - mode    - The insertion mode for values
76394d6f44ffSToby Isaac 
76404d6f44ffSToby Isaac   Output Parameter:
76414d6f44ffSToby Isaac . X - vector
76424d6f44ffSToby Isaac 
76434d6f44ffSToby Isaac    Calling sequence of func:
764477b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
76454d6f44ffSToby Isaac 
76464d6f44ffSToby Isaac +  dim - The spatial dimension
76478ec8862eSJed Brown .  time - The time at which to sample
76484d6f44ffSToby Isaac .  x   - The coordinates
764977b739a6SMatthew Knepley .  Nc  - The number of components
76504d6f44ffSToby Isaac .  u   - The output field values
76514d6f44ffSToby Isaac -  ctx - optional user-defined function context
76524d6f44ffSToby Isaac 
76534d6f44ffSToby Isaac   Level: developer
76544d6f44ffSToby Isaac 
7655db781477SPatrick Sanan .seealso: `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
76564d6f44ffSToby Isaac @*/
76570709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
76584d6f44ffSToby Isaac {
76594d6f44ffSToby Isaac   Vec            localX;
76604d6f44ffSToby Isaac 
76614d6f44ffSToby Isaac   PetscFunctionBegin;
76624d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
76639566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
76649566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
76659566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
76669566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
76679566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
76684d6f44ffSToby Isaac   PetscFunctionReturn(0);
76694d6f44ffSToby Isaac }
76704d6f44ffSToby Isaac 
7671a6e0b375SMatthew G. Knepley /*@C
7672a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
7673a6e0b375SMatthew G. Knepley 
7674a6e0b375SMatthew G. Knepley   Not collective
7675a6e0b375SMatthew G. Knepley 
7676a6e0b375SMatthew G. Knepley   Input Parameters:
7677a6e0b375SMatthew G. Knepley + dm      - The DM
7678a6e0b375SMatthew G. Knepley . time    - The time
7679a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7680a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7681a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7682a6e0b375SMatthew G. Knepley 
7683a6e0b375SMatthew G. Knepley   Output Parameter:
7684a6e0b375SMatthew G. Knepley . localX - vector
7685a6e0b375SMatthew G. Knepley 
7686a6e0b375SMatthew G. Knepley    Calling sequence of func:
768777b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7688a6e0b375SMatthew G. Knepley 
7689a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7690a6e0b375SMatthew G. Knepley .  x   - The coordinates
769177b739a6SMatthew Knepley .  Nc  - The number of components
7692a6e0b375SMatthew G. Knepley .  u   - The output field values
7693a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7694a6e0b375SMatthew G. Knepley 
7695a6e0b375SMatthew G. Knepley   Level: developer
7696a6e0b375SMatthew G. Knepley 
7697db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7698a6e0b375SMatthew G. Knepley @*/
76990709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
77004d6f44ffSToby Isaac {
77014d6f44ffSToby Isaac   PetscFunctionBegin;
77024d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7703064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
77047a8be351SBarry Smith   PetscCheck(dm->ops->projectfunctionlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
77059566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX));
77064d6f44ffSToby Isaac   PetscFunctionReturn(0);
77074d6f44ffSToby Isaac }
77084d6f44ffSToby Isaac 
7709a6e0b375SMatthew G. Knepley /*@C
7710a6e0b375SMatthew 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.
7711a6e0b375SMatthew G. Knepley 
7712a6e0b375SMatthew G. Knepley   Collective on DM
7713a6e0b375SMatthew G. Knepley 
7714a6e0b375SMatthew G. Knepley   Input Parameters:
7715a6e0b375SMatthew G. Knepley + dm      - The DM
7716a6e0b375SMatthew G. Knepley . time    - The time
7717a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
7718a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7719a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7720a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7721a6e0b375SMatthew G. Knepley 
7722a6e0b375SMatthew G. Knepley   Output Parameter:
7723a6e0b375SMatthew G. Knepley . X - vector
7724a6e0b375SMatthew G. Knepley 
7725a6e0b375SMatthew G. Knepley    Calling sequence of func:
772677b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7727a6e0b375SMatthew G. Knepley 
7728a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7729a6e0b375SMatthew G. Knepley .  x   - The coordinates
773077b739a6SMatthew Knepley .  Nc  - The number of components
7731a6e0b375SMatthew G. Knepley .  u   - The output field values
7732a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7733a6e0b375SMatthew G. Knepley 
7734a6e0b375SMatthew G. Knepley   Level: developer
7735a6e0b375SMatthew G. Knepley 
7736db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
7737a6e0b375SMatthew G. Knepley @*/
77382c53366bSMatthew 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)
77392c53366bSMatthew G. Knepley {
77402c53366bSMatthew G. Knepley   Vec            localX;
77412c53366bSMatthew G. Knepley 
77422c53366bSMatthew G. Knepley   PetscFunctionBegin;
77432c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77449566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
77459566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
77469566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
77479566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
77489566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
77492c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
77502c53366bSMatthew G. Knepley }
77512c53366bSMatthew G. Knepley 
7752a6e0b375SMatthew G. Knepley /*@C
7753a6e0b375SMatthew 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.
7754a6e0b375SMatthew G. Knepley 
7755a6e0b375SMatthew G. Knepley   Not collective
7756a6e0b375SMatthew G. Knepley 
7757a6e0b375SMatthew G. Knepley   Input Parameters:
7758a6e0b375SMatthew G. Knepley + dm      - The DM
7759a6e0b375SMatthew G. Knepley . time    - The time
7760a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
7761a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7762a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7763a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7764a6e0b375SMatthew G. Knepley 
7765a6e0b375SMatthew G. Knepley   Output Parameter:
7766a6e0b375SMatthew G. Knepley . localX - vector
7767a6e0b375SMatthew G. Knepley 
7768a6e0b375SMatthew G. Knepley    Calling sequence of func:
776977b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7770a6e0b375SMatthew G. Knepley 
7771a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7772a6e0b375SMatthew G. Knepley .  x   - The coordinates
777377b739a6SMatthew Knepley .  Nc  - The number of components
7774a6e0b375SMatthew G. Knepley .  u   - The output field values
7775a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7776a6e0b375SMatthew G. Knepley 
7777a6e0b375SMatthew G. Knepley   Level: developer
7778a6e0b375SMatthew G. Knepley 
7779db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7780a6e0b375SMatthew G. Knepley @*/
77811c531cf8SMatthew 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)
77824d6f44ffSToby Isaac {
77834d6f44ffSToby Isaac   PetscFunctionBegin;
77844d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7785064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
77867a8be351SBarry Smith   PetscCheck(dm->ops->projectfunctionlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
77879566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
77884d6f44ffSToby Isaac   PetscFunctionReturn(0);
77894d6f44ffSToby Isaac }
77902716604bSToby Isaac 
7791a6e0b375SMatthew G. Knepley /*@C
7792a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
7793a6e0b375SMatthew G. Knepley 
7794a6e0b375SMatthew G. Knepley   Not collective
7795a6e0b375SMatthew G. Knepley 
7796a6e0b375SMatthew G. Knepley   Input Parameters:
7797a6e0b375SMatthew G. Knepley + dm      - The DM
7798a6e0b375SMatthew G. Knepley . time    - The time
7799a6e0b375SMatthew G. Knepley . localU  - The input field vector
7800a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7801a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7802a6e0b375SMatthew G. Knepley 
7803a6e0b375SMatthew G. Knepley   Output Parameter:
7804a6e0b375SMatthew G. Knepley . localX  - The output vector
7805a6e0b375SMatthew G. Knepley 
7806a6e0b375SMatthew G. Knepley    Calling sequence of func:
7807a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7808a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7809a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7810a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7811a6e0b375SMatthew G. Knepley 
7812a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7813a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7814a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7815a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7816a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7817a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7818a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7819a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7820a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7821a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7822a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7823a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7824a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7825a6e0b375SMatthew G. Knepley .  t            - The current time
7826a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
7827a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
7828a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
7829a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
7830a6e0b375SMatthew G. Knepley 
7831a6e0b375SMatthew 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.
7832a6e0b375SMatthew 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
7833a6e0b375SMatthew 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
7834a6e0b375SMatthew 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.
7835a6e0b375SMatthew G. Knepley 
7836a6e0b375SMatthew G. Knepley   Level: intermediate
7837a6e0b375SMatthew G. Knepley 
7838db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
7839a6e0b375SMatthew G. Knepley @*/
78408c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
78418c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
78428c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
78438c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7844191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
78458c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
78468c6c5593SMatthew G. Knepley {
78478c6c5593SMatthew G. Knepley   PetscFunctionBegin;
78488c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
78498c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
78508c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
78517a8be351SBarry Smith   PetscCheck(dm->ops->projectfieldlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
78529566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX));
78538c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
78548c6c5593SMatthew G. Knepley }
78558c6c5593SMatthew G. Knepley 
7856a6e0b375SMatthew G. Knepley /*@C
7857a6e0b375SMatthew 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.
7858a6e0b375SMatthew G. Knepley 
7859a6e0b375SMatthew G. Knepley   Not collective
7860a6e0b375SMatthew G. Knepley 
7861a6e0b375SMatthew G. Knepley   Input Parameters:
7862a6e0b375SMatthew G. Knepley + dm      - The DM
7863a6e0b375SMatthew G. Knepley . time    - The time
7864a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
7865a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
7866a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
7867a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
7868a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
7869a6e0b375SMatthew G. Knepley . localU  - The input field vector
7870a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7871a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7872a6e0b375SMatthew G. Knepley 
7873a6e0b375SMatthew G. Knepley   Output Parameter:
7874a6e0b375SMatthew G. Knepley . localX  - The output vector
7875a6e0b375SMatthew G. Knepley 
7876a6e0b375SMatthew G. Knepley    Calling sequence of func:
7877a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7878a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7879a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7880a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7881a6e0b375SMatthew G. Knepley 
7882a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7883a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7884a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7885a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7886a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7887a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7888a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7889a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7890a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7891a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7892a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7893a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7894a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7895a6e0b375SMatthew G. Knepley .  t            - The current time
7896a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
7897a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
7898a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
7899a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
7900a6e0b375SMatthew G. Knepley 
7901a6e0b375SMatthew 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.
7902a6e0b375SMatthew 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
7903a6e0b375SMatthew 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
7904a6e0b375SMatthew 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.
7905a6e0b375SMatthew G. Knepley 
7906a6e0b375SMatthew G. Knepley   Level: intermediate
7907a6e0b375SMatthew G. Knepley 
7908db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
7909a6e0b375SMatthew G. Knepley @*/
79101c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
79118c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
79128c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
79138c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7914191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
79158c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
79168c6c5593SMatthew G. Knepley {
79178c6c5593SMatthew G. Knepley   PetscFunctionBegin;
79188c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7919064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
7920064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
79217a8be351SBarry Smith   PetscCheck(dm->ops->projectfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
79229566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
79238c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
79248c6c5593SMatthew G. Knepley }
79258c6c5593SMatthew G. Knepley 
79262716604bSToby Isaac /*@C
7927ece3a9fcSMatthew 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.
7928ece3a9fcSMatthew G. Knepley 
7929ece3a9fcSMatthew G. Knepley   Not collective
7930ece3a9fcSMatthew G. Knepley 
7931ece3a9fcSMatthew G. Knepley   Input Parameters:
7932ece3a9fcSMatthew G. Knepley + dm      - The DM
7933ece3a9fcSMatthew G. Knepley . time    - The time
7934ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
7935ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
7936ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
7937ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
7938ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
7939ece3a9fcSMatthew G. Knepley . localU  - The input field vector
7940ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7941ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
7942ece3a9fcSMatthew G. Knepley 
7943ece3a9fcSMatthew G. Knepley   Output Parameter:
7944ece3a9fcSMatthew G. Knepley . localX  - The output vector
7945ece3a9fcSMatthew G. Knepley 
7946ece3a9fcSMatthew G. Knepley    Calling sequence of func:
7947ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7948ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7949ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7950ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7951ece3a9fcSMatthew G. Knepley 
7952ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
7953ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
7954ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7955ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
7956ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7957ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
7958ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7959ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7960ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7961ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7962ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7963ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7964ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7965ece3a9fcSMatthew G. Knepley .  t            - The current time
7966ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
7967ece3a9fcSMatthew G. Knepley .  n            - The face normal
7968ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
7969ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
7970ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
7971ece3a9fcSMatthew G. Knepley 
7972ece3a9fcSMatthew G. Knepley   Note:
7973ece3a9fcSMatthew 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.
7974ece3a9fcSMatthew 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
7975ece3a9fcSMatthew 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
7976ece3a9fcSMatthew 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.
7977ece3a9fcSMatthew G. Knepley 
7978ece3a9fcSMatthew G. Knepley   Level: intermediate
7979ece3a9fcSMatthew G. Knepley 
7980db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
7981ece3a9fcSMatthew G. Knepley @*/
7982ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
7983ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
7984ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7985ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7986ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
7987ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
7988ece3a9fcSMatthew G. Knepley {
7989ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
7990ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7991064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
7992064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
79937a8be351SBarry Smith   PetscCheck(dm->ops->projectbdfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
79949566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
7995ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
7996ece3a9fcSMatthew G. Knepley }
7997ece3a9fcSMatthew G. Knepley 
7998ece3a9fcSMatthew G. Knepley /*@C
79992716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
80002716604bSToby Isaac 
80012716604bSToby Isaac   Input Parameters:
80022716604bSToby Isaac + dm    - The DM
80030709b2feSToby Isaac . time  - The time
80042716604bSToby Isaac . funcs - The functions to evaluate for each field component
80052716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8006574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
80072716604bSToby Isaac 
80082716604bSToby Isaac   Output Parameter:
80092716604bSToby Isaac . diff - The diff ||u - u_h||_2
80102716604bSToby Isaac 
80112716604bSToby Isaac   Level: developer
80122716604bSToby Isaac 
8013db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
80142716604bSToby Isaac @*/
80150709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
80162716604bSToby Isaac {
80172716604bSToby Isaac   PetscFunctionBegin;
80182716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8019b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
80207a8be351SBarry Smith   PetscCheck(dm->ops->computel2diff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
80219566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff));
80222716604bSToby Isaac   PetscFunctionReturn(0);
80232716604bSToby Isaac }
8024b698f381SToby Isaac 
8025b698f381SToby Isaac /*@C
8026b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8027b698f381SToby Isaac 
8028d083f849SBarry Smith   Collective on dm
8029d083f849SBarry Smith 
8030b698f381SToby Isaac   Input Parameters:
8031b698f381SToby Isaac + dm    - The DM
8032b698f381SToby Isaac , time  - The time
8033b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8034b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8035574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8036b698f381SToby Isaac - n     - The vector to project along
8037b698f381SToby Isaac 
8038b698f381SToby Isaac   Output Parameter:
8039b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8040b698f381SToby Isaac 
8041b698f381SToby Isaac   Level: developer
8042b698f381SToby Isaac 
8043db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2Diff()`
8044b698f381SToby Isaac @*/
8045b698f381SToby 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)
8046b698f381SToby Isaac {
8047b698f381SToby Isaac   PetscFunctionBegin;
8048b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8049b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
80507a8be351SBarry Smith   PetscCheck(dm->ops->computel2gradientdiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
80519566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff));
8052b698f381SToby Isaac   PetscFunctionReturn(0);
8053b698f381SToby Isaac }
8054b698f381SToby Isaac 
80552a16baeaSToby Isaac /*@C
80562a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
80572a16baeaSToby Isaac 
8058d083f849SBarry Smith   Collective on dm
8059d083f849SBarry Smith 
80602a16baeaSToby Isaac   Input Parameters:
80612a16baeaSToby Isaac + dm    - The DM
80622a16baeaSToby Isaac . time  - The time
80632a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
80642a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8065574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
80662a16baeaSToby Isaac 
80672a16baeaSToby Isaac   Output Parameter:
80682a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
80692a16baeaSToby Isaac 
80702a16baeaSToby Isaac   Level: developer
80712a16baeaSToby Isaac 
8072db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
80732a16baeaSToby Isaac @*/
80741189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
80752a16baeaSToby Isaac {
80762a16baeaSToby Isaac   PetscFunctionBegin;
80772a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
80782a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
80797a8be351SBarry Smith   PetscCheck(dm->ops->computel2fielddiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
80809566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff));
80812a16baeaSToby Isaac   PetscFunctionReturn(0);
80822a16baeaSToby Isaac }
80832a16baeaSToby Isaac 
8084df0b854cSToby Isaac /*@C
8085502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8086502a2867SDave May 
8087502a2867SDave May  Not Collective
8088502a2867SDave May 
8089502a2867SDave May  Input Parameter:
8090502a2867SDave May .  dm    - The DM
8091502a2867SDave May 
80920a19bb7dSprj-  Output Parameters:
80930a19bb7dSprj- +  nranks - the number of neighbours
80940a19bb7dSprj- -  ranks - the neighbors ranks
8095502a2867SDave May 
8096502a2867SDave May  Notes:
8097502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8098502a2867SDave May 
8099502a2867SDave May  Level: beginner
8100502a2867SDave May 
8101db781477SPatrick Sanan  .seealso: `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8102502a2867SDave May @*/
8103502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8104502a2867SDave May {
8105502a2867SDave May   PetscFunctionBegin;
8106502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81077a8be351SBarry Smith   PetscCheck(dm->ops->getneighbors,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
81089566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm,nranks,ranks));
8109502a2867SDave May   PetscFunctionReturn(0);
8110502a2867SDave May }
8111502a2867SDave May 
8112531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8113531c7667SBarry Smith 
8114531c7667SBarry Smith /*
8115531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8116531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8117531c7667SBarry Smith */
8118531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8119531c7667SBarry Smith {
8120531c7667SBarry Smith   PetscFunctionBegin;
8121531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8122531c7667SBarry Smith     Vec x1local;
8123531c7667SBarry Smith     DM  dm;
81249566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J,&dm));
81257a8be351SBarry Smith     PetscCheck(dm,PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
81269566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm,&x1local));
81279566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local));
81289566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local));
8129531c7667SBarry Smith     x1   = x1local;
8130531c7667SBarry Smith   }
81319566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J,coloring,x1,sctx));
8132531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8133531c7667SBarry Smith     DM  dm;
81349566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J,&dm));
81359566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm,&x1));
8136531c7667SBarry Smith   }
8137531c7667SBarry Smith   PetscFunctionReturn(0);
8138531c7667SBarry Smith }
8139531c7667SBarry Smith 
8140531c7667SBarry Smith /*@
8141531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8142531c7667SBarry Smith 
8143531c7667SBarry Smith     Input Parameter:
8144531c7667SBarry Smith .    coloring - the MatFDColoring object
8145531c7667SBarry Smith 
814695452b02SPatrick Sanan     Developer Notes:
814795452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8148531c7667SBarry Smith 
81491b266c99SBarry Smith     Level: advanced
81501b266c99SBarry Smith 
8151db781477SPatrick Sanan .seealso: `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8152531c7667SBarry Smith @*/
8153531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8154531c7667SBarry Smith {
8155531c7667SBarry Smith   PetscFunctionBegin;
8156531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8157531c7667SBarry Smith   PetscFunctionReturn(0);
8158531c7667SBarry Smith }
81598320bc6fSPatrick Sanan 
81608320bc6fSPatrick Sanan /*@
81618320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
81628320bc6fSPatrick Sanan 
81638320bc6fSPatrick Sanan     Collective
81648320bc6fSPatrick Sanan 
81658320bc6fSPatrick Sanan     Input Parameters:
8166a5bc1bf3SBarry Smith +    dm1 - the first DM
81678320bc6fSPatrick Sanan -    dm2 - the second DM
81688320bc6fSPatrick Sanan 
81698320bc6fSPatrick Sanan     Output Parameters:
81708320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
81718320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
81728320bc6fSPatrick Sanan 
81738320bc6fSPatrick Sanan     Notes:
81748320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
81753d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
81768320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
81773d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
81783d862458SPatrick Sanan     decomposition, but hold different data.
81798320bc6fSPatrick Sanan 
81808320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
81818320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
81828320bc6fSPatrick Sanan 
81838320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
81848320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
81858320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
81868320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
81878320bc6fSPatrick Sanan 
81888320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
81898320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
81908320bc6fSPatrick Sanan .vb
81918320bc6fSPatrick Sanan   ...
81929566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
81938320bc6fSPatrick Sanan   if (set && compatible)  {
81949566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
81959566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
81969566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
81978320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
81988320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
81998320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
82008320bc6fSPatrick Sanan       }
82018320bc6fSPatrick Sanan     }
82029566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
82039566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
82048320bc6fSPatrick Sanan   } else {
82058320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
82068320bc6fSPatrick Sanan   }
82078320bc6fSPatrick Sanan   ...
82088320bc6fSPatrick Sanan .ve
82098320bc6fSPatrick Sanan 
82108320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
82118320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
82128320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
82138320bc6fSPatrick Sanan     always check the "set" output parameter.
82148320bc6fSPatrick Sanan 
82158320bc6fSPatrick Sanan     A DM is always compatible with itself.
82168320bc6fSPatrick Sanan 
82178320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
82188320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
82198320bc6fSPatrick Sanan     incompatible.
82208320bc6fSPatrick Sanan 
82218320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
82228320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
82238320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
82248320bc6fSPatrick Sanan 
82258320bc6fSPatrick Sanan     Developer Notes:
82263d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
82273d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8228a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
82298320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
82308320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
82313d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
82323d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
82338320bc6fSPatrick Sanan 
82348320bc6fSPatrick Sanan     Level: advanced
82358320bc6fSPatrick Sanan 
8236db781477SPatrick Sanan .seealso: `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
82378320bc6fSPatrick Sanan @*/
82388320bc6fSPatrick Sanan 
8239a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
82408320bc6fSPatrick Sanan {
82418320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
82428320bc6fSPatrick Sanan   DMType         type,type2;
82438320bc6fSPatrick Sanan   PetscBool      sameType;
82448320bc6fSPatrick Sanan 
82458320bc6fSPatrick Sanan   PetscFunctionBegin;
8246a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
82478320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
82488320bc6fSPatrick Sanan 
82498320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8250a5bc1bf3SBarry Smith   if (dm1 == dm2) {
82518320bc6fSPatrick Sanan     *set = PETSC_TRUE;
82528320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
82538320bc6fSPatrick Sanan     PetscFunctionReturn(0);
82548320bc6fSPatrick Sanan   }
82558320bc6fSPatrick Sanan 
82568320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
82578320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
82588320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
82598320bc6fSPatrick Sanan      determined by the implementation-specific logic */
82609566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult));
82618320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
82628320bc6fSPatrick Sanan     *set = PETSC_TRUE;
82638320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
82648320bc6fSPatrick Sanan     PetscFunctionReturn(0);
82658320bc6fSPatrick Sanan   }
82668320bc6fSPatrick Sanan 
82678320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8268a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
82699566063dSJacob Faibussowitsch     PetscCall((*dm1->ops->getcompatibility)(dm1,dm2,compatible,set));
8270b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
82718320bc6fSPatrick Sanan   }
82728320bc6fSPatrick Sanan 
8273a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
82748320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
82759566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1,&type));
82769566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2,&type2));
82779566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type,type2,&sameType));
82788320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
82799566063dSJacob Faibussowitsch     PetscCall((*dm2->ops->getcompatibility)(dm2,dm1,compatible,set)); /* Note argument order */
82808320bc6fSPatrick Sanan   } else {
82818320bc6fSPatrick Sanan     *set = PETSC_FALSE;
82828320bc6fSPatrick Sanan   }
82838320bc6fSPatrick Sanan   PetscFunctionReturn(0);
82848320bc6fSPatrick Sanan }
8285c0f0dcc3SMatthew G. Knepley 
8286c0f0dcc3SMatthew G. Knepley /*@C
8287c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8288c0f0dcc3SMatthew G. Knepley 
8289c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8290c0f0dcc3SMatthew G. Knepley 
8291c0f0dcc3SMatthew G. Knepley   Input Parameters:
8292c0f0dcc3SMatthew G. Knepley + DM - the DM
8293c0f0dcc3SMatthew G. Knepley . f - the monitor function
8294c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8295c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8296c0f0dcc3SMatthew G. Knepley 
8297c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8298c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
8299c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8300c0f0dcc3SMatthew G. Knepley 
8301c0f0dcc3SMatthew G. Knepley   Notes:
8302c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8303c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
8304c0f0dcc3SMatthew G. Knepley   order in which they were set.
8305c0f0dcc3SMatthew G. Knepley 
8306c0f0dcc3SMatthew G. Knepley   Fortran Notes:
8307c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
8308c0f0dcc3SMatthew G. Knepley 
8309c0f0dcc3SMatthew G. Knepley   Level: intermediate
8310c0f0dcc3SMatthew G. Knepley 
8311db781477SPatrick Sanan .seealso: `DMMonitorCancel()`
8312c0f0dcc3SMatthew G. Knepley @*/
8313c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
8314c0f0dcc3SMatthew G. Knepley {
8315c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8316c0f0dcc3SMatthew G. Knepley 
8317c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8318c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8319c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8320c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8321c0f0dcc3SMatthew G. Knepley 
83229566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
8323c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8324c0f0dcc3SMatthew G. Knepley   }
83257a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8326c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8327c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8328c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
8329c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8330c0f0dcc3SMatthew G. Knepley }
8331c0f0dcc3SMatthew G. Knepley 
8332c0f0dcc3SMatthew G. Knepley /*@
8333c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
8334c0f0dcc3SMatthew G. Knepley 
8335c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8336c0f0dcc3SMatthew G. Knepley 
8337c0f0dcc3SMatthew G. Knepley   Input Parameter:
8338c0f0dcc3SMatthew G. Knepley . dm - the DM
8339c0f0dcc3SMatthew G. Knepley 
8340c0f0dcc3SMatthew G. Knepley   Options Database Key:
8341c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8342c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
8343c0f0dcc3SMatthew G. Knepley   set via the options database
8344c0f0dcc3SMatthew G. Knepley 
8345c0f0dcc3SMatthew G. Knepley   Notes:
8346c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
8347c0f0dcc3SMatthew G. Knepley 
8348c0f0dcc3SMatthew G. Knepley   Level: intermediate
8349c0f0dcc3SMatthew G. Knepley 
8350db781477SPatrick Sanan .seealso: `DMMonitorSet()`
8351c0f0dcc3SMatthew G. Knepley @*/
8352c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
8353c0f0dcc3SMatthew G. Knepley {
8354c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8355c0f0dcc3SMatthew G. Knepley 
8356c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8357c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8358c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
83599566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8360c0f0dcc3SMatthew G. Knepley   }
8361c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8362c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8363c0f0dcc3SMatthew G. Knepley }
8364c0f0dcc3SMatthew G. Knepley 
8365c0f0dcc3SMatthew G. Knepley /*@C
8366c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8367c0f0dcc3SMatthew G. Knepley 
8368c0f0dcc3SMatthew G. Knepley   Collective on DM
8369c0f0dcc3SMatthew G. Knepley 
8370c0f0dcc3SMatthew G. Knepley   Input Parameters:
8371c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
8372c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8373c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8374c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8375c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8376c0f0dcc3SMatthew 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
8377c0f0dcc3SMatthew G. Knepley 
8378c0f0dcc3SMatthew G. Knepley   Output Parameter:
8379c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8380c0f0dcc3SMatthew G. Knepley 
8381c0f0dcc3SMatthew G. Knepley   Level: developer
8382c0f0dcc3SMatthew G. Knepley 
8383db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8384db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
8385db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
8386db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8387c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8388db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8389db781477SPatrick Sanan           `PetscOptionsFList()`, `PetscOptionsEList()`
8390c0f0dcc3SMatthew G. Knepley @*/
8391c0f0dcc3SMatthew 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)
8392c0f0dcc3SMatthew G. Knepley {
8393c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8394c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8395c0f0dcc3SMatthew G. Knepley 
8396c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8397c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
83989566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg));
8399c0f0dcc3SMatthew G. Knepley   if (*flg) {
8400c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8401c0f0dcc3SMatthew G. Knepley 
84029566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
84039566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject) viewer));
84049566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
84059566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy));
8406c0f0dcc3SMatthew G. Knepley   }
8407c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8408c0f0dcc3SMatthew G. Knepley }
8409c0f0dcc3SMatthew G. Knepley 
8410c0f0dcc3SMatthew G. Knepley /*@
8411c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8412c0f0dcc3SMatthew G. Knepley 
8413c0f0dcc3SMatthew G. Knepley    Collective on DM
8414c0f0dcc3SMatthew G. Knepley 
8415c0f0dcc3SMatthew G. Knepley    Input Parameters:
8416c0f0dcc3SMatthew G. Knepley .  dm - The DM
8417c0f0dcc3SMatthew G. Knepley 
8418c0f0dcc3SMatthew G. Knepley    Level: developer
8419c0f0dcc3SMatthew G. Knepley 
8420db781477SPatrick Sanan .seealso: `DMMonitorSet()`
8421c0f0dcc3SMatthew G. Knepley @*/
8422c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
8423c0f0dcc3SMatthew G. Knepley {
8424c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8425c0f0dcc3SMatthew G. Knepley 
8426c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8427c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8428c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8429c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
84309566063dSJacob Faibussowitsch     PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
8431c0f0dcc3SMatthew G. Knepley   }
8432c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8433c0f0dcc3SMatthew G. Knepley }
84342e4af2aeSMatthew G. Knepley 
84352e4af2aeSMatthew G. Knepley /*@
84362e4af2aeSMatthew G. Knepley   DMComputeError - Computes the error assuming the user has given exact solution functions
84372e4af2aeSMatthew G. Knepley 
84382e4af2aeSMatthew G. Knepley   Collective on DM
84392e4af2aeSMatthew G. Knepley 
84402e4af2aeSMatthew G. Knepley   Input Parameters:
84412e4af2aeSMatthew G. Knepley + dm     - The DM
84426b867d5aSJose E. Roman - sol    - The solution vector
84432e4af2aeSMatthew G. Knepley 
84446b867d5aSJose E. Roman   Input/Output Parameter:
84456b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
84466b867d5aSJose E. Roman            contains the error in each field
84476b867d5aSJose E. Roman 
84486b867d5aSJose E. Roman   Output Parameter:
84496b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
84502e4af2aeSMatthew G. Knepley 
84512e4af2aeSMatthew G. Knepley   Note: The exact solutions come from the PetscDS object, and the time comes from DMGetOutputSequenceNumber().
84522e4af2aeSMatthew G. Knepley 
84532e4af2aeSMatthew G. Knepley   Level: developer
84542e4af2aeSMatthew G. Knepley 
8455db781477SPatrick Sanan .seealso: `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
84562e4af2aeSMatthew G. Knepley @*/
84572e4af2aeSMatthew G. Knepley PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
84582e4af2aeSMatthew G. Knepley {
84592e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
84602e4af2aeSMatthew G. Knepley   void            **ctxs;
84612e4af2aeSMatthew G. Knepley   PetscReal         time;
84622e4af2aeSMatthew G. Knepley   PetscInt          Nf, f, Nds, s;
84632e4af2aeSMatthew G. Knepley 
84642e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
84659566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
84669566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
84679566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
84682e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
84692e4af2aeSMatthew G. Knepley     PetscDS         ds;
84702e4af2aeSMatthew G. Knepley     DMLabel         label;
84712e4af2aeSMatthew G. Knepley     IS              fieldIS;
84722e4af2aeSMatthew G. Knepley     const PetscInt *fields;
84732e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
84742e4af2aeSMatthew G. Knepley 
84759566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
84769566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
84779566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
84782e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
84792e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
84809566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
84812e4af2aeSMatthew G. Knepley     }
84829566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
84832e4af2aeSMatthew G. Knepley   }
84842e4af2aeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
848563a3b9bcSJacob 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);
84862e4af2aeSMatthew G. Knepley   }
84879566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
84889566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
84892e4af2aeSMatthew G. Knepley   if (errorVec) {
84902e4af2aeSMatthew G. Knepley     DM             edm;
84912e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
84922e4af2aeSMatthew G. Knepley     PetscBool      simplex;
84932e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
84942e4af2aeSMatthew G. Knepley 
84959566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
84969566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
84979566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
84989566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
84992e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
85009566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
85012e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
85022e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
85032e4af2aeSMatthew G. Knepley       PetscQuadrature q;
85042e4af2aeSMatthew G. Knepley       const char     *name;
85052e4af2aeSMatthew G. Knepley 
85069566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *) &fe));
85079566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
85089566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject) fe, &name));
85099566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject) efe, name));
85109566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
85119566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
85129566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject) efe));
85139566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
85142e4af2aeSMatthew G. Knepley     }
85159566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
85162e4af2aeSMatthew G. Knepley 
85179566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
85189566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) *errorVec, "Error"));
85199566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
85209566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
85212e4af2aeSMatthew G. Knepley   }
85229566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
85232e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
85242e4af2aeSMatthew G. Knepley }
85259a2a23afSMatthew G. Knepley 
85269a2a23afSMatthew G. Knepley /*@
85279a2a23afSMatthew G. Knepley   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this DM
85289a2a23afSMatthew G. Knepley 
85299a2a23afSMatthew G. Knepley   Not collective
85309a2a23afSMatthew G. Knepley 
85319a2a23afSMatthew G. Knepley   Input Parameter:
85329a2a23afSMatthew G. Knepley . dm     - The DM
85339a2a23afSMatthew G. Knepley 
85349a2a23afSMatthew G. Knepley   Output Parameter:
8535a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
85369a2a23afSMatthew G. Knepley 
85379a2a23afSMatthew G. Knepley   Level: advanced
85389a2a23afSMatthew G. Knepley 
8539db781477SPatrick Sanan .seealso: `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
85409a2a23afSMatthew G. Knepley @*/
85419a2a23afSMatthew G. Knepley PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
85429a2a23afSMatthew G. Knepley {
85439a2a23afSMatthew G. Knepley   PetscFunctionBegin;
85449a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85459566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
85469a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
85479a2a23afSMatthew G. Knepley }
85489a2a23afSMatthew G. Knepley 
85499a2a23afSMatthew G. Knepley /*@
8550ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
85519a2a23afSMatthew G. Knepley 
85529a2a23afSMatthew G. Knepley   Not collective
85539a2a23afSMatthew G. Knepley 
85549a2a23afSMatthew G. Knepley   Input Parameters:
85559a2a23afSMatthew G. Knepley + dm     - The DM
85569a2a23afSMatthew G. Knepley . label  - The DMLabel
8557ac17215fSMatthew G. Knepley . value  - The label value indicating the region
8558ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
85599a2a23afSMatthew G. Knepley 
85609a2a23afSMatthew G. Knepley   Output Parameter:
85619a2a23afSMatthew G. Knepley . aux    - The Vec holding auxiliary field data
85629a2a23afSMatthew G. Knepley 
8563ac17215fSMatthew G. Knepley   Note: If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
856404c51a94SMatthew G. Knepley 
85659a2a23afSMatthew G. Knepley   Level: advanced
85669a2a23afSMatthew G. Knepley 
8567db781477SPatrick Sanan .seealso: `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`
85689a2a23afSMatthew G. Knepley @*/
8569ac17215fSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
85709a2a23afSMatthew G. Knepley {
8571ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
857204c51a94SMatthew G. Knepley   PetscBool       has;
85739a2a23afSMatthew G. Knepley 
85749a2a23afSMatthew G. Knepley   PetscFunctionBegin;
85759a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85769a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
85779a2a23afSMatthew G. Knepley   key.label = label;
85789a2a23afSMatthew G. Knepley   key.value = value;
8579ac17215fSMatthew G. Knepley   key.part  = part;
85809566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
85819566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key,  aux));
85829566063dSJacob Faibussowitsch   else     PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
85839a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
85849a2a23afSMatthew G. Knepley }
85859a2a23afSMatthew G. Knepley 
85869a2a23afSMatthew G. Knepley /*@
8587ac17215fSMatthew G. Knepley   DMSetAuxiliaryVec - Set the auxiliary vector for region specified by the given label and value, and equation part
85889a2a23afSMatthew G. Knepley 
85899a2a23afSMatthew G. Knepley   Not collective
85909a2a23afSMatthew G. Knepley 
85919a2a23afSMatthew G. Knepley   Input Parameters:
85929a2a23afSMatthew G. Knepley + dm     - The DM
85939a2a23afSMatthew G. Knepley . label  - The DMLabel
85949a2a23afSMatthew G. Knepley . value  - The label value indicating the region
8595ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
85969a2a23afSMatthew G. Knepley - aux    - The Vec holding auxiliary field data
85979a2a23afSMatthew G. Knepley 
85989a2a23afSMatthew G. Knepley   Level: advanced
85999a2a23afSMatthew G. Knepley 
8600db781477SPatrick Sanan .seealso: `DMGetAuxiliaryVec()`
86019a2a23afSMatthew G. Knepley @*/
8602ac17215fSMatthew G. Knepley PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
86039a2a23afSMatthew G. Knepley {
86049a2a23afSMatthew G. Knepley   Vec             old;
86059a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
86069a2a23afSMatthew G. Knepley 
86079a2a23afSMatthew G. Knepley   PetscFunctionBegin;
86089a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86099a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
86109a2a23afSMatthew G. Knepley   key.label = label;
86119a2a23afSMatthew G. Knepley   key.value = value;
8612ac17215fSMatthew G. Knepley   key.part  = part;
86139566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
86149566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) aux));
86159566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject) old));
86169566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
86179566063dSJacob Faibussowitsch   else      PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
86189a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
86199a2a23afSMatthew G. Knepley }
86209a2a23afSMatthew G. Knepley 
86219a2a23afSMatthew G. Knepley /*@C
8622ac17215fSMatthew G. Knepley   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this DM
86239a2a23afSMatthew G. Knepley 
86249a2a23afSMatthew G. Knepley   Not collective
86259a2a23afSMatthew G. Knepley 
86269a2a23afSMatthew G. Knepley   Input Parameter:
86279a2a23afSMatthew G. Knepley . dm      - The DM
86289a2a23afSMatthew G. Knepley 
86299a2a23afSMatthew G. Knepley   Output Parameters:
86309a2a23afSMatthew G. Knepley + labels  - The DMLabels for each Vec
8631ac17215fSMatthew G. Knepley . values  - The label values for each Vec
8632ac17215fSMatthew G. Knepley - parts   - The equation parts for each Vec
86339a2a23afSMatthew G. Knepley 
86349a2a23afSMatthew G. Knepley   Note: The arrays passed in must be at least as large as DMGetNumAuxiliaryVec().
86359a2a23afSMatthew G. Knepley 
86369a2a23afSMatthew G. Knepley   Level: advanced
86379a2a23afSMatthew G. Knepley 
8638db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
86399a2a23afSMatthew G. Knepley @*/
8640ac17215fSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
86419a2a23afSMatthew G. Knepley {
86429a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
86439a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
86449a2a23afSMatthew G. Knepley 
86459a2a23afSMatthew G. Knepley   PetscFunctionBegin;
86469a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86479a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
8648dadcf809SJacob Faibussowitsch   PetscValidIntPointer(values, 3);
8649dadcf809SJacob Faibussowitsch   PetscValidIntPointer(parts,  4);
86509566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
86519566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
86529566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
8653ac17215fSMatthew G. Knepley   for (i = 0; i < n; ++i) {labels[i] = keys[i].label; values[i] = keys[i].value; parts[i] = keys[i].part;}
86549566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
86559a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
86569a2a23afSMatthew G. Knepley }
86579a2a23afSMatthew G. Knepley 
86589a2a23afSMatthew G. Knepley /*@
86599a2a23afSMatthew G. Knepley   DMCopyAuxiliaryVec - Copy the auxiliary data to a new DM
86609a2a23afSMatthew G. Knepley 
86619a2a23afSMatthew G. Knepley   Not collective
86629a2a23afSMatthew G. Knepley 
86639a2a23afSMatthew G. Knepley   Input Parameter:
86649a2a23afSMatthew G. Knepley . dm    - The DM
86659a2a23afSMatthew G. Knepley 
86669a2a23afSMatthew G. Knepley   Output Parameter:
86679a2a23afSMatthew G. Knepley . dmNew - The new DM, now with the same auxiliary data
86689a2a23afSMatthew G. Knepley 
86699a2a23afSMatthew G. Knepley   Level: advanced
86709a2a23afSMatthew G. Knepley 
8671db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
86729a2a23afSMatthew G. Knepley @*/
86739a2a23afSMatthew G. Knepley PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
86749a2a23afSMatthew G. Knepley {
86759a2a23afSMatthew G. Knepley   PetscFunctionBegin;
86769a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86779566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
86789566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
86799a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
86809a2a23afSMatthew G. Knepley }
8681b5a892a1SMatthew G. Knepley 
8682b5a892a1SMatthew G. Knepley /*@C
8683b5a892a1SMatthew G. Knepley   DMPolytopeMatchOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
8684b5a892a1SMatthew G. Knepley 
8685b5a892a1SMatthew G. Knepley   Not collective
8686b5a892a1SMatthew G. Knepley 
8687b5a892a1SMatthew G. Knepley   Input Parameters:
8688b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
8689b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
8690b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
8691b5a892a1SMatthew G. Knepley 
8692b5a892a1SMatthew G. Knepley   Output Parameters:
8693b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
8694b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
8695b5a892a1SMatthew G. Knepley 
8696b5a892a1SMatthew G. Knepley   Level: advanced
8697b5a892a1SMatthew G. Knepley 
8698db781477SPatrick Sanan .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`
8699b5a892a1SMatthew G. Knepley @*/
8700b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
8701b5a892a1SMatthew G. Knepley {
8702b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
8703b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
8704b5a892a1SMatthew G. Knepley   PetscInt       o, c;
8705b5a892a1SMatthew G. Knepley 
8706b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
8707b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
8708b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
8709b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
8710b5a892a1SMatthew G. Knepley 
8711b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceCone[arr[c*2]] != targetCone[c]) break;
8712b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
8713b5a892a1SMatthew G. Knepley   }
8714b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
8715b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8716b5a892a1SMatthew G. Knepley }
8717b5a892a1SMatthew G. Knepley 
8718b5a892a1SMatthew G. Knepley /*@C
8719b5a892a1SMatthew G. Knepley   DMPolytopeGetOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
8720b5a892a1SMatthew G. Knepley 
8721b5a892a1SMatthew G. Knepley   Not collective
8722b5a892a1SMatthew G. Knepley 
8723b5a892a1SMatthew G. Knepley   Input Parameters:
8724b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
8725b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
8726b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
8727b5a892a1SMatthew G. Knepley 
8728b5a892a1SMatthew G. Knepley   Output Parameters:
8729b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
8730b5a892a1SMatthew G. Knepley 
8731b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
8732b5a892a1SMatthew G. Knepley 
8733b5a892a1SMatthew G. Knepley   Level: advanced
8734b5a892a1SMatthew G. Knepley 
8735db781477SPatrick Sanan .seealso: `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`
8736b5a892a1SMatthew G. Knepley @*/
8737b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
8738b5a892a1SMatthew G. Knepley {
8739b5a892a1SMatthew G. Knepley   PetscBool      found;
8740b5a892a1SMatthew G. Knepley 
8741b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
87429566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
87437a8be351SBarry Smith   PetscCheck(found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
8744b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8745b5a892a1SMatthew G. Knepley }
8746b5a892a1SMatthew G. Knepley 
8747b5a892a1SMatthew G. Knepley /*@C
8748b5a892a1SMatthew G. Knepley   DMPolytopeMatchVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
8749b5a892a1SMatthew G. Knepley 
8750b5a892a1SMatthew G. Knepley   Not collective
8751b5a892a1SMatthew G. Knepley 
8752b5a892a1SMatthew G. Knepley   Input Parameters:
8753b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
8754b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
8755b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
8756b5a892a1SMatthew G. Knepley 
8757b5a892a1SMatthew G. Knepley   Output Parameters:
8758b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
8759b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
8760b5a892a1SMatthew G. Knepley 
8761b5a892a1SMatthew G. Knepley   Level: advanced
8762b5a892a1SMatthew G. Knepley 
8763db781477SPatrick Sanan .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`
8764b5a892a1SMatthew G. Knepley @*/
8765b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
8766b5a892a1SMatthew G. Knepley {
8767b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
8768b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
8769b5a892a1SMatthew G. Knepley   PetscInt       o, c;
8770b5a892a1SMatthew G. Knepley 
8771b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
8772b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
8773b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
8774b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
8775b5a892a1SMatthew G. Knepley 
8776b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceVert[arr[c]] != targetVert[c]) break;
8777b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
8778b5a892a1SMatthew G. Knepley   }
8779b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
8780b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8781b5a892a1SMatthew G. Knepley }
8782b5a892a1SMatthew G. Knepley 
8783b5a892a1SMatthew G. Knepley /*@C
8784b5a892a1SMatthew G. Knepley   DMPolytopeGetVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
8785b5a892a1SMatthew G. Knepley 
8786b5a892a1SMatthew G. Knepley   Not collective
8787b5a892a1SMatthew G. Knepley 
8788b5a892a1SMatthew G. Knepley   Input Parameters:
8789b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
8790b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
8791b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
8792b5a892a1SMatthew G. Knepley 
8793b5a892a1SMatthew G. Knepley   Output Parameters:
8794b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
8795b5a892a1SMatthew G. Knepley 
8796b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
8797b5a892a1SMatthew G. Knepley 
8798b5a892a1SMatthew G. Knepley   Level: advanced
8799b5a892a1SMatthew G. Knepley 
8800db781477SPatrick Sanan .seealso: `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
8801b5a892a1SMatthew G. Knepley @*/
8802b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
8803b5a892a1SMatthew G. Knepley {
8804b5a892a1SMatthew G. Knepley   PetscBool      found;
8805b5a892a1SMatthew G. Knepley 
8806b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
88079566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
88087a8be351SBarry Smith   PetscCheck(found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
8809b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8810b5a892a1SMatthew G. Knepley }
8811012bc364SMatthew G. Knepley 
8812012bc364SMatthew G. Knepley /*@C
8813012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
8814012bc364SMatthew G. Knepley 
8815012bc364SMatthew G. Knepley   Not collective
8816012bc364SMatthew G. Knepley 
8817012bc364SMatthew G. Knepley   Input Parameters:
8818012bc364SMatthew G. Knepley + ct    - The DMPolytopeType
8819012bc364SMatthew G. Knepley - point - Coordinates of the point
8820012bc364SMatthew G. Knepley 
8821012bc364SMatthew G. Knepley   Output Parameters:
8822012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
8823012bc364SMatthew G. Knepley 
8824012bc364SMatthew G. Knepley   Level: advanced
8825012bc364SMatthew G. Knepley 
8826db781477SPatrick Sanan .seealso: `DMLocatePoints()`
8827012bc364SMatthew G. Knepley @*/
8828012bc364SMatthew G. Knepley PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
8829012bc364SMatthew G. Knepley {
8830012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
8831012bc364SMatthew G. Knepley   PetscInt  d;
8832012bc364SMatthew G. Knepley 
8833012bc364SMatthew G. Knepley   PetscFunctionBegin;
8834012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
8835012bc364SMatthew G. Knepley   switch (ct) {
8836012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
8837012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
8838012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
8839012bc364SMatthew G. Knepley       if (point[d] < -1.0) {*inside = PETSC_FALSE; break;}
8840012bc364SMatthew G. Knepley       sum += point[d];
8841012bc364SMatthew G. Knepley     }
8842012bc364SMatthew G. Knepley     if (sum > PETSC_SMALL) {*inside = PETSC_FALSE; break;}
8843012bc364SMatthew G. Knepley     break;
8844012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
8845012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
8846012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
8847012bc364SMatthew G. Knepley       if (PetscAbsReal(point[d]) > 1.+PETSC_SMALL) {*inside = PETSC_FALSE; break;}
8848012bc364SMatthew G. Knepley     break;
8849012bc364SMatthew G. Knepley   default:
885098921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
8851012bc364SMatthew G. Knepley   }
8852012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
8853012bc364SMatthew G. Knepley }
8854