xref: /petsc/src/dm/interface/dm.c (revision 799db056a6eb5b741e53662a598f3038bee9c4c9)
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;
75c6b900c6SMatthew G. Knepley   v->L                        = NULL;
76c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
775dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
789a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7996173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
80435a35e8SMatthew G Knepley   {
81435a35e8SMatthew G Knepley     PetscInt i;
82435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
830298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
84f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
85435a35e8SMatthew G Knepley     }
86435a35e8SMatthew G Knepley   }
879566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
889566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(v, NULL, NULL, ds));
899566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
909566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
9114f150ffSMatthew G. Knepley   v->dmBC = NULL;
92a8fb8f29SToby Isaac   v->coarseMesh = NULL;
93f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
94cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
959566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v,VECSTANDARD));
969566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v,MATAIJ));
974a7a4c06SLawrence Mitchell 
981411c6eeSJed Brown   *dm = v;
99a4121054SBarry Smith   PetscFunctionReturn(0);
100a4121054SBarry Smith }
101a4121054SBarry Smith 
10238221697SMatthew G. Knepley /*@
10338221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
10438221697SMatthew G. Knepley 
105d083f849SBarry Smith   Collective
10638221697SMatthew G. Knepley 
10738221697SMatthew G. Knepley   Input Parameter:
10838221697SMatthew G. Knepley . dm - The original DM object
10938221697SMatthew G. Knepley 
11038221697SMatthew G. Knepley   Output Parameter:
11138221697SMatthew G. Knepley . newdm  - The new DM object
11238221697SMatthew G. Knepley 
11338221697SMatthew G. Knepley   Level: beginner
11438221697SMatthew G. Knepley 
1151cb8cacdSPatrick Sanan   Notes:
1161cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1171cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1181cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1191bb6d2a8SBarry Smith 
12089706ed2SPatrick Sanan   The clone is considered set up iff the original is.
12189706ed2SPatrick Sanan 
122db781477SPatrick Sanan .seealso: `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`
1231bb6d2a8SBarry Smith 
12438221697SMatthew G. Knepley @*/
12538221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
12638221697SMatthew G. Knepley {
12738221697SMatthew G. Knepley   PetscSF        sf;
12838221697SMatthew G. Knepley   Vec            coords;
12938221697SMatthew G. Knepley   void          *ctx;
130a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
13138221697SMatthew G. Knepley 
13238221697SMatthew G. Knepley   PetscFunctionBegin;
13338221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13438221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
1359566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject) dm), newdm));
1369566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
137ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
138ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
139c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
1409566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1419566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype,(char**)&(*newdm)->vectype));
1429566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1439566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype,(char**)&(*newdm)->mattype));
1449566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1459566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
14638221697SMatthew G. Knepley   if (dm->ops->clone) {
1479566063dSJacob Faibussowitsch     PetscCall((*dm->ops->clone)(dm, newdm));
14838221697SMatthew G. Knepley   }
1493f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1509566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1519566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1529566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1539566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
154be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
155be4c1c3eSMatthew G. Knepley     DM           ncdm;
156be4c1c3eSMatthew G. Knepley     PetscSection cs;
1575a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
158be4c1c3eSMatthew G. Knepley 
1599566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm->coordinateDM, &cs));
1609566063dSJacob Faibussowitsch     if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
1619566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm)));
1625a0206caSToby Isaac     if (pEndMax >= 0) {
1639566063dSJacob Faibussowitsch       PetscCall(DMClone(dm->coordinateDM, &ncdm));
1649566063dSJacob Faibussowitsch       PetscCall(DMCopyDisc(dm->coordinateDM, ncdm));
1659566063dSJacob Faibussowitsch       PetscCall(DMSetLocalSection(ncdm, cs));
1669566063dSJacob Faibussowitsch       PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1679566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&ncdm));
168be4c1c3eSMatthew G. Knepley     }
169be4c1c3eSMatthew 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   }
17990b157c4SStefano Zampini   {
18090b157c4SStefano Zampini     PetscBool             isper;
181c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1825dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
1839566063dSJacob Faibussowitsch     PetscCall(DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd));
1849566063dSJacob Faibussowitsch     PetscCall(DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd));
185c6b900c6SMatthew G. Knepley   }
18634aa8a36SMatthew G. Knepley   {
18734aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
18834aa8a36SMatthew G. Knepley 
1899566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
1909566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
19134aa8a36SMatthew G. Knepley   }
19238221697SMatthew G. Knepley   PetscFunctionReturn(0);
19338221697SMatthew G. Knepley }
19438221697SMatthew G. Knepley 
1959a42bb27SBarry Smith /*@C
196564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1979a42bb27SBarry Smith 
198d083f849SBarry Smith    Logically Collective on da
1999a42bb27SBarry Smith 
200147403d9SBarry Smith    Input Parameters:
2019a42bb27SBarry Smith +  da - initial distributed array
202147403d9SBarry Smith -  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
2039a42bb27SBarry Smith 
2049a42bb27SBarry Smith    Options Database:
205147403d9SBarry Smith .   -dm_vec_type ctype - the type of vector to create
2069a42bb27SBarry Smith 
2079a42bb27SBarry Smith    Level: intermediate
2089a42bb27SBarry Smith 
209db781477SPatrick Sanan .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`
2109a42bb27SBarry Smith @*/
21119fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2129a42bb27SBarry Smith {
2139a42bb27SBarry Smith   PetscFunctionBegin;
2149a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2159566063dSJacob Faibussowitsch   PetscCall(PetscFree(da->vectype));
2169566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype,(char**)&da->vectype));
2179a42bb27SBarry Smith   PetscFunctionReturn(0);
2189a42bb27SBarry Smith }
2199a42bb27SBarry Smith 
220c0dedaeaSBarry Smith /*@C
221c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
222c0dedaeaSBarry Smith 
223d083f849SBarry Smith    Logically Collective on da
224c0dedaeaSBarry Smith 
225c0dedaeaSBarry Smith    Input Parameter:
226c0dedaeaSBarry Smith .  da - initial distributed array
227c0dedaeaSBarry Smith 
228c0dedaeaSBarry Smith    Output Parameter:
229c0dedaeaSBarry Smith .  ctype - the vector type
230c0dedaeaSBarry Smith 
231c0dedaeaSBarry Smith    Level: intermediate
232c0dedaeaSBarry Smith 
233db781477SPatrick Sanan .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
234c0dedaeaSBarry Smith @*/
235c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
236c0dedaeaSBarry Smith {
237c0dedaeaSBarry Smith   PetscFunctionBegin;
238c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
239c0dedaeaSBarry Smith   *ctype = da->vectype;
240c0dedaeaSBarry Smith   PetscFunctionReturn(0);
241c0dedaeaSBarry Smith }
242c0dedaeaSBarry Smith 
2435f1ad066SMatthew G Knepley /*@
24434f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2455f1ad066SMatthew G Knepley 
2465f1ad066SMatthew G Knepley   Not collective
2475f1ad066SMatthew G Knepley 
2485f1ad066SMatthew G Knepley   Input Parameter:
2495f1ad066SMatthew G Knepley . v - The Vec
2505f1ad066SMatthew G Knepley 
2515f1ad066SMatthew G Knepley   Output Parameter:
2525f1ad066SMatthew G Knepley . dm - The DM
2535f1ad066SMatthew G Knepley 
2545f1ad066SMatthew G Knepley   Level: intermediate
2555f1ad066SMatthew G Knepley 
256db781477SPatrick Sanan .seealso: `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2575f1ad066SMatthew G Knepley @*/
2585f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2595f1ad066SMatthew G Knepley {
2605f1ad066SMatthew G Knepley   PetscFunctionBegin;
2615f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2625f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2639566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm));
2645f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2655f1ad066SMatthew G Knepley }
2665f1ad066SMatthew G Knepley 
2675f1ad066SMatthew G Knepley /*@
268d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2695f1ad066SMatthew G Knepley 
2705f1ad066SMatthew G Knepley   Not collective
2715f1ad066SMatthew G Knepley 
2725f1ad066SMatthew G Knepley   Input Parameters:
2735f1ad066SMatthew G Knepley + v - The Vec
2745f1ad066SMatthew G Knepley - dm - The DM
2755f1ad066SMatthew G Knepley 
276d9805387SMatthew 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.
277d9805387SMatthew G. Knepley 
2785f1ad066SMatthew G Knepley   Level: intermediate
2795f1ad066SMatthew G Knepley 
280db781477SPatrick Sanan .seealso: `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2815f1ad066SMatthew G Knepley @*/
2825f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2835f1ad066SMatthew G Knepley {
2845f1ad066SMatthew G Knepley   PetscFunctionBegin;
2855f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
286d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2879566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm));
2885f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2895f1ad066SMatthew G Knepley }
2905f1ad066SMatthew G Knepley 
291521d9a4cSLisandro Dalcin /*@C
2928f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2938f1509bcSBarry Smith 
294d083f849SBarry Smith    Logically Collective on dm
2958f1509bcSBarry Smith 
2968f1509bcSBarry Smith    Input Parameters:
2978f1509bcSBarry Smith +  dm - the DM context
2988f1509bcSBarry Smith -  ctype - the matrix type
2998f1509bcSBarry Smith 
3008f1509bcSBarry Smith    Options Database:
3018f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3028f1509bcSBarry Smith 
3038f1509bcSBarry Smith    Level: intermediate
3048f1509bcSBarry Smith 
305db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
306db781477SPatrick Sanan           `DMGetISColoringType()`
3078f1509bcSBarry Smith @*/
3088f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3098f1509bcSBarry Smith {
3108f1509bcSBarry Smith   PetscFunctionBegin;
3118f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3128f1509bcSBarry Smith   dm->coloringtype = ctype;
3138f1509bcSBarry Smith   PetscFunctionReturn(0);
3148f1509bcSBarry Smith }
3158f1509bcSBarry Smith 
3168f1509bcSBarry Smith /*@C
3178f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
318521d9a4cSLisandro Dalcin 
319d083f849SBarry Smith    Logically Collective on dm
320521d9a4cSLisandro Dalcin 
321521d9a4cSLisandro Dalcin    Input Parameter:
3228f1509bcSBarry Smith .  dm - the DM context
3238f1509bcSBarry Smith 
3248f1509bcSBarry Smith    Output Parameter:
3258f1509bcSBarry Smith .  ctype - the matrix type
3268f1509bcSBarry Smith 
3278f1509bcSBarry Smith    Options Database:
3288f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3298f1509bcSBarry Smith 
3308f1509bcSBarry Smith    Level: intermediate
3318f1509bcSBarry Smith 
332db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
333db781477SPatrick Sanan           `DMGetISColoringType()`
3348f1509bcSBarry Smith @*/
3358f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3368f1509bcSBarry Smith {
3378f1509bcSBarry Smith   PetscFunctionBegin;
3388f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3398f1509bcSBarry Smith   *ctype = dm->coloringtype;
3408f1509bcSBarry Smith   PetscFunctionReturn(0);
3418f1509bcSBarry Smith }
3428f1509bcSBarry Smith 
3438f1509bcSBarry Smith /*@C
3448f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3458f1509bcSBarry Smith 
346d083f849SBarry Smith    Logically Collective on dm
3478f1509bcSBarry Smith 
3488f1509bcSBarry Smith    Input Parameters:
349521d9a4cSLisandro Dalcin +  dm - the DM context
350a2b5a043SBarry Smith -  ctype - the matrix type
351521d9a4cSLisandro Dalcin 
352521d9a4cSLisandro Dalcin    Options Database:
353147403d9SBarry Smith .   -dm_mat_type ctype - the type of the matrix to create
354521d9a4cSLisandro Dalcin 
355521d9a4cSLisandro Dalcin    Level: intermediate
356521d9a4cSLisandro Dalcin 
357db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`, `DMSetMatType()`, `DMGetMatType()`
358521d9a4cSLisandro Dalcin @*/
35919fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
360521d9a4cSLisandro Dalcin {
361521d9a4cSLisandro Dalcin   PetscFunctionBegin;
362521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3639566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->mattype));
3649566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype,(char**)&dm->mattype));
365521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
366521d9a4cSLisandro Dalcin }
367521d9a4cSLisandro Dalcin 
368c0dedaeaSBarry Smith /*@C
369c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
370c0dedaeaSBarry Smith 
371d083f849SBarry Smith    Logically Collective on dm
372c0dedaeaSBarry Smith 
373c0dedaeaSBarry Smith    Input Parameter:
374c0dedaeaSBarry Smith .  dm - the DM context
375c0dedaeaSBarry Smith 
376c0dedaeaSBarry Smith    Output Parameter:
377c0dedaeaSBarry Smith .  ctype - the matrix type
378c0dedaeaSBarry Smith 
379c0dedaeaSBarry Smith    Options Database:
380147403d9SBarry Smith .   -dm_mat_type ctype - the matrix type
381c0dedaeaSBarry Smith 
382c0dedaeaSBarry Smith    Level: intermediate
383c0dedaeaSBarry Smith 
384db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`, `DMSetMatType()`, `DMGetMatType()`
385c0dedaeaSBarry Smith @*/
386c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
387c0dedaeaSBarry Smith {
388c0dedaeaSBarry Smith   PetscFunctionBegin;
389c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
390c0dedaeaSBarry Smith   *ctype = dm->mattype;
391c0dedaeaSBarry Smith   PetscFunctionReturn(0);
392c0dedaeaSBarry Smith }
393c0dedaeaSBarry Smith 
394c688c046SMatthew G Knepley /*@
39534f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
396c688c046SMatthew G Knepley 
397c688c046SMatthew G Knepley   Not collective
398c688c046SMatthew G Knepley 
399c688c046SMatthew G Knepley   Input Parameter:
400c688c046SMatthew G Knepley . A - The Mat
401c688c046SMatthew G Knepley 
402c688c046SMatthew G Knepley   Output Parameter:
403c688c046SMatthew G Knepley . dm - The DM
404c688c046SMatthew G Knepley 
405c688c046SMatthew G Knepley   Level: intermediate
406c688c046SMatthew G Knepley 
4078f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4088f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4098f1509bcSBarry Smith 
410db781477SPatrick Sanan .seealso: `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
411c688c046SMatthew G Knepley @*/
412c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
413c688c046SMatthew G Knepley {
414c688c046SMatthew G Knepley   PetscFunctionBegin;
415c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
416c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
4179566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm));
418c688c046SMatthew G Knepley   PetscFunctionReturn(0);
419c688c046SMatthew G Knepley }
420c688c046SMatthew G Knepley 
421c688c046SMatthew G Knepley /*@
422c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
423c688c046SMatthew G Knepley 
424c688c046SMatthew G Knepley   Not collective
425c688c046SMatthew G Knepley 
426c688c046SMatthew G Knepley   Input Parameters:
427c688c046SMatthew G Knepley + A - The Mat
428c688c046SMatthew G Knepley - dm - The DM
429c688c046SMatthew G Knepley 
430c688c046SMatthew G Knepley   Level: intermediate
431c688c046SMatthew G Knepley 
4328f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4338f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4348f1509bcSBarry Smith 
435db781477SPatrick Sanan .seealso: `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
436c688c046SMatthew G Knepley @*/
437c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
438c688c046SMatthew G Knepley {
439c688c046SMatthew G Knepley   PetscFunctionBegin;
440c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4418865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
4429566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm));
443c688c046SMatthew G Knepley   PetscFunctionReturn(0);
444c688c046SMatthew G Knepley }
445c688c046SMatthew G Knepley 
4469a42bb27SBarry Smith /*@C
4479a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4486757b960SDave May    DM options in the database.
4499a42bb27SBarry Smith 
450d083f849SBarry Smith    Logically Collective on dm
4519a42bb27SBarry Smith 
452d8d19677SJose E. Roman    Input Parameters:
4538353ddbbSDave May +  da - the DM context
4549a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4559a42bb27SBarry Smith 
4569a42bb27SBarry Smith    Notes:
4579a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4589a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4599a42bb27SBarry Smith 
4609a42bb27SBarry Smith    Level: advanced
4619a42bb27SBarry Smith 
462db781477SPatrick Sanan .seealso: `DMSetFromOptions()`
4639a42bb27SBarry Smith @*/
4647087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4659a42bb27SBarry Smith {
4669a42bb27SBarry Smith   PetscFunctionBegin;
4679a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4689566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm,prefix));
469691be533SLawrence Mitchell   if (dm->sf) {
4709566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix));
471691be533SLawrence Mitchell   }
4721bb6d2a8SBarry Smith   if (dm->sectionSF) {
4739566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix));
474691be533SLawrence Mitchell   }
4759a42bb27SBarry Smith   PetscFunctionReturn(0);
4769a42bb27SBarry Smith }
4779a42bb27SBarry Smith 
47831697293SDave May /*@C
47931697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
48031697293SDave May    DM options in the database.
48131697293SDave May 
482d083f849SBarry Smith    Logically Collective on dm
48331697293SDave May 
48431697293SDave May    Input Parameters:
48531697293SDave May +  dm - the DM context
48631697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48731697293SDave May 
48831697293SDave May    Notes:
48931697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
49031697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
49131697293SDave May 
49231697293SDave May    Level: advanced
49331697293SDave May 
494db781477SPatrick Sanan .seealso: `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`
49531697293SDave May @*/
49631697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49731697293SDave May {
49831697293SDave May   PetscFunctionBegin;
49931697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5009566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix));
50131697293SDave May   PetscFunctionReturn(0);
50231697293SDave May }
50331697293SDave May 
50431697293SDave May /*@C
50531697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50631697293SDave May    DM options in the database.
50731697293SDave May 
50831697293SDave May    Not Collective
50931697293SDave May 
51031697293SDave May    Input Parameters:
51131697293SDave May .  dm - the DM context
51231697293SDave May 
51331697293SDave May    Output Parameters:
51431697293SDave May .  prefix - pointer to the prefix string used is returned
51531697293SDave May 
51695452b02SPatrick Sanan    Notes:
51795452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
51831697293SDave May    sufficient length to hold the prefix.
51931697293SDave May 
52031697293SDave May    Level: advanced
52131697293SDave May 
522db781477SPatrick Sanan .seealso: `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`
52331697293SDave May @*/
52431697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52531697293SDave May {
52631697293SDave May   PetscFunctionBegin;
52731697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5289566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm,prefix));
52931697293SDave May   PetscFunctionReturn(0);
53031697293SDave May }
53131697293SDave May 
53288bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53388bdff64SToby Isaac {
5346eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
53588bdff64SToby Isaac 
53688bdff64SToby Isaac   PetscFunctionBegin;
537aab5bcd8SJed Brown   *ncrefct = 0;
53888bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
53988bdff64SToby Isaac     refct--;
54088bdff64SToby Isaac     if (recurseCoarse) {
54188bdff64SToby Isaac       PetscInt coarseCount;
54288bdff64SToby Isaac 
5439566063dSJacob Faibussowitsch       PetscCall(DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount));
54488bdff64SToby Isaac       refct += coarseCount;
54588bdff64SToby Isaac     }
54688bdff64SToby Isaac   }
54788bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
54888bdff64SToby Isaac     refct--;
54988bdff64SToby Isaac     if (recurseFine) {
55088bdff64SToby Isaac       PetscInt fineCount;
55188bdff64SToby Isaac 
5529566063dSJacob Faibussowitsch       PetscCall(DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount));
55388bdff64SToby Isaac       refct += fineCount;
55488bdff64SToby Isaac     }
55588bdff64SToby Isaac   }
55688bdff64SToby Isaac   *ncrefct = refct;
55788bdff64SToby Isaac   PetscFunctionReturn(0);
55888bdff64SToby Isaac }
55988bdff64SToby Isaac 
560f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
561354557abSToby Isaac {
5625d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
563354557abSToby Isaac 
564354557abSToby Isaac   PetscFunctionBegin;
565354557abSToby Isaac   /* destroy the labels */
566354557abSToby Isaac   while (next) {
567354557abSToby Isaac     DMLabelLink tmp = next->next;
568354557abSToby Isaac 
5695d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
570ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
5719566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
5729566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
573354557abSToby Isaac     next = tmp;
574354557abSToby Isaac   }
5755d80c0bfSVaclav Hapla   dm->labels = NULL;
576354557abSToby Isaac   PetscFunctionReturn(0);
577354557abSToby Isaac }
578354557abSToby Isaac 
5791fb7b255SJunchao Zhang /*@C
5808472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
58147c6ae99SBarry Smith 
582d083f849SBarry Smith     Collective on dm
58347c6ae99SBarry Smith 
58447c6ae99SBarry Smith     Input Parameter:
58547c6ae99SBarry Smith .   dm - the DM object to destroy
58647c6ae99SBarry Smith 
58747c6ae99SBarry Smith     Level: developer
58847c6ae99SBarry Smith 
589db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
59047c6ae99SBarry Smith 
59147c6ae99SBarry Smith @*/
592fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
59347c6ae99SBarry Smith {
5946eb26441SStefano Zampini   PetscInt       cnt;
595dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
59647c6ae99SBarry Smith 
59747c6ae99SBarry Smith   PetscFunctionBegin;
5986bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
5996bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
60087e657c6SBarry Smith 
60188bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
6029566063dSJacob Faibussowitsch   PetscCall(DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt));
60388bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
604ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
6056bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6066bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6076eb26441SStefano Zampini 
6089566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6099566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
6106eb26441SStefano Zampini 
611f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6120298fd71SBarry Smith   (*dm)->namedglobal = NULL;
613f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6142348bcf4SPeter Brune     nnext = nlink->next;
6157a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN,((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
6169566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6179566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6189566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
6192348bcf4SPeter Brune   }
620f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6210298fd71SBarry Smith   (*dm)->namedlocal = NULL;
622f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
623f490541aSPeter Brune     nnext = nlink->next;
6247a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN,((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
6259566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6269566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6279566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
628f490541aSPeter Brune   }
6292348bcf4SPeter Brune 
630b17ce1afSJed Brown   /* Destroy the list of hooks */
631c833c3b5SJed Brown   {
632c833c3b5SJed Brown     DMCoarsenHookLink link,next;
633b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
634b17ce1afSJed Brown       next = link->next;
6359566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
636b17ce1afSJed Brown     }
6370298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
638c833c3b5SJed Brown   }
639c833c3b5SJed Brown   {
640c833c3b5SJed Brown     DMRefineHookLink link,next;
641c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
642c833c3b5SJed Brown       next = link->next;
6439566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
644c833c3b5SJed Brown     }
6450298fd71SBarry Smith     (*dm)->refinehook = NULL;
646c833c3b5SJed Brown   }
647be081cd6SPeter Brune   {
648be081cd6SPeter Brune     DMSubDomainHookLink link,next;
649be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
650be081cd6SPeter Brune       next = link->next;
6519566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
652be081cd6SPeter Brune     }
6530298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
654be081cd6SPeter Brune   }
655baf369e7SPeter Brune   {
656baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
657baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
658baf369e7SPeter Brune       next = link->next;
6599566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
660baf369e7SPeter Brune     }
6610298fd71SBarry Smith     (*dm)->gtolhook = NULL;
662baf369e7SPeter Brune   }
663d4d07f1eSToby Isaac   {
664d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
665d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
666d4d07f1eSToby Isaac       next = link->next;
6679566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
668d4d07f1eSToby Isaac     }
669d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
670d4d07f1eSToby Isaac   }
671aa1993deSMatthew G Knepley   /* Destroy the work arrays */
672aa1993deSMatthew G Knepley   {
673aa1993deSMatthew G Knepley     DMWorkLink link,next;
6747a8be351SBarry Smith     PetscCheck(!(*dm)->workout,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
675aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
676aa1993deSMatthew G Knepley       next = link->next;
6779566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
6789566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
679aa1993deSMatthew G Knepley     }
6800298fd71SBarry Smith     (*dm)->workin = NULL;
681aa1993deSMatthew G Knepley   }
682c58f1c22SToby Isaac   /* destroy the labels */
6839566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
684f4cdcedcSVaclav Hapla   /* destroy the fields */
6859566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
686f4cdcedcSVaclav Hapla   /* destroy the boundaries */
687e6f8dbb6SToby Isaac   {
688e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
689e6f8dbb6SToby Isaac     while (next) {
690e6f8dbb6SToby Isaac       DMBoundary b = next;
691e6f8dbb6SToby Isaac 
692e6f8dbb6SToby Isaac       next = b->next;
6939566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
694e6f8dbb6SToby Isaac     }
695e6f8dbb6SToby Isaac   }
696b17ce1afSJed Brown 
6979566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
6989566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
6999566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
70052536dc3SBarry Smith 
7011a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7029566063dSJacob Faibussowitsch     PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7031a266240SBarry Smith   }
7049566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7059566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7069566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7079566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
70888ed4aceSMatthew G Knepley 
7099566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7109566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
7119566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7129566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7139566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7149566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7159566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
716736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
717736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7189566063dSJacob Faibussowitsch       PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
719736995cdSBlaise Bourdin     }
7209566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject) (*dm)->sfMigration));
721736995cdSBlaise Bourdin   }
7229a2a23afSMatthew G. Knepley   {
7239a2a23afSMatthew G. Knepley     Vec     *auxData;
7249a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7259a2a23afSMatthew G. Knepley 
7269566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetSize((*dm)->auxData, &n));
7279566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &auxData));
7289566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetVals((*dm)->auxData, &off, auxData));
7299566063dSJacob Faibussowitsch     for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
7309566063dSJacob Faibussowitsch     PetscCall(PetscFree(auxData));
7319566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
7329a2a23afSMatthew G. Knepley   }
73388bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
7349566063dSJacob Faibussowitsch     PetscCall(DMSetFineDM((*dm)->coarseMesh,NULL));
73588bdff64SToby Isaac   }
7366eb26441SStefano Zampini 
7379566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
73888bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
7399566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM((*dm)->fineMesh,NULL));
74088bdff64SToby Isaac   }
7419566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7429566063dSJacob Faibussowitsch   PetscCall(DMFieldDestroy(&(*dm)->coordinateField));
7439566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coordinateDM));
7449566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->coordinates));
7459566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->coordinatesLocal));
7469566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7479566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7489566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->bdtype));
7499566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7509566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7519566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7526636e97aSMatthew G Knepley 
7539566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7549566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
755e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7569566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
757732e2eb9SMatthew G Knepley 
758ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7599566063dSJacob Faibussowitsch     PetscCall((*(*dm)->ops->destroy)(*dm));
760ed3c66a1SDave May   }
7619566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
762f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7639566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7649566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
765f918ec44SMatthew G. Knepley #endif
766435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7679566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
76847c6ae99SBarry Smith   PetscFunctionReturn(0);
76947c6ae99SBarry Smith }
77047c6ae99SBarry Smith 
771d7bf68aeSBarry Smith /*@
772d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
773d7bf68aeSBarry Smith 
774d083f849SBarry Smith     Collective on dm
775d7bf68aeSBarry Smith 
776d7bf68aeSBarry Smith     Input Parameter:
777d7bf68aeSBarry Smith .   dm - the DM object to setup
778d7bf68aeSBarry Smith 
779d7bf68aeSBarry Smith     Level: developer
780d7bf68aeSBarry Smith 
781db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
782d7bf68aeSBarry Smith 
783d7bf68aeSBarry Smith @*/
7847087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
785d7bf68aeSBarry Smith {
786d7bf68aeSBarry Smith   PetscFunctionBegin;
787171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7888387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
789d7bf68aeSBarry Smith   if (dm->ops->setup) {
7909566063dSJacob Faibussowitsch     PetscCall((*dm->ops->setup)(dm));
791d7bf68aeSBarry Smith   }
7928387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
793d7bf68aeSBarry Smith   PetscFunctionReturn(0);
794d7bf68aeSBarry Smith }
795d7bf68aeSBarry Smith 
796d7bf68aeSBarry Smith /*@
797d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
798d7bf68aeSBarry Smith 
799d083f849SBarry Smith     Collective on dm
800d7bf68aeSBarry Smith 
801d7bf68aeSBarry Smith     Input Parameter:
802d7bf68aeSBarry Smith .   dm - the DM object to set options for
803d7bf68aeSBarry Smith 
804732e2eb9SMatthew G Knepley     Options Database:
8055b8ffe73SMark Adams +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix() and DMCreateMassMatrix(), but do not fill it with zeros
8064164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8074164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
808a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
809a4ea9b21SRichard 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
810732e2eb9SMatthew G Knepley 
8119318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8129318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8139318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
814cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
8159318fe57SMatthew G. Knepley . -dm_plex_shape <shape>            - The domain shape, such as DM_SHAPE_BOX, DM_SHAPE_SPHERE, etc.
8169318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8179318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8189318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
8199318fe57SMatthew G. Knepley . -dm_plex_simplex <bool>           - PETSC_TRUE for simplex elements, PETSC_FALSE for tensor elements
8209318fe57SMatthew G. Knepley . -dm_plex_interpolate <bool>       - PETSC_TRUE turns on topological interpolation (creating edges and faces)
8219318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8229318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8239318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8249318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
8259318fe57SMatthew G. Knepley . -dm_plex_box_bd <bx,by,bz>        - Specify the DMBoundaryType for each direction
8269318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8279318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8289318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8299318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
830bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8319318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8329318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8339318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8349318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
835bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
836d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
837d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
838d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
839d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
840d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
841909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
842909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8439318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8449318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8459318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8469318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8479318fe57SMatthew G. Knepley 
848384a6580SVaclav Hapla     DMPLEX Specific Checks
849384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
850384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
851384a6580SVaclav 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()
852384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
853384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
854384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
855384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
856d7bf68aeSBarry Smith 
85795eb5ee5SVaclav Hapla     Level: intermediate
85895eb5ee5SVaclav Hapla 
859db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
860db781477SPatrick Sanan          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`
861d7bf68aeSBarry Smith 
862d7bf68aeSBarry Smith @*/
8637087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
864d7bf68aeSBarry Smith {
8657781c08eSBarry Smith   char           typeName[256];
866ca266f36SBarry Smith   PetscBool      flg;
867d7bf68aeSBarry Smith 
868d7bf68aeSBarry Smith   PetscFunctionBegin;
869171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87049be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
8719566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
8729566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
873d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
8749566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,NULL));
8759566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg));
876f9ba7244SBarry Smith   if (flg) {
8779566063dSJacob Faibussowitsch     PetscCall(DMSetVecType(dm,typeName));
878f9ba7244SBarry Smith   }
8799566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg));
880073dac72SJed Brown   if (flg) {
8819566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(dm,typeName));
882073dac72SJed Brown   }
8839566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL));
8849566063dSJacob 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));
885f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
8869566063dSJacob Faibussowitsch     PetscCall((*dm->ops->setfromoptions)(PetscOptionsObject,dm));
887f9ba7244SBarry Smith   }
888f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8899566063dSJacob Faibussowitsch   PetscCall(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm));
890d0609cedSBarry Smith   PetscOptionsEnd();
891d7bf68aeSBarry Smith   PetscFunctionReturn(0);
892d7bf68aeSBarry Smith }
893d7bf68aeSBarry Smith 
894fc9bc008SSatish Balay /*@C
895fe2efc57SMark    DMViewFromOptions - View from Options
896fe2efc57SMark 
897fe2efc57SMark    Collective on DM
898fe2efc57SMark 
899fe2efc57SMark    Input Parameters:
900fe2efc57SMark +  dm - the DM object
901736c3998SJose E. Roman .  obj - Optional object
902736c3998SJose E. Roman -  name - command line option
903fe2efc57SMark 
904fe2efc57SMark    Level: intermediate
905db781477SPatrick Sanan .seealso: `DM`, `DMView`, `PetscObjectViewFromOptions()`, `DMCreate()`
906fe2efc57SMark @*/
907fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
908fe2efc57SMark {
909fe2efc57SMark   PetscFunctionBegin;
910fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9119566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm,obj,name));
912fe2efc57SMark   PetscFunctionReturn(0);
913fe2efc57SMark }
914fe2efc57SMark 
915fe2efc57SMark /*@C
916224748a4SBarry Smith     DMView - Views a DM
91747c6ae99SBarry Smith 
918d083f849SBarry Smith     Collective on dm
91947c6ae99SBarry Smith 
920d8d19677SJose E. Roman     Input Parameters:
92147c6ae99SBarry Smith +   dm - the DM object to view
92247c6ae99SBarry Smith -   v - the viewer
92347c6ae99SBarry Smith 
924cd7e8a5eSksagiyam     Notes:
925cd7e8a5eSksagiyam     Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
926cd7e8a5eSksagiyam     meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
927cd7e8a5eSksagiyam     before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
928cd7e8a5eSksagiyam 
929224748a4SBarry Smith     Level: beginner
93047c6ae99SBarry Smith 
931db781477SPatrick Sanan .seealso `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
93247c6ae99SBarry Smith 
93347c6ae99SBarry Smith @*/
9347087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
93547c6ae99SBarry Smith {
93632c0f0efSBarry Smith   PetscBool         isbinary;
93776a8abe0SBarry Smith   PetscMPIInt       size;
93876a8abe0SBarry Smith   PetscViewerFormat format;
93947c6ae99SBarry Smith 
94047c6ae99SBarry Smith   PetscFunctionBegin;
941171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9423014e516SBarry Smith   if (!v) {
9439566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v));
9443014e516SBarry Smith   }
945b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
94674903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
94774903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
94874903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
94974903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
95074903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
95174903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
95274903a4fSStefano Zampini      in an error here */
95374903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9549566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
955b1b135c8SBarry Smith 
9569566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v,&format));
9579566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size));
95876a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
9599566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm,v));
9609566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary));
96132c0f0efSBarry Smith   if (isbinary) {
96255849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
96332c0f0efSBarry Smith     char     type[256];
96432c0f0efSBarry Smith 
9659566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v,&classid,1,PETSC_INT));
9669566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(type,((PetscObject)dm)->type_name,256));
9679566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v,type,256,PETSC_CHAR));
96832c0f0efSBarry Smith   }
9690c010503SBarry Smith   if (dm->ops->view) {
9709566063dSJacob Faibussowitsch     PetscCall((*dm->ops->view)(dm,v));
97147c6ae99SBarry Smith   }
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));
18808865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
18819566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createfieldis)(dm, numFields, fieldNames, fields));
188269ca1f37SDmitry Karpeev   }
18834d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
18844d343eeaSMatthew G Knepley }
18854d343eeaSMatthew G Knepley 
188616621825SDmitry Karpeev /*@C
188716621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
188816621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
188916621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1890e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1891e7c4fc90SDmitry Karpeev 
1892e7c4fc90SDmitry Karpeev   Not collective
1893e7c4fc90SDmitry Karpeev 
1894e7c4fc90SDmitry Karpeev   Input Parameter:
1895e7c4fc90SDmitry Karpeev . dm - the DM object
1896e7c4fc90SDmitry Karpeev 
1897e7c4fc90SDmitry Karpeev   Output Parameters:
18980298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
18990298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
19000298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
19010298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1902e7c4fc90SDmitry Karpeev 
1903e7c4fc90SDmitry Karpeev   Level: intermediate
1904e7c4fc90SDmitry Karpeev 
1905e7c4fc90SDmitry Karpeev   Notes:
1906e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1907e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1908e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1909e7c4fc90SDmitry Karpeev 
1910db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
1911e7c4fc90SDmitry Karpeev @*/
191216621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1913e7c4fc90SDmitry Karpeev {
1914e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1915e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19168865f1eaSKarl Rupp   if (len) {
1917534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
19188865f1eaSKarl Rupp     *len = 0;
19198865f1eaSKarl Rupp   }
19208865f1eaSKarl Rupp   if (namelist) {
19218865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1922ea78f98cSLisandro Dalcin     *namelist = NULL;
19238865f1eaSKarl Rupp   }
19248865f1eaSKarl Rupp   if (islist) {
19258865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1926ea78f98cSLisandro Dalcin     *islist = NULL;
19278865f1eaSKarl Rupp   }
19288865f1eaSKarl Rupp   if (dmlist) {
19298865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1930ea78f98cSLisandro Dalcin     *dmlist = NULL;
19318865f1eaSKarl Rupp   }
1932f3f0edfdSDmitry Karpeev   /*
1933f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1934f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1935f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1936f3f0edfdSDmitry Karpeev    */
19377a8be351SBarry Smith   PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
193816621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1939435a35e8SMatthew G Knepley     PetscSection section;
1940435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1941435a35e8SMatthew G Knepley 
19429566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
19439566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
1944435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1945f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
19469566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields,namelist));
19479566063dSJacob Faibussowitsch       if (islist)   PetscCall(PetscMalloc1(numFields,islist));
19489566063dSJacob Faibussowitsch       if (dmlist)   PetscCall(PetscMalloc1(numFields,dmlist));
1949435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1950435a35e8SMatthew G Knepley         const char *fieldName;
1951435a35e8SMatthew G Knepley 
19529566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
195303dc3394SMatthew G. Knepley         if (namelist) {
19549566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19559566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char**) &(*namelist)[f]));
1956435a35e8SMatthew G Knepley         }
195703dc3394SMatthew G. Knepley       }
1958435a35e8SMatthew G Knepley     } else {
19599566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
1960e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
19610298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1962e7c4fc90SDmitry Karpeev     }
19638865f1eaSKarl Rupp   } else {
19649566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist));
196516621825SDmitry Karpeev   }
196616621825SDmitry Karpeev   PetscFunctionReturn(0);
196716621825SDmitry Karpeev }
196816621825SDmitry Karpeev 
1969564cec59SMatthew G. Knepley /*@
1970435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1971435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1972435a35e8SMatthew G Knepley 
1973435a35e8SMatthew G Knepley   Not collective
1974435a35e8SMatthew G Knepley 
1975435a35e8SMatthew G Knepley   Input Parameters:
19762adcc780SMatthew G. Knepley + dm        - The DM object
19772adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
19782adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1979435a35e8SMatthew G Knepley 
1980435a35e8SMatthew G Knepley   Output Parameters:
19812adcc780SMatthew G. Knepley + is - The global indices for the subproblem
19822adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1983435a35e8SMatthew G Knepley 
19845d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19855d3b26e6SMatthew G. Knepley 
1986435a35e8SMatthew G Knepley   Level: intermediate
1987435a35e8SMatthew G Knepley 
1988db781477SPatrick Sanan .seealso `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
1989435a35e8SMatthew G Knepley @*/
199037bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1991435a35e8SMatthew G Knepley {
1992435a35e8SMatthew G Knepley   PetscFunctionBegin;
1993435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1994dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields,3);
19958865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
19968865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
19977a8be351SBarry Smith   PetscCheck(dm->ops->createsubdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
19989566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createsubdm)(dm, numFields, fields, is, subdm));
1999435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
2000435a35e8SMatthew G Knepley }
2001435a35e8SMatthew G Knepley 
20022adcc780SMatthew G. Knepley /*@C
20032adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
20042adcc780SMatthew G. Knepley 
20052adcc780SMatthew G. Knepley   Not collective
20062adcc780SMatthew G. Knepley 
2007d8d19677SJose E. Roman   Input Parameters:
20082adcc780SMatthew G. Knepley + dms - The DM objects
20092adcc780SMatthew G. Knepley - len - The number of DMs
20102adcc780SMatthew G. Knepley 
20112adcc780SMatthew G. Knepley   Output Parameters:
2012a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
20132adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
20142adcc780SMatthew G. Knepley 
20155d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
20165d3b26e6SMatthew G. Knepley 
20172adcc780SMatthew G. Knepley   Level: intermediate
20182adcc780SMatthew G. Knepley 
2019db781477SPatrick Sanan .seealso `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
20202adcc780SMatthew G. Knepley @*/
20212adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
20222adcc780SMatthew G. Knepley {
20232adcc780SMatthew G. Knepley   PetscInt       i;
20242adcc780SMatthew G. Knepley 
20252adcc780SMatthew G. Knepley   PetscFunctionBegin;
20262adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
20272adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
20282adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
2029a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
203063a3b9bcSJacob Faibussowitsch   PetscCheck(len >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, len);
20312adcc780SMatthew G. Knepley   if (len) {
2032b9d85ea2SLisandro Dalcin     DM dm = dms[0];
20337a8be351SBarry Smith     PetscCheck(dm->ops->createsuperdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
20349566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createsuperdm)(dms, len, is, superdm));
20352adcc780SMatthew G. Knepley   }
20362adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
20372adcc780SMatthew G. Knepley }
20382adcc780SMatthew G. Knepley 
203916621825SDmitry Karpeev /*@C
20408d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
20418d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
20428d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
20438d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
20448d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
204516621825SDmitry Karpeev 
204616621825SDmitry Karpeev   Not collective
204716621825SDmitry Karpeev 
204816621825SDmitry Karpeev   Input Parameter:
204916621825SDmitry Karpeev . dm - the DM object
205016621825SDmitry Karpeev 
205116621825SDmitry Karpeev   Output Parameters:
20520298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
20530298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
20540298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
20550298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
20560298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
205716621825SDmitry Karpeev 
205816621825SDmitry Karpeev   Level: intermediate
205916621825SDmitry Karpeev 
206016621825SDmitry Karpeev   Notes:
206116621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
206216621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
206316621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
206416621825SDmitry Karpeev 
2065db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldDecomposition()`
206616621825SDmitry Karpeev @*/
20678d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
206816621825SDmitry Karpeev {
2069be081cd6SPeter Brune   DMSubDomainHookLink link;
2070be081cd6SPeter Brune   PetscInt            i,l;
207116621825SDmitry Karpeev 
207216621825SDmitry Karpeev   PetscFunctionBegin;
207316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2074dadcf809SJacob Faibussowitsch   if (len)           {PetscValidIntPointer(len,2);            *len         = 0;}
20750298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
20760298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
20770298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
20780298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
2079f3f0edfdSDmitry Karpeev   /*
2080f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2081f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2082f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2083f3f0edfdSDmitry Karpeev    */
20847a8be351SBarry Smith   PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
208516621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
20869566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist));
208714a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2088f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2089be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2090be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
20919566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm,(*dmlist)[i],link->ctx));
2092be081cd6SPeter Brune         }
2093648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2094e7c4fc90SDmitry Karpeev       }
209514a18fd3SPeter Brune     }
209614a18fd3SPeter Brune     if (len) *len = l;
209714a18fd3SPeter Brune   }
2098e30e807fSPeter Brune   PetscFunctionReturn(0);
2099e30e807fSPeter Brune }
2100e30e807fSPeter Brune 
2101e30e807fSPeter Brune /*@C
2102e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2103e30e807fSPeter Brune 
2104e30e807fSPeter Brune   Not collective
2105e30e807fSPeter Brune 
2106e30e807fSPeter Brune   Input Parameters:
2107e30e807fSPeter Brune + dm - the DM object
2108e30e807fSPeter Brune . n  - the number of subdomain scatters
2109e30e807fSPeter Brune - subdms - the local subdomains
2110e30e807fSPeter Brune 
2111e30e807fSPeter Brune   Output Parameters:
21126b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2113e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2114e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2115e30e807fSPeter Brune 
211695452b02SPatrick Sanan   Notes:
211795452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2118e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2119e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2120e30e807fSPeter Brune   solution and residual data.
2121e30e807fSPeter Brune 
2122e30e807fSPeter Brune   Level: developer
2123e30e807fSPeter Brune 
2124db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2125e30e807fSPeter Brune @*/
2126e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2127e30e807fSPeter Brune {
2128e30e807fSPeter Brune   PetscFunctionBegin;
2129e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2130e30e807fSPeter Brune   PetscValidPointer(subdms,3);
21317a8be351SBarry Smith   PetscCheck(dm->ops->createddscatters,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
21329566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat));
2133e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2134e7c4fc90SDmitry Karpeev }
2135e7c4fc90SDmitry Karpeev 
213647c6ae99SBarry Smith /*@
213747c6ae99SBarry Smith   DMRefine - Refines a DM object
213847c6ae99SBarry Smith 
2139d083f849SBarry Smith   Collective on dm
214047c6ae99SBarry Smith 
2141d8d19677SJose E. Roman   Input Parameters:
214247c6ae99SBarry Smith + dm   - the DM object
214391d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
214447c6ae99SBarry Smith 
214547c6ae99SBarry Smith   Output Parameter:
21460298fd71SBarry Smith . dmf - the refined DM, or NULL
2147ae0a1c52SMatthew G Knepley 
2148f27dd7c6SMatthew G. Knepley   Options Database Keys:
2149412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2150412e9a14SMatthew G. Knepley 
21510298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
215247c6ae99SBarry Smith 
215347c6ae99SBarry Smith   Level: developer
215447c6ae99SBarry Smith 
2155db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
215647c6ae99SBarry Smith @*/
21577087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
215847c6ae99SBarry Smith {
2159c833c3b5SJed Brown   DMRefineHookLink link;
216047c6ae99SBarry Smith 
216147c6ae99SBarry Smith   PetscFunctionBegin;
2162732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21637a8be351SBarry Smith   PetscCheck(dm->ops->refine,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
21649566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine,dm,0,0,0));
21659566063dSJacob Faibussowitsch   PetscCall((*dm->ops->refine)(dm,comm,dmf));
21664057135bSMatthew G Knepley   if (*dmf) {
216743842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
21688865f1eaSKarl Rupp 
21699566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf));
21708865f1eaSKarl Rupp 
2171644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
21720598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2173656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
21748865f1eaSKarl Rupp 
21759566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf,dm->mattype));
2176c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
21778865f1eaSKarl Rupp       if (link->refinehook) {
21789566063dSJacob Faibussowitsch         PetscCall((*link->refinehook)(dm,*dmf,link->ctx));
21798865f1eaSKarl Rupp       }
2180c833c3b5SJed Brown     }
2181c833c3b5SJed Brown   }
21829566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine,dm,0,0,0));
2183c833c3b5SJed Brown   PetscFunctionReturn(0);
2184c833c3b5SJed Brown }
2185c833c3b5SJed Brown 
2186bb9467b5SJed Brown /*@C
2187c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2188c833c3b5SJed Brown 
2189c833c3b5SJed Brown    Logically Collective
2190c833c3b5SJed Brown 
21914165533cSJose E. Roman    Input Parameters:
2192c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2193c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2194c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21950298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2196c833c3b5SJed Brown 
2197c833c3b5SJed Brown    Calling sequence of refinehook:
2198c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2199c833c3b5SJed Brown 
2200c833c3b5SJed Brown +  coarse - coarse level DM
2201c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2202c833c3b5SJed Brown -  ctx - optional user-defined function context
2203c833c3b5SJed Brown 
2204c833c3b5SJed Brown    Calling sequence for interphook:
2205c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2206c833c3b5SJed Brown 
2207c833c3b5SJed Brown +  coarse - coarse level DM
2208c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2209c833c3b5SJed Brown .  fine - fine level DM to update
2210c833c3b5SJed Brown -  ctx - optional user-defined function context
2211c833c3b5SJed Brown 
2212c833c3b5SJed Brown    Level: advanced
2213c833c3b5SJed Brown 
2214c833c3b5SJed Brown    Notes:
2215c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2216c833c3b5SJed Brown 
2217c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2218c833c3b5SJed Brown 
2219bb9467b5SJed Brown    This function is currently not available from Fortran.
2220bb9467b5SJed Brown 
2221db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2222c833c3b5SJed Brown @*/
2223c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2224c833c3b5SJed Brown {
2225c833c3b5SJed Brown   DMRefineHookLink link,*p;
2226c833c3b5SJed Brown 
2227c833c3b5SJed Brown   PetscFunctionBegin;
2228c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22293d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
22303d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
22313d8e3701SJed Brown   }
22329566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2233c833c3b5SJed Brown   link->refinehook = refinehook;
2234c833c3b5SJed Brown   link->interphook = interphook;
2235c833c3b5SJed Brown   link->ctx        = ctx;
22360298fd71SBarry Smith   link->next       = NULL;
2237c833c3b5SJed Brown   *p               = link;
2238c833c3b5SJed Brown   PetscFunctionReturn(0);
2239c833c3b5SJed Brown }
2240c833c3b5SJed Brown 
22413d8e3701SJed Brown /*@C
22423d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
22433d8e3701SJed Brown 
22443d8e3701SJed Brown    Logically Collective
22453d8e3701SJed Brown 
22464165533cSJose E. Roman    Input Parameters:
22473d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
22483d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
22493d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
22503d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
22513d8e3701SJed Brown 
22523d8e3701SJed Brown    Level: advanced
22533d8e3701SJed Brown 
22543d8e3701SJed Brown    Notes:
22553d8e3701SJed Brown    This function does nothing if the hook is not in the list.
22563d8e3701SJed Brown 
22573d8e3701SJed Brown    This function is currently not available from Fortran.
22583d8e3701SJed Brown 
2259db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
22603d8e3701SJed Brown @*/
22613d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
22623d8e3701SJed Brown {
22633d8e3701SJed Brown   DMRefineHookLink link,*p;
22643d8e3701SJed Brown 
22653d8e3701SJed Brown   PetscFunctionBegin;
22663d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22673d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
22683d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
22693d8e3701SJed Brown       link = *p;
22703d8e3701SJed Brown       *p = link->next;
22719566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
22723d8e3701SJed Brown       break;
22733d8e3701SJed Brown     }
22743d8e3701SJed Brown   }
22753d8e3701SJed Brown   PetscFunctionReturn(0);
22763d8e3701SJed Brown }
22773d8e3701SJed Brown 
2278c833c3b5SJed Brown /*@
2279c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2280c833c3b5SJed Brown 
2281c833c3b5SJed Brown    Collective if any hooks are
2282c833c3b5SJed Brown 
22834165533cSJose E. Roman    Input Parameters:
2284c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2285e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2286c833c3b5SJed Brown -  fine - finer DM to update
2287c833c3b5SJed Brown 
2288c833c3b5SJed Brown    Level: developer
2289c833c3b5SJed Brown 
2290db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `MatInterpolate()`
2291c833c3b5SJed Brown @*/
2292c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2293c833c3b5SJed Brown {
2294c833c3b5SJed Brown   DMRefineHookLink link;
2295c833c3b5SJed Brown 
2296c833c3b5SJed Brown   PetscFunctionBegin;
2297c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
22988865f1eaSKarl Rupp     if (link->interphook) {
22999566063dSJacob Faibussowitsch       PetscCall((*link->interphook)(coarse,interp,fine,link->ctx));
23008865f1eaSKarl Rupp     }
23014057135bSMatthew G Knepley   }
230247c6ae99SBarry Smith   PetscFunctionReturn(0);
230347c6ae99SBarry Smith }
230447c6ae99SBarry Smith 
2305eb3f98d2SBarry Smith /*@
23061f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
23071f3379b2SToby Isaac 
23081f3379b2SToby Isaac    Collective on DM
23091f3379b2SToby Isaac 
23104165533cSJose E. Roman    Input Parameters:
23111f3379b2SToby Isaac +  coarse - coarse DM
23121f3379b2SToby Isaac .  fine   - fine DM
23131f3379b2SToby Isaac .  interp - (optional) the matrix computed by DMCreateInterpolation().  Implementations may not need this, but if it
23141f3379b2SToby Isaac             is available it can avoid some recomputation.  If it is provided, MatInterpolate() will be used if
23151f3379b2SToby Isaac             the coarse DM does not have a specialized implementation.
23161f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
23171f3379b2SToby Isaac 
23184165533cSJose E. Roman    Output Parameter:
23191f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
23201f3379b2SToby Isaac 
23211f3379b2SToby Isaac    Level: developer
23221f3379b2SToby Isaac 
23231f3379b2SToby Isaac    Note: This function exists because the interpolation of a solution vector between meshes is not always a linear
23241f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
23251f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
23261f3379b2SToby Isaac    slope-limiting reconstruction.
23271f3379b2SToby Isaac 
2328db781477SPatrick Sanan .seealso `DMInterpolate()`, `DMCreateInterpolation()`
23291f3379b2SToby Isaac @*/
23301f3379b2SToby Isaac PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
23311f3379b2SToby Isaac {
23321f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM,DM,Mat,Vec,Vec) = NULL;
23331f3379b2SToby Isaac 
23341f3379b2SToby Isaac   PetscFunctionBegin;
23351f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
23361f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp,MAT_CLASSID,3);
23371f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol,VEC_CLASSID,4);
23381f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol,VEC_CLASSID,5);
23391f3379b2SToby Isaac 
23409566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse,"DMInterpolateSolution_C", &interpsol));
23411f3379b2SToby Isaac   if (interpsol) {
23429566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
23431f3379b2SToby Isaac   } else if (interp) {
23449566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
234598921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
23461f3379b2SToby Isaac   PetscFunctionReturn(0);
23471f3379b2SToby Isaac }
23481f3379b2SToby Isaac 
23491f3379b2SToby Isaac /*@
2350aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2351eb3f98d2SBarry Smith 
2352eb3f98d2SBarry Smith     Not Collective
2353eb3f98d2SBarry Smith 
2354eb3f98d2SBarry Smith     Input Parameter:
2355eb3f98d2SBarry Smith .   dm - the DM object
2356eb3f98d2SBarry Smith 
2357eb3f98d2SBarry Smith     Output Parameter:
2358eb3f98d2SBarry Smith .   level - number of refinements
2359eb3f98d2SBarry Smith 
2360eb3f98d2SBarry Smith     Level: developer
2361eb3f98d2SBarry Smith 
2362db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2363eb3f98d2SBarry Smith 
2364eb3f98d2SBarry Smith @*/
2365eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2366eb3f98d2SBarry Smith {
2367eb3f98d2SBarry Smith   PetscFunctionBegin;
2368eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2369eb3f98d2SBarry Smith   *level = dm->levelup;
2370eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2371eb3f98d2SBarry Smith }
2372eb3f98d2SBarry Smith 
2373fef3a512SBarry Smith /*@
2374aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2375fef3a512SBarry Smith 
2376fef3a512SBarry Smith     Not Collective
2377fef3a512SBarry Smith 
2378d8d19677SJose E. Roman     Input Parameters:
2379fef3a512SBarry Smith +   dm - the DM object
2380fef3a512SBarry Smith -   level - number of refinements
2381fef3a512SBarry Smith 
2382fef3a512SBarry Smith     Level: advanced
2383fef3a512SBarry Smith 
238495452b02SPatrick Sanan     Notes:
238595452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2386fef3a512SBarry Smith 
2387db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2388fef3a512SBarry Smith 
2389fef3a512SBarry Smith @*/
2390fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2391fef3a512SBarry Smith {
2392fef3a512SBarry Smith   PetscFunctionBegin;
2393fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2394fef3a512SBarry Smith   dm->levelup = level;
2395fef3a512SBarry Smith   PetscFunctionReturn(0);
2396fef3a512SBarry Smith }
2397fef3a512SBarry Smith 
2398d410b0cfSMatthew G. Knepley /*@
2399d410b0cfSMatthew G. Knepley   DMExtrude - Extrude a DM object from a surface
2400d410b0cfSMatthew G. Knepley 
2401d410b0cfSMatthew G. Knepley   Collective on dm
2402d410b0cfSMatthew G. Knepley 
2403f1a722f8SMatthew G. Knepley   Input Parameters:
2404d410b0cfSMatthew G. Knepley + dm     - the DM object
2405d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2406d410b0cfSMatthew G. Knepley 
2407d410b0cfSMatthew G. Knepley   Output Parameter:
2408d410b0cfSMatthew G. Knepley . dme - the extruded DM, or NULL
2409d410b0cfSMatthew G. Knepley 
2410d410b0cfSMatthew G. Knepley   Note: If no extrusion was done, the return value is NULL
2411d410b0cfSMatthew G. Knepley 
2412d410b0cfSMatthew G. Knepley   Level: developer
2413d410b0cfSMatthew G. Knepley 
2414db781477SPatrick Sanan .seealso `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2415d410b0cfSMatthew G. Knepley @*/
2416d410b0cfSMatthew G. Knepley PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2417d410b0cfSMatthew G. Knepley {
2418d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2419d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24207a8be351SBarry Smith   PetscCheck(dm->ops->extrude,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "DM type %s does not implement DMExtrude", ((PetscObject) dm)->type_name);
24219566063dSJacob Faibussowitsch   PetscCall((*dm->ops->extrude)(dm, layers, dme));
2422d410b0cfSMatthew G. Knepley   if (*dme) {
2423d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
24249566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject) dm, (PetscObject) *dme));
2425d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
24269566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2427d410b0cfSMatthew G. Knepley   }
2428d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2429d410b0cfSMatthew G. Knepley }
2430d410b0cfSMatthew G. Knepley 
2431ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2432ca3d3a14SMatthew G. Knepley {
2433ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2434ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2435ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2436ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2437ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2438ca3d3a14SMatthew G. Knepley }
2439ca3d3a14SMatthew G. Knepley 
2440ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2441ca3d3a14SMatthew G. Knepley {
2442ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2443ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2444ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2445ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2446ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2447ca3d3a14SMatthew G. Knepley }
2448ca3d3a14SMatthew G. Knepley 
2449ca3d3a14SMatthew G. Knepley /*@
2450c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2451ca3d3a14SMatthew G. Knepley 
2452ca3d3a14SMatthew G. Knepley   Input Parameter:
2453ca3d3a14SMatthew G. Knepley . dm - The DM
2454ca3d3a14SMatthew G. Knepley 
2455ca3d3a14SMatthew G. Knepley   Output Parameter:
2456ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2457ca3d3a14SMatthew G. Knepley 
2458ca3d3a14SMatthew G. Knepley   Level: developer
2459ca3d3a14SMatthew G. Knepley 
2460db781477SPatrick Sanan .seealso: `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2461ca3d3a14SMatthew G. Knepley @*/
2462ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2463ca3d3a14SMatthew G. Knepley {
2464ca3d3a14SMatthew G. Knepley   Vec            tv;
2465ca3d3a14SMatthew G. Knepley 
2466ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2467ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2468534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
24699566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2470ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2471ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2472ca3d3a14SMatthew G. Knepley }
2473ca3d3a14SMatthew G. Knepley 
2474ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2475ca3d3a14SMatthew G. Knepley {
2476ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2477ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2478ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2479ca3d3a14SMatthew G. Knepley 
2480ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
24819566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
24829566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
24839566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
24849566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
24859566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
24869566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
24879566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
24889566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2489ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
24909566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2491ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2492ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2493ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
24949566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2495ca3d3a14SMatthew G. Knepley       if (!dof) continue;
24969566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
24979566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2498ca3d3a14SMatthew G. Knepley     }
2499ca3d3a14SMatthew G. Knepley   }
25009566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
25019566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
25029566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2503ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2504ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
25059566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2506ca3d3a14SMatthew G. Knepley       if (dof) {
2507ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2508ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2509ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2510ca3d3a14SMatthew G. Knepley 
2511ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
25129566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
25139566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva));
25149566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2515ca3d3a14SMatthew G. Knepley       }
2516ca3d3a14SMatthew G. Knepley     }
2517ca3d3a14SMatthew G. Knepley   }
25189566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
2519ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2520ca3d3a14SMatthew G. Knepley }
2521ca3d3a14SMatthew G. Knepley 
2522ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2523ca3d3a14SMatthew G. Knepley {
2524ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2525ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2526ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2527ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2528ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2529ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2530ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
25319566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
2532ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2533ca3d3a14SMatthew G. Knepley }
2534ca3d3a14SMatthew G. Knepley 
2535bb9467b5SJed Brown /*@C
2536baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2537baf369e7SPeter Brune 
2538baf369e7SPeter Brune    Logically Collective
2539baf369e7SPeter Brune 
25404165533cSJose E. Roman    Input Parameters:
2541baf369e7SPeter Brune +  dm - the DM
2542baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2543baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
25440298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2545baf369e7SPeter Brune 
2546baf369e7SPeter Brune    Calling sequence for beginhook:
2547baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2548baf369e7SPeter Brune 
2549baf369e7SPeter Brune +  dm - global DM
2550baf369e7SPeter Brune .  g - global vector
2551baf369e7SPeter Brune .  mode - mode
2552baf369e7SPeter Brune .  l - local vector
2553baf369e7SPeter Brune -  ctx - optional user-defined function context
2554baf369e7SPeter Brune 
2555baf369e7SPeter Brune    Calling sequence for endhook:
2556ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2557baf369e7SPeter Brune 
2558baf369e7SPeter Brune +  global - global DM
2559baf369e7SPeter Brune -  ctx - optional user-defined function context
2560baf369e7SPeter Brune 
2561baf369e7SPeter Brune    Level: advanced
2562baf369e7SPeter Brune 
2563db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2564baf369e7SPeter Brune @*/
2565baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2566baf369e7SPeter Brune {
2567baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2568baf369e7SPeter Brune 
2569baf369e7SPeter Brune   PetscFunctionBegin;
2570baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2571baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
25729566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2573baf369e7SPeter Brune   link->beginhook = beginhook;
2574baf369e7SPeter Brune   link->endhook   = endhook;
2575baf369e7SPeter Brune   link->ctx       = ctx;
25760298fd71SBarry Smith   link->next      = NULL;
2577baf369e7SPeter Brune   *p              = link;
2578baf369e7SPeter Brune   PetscFunctionReturn(0);
2579baf369e7SPeter Brune }
2580baf369e7SPeter Brune 
25814c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
25824c274da1SToby Isaac {
25834c274da1SToby Isaac   Mat cMat;
258479769bd5SJed Brown   Vec cVec, cBias;
25854c274da1SToby Isaac   PetscSection section, cSec;
25864c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25874c274da1SToby Isaac 
25884c274da1SToby Isaac   PetscFunctionBegin;
25894c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25909566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm,&cSec,&cMat,&cBias));
25914c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
25925db9a05bSToby Isaac     PetscInt nRows;
25935db9a05bSToby Isaac 
25949566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat,&nRows,NULL));
25955db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
25969566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm,&section));
25979566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat,NULL,&cVec));
25989566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat,l,cVec));
25999566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec,1.,cBias));
26009566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec,&pStart,&pEnd));
26014c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
26029566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec,p,&dof));
26034c274da1SToby Isaac       if (dof) {
26044c274da1SToby Isaac         PetscScalar *vals;
26059566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec,cSec,p,&vals));
26069566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES));
26074c274da1SToby Isaac       }
26084c274da1SToby Isaac     }
26099566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
26104c274da1SToby Isaac   }
26114c274da1SToby Isaac   PetscFunctionReturn(0);
26124c274da1SToby Isaac }
26134c274da1SToby Isaac 
261447c6ae99SBarry Smith /*@
261501729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
261601729b5cSPatrick Sanan 
2617d083f849SBarry Smith     Neighbor-wise Collective on dm
261801729b5cSPatrick Sanan 
261901729b5cSPatrick Sanan     Input Parameters:
262001729b5cSPatrick Sanan +   dm - the DM object
262101729b5cSPatrick Sanan .   g - the global vector
262201729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
262301729b5cSPatrick Sanan -   l - the local vector
262401729b5cSPatrick Sanan 
262501729b5cSPatrick Sanan     Notes:
262601729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
262701729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
262801729b5cSPatrick Sanan 
262901729b5cSPatrick Sanan     Level: beginner
263001729b5cSPatrick Sanan 
2631db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
263201729b5cSPatrick Sanan 
263301729b5cSPatrick Sanan @*/
263401729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
263501729b5cSPatrick Sanan {
263601729b5cSPatrick Sanan   PetscFunctionBegin;
26379566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm,g,mode,l));
26389566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm,g,mode,l));
263901729b5cSPatrick Sanan   PetscFunctionReturn(0);
264001729b5cSPatrick Sanan }
264101729b5cSPatrick Sanan 
264201729b5cSPatrick Sanan /*@
264347c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
264447c6ae99SBarry Smith 
2645d083f849SBarry Smith     Neighbor-wise Collective on dm
264647c6ae99SBarry Smith 
264747c6ae99SBarry Smith     Input Parameters:
264847c6ae99SBarry Smith +   dm - the DM object
264947c6ae99SBarry Smith .   g - the global vector
265047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
265147c6ae99SBarry Smith -   l - the local vector
265247c6ae99SBarry Smith 
265301729b5cSPatrick Sanan     Level: intermediate
265447c6ae99SBarry Smith 
2655db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
265647c6ae99SBarry Smith 
265747c6ae99SBarry Smith @*/
26587087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
265947c6ae99SBarry Smith {
26607128ae9fSMatthew G Knepley   PetscSF                 sf;
2661baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
266247c6ae99SBarry Smith 
266347c6ae99SBarry Smith   PetscFunctionBegin;
2664171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2665baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
26668865f1eaSKarl Rupp     if (link->beginhook) {
26679566063dSJacob Faibussowitsch       PetscCall((*link->beginhook)(dm,g,mode,l,link->ctx));
26688865f1eaSKarl Rupp     }
2669baf369e7SPeter Brune   }
26709566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
26717128ae9fSMatthew G Knepley   if (sf) {
2672ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2673ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2674d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
26757128ae9fSMatthew G Knepley 
26767a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
26779566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
26789566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
26799566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
26809566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
26819566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
26827128ae9fSMatthew G Knepley   } else {
26837a8be351SBarry Smith     PetscCheck(dm->ops->globaltolocalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
26849566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
26857128ae9fSMatthew G Knepley   }
268647c6ae99SBarry Smith   PetscFunctionReturn(0);
268747c6ae99SBarry Smith }
268847c6ae99SBarry Smith 
268947c6ae99SBarry Smith /*@
269047c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
269147c6ae99SBarry Smith 
2692d083f849SBarry Smith     Neighbor-wise Collective on dm
269347c6ae99SBarry Smith 
269447c6ae99SBarry Smith     Input Parameters:
269547c6ae99SBarry Smith +   dm - the DM object
269647c6ae99SBarry Smith .   g - the global vector
269747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
269847c6ae99SBarry Smith -   l - the local vector
269947c6ae99SBarry Smith 
270001729b5cSPatrick Sanan     Level: intermediate
270147c6ae99SBarry Smith 
2702db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
270347c6ae99SBarry Smith 
270447c6ae99SBarry Smith @*/
27057087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
270647c6ae99SBarry Smith {
27077128ae9fSMatthew G Knepley   PetscSF                 sf;
2708ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2709ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2710ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2711baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2712d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
271347c6ae99SBarry Smith 
271447c6ae99SBarry Smith   PetscFunctionBegin;
2715171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27169566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
27179566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
27187128ae9fSMatthew G Knepley   if (sf) {
27197a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
27207128ae9fSMatthew G Knepley 
27219566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
27229566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
27239566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray,MPI_REPLACE));
27249566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
27259566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
27269566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
27277128ae9fSMatthew G Knepley   } else {
27287a8be351SBarry Smith     PetscCheck(dm->ops->globaltolocalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
27299566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
27307128ae9fSMatthew G Knepley   }
27319566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL));
2732baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
27339566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm,g,mode,l,link->ctx));
2734baf369e7SPeter Brune   }
273547c6ae99SBarry Smith   PetscFunctionReturn(0);
273647c6ae99SBarry Smith }
273747c6ae99SBarry Smith 
2738d4d07f1eSToby Isaac /*@C
2739d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2740d4d07f1eSToby Isaac 
2741d4d07f1eSToby Isaac    Logically Collective
2742d4d07f1eSToby Isaac 
27434165533cSJose E. Roman    Input Parameters:
2744d4d07f1eSToby Isaac +  dm - the DM
2745d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2746d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2747d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2748d4d07f1eSToby Isaac 
2749d4d07f1eSToby Isaac    Calling sequence for beginhook:
2750d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2751d4d07f1eSToby Isaac 
2752d4d07f1eSToby Isaac +  dm - global DM
2753d4d07f1eSToby Isaac .  l - local vector
2754d4d07f1eSToby Isaac .  mode - mode
2755d4d07f1eSToby Isaac .  g - global vector
2756d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2757d4d07f1eSToby Isaac 
2758d4d07f1eSToby Isaac    Calling sequence for endhook:
2759d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2760d4d07f1eSToby Isaac 
2761d4d07f1eSToby Isaac +  global - global DM
2762d4d07f1eSToby Isaac .  l - local vector
2763d4d07f1eSToby Isaac .  mode - mode
2764d4d07f1eSToby Isaac .  g - global vector
2765d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2766d4d07f1eSToby Isaac 
2767d4d07f1eSToby Isaac    Level: advanced
2768d4d07f1eSToby Isaac 
2769db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2770d4d07f1eSToby Isaac @*/
2771d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2772d4d07f1eSToby Isaac {
2773d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2774d4d07f1eSToby Isaac 
2775d4d07f1eSToby Isaac   PetscFunctionBegin;
2776d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2777d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
27789566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2779d4d07f1eSToby Isaac   link->beginhook = beginhook;
2780d4d07f1eSToby Isaac   link->endhook   = endhook;
2781d4d07f1eSToby Isaac   link->ctx       = ctx;
2782d4d07f1eSToby Isaac   link->next      = NULL;
2783d4d07f1eSToby Isaac   *p              = link;
2784d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2785d4d07f1eSToby Isaac }
2786d4d07f1eSToby Isaac 
27874c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
27884c274da1SToby Isaac {
27894c274da1SToby Isaac   Mat cMat;
27904c274da1SToby Isaac   Vec cVec;
27914c274da1SToby Isaac   PetscSection section, cSec;
27924c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
27934c274da1SToby Isaac 
27944c274da1SToby Isaac   PetscFunctionBegin;
27954c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27969566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm,&cSec,&cMat,NULL));
27974c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
27985db9a05bSToby Isaac     PetscInt nRows;
27995db9a05bSToby Isaac 
28009566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat,&nRows,NULL));
28015db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
28029566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm,&section));
28039566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat,NULL,&cVec));
28049566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec,&pStart,&pEnd));
28054c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
28069566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec,p,&dof));
28074c274da1SToby Isaac       if (dof) {
28084c274da1SToby Isaac         PetscInt d;
28094c274da1SToby Isaac         PetscScalar *vals;
28109566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l,section,p,&vals));
28119566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec,cSec,p,vals,mode));
28124c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
28134c274da1SToby Isaac          * we just extracted */
28144c274da1SToby Isaac         for (d = 0; d < dof; d++) {
28154c274da1SToby Isaac           vals[d] = 0.;
28164c274da1SToby Isaac         }
28174c274da1SToby Isaac       }
28184c274da1SToby Isaac     }
28199566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat,cVec,l,l));
28209566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
28214c274da1SToby Isaac   }
28224c274da1SToby Isaac   PetscFunctionReturn(0);
28234c274da1SToby Isaac }
282401729b5cSPatrick Sanan /*@
282501729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
282601729b5cSPatrick Sanan 
2827d083f849SBarry Smith     Neighbor-wise Collective on dm
282801729b5cSPatrick Sanan 
282901729b5cSPatrick Sanan     Input Parameters:
283001729b5cSPatrick Sanan +   dm - the DM object
283101729b5cSPatrick Sanan .   l - the local vector
283201729b5cSPatrick 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.
283301729b5cSPatrick Sanan -   g - the global vector
283401729b5cSPatrick Sanan 
283501729b5cSPatrick Sanan     Notes:
283601729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
283701729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
283801729b5cSPatrick Sanan 
283901729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
284001729b5cSPatrick 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.
284101729b5cSPatrick Sanan 
284201729b5cSPatrick Sanan     Level: beginner
284301729b5cSPatrick Sanan 
2844db781477SPatrick Sanan .seealso `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
284501729b5cSPatrick Sanan 
284601729b5cSPatrick Sanan @*/
284701729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
284801729b5cSPatrick Sanan {
284901729b5cSPatrick Sanan   PetscFunctionBegin;
28509566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm,l,mode,g));
28519566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm,l,mode,g));
285201729b5cSPatrick Sanan   PetscFunctionReturn(0);
285301729b5cSPatrick Sanan }
28544c274da1SToby Isaac 
285547c6ae99SBarry Smith /*@
285601729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
28579a42bb27SBarry Smith 
2858d083f849SBarry Smith     Neighbor-wise Collective on dm
28599a42bb27SBarry Smith 
28609a42bb27SBarry Smith     Input Parameters:
28619a42bb27SBarry Smith +   dm - the DM object
2862f6813fd5SJed Brown .   l - the local vector
28631eb28f2eSBarry 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.
28641eb28f2eSBarry Smith -   g - the global vector
28659a42bb27SBarry Smith 
286695452b02SPatrick Sanan     Notes:
286795452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
286884330215SMatthew 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.
28699a42bb27SBarry Smith 
287001729b5cSPatrick Sanan     Level: intermediate
28719a42bb27SBarry Smith 
2872db781477SPatrick Sanan .seealso `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
28739a42bb27SBarry Smith 
28749a42bb27SBarry Smith @*/
28757087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
28769a42bb27SBarry Smith {
28777128ae9fSMatthew G Knepley   PetscSF                 sf;
287884330215SMatthew G. Knepley   PetscSection            s, gs;
2879d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2880ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2881ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2882ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2883fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
2884d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
28859a42bb27SBarry Smith 
28869a42bb27SBarry Smith   PetscFunctionBegin;
2887171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2888d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2889d4d07f1eSToby Isaac     if (link->beginhook) {
28909566063dSJacob Faibussowitsch       PetscCall((*link->beginhook)(dm,l,mode,g,link->ctx));
2891d4d07f1eSToby Isaac     }
2892d4d07f1eSToby Isaac   }
28939566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL));
28949566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28959566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
28967128ae9fSMatthew G Knepley   switch (mode) {
28977128ae9fSMatthew G Knepley   case INSERT_VALUES:
28987128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2899304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
290084330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
29017128ae9fSMatthew G Knepley   case ADD_VALUES:
29027128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2903304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
290484330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
29057128ae9fSMatthew G Knepley   default:
290663a3b9bcSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
29077128ae9fSMatthew G Knepley   }
2908ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
29099566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
2910ca3d3a14SMatthew G. Knepley     if (transform) {
29119566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
29129566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
29139566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
29149566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
2915fa88e482SJed Brown     } else if (isInsert) {
29169566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
2917fa88e482SJed Brown     } else {
29189566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
2919fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2920ca3d3a14SMatthew G. Knepley     }
2921fa88e482SJed Brown     if (s && isInsert) {
29229566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
2923fa88e482SJed Brown     } else {
29249566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
2925fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2926fa88e482SJed Brown     }
2927ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
29289566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
292984330215SMatthew G. Knepley     } else if (s && isInsert) {
293084330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
293184330215SMatthew G. Knepley 
29329566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
29339566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
29349566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
293584330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2936b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
293784330215SMatthew G. Knepley 
29389566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
29399566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
29409566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
29419566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
29429566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
29439566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
2944b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
294503442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
29467a8be351SBarry 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);
2947b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2948b3b16f48SMatthew G. Knepley         if (!gcdof) {
294984330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2950b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2951b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
295284330215SMatthew G. Knepley           const PetscInt *cdofs;
295384330215SMatthew G. Knepley           PetscInt        cind = 0;
295484330215SMatthew G. Knepley 
29559566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
2956b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
295784330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2958b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
295984330215SMatthew G. Knepley           }
29607a8be351SBarry 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);
296184330215SMatthew G. Knepley       }
2962ca3d3a14SMatthew G. Knepley     }
2963fa88e482SJed Brown     if (g_inplace) {
29649566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
2965fa88e482SJed Brown     } else {
29669566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
2967fa88e482SJed Brown     }
2968ca3d3a14SMatthew G. Knepley     if (transform) {
29699566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
29709566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
2971fa88e482SJed Brown     } else if (l_inplace) {
29729566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
2973ca3d3a14SMatthew G. Knepley     } else {
29749566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
2975ca3d3a14SMatthew G. Knepley     }
29767128ae9fSMatthew G Knepley   } else {
29777a8be351SBarry Smith     PetscCheck(dm->ops->localtoglobalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
29789566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g));
29797128ae9fSMatthew G Knepley   }
29809a42bb27SBarry Smith   PetscFunctionReturn(0);
29819a42bb27SBarry Smith }
29829a42bb27SBarry Smith 
29839a42bb27SBarry Smith /*@
29849a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
298547c6ae99SBarry Smith 
2986d083f849SBarry Smith     Neighbor-wise Collective on dm
298747c6ae99SBarry Smith 
298847c6ae99SBarry Smith     Input Parameters:
298947c6ae99SBarry Smith +   dm - the DM object
2990f6813fd5SJed Brown .   l - the local vector
299147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2992f6813fd5SJed Brown -   g - the global vector
299347c6ae99SBarry Smith 
299401729b5cSPatrick Sanan     Level: intermediate
299547c6ae99SBarry Smith 
2996db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalEnd()`
299747c6ae99SBarry Smith 
299847c6ae99SBarry Smith @*/
29997087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
300047c6ae99SBarry Smith {
30017128ae9fSMatthew G Knepley   PetscSF                 sf;
300284330215SMatthew G. Knepley   PetscSection            s;
3003d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3004ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
300547c6ae99SBarry Smith 
300647c6ae99SBarry Smith   PetscFunctionBegin;
3007171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30089566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30099566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30107128ae9fSMatthew G Knepley   switch (mode) {
30117128ae9fSMatthew G Knepley   case INSERT_VALUES:
30127128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
301384330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
30147128ae9fSMatthew G Knepley   case ADD_VALUES:
30157128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
301684330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
30177128ae9fSMatthew G Knepley   default:
301863a3b9bcSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30197128ae9fSMatthew G Knepley   }
302084330215SMatthew G. Knepley   if (sf && !isInsert) {
3021ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3022ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3023ca3d3a14SMatthew G. Knepley     Vec                tmpl;
302484330215SMatthew G. Knepley 
30259566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3026ca3d3a14SMatthew G. Knepley     if (transform) {
30279566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30289566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3029ca3d3a14SMatthew G. Knepley     } else {
30309566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3031ca3d3a14SMatthew G. Knepley     }
30329566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
30339566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3034ca3d3a14SMatthew G. Knepley     if (transform) {
30359566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
30369566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3037ca3d3a14SMatthew G. Knepley     } else {
30389566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3039ca3d3a14SMatthew G. Knepley     }
30409566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
304184330215SMatthew G. Knepley   } else if (s && isInsert) {
30427128ae9fSMatthew G Knepley   } else {
30437a8be351SBarry Smith     PetscCheck(dm->ops->localtoglobalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
30449566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g));
30457128ae9fSMatthew G Knepley   }
3046d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
30479566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm,g,mode,l,link->ctx));
3048d4d07f1eSToby Isaac   }
304947c6ae99SBarry Smith   PetscFunctionReturn(0);
305047c6ae99SBarry Smith }
305147c6ae99SBarry Smith 
3052f089877aSRichard Tran Mills /*@
3053bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
3054bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3055d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
3056f089877aSRichard Tran Mills 
3057d083f849SBarry Smith    Neighbor-wise Collective on dm
3058f089877aSRichard Tran Mills 
3059f089877aSRichard Tran Mills    Input Parameters:
3060f089877aSRichard Tran Mills +  dm - the DM object
3061bc0a1609SRichard Tran Mills .  g - the original local vector
3062bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3063f089877aSRichard Tran Mills 
3064bc0a1609SRichard Tran Mills    Output Parameter:
3065bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3066f089877aSRichard Tran Mills 
3067f089877aSRichard Tran Mills    Level: intermediate
3068f089877aSRichard Tran Mills 
3069bc0a1609SRichard Tran Mills    Notes:
3070bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3071bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3072bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3073bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3074bc0a1609SRichard Tran Mills 
3075db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalEnd()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3076f089877aSRichard Tran Mills 
3077f089877aSRichard Tran Mills @*/
3078f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
3079f089877aSRichard Tran Mills {
3080f089877aSRichard Tran Mills   PetscFunctionBegin;
3081f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30827a8be351SBarry Smith   PetscCheck(dm->ops->localtolocalbegin,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
30839566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
3084f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3085f089877aSRichard Tran Mills }
3086f089877aSRichard Tran Mills 
3087f089877aSRichard Tran Mills /*@
3088bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
3089bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3090d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
3091f089877aSRichard Tran Mills 
3092d083f849SBarry Smith    Neighbor-wise Collective on dm
3093f089877aSRichard Tran Mills 
3094f089877aSRichard Tran Mills    Input Parameters:
3095bc0a1609SRichard Tran Mills +  da - the DM object
3096bc0a1609SRichard Tran Mills .  g - the original local vector
3097bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3098f089877aSRichard Tran Mills 
3099bc0a1609SRichard Tran Mills    Output Parameter:
3100bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3101f089877aSRichard Tran Mills 
3102f089877aSRichard Tran Mills    Level: intermediate
3103f089877aSRichard Tran Mills 
3104bc0a1609SRichard Tran Mills    Notes:
3105bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3106bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3107bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3108bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3109bc0a1609SRichard Tran Mills 
3110db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalBegin()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3111f089877aSRichard Tran Mills 
3112f089877aSRichard Tran Mills @*/
3113f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
3114f089877aSRichard Tran Mills {
3115f089877aSRichard Tran Mills   PetscFunctionBegin;
3116f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31177a8be351SBarry Smith   PetscCheck(dm->ops->localtolocalend,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
31189566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
3119f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3120f089877aSRichard Tran Mills }
3121f089877aSRichard Tran Mills 
312247c6ae99SBarry Smith /*@
312347c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
312447c6ae99SBarry Smith 
3125d083f849SBarry Smith     Collective on dm
312647c6ae99SBarry Smith 
3127d8d19677SJose E. Roman     Input Parameters:
312847c6ae99SBarry Smith +   dm - the DM object
312991d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
313047c6ae99SBarry Smith 
313147c6ae99SBarry Smith     Output Parameter:
313247c6ae99SBarry Smith .   dmc - the coarsened DM
313347c6ae99SBarry Smith 
313447c6ae99SBarry Smith     Level: developer
313547c6ae99SBarry Smith 
3136db781477SPatrick Sanan .seealso `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
313747c6ae99SBarry Smith 
313847c6ae99SBarry Smith @*/
31397087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
314047c6ae99SBarry Smith {
3141b17ce1afSJed Brown   DMCoarsenHookLink link;
314247c6ae99SBarry Smith 
314347c6ae99SBarry Smith   PetscFunctionBegin;
3144171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31457a8be351SBarry Smith   PetscCheck(dm->ops->coarsen,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
31469566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen,dm,0,0,0));
31479566063dSJacob Faibussowitsch   PetscCall((*dm->ops->coarsen)(dm, comm, dmc));
3148b9d85ea2SLisandro Dalcin   if (*dmc) {
3149a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
31509566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm,*dmc));
315143842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
31529566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc));
3153644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
31540598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3155656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
31569566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc,dm->mattype));
3157b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
31589566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm,*dmc,link->ctx));
3159b17ce1afSJed Brown     }
3160b9d85ea2SLisandro Dalcin   }
31619566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen,dm,0,0,0));
31627a8be351SBarry Smith   PetscCheck(*dmc,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3163b17ce1afSJed Brown   PetscFunctionReturn(0);
3164b17ce1afSJed Brown }
3165b17ce1afSJed Brown 
3166bb9467b5SJed Brown /*@C
3167b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3168b17ce1afSJed Brown 
3169b17ce1afSJed Brown    Logically Collective
3170b17ce1afSJed Brown 
31714165533cSJose E. Roman    Input Parameters:
3172b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3173b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3174b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
31750298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3176b17ce1afSJed Brown 
3177b17ce1afSJed Brown    Calling sequence of coarsenhook:
3178b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3179b17ce1afSJed Brown 
3180b17ce1afSJed Brown +  fine - fine level DM
3181b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3182b17ce1afSJed Brown -  ctx - optional user-defined function context
3183b17ce1afSJed Brown 
3184b17ce1afSJed Brown    Calling sequence for restricthook:
3185c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3186b17ce1afSJed Brown 
3187b17ce1afSJed Brown +  fine - fine level DM
3188b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3189c833c3b5SJed Brown .  rscale - scaling vector for restriction
3190c833c3b5SJed Brown .  inject - matrix restricting by injection
3191b17ce1afSJed Brown .  coarse - coarse level DM to update
3192b17ce1afSJed Brown -  ctx - optional user-defined function context
3193b17ce1afSJed Brown 
3194b17ce1afSJed Brown    Level: advanced
3195b17ce1afSJed Brown 
3196b17ce1afSJed Brown    Notes:
3197b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3198b17ce1afSJed Brown 
3199b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3200b17ce1afSJed Brown 
3201b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3202b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3203b17ce1afSJed Brown 
3204bb9467b5SJed Brown    This function is currently not available from Fortran.
3205bb9467b5SJed Brown 
3206db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3207b17ce1afSJed Brown @*/
3208b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3209b17ce1afSJed Brown {
3210b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3211b17ce1afSJed Brown 
3212b17ce1afSJed Brown   PetscFunctionBegin;
3213b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
32141e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
32151e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
32161e3d8eccSJed Brown   }
32179566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3218b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3219b17ce1afSJed Brown   link->restricthook = restricthook;
3220b17ce1afSJed Brown   link->ctx          = ctx;
32210298fd71SBarry Smith   link->next         = NULL;
3222b17ce1afSJed Brown   *p                 = link;
3223b17ce1afSJed Brown   PetscFunctionReturn(0);
3224b17ce1afSJed Brown }
3225b17ce1afSJed Brown 
3226dc822a44SJed Brown /*@C
3227dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3228dc822a44SJed Brown 
3229dc822a44SJed Brown    Logically Collective
3230dc822a44SJed Brown 
32314165533cSJose E. Roman    Input Parameters:
3232dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3233dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3234dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3235dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3236dc822a44SJed Brown 
3237dc822a44SJed Brown    Level: advanced
3238dc822a44SJed Brown 
3239dc822a44SJed Brown    Notes:
3240dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3241dc822a44SJed Brown 
3242dc822a44SJed Brown    This function is currently not available from Fortran.
3243dc822a44SJed Brown 
3244db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3245dc822a44SJed Brown @*/
3246dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3247dc822a44SJed Brown {
3248dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3249dc822a44SJed Brown 
3250dc822a44SJed Brown   PetscFunctionBegin;
3251dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3252dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3253dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3254dc822a44SJed Brown       link = *p;
3255dc822a44SJed Brown       *p = link->next;
32569566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3257dc822a44SJed Brown       break;
3258dc822a44SJed Brown     }
3259dc822a44SJed Brown   }
3260dc822a44SJed Brown   PetscFunctionReturn(0);
3261dc822a44SJed Brown }
3262dc822a44SJed Brown 
3263b17ce1afSJed Brown /*@
3264b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3265b17ce1afSJed Brown 
3266b17ce1afSJed Brown    Collective if any hooks are
3267b17ce1afSJed Brown 
32684165533cSJose E. Roman    Input Parameters:
3269b17ce1afSJed Brown +  fine - finer DM to use as a base
3270b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3271e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3272b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3273e91eccc2SStefano Zampini -  coarse - coarser DM to update
3274b17ce1afSJed Brown 
3275b17ce1afSJed Brown    Level: developer
3276b17ce1afSJed Brown 
3277db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
3278b17ce1afSJed Brown @*/
3279b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3280b17ce1afSJed Brown {
3281b17ce1afSJed Brown   DMCoarsenHookLink link;
3282b17ce1afSJed Brown 
3283b17ce1afSJed Brown   PetscFunctionBegin;
3284b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
32858865f1eaSKarl Rupp     if (link->restricthook) {
32869566063dSJacob Faibussowitsch       PetscCall((*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx));
32878865f1eaSKarl Rupp     }
3288b17ce1afSJed Brown   }
328947c6ae99SBarry Smith   PetscFunctionReturn(0);
329047c6ae99SBarry Smith }
329147c6ae99SBarry Smith 
3292bb9467b5SJed Brown /*@C
3293be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
32945dbd56e3SPeter Brune 
3295d083f849SBarry Smith    Logically Collective on global
32965dbd56e3SPeter Brune 
32974165533cSJose E. Roman    Input Parameters:
32985dbd56e3SPeter Brune +  global - global DM
3299ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
33005dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
33010298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
33025dbd56e3SPeter Brune 
3303ec4806b8SPeter Brune    Calling sequence for ddhook:
3304ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3305ec4806b8SPeter Brune 
3306ec4806b8SPeter Brune +  global - global DM
3307ec4806b8SPeter Brune .  block  - block DM
3308ec4806b8SPeter Brune -  ctx - optional user-defined function context
3309ec4806b8SPeter Brune 
33105dbd56e3SPeter Brune    Calling sequence for restricthook:
3311ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
33125dbd56e3SPeter Brune 
33135dbd56e3SPeter Brune +  global - global DM
33145dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
33155dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3316ec4806b8SPeter Brune .  block  - block DM
33175dbd56e3SPeter Brune -  ctx - optional user-defined function context
33185dbd56e3SPeter Brune 
33195dbd56e3SPeter Brune    Level: advanced
33205dbd56e3SPeter Brune 
33215dbd56e3SPeter Brune    Notes:
3322ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
33235dbd56e3SPeter Brune 
33245dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
33255dbd56e3SPeter Brune 
33265dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3327ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
33285dbd56e3SPeter Brune 
3329bb9467b5SJed Brown    This function is currently not available from Fortran.
3330bb9467b5SJed Brown 
3331db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
33325dbd56e3SPeter Brune @*/
3333be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
33345dbd56e3SPeter Brune {
3335be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
33365dbd56e3SPeter Brune 
33375dbd56e3SPeter Brune   PetscFunctionBegin;
33385dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3339b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3340b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3341b3a6b972SJed Brown   }
33429566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
33435dbd56e3SPeter Brune   link->restricthook = restricthook;
3344be081cd6SPeter Brune   link->ddhook       = ddhook;
33455dbd56e3SPeter Brune   link->ctx          = ctx;
33460298fd71SBarry Smith   link->next         = NULL;
33475dbd56e3SPeter Brune   *p                 = link;
33485dbd56e3SPeter Brune   PetscFunctionReturn(0);
33495dbd56e3SPeter Brune }
33505dbd56e3SPeter Brune 
3351b3a6b972SJed Brown /*@C
3352b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3353b3a6b972SJed Brown 
3354b3a6b972SJed Brown    Logically Collective
3355b3a6b972SJed Brown 
33564165533cSJose E. Roman    Input Parameters:
3357b3a6b972SJed Brown +  global - global DM
3358b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3359b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3360b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3361b3a6b972SJed Brown 
3362b3a6b972SJed Brown    Level: advanced
3363b3a6b972SJed Brown 
3364b3a6b972SJed Brown    Notes:
3365b3a6b972SJed Brown 
3366b3a6b972SJed Brown    This function is currently not available from Fortran.
3367b3a6b972SJed Brown 
3368db781477SPatrick Sanan .seealso: `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3369b3a6b972SJed Brown @*/
3370b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3371b3a6b972SJed Brown {
3372b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3373b3a6b972SJed Brown 
3374b3a6b972SJed Brown   PetscFunctionBegin;
3375b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3376b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3377b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3378b3a6b972SJed Brown       link = *p;
3379b3a6b972SJed Brown       *p = link->next;
33809566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3381b3a6b972SJed Brown       break;
3382b3a6b972SJed Brown     }
3383b3a6b972SJed Brown   }
3384b3a6b972SJed Brown   PetscFunctionReturn(0);
3385b3a6b972SJed Brown }
3386b3a6b972SJed Brown 
33875dbd56e3SPeter Brune /*@
3388be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
33895dbd56e3SPeter Brune 
33905dbd56e3SPeter Brune    Collective if any hooks are
33915dbd56e3SPeter Brune 
33924165533cSJose E. Roman    Input Parameters:
33935dbd56e3SPeter Brune +  fine - finer DM to use as a base
3394be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3395be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
33965dbd56e3SPeter Brune -  coarse - coarer DM to update
33975dbd56e3SPeter Brune 
33985dbd56e3SPeter Brune    Level: developer
33995dbd56e3SPeter Brune 
3400db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
34015dbd56e3SPeter Brune @*/
3402be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
34035dbd56e3SPeter Brune {
3404be081cd6SPeter Brune   DMSubDomainHookLink link;
34055dbd56e3SPeter Brune 
34065dbd56e3SPeter Brune   PetscFunctionBegin;
3407be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
34088865f1eaSKarl Rupp     if (link->restricthook) {
34099566063dSJacob Faibussowitsch       PetscCall((*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx));
34108865f1eaSKarl Rupp     }
34115dbd56e3SPeter Brune   }
34125dbd56e3SPeter Brune   PetscFunctionReturn(0);
34135dbd56e3SPeter Brune }
34145dbd56e3SPeter Brune 
34155fe1f584SPeter Brune /*@
34166a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
34175fe1f584SPeter Brune 
34185fe1f584SPeter Brune     Not Collective
34195fe1f584SPeter Brune 
34205fe1f584SPeter Brune     Input Parameter:
34215fe1f584SPeter Brune .   dm - the DM object
34225fe1f584SPeter Brune 
34235fe1f584SPeter Brune     Output Parameter:
34246a7d9d85SPeter Brune .   level - number of coarsenings
34255fe1f584SPeter Brune 
34265fe1f584SPeter Brune     Level: developer
34275fe1f584SPeter Brune 
3428db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
34295fe1f584SPeter Brune 
34305fe1f584SPeter Brune @*/
34315fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
34325fe1f584SPeter Brune {
34335fe1f584SPeter Brune   PetscFunctionBegin;
34345fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3435b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
34365fe1f584SPeter Brune   *level = dm->leveldown;
34375fe1f584SPeter Brune   PetscFunctionReturn(0);
34385fe1f584SPeter Brune }
34395fe1f584SPeter Brune 
34409a64c4a8SMatthew G. Knepley /*@
34419a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
34429a64c4a8SMatthew G. Knepley 
34439a64c4a8SMatthew G. Knepley     Not Collective
34449a64c4a8SMatthew G. Knepley 
34459a64c4a8SMatthew G. Knepley     Input Parameters:
34469a64c4a8SMatthew G. Knepley +   dm - the DM object
34479a64c4a8SMatthew G. Knepley -   level - number of coarsenings
34489a64c4a8SMatthew G. Knepley 
34499a64c4a8SMatthew G. Knepley     Level: developer
34509a64c4a8SMatthew G. Knepley 
3451db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
34529a64c4a8SMatthew G. Knepley @*/
34539a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
34549a64c4a8SMatthew G. Knepley {
34559a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
34569a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34579a64c4a8SMatthew G. Knepley   dm->leveldown = level;
34589a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
34599a64c4a8SMatthew G. Knepley }
34609a64c4a8SMatthew G. Knepley 
346147c6ae99SBarry Smith /*@C
346247c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
346347c6ae99SBarry Smith 
3464d083f849SBarry Smith     Collective on dm
346547c6ae99SBarry Smith 
3466d8d19677SJose E. Roman     Input Parameters:
346747c6ae99SBarry Smith +   dm - the DM object
346847c6ae99SBarry Smith -   nlevels - the number of levels of refinement
346947c6ae99SBarry Smith 
347047c6ae99SBarry Smith     Output Parameter:
347147c6ae99SBarry Smith .   dmf - the refined DM hierarchy
347247c6ae99SBarry Smith 
347347c6ae99SBarry Smith     Level: developer
347447c6ae99SBarry Smith 
3475db781477SPatrick Sanan .seealso `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
347647c6ae99SBarry Smith 
347747c6ae99SBarry Smith @*/
34787087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
347947c6ae99SBarry Smith {
348047c6ae99SBarry Smith   PetscFunctionBegin;
3481171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34827a8be351SBarry Smith   PetscCheck(nlevels >= 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
348347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3484b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
348547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
34869566063dSJacob Faibussowitsch     PetscCall((*dm->ops->refinehierarchy)(dm,nlevels,dmf));
348747c6ae99SBarry Smith   } else if (dm->ops->refine) {
348847c6ae99SBarry Smith     PetscInt i;
348947c6ae99SBarry Smith 
34909566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]));
349147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
34929566063dSJacob Faibussowitsch       PetscCall(DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]));
349347c6ae99SBarry Smith     }
3494ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
349547c6ae99SBarry Smith   PetscFunctionReturn(0);
349647c6ae99SBarry Smith }
349747c6ae99SBarry Smith 
349847c6ae99SBarry Smith /*@C
349947c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
350047c6ae99SBarry Smith 
3501d083f849SBarry Smith     Collective on dm
350247c6ae99SBarry Smith 
3503d8d19677SJose E. Roman     Input Parameters:
350447c6ae99SBarry Smith +   dm - the DM object
350547c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
350647c6ae99SBarry Smith 
350747c6ae99SBarry Smith     Output Parameter:
350847c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
350947c6ae99SBarry Smith 
351047c6ae99SBarry Smith     Level: developer
351147c6ae99SBarry Smith 
3512db781477SPatrick Sanan .seealso `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
351347c6ae99SBarry Smith 
351447c6ae99SBarry Smith @*/
35157087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
351647c6ae99SBarry Smith {
351747c6ae99SBarry Smith   PetscFunctionBegin;
3518171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35197a8be351SBarry Smith   PetscCheck(nlevels >= 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
352047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
352147c6ae99SBarry Smith   PetscValidPointer(dmc,3);
352247c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
35239566063dSJacob Faibussowitsch     PetscCall((*dm->ops->coarsenhierarchy)(dm, nlevels, dmc));
352447c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
352547c6ae99SBarry Smith     PetscInt i;
352647c6ae99SBarry Smith 
35279566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]));
352847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
35299566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]));
353047c6ae99SBarry Smith     }
3531ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
353247c6ae99SBarry Smith   PetscFunctionReturn(0);
353347c6ae99SBarry Smith }
353447c6ae99SBarry Smith 
35351a266240SBarry Smith /*@C
35361a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
35371a266240SBarry Smith 
35381a266240SBarry Smith     Not Collective
35391a266240SBarry Smith 
35401a266240SBarry Smith     Input Parameters:
35411a266240SBarry Smith +   dm - the DM object
35421a266240SBarry Smith -   destroy - the destroy function
35431a266240SBarry Smith 
35441a266240SBarry Smith     Level: intermediate
35451a266240SBarry Smith 
3546db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
35471a266240SBarry Smith 
3548f07f9ceaSJed Brown @*/
35491a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
35501a266240SBarry Smith {
35511a266240SBarry Smith   PetscFunctionBegin;
3552171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35531a266240SBarry Smith   dm->ctxdestroy = destroy;
35541a266240SBarry Smith   PetscFunctionReturn(0);
35551a266240SBarry Smith }
35561a266240SBarry Smith 
3557b07ff414SBarry Smith /*@
35581b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
355947c6ae99SBarry Smith 
356047c6ae99SBarry Smith     Not Collective
356147c6ae99SBarry Smith 
356247c6ae99SBarry Smith     Input Parameters:
356347c6ae99SBarry Smith +   dm - the DM object
356447c6ae99SBarry Smith -   ctx - the user context
356547c6ae99SBarry Smith 
356647c6ae99SBarry Smith     Level: intermediate
356747c6ae99SBarry Smith 
3568db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
356947c6ae99SBarry Smith 
357047c6ae99SBarry Smith @*/
35711b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
357247c6ae99SBarry Smith {
357347c6ae99SBarry Smith   PetscFunctionBegin;
3574171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
357547c6ae99SBarry Smith   dm->ctx = ctx;
357647c6ae99SBarry Smith   PetscFunctionReturn(0);
357747c6ae99SBarry Smith }
357847c6ae99SBarry Smith 
357947c6ae99SBarry Smith /*@
35801b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
358147c6ae99SBarry Smith 
358247c6ae99SBarry Smith     Not Collective
358347c6ae99SBarry Smith 
358447c6ae99SBarry Smith     Input Parameter:
358547c6ae99SBarry Smith .   dm - the DM object
358647c6ae99SBarry Smith 
358747c6ae99SBarry Smith     Output Parameter:
358847c6ae99SBarry Smith .   ctx - the user context
358947c6ae99SBarry Smith 
359047c6ae99SBarry Smith     Level: intermediate
359147c6ae99SBarry Smith 
3592db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
359347c6ae99SBarry Smith 
359447c6ae99SBarry Smith @*/
35951b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
359647c6ae99SBarry Smith {
359747c6ae99SBarry Smith   PetscFunctionBegin;
3598171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35991b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
360047c6ae99SBarry Smith   PetscFunctionReturn(0);
360147c6ae99SBarry Smith }
360247c6ae99SBarry Smith 
360308da532bSDmitry Karpeev /*@C
3604df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
360508da532bSDmitry Karpeev 
3606d083f849SBarry Smith     Logically Collective on dm
360708da532bSDmitry Karpeev 
3608d8d19677SJose E. Roman     Input Parameters:
360908da532bSDmitry Karpeev +   dm - the DM object
36100298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
361108da532bSDmitry Karpeev 
361208da532bSDmitry Karpeev     Level: intermediate
361308da532bSDmitry Karpeev 
3614db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3615db781477SPatrick Sanan          `DMSetJacobian()`
361608da532bSDmitry Karpeev 
361708da532bSDmitry Karpeev @*/
361808da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
361908da532bSDmitry Karpeev {
362008da532bSDmitry Karpeev   PetscFunctionBegin;
36215a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
362208da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
362308da532bSDmitry Karpeev   PetscFunctionReturn(0);
362408da532bSDmitry Karpeev }
362508da532bSDmitry Karpeev 
362608da532bSDmitry Karpeev /*@
362708da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
362808da532bSDmitry Karpeev 
362908da532bSDmitry Karpeev     Not Collective
363008da532bSDmitry Karpeev 
363108da532bSDmitry Karpeev     Input Parameter:
363208da532bSDmitry Karpeev .   dm - the DM object to destroy
363308da532bSDmitry Karpeev 
363408da532bSDmitry Karpeev     Output Parameter:
363508da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
363608da532bSDmitry Karpeev 
363708da532bSDmitry Karpeev     Level: developer
363808da532bSDmitry Karpeev 
3639db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
364008da532bSDmitry Karpeev 
364108da532bSDmitry Karpeev @*/
364208da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
364308da532bSDmitry Karpeev {
364408da532bSDmitry Karpeev   PetscFunctionBegin;
36455a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3646534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
364708da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
364808da532bSDmitry Karpeev   PetscFunctionReturn(0);
364908da532bSDmitry Karpeev }
365008da532bSDmitry Karpeev 
365108da532bSDmitry Karpeev /*@C
365208da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
365308da532bSDmitry Karpeev 
3654d083f849SBarry Smith     Logically Collective on dm
365508da532bSDmitry Karpeev 
3656f899ff85SJose E. Roman     Input Parameter:
3657907376e6SBarry Smith .   dm - the DM object
365808da532bSDmitry Karpeev 
365908da532bSDmitry Karpeev     Output parameters:
366008da532bSDmitry Karpeev +   xl - lower bound
366108da532bSDmitry Karpeev -   xu - upper bound
366208da532bSDmitry Karpeev 
3663907376e6SBarry Smith     Level: advanced
3664907376e6SBarry Smith 
366595452b02SPatrick Sanan     Notes:
366695452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
366708da532bSDmitry Karpeev 
3668db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
366908da532bSDmitry Karpeev 
367008da532bSDmitry Karpeev @*/
367108da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
367208da532bSDmitry Karpeev {
367308da532bSDmitry Karpeev   PetscFunctionBegin;
36745a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
367508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
36765a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
36777a8be351SBarry Smith   PetscCheck(dm->ops->computevariablebounds,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
36789566063dSJacob Faibussowitsch   PetscCall((*dm->ops->computevariablebounds)(dm, xl,xu));
367908da532bSDmitry Karpeev   PetscFunctionReturn(0);
368008da532bSDmitry Karpeev }
368108da532bSDmitry Karpeev 
3682b0ae01b7SPeter Brune /*@
3683b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3684b0ae01b7SPeter Brune 
3685b0ae01b7SPeter Brune     Not Collective
3686b0ae01b7SPeter Brune 
3687b0ae01b7SPeter Brune     Input Parameter:
3688b0ae01b7SPeter Brune .   dm - the DM object
3689b0ae01b7SPeter Brune 
3690b0ae01b7SPeter Brune     Output Parameter:
3691b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3692b0ae01b7SPeter Brune 
3693b0ae01b7SPeter Brune     Level: developer
3694b0ae01b7SPeter Brune 
3695db781477SPatrick Sanan .seealso `DMCreateColoring()`
3696b0ae01b7SPeter Brune 
3697b0ae01b7SPeter Brune @*/
3698b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3699b0ae01b7SPeter Brune {
3700b0ae01b7SPeter Brune   PetscFunctionBegin;
37015a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3702534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3703b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3704b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3705b0ae01b7SPeter Brune }
3706b0ae01b7SPeter Brune 
37073ad4599aSBarry Smith /*@
37083ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
37093ad4599aSBarry Smith 
37103ad4599aSBarry Smith     Not Collective
37113ad4599aSBarry Smith 
37123ad4599aSBarry Smith     Input Parameter:
37133ad4599aSBarry Smith .   dm - the DM object
37143ad4599aSBarry Smith 
37153ad4599aSBarry Smith     Output Parameter:
37163ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
37173ad4599aSBarry Smith 
37183ad4599aSBarry Smith     Level: developer
37193ad4599aSBarry Smith 
3720db781477SPatrick Sanan .seealso `DMCreateRestriction()`
37213ad4599aSBarry Smith 
37223ad4599aSBarry Smith @*/
37233ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
37243ad4599aSBarry Smith {
37253ad4599aSBarry Smith   PetscFunctionBegin;
37265a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3727534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37283ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
37293ad4599aSBarry Smith   PetscFunctionReturn(0);
37303ad4599aSBarry Smith }
37313ad4599aSBarry Smith 
3732a7058e45SLawrence Mitchell /*@
3733a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3734a7058e45SLawrence Mitchell 
3735a7058e45SLawrence Mitchell     Not Collective
3736a7058e45SLawrence Mitchell 
3737a7058e45SLawrence Mitchell     Input Parameter:
3738a7058e45SLawrence Mitchell .   dm - the DM object
3739a7058e45SLawrence Mitchell 
3740a7058e45SLawrence Mitchell     Output Parameter:
3741a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3742a7058e45SLawrence Mitchell 
3743a7058e45SLawrence Mitchell     Level: developer
3744a7058e45SLawrence Mitchell 
3745db781477SPatrick Sanan .seealso `DMCreateInjection()`
3746a7058e45SLawrence Mitchell 
3747a7058e45SLawrence Mitchell @*/
3748a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3749a7058e45SLawrence Mitchell {
3750a7058e45SLawrence Mitchell   PetscFunctionBegin;
37515a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3752534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37535a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
37549566063dSJacob Faibussowitsch     PetscCall((*dm->ops->hascreateinjection)(dm,flg));
37555a84ad33SLisandro Dalcin   } else {
37565a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
37575a84ad33SLisandro Dalcin   }
3758a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3759a7058e45SLawrence Mitchell }
3760a7058e45SLawrence Mitchell 
37610298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3762264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3763264ace61SBarry Smith 
3764264ace61SBarry Smith /*@C
3765264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3766264ace61SBarry Smith 
3767d083f849SBarry Smith   Collective on dm
3768264ace61SBarry Smith 
3769264ace61SBarry Smith   Input Parameters:
3770264ace61SBarry Smith + dm     - The DM object
3771264ace61SBarry Smith - method - The name of the DM type
3772264ace61SBarry Smith 
3773264ace61SBarry Smith   Options Database Key:
3774264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3775264ace61SBarry Smith 
3776264ace61SBarry Smith   Notes:
3777e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3778264ace61SBarry Smith 
3779264ace61SBarry Smith   Level: intermediate
3780264ace61SBarry Smith 
3781db781477SPatrick Sanan .seealso: `DMGetType()`, `DMCreate()`
3782264ace61SBarry Smith @*/
378319fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3784264ace61SBarry Smith {
3785264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3786264ace61SBarry Smith   PetscBool      match;
3787264ace61SBarry Smith 
3788264ace61SBarry Smith   PetscFunctionBegin;
3789264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
37909566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) dm, method, &match));
3791264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3792264ace61SBarry Smith 
37939566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
37949566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList,method,&r));
37957a8be351SBarry Smith   PetscCheck(r,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3796264ace61SBarry Smith 
3797264ace61SBarry Smith   if (dm->ops->destroy) {
37989566063dSJacob Faibussowitsch     PetscCall((*dm->ops->destroy)(dm));
3799264ace61SBarry Smith   }
38009566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops,sizeof(*dm->ops)));
38019566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm,method));
38029566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
3803264ace61SBarry Smith   PetscFunctionReturn(0);
3804264ace61SBarry Smith }
3805264ace61SBarry Smith 
3806264ace61SBarry Smith /*@C
3807264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3808264ace61SBarry Smith 
3809264ace61SBarry Smith   Not Collective
3810264ace61SBarry Smith 
3811264ace61SBarry Smith   Input Parameter:
3812264ace61SBarry Smith . dm  - The DM
3813264ace61SBarry Smith 
3814264ace61SBarry Smith   Output Parameter:
3815264ace61SBarry Smith . type - The DM type name
3816264ace61SBarry Smith 
3817264ace61SBarry Smith   Level: intermediate
3818264ace61SBarry Smith 
3819db781477SPatrick Sanan .seealso: `DMSetType()`, `DMCreate()`
3820264ace61SBarry Smith @*/
382119fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3822264ace61SBarry Smith {
3823264ace61SBarry Smith   PetscFunctionBegin;
3824264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3825c959eef4SJed Brown   PetscValidPointer(type,2);
38269566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3827264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3828264ace61SBarry Smith   PetscFunctionReturn(0);
3829264ace61SBarry Smith }
3830264ace61SBarry Smith 
383167a56275SMatthew G Knepley /*@C
383267a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
383367a56275SMatthew G Knepley 
3834d083f849SBarry Smith   Collective on dm
383567a56275SMatthew G Knepley 
383667a56275SMatthew G Knepley   Input Parameters:
383767a56275SMatthew G Knepley + dm - the DM
383867a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
383967a56275SMatthew G Knepley 
384067a56275SMatthew G Knepley   Output Parameter:
384167a56275SMatthew G Knepley . M - pointer to new DM
384267a56275SMatthew G Knepley 
384367a56275SMatthew G Knepley   Notes:
384467a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
384567a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
384667a56275SMatthew G Knepley   of the input DM.
384767a56275SMatthew G Knepley 
384867a56275SMatthew G Knepley   Level: intermediate
384967a56275SMatthew G Knepley 
3850db781477SPatrick Sanan .seealso: `DMCreate()`
385167a56275SMatthew G Knepley @*/
385219fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
385367a56275SMatthew G Knepley {
385467a56275SMatthew G Knepley   DM             B;
385567a56275SMatthew G Knepley   char           convname[256];
3856c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
385767a56275SMatthew G Knepley 
385867a56275SMatthew G Knepley   PetscFunctionBegin;
385967a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
386067a56275SMatthew G Knepley   PetscValidType(dm,1);
386167a56275SMatthew G Knepley   PetscValidPointer(M,3);
38629566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype));
38639566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3864c067b6caSMatthew G. Knepley   if (sametype) {
3865c067b6caSMatthew G. Knepley     *M   = dm;
38669566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) dm));
3867c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3868c067b6caSMatthew G. Knepley   } else {
38690298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
387067a56275SMatthew G Knepley 
387167a56275SMatthew G Knepley     /*
387267a56275SMatthew G Knepley        Order of precedence:
387367a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
387467a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
387567a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
387667a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
387767a56275SMatthew G Knepley        5) Use a really basic converter.
387867a56275SMatthew G Knepley     */
387967a56275SMatthew G Knepley 
388067a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
38819566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname,"DMConvert_",sizeof(convname)));
38829566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname)));
38839566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_",sizeof(convname)));
38849566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,newtype,sizeof(convname)));
38859566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_C",sizeof(convname)));
38869566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm,convname,&conv));
388767a56275SMatthew G Knepley     if (conv) goto foundconv;
388867a56275SMatthew G Knepley 
388967a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
38909566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
38919566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
38929566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname,"DMConvert_",sizeof(convname)));
38939566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname)));
38949566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_",sizeof(convname)));
38959566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,newtype,sizeof(convname)));
38969566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_C",sizeof(convname)));
38979566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B,convname,&conv));
389867a56275SMatthew G Knepley     if (conv) {
38999566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
390067a56275SMatthew G Knepley       goto foundconv;
390167a56275SMatthew G Knepley     }
390267a56275SMatthew G Knepley 
390367a56275SMatthew G Knepley #if 0
390467a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
390567a56275SMatthew G Knepley     conv = B->ops->convertfrom;
39069566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
390767a56275SMatthew G Knepley     if (conv) goto foundconv;
390867a56275SMatthew G Knepley 
390967a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
391067a56275SMatthew G Knepley     if (dm->ops->convert) {
391167a56275SMatthew G Knepley       conv = dm->ops->convert;
391267a56275SMatthew G Knepley     }
391367a56275SMatthew G Knepley     if (conv) goto foundconv;
391467a56275SMatthew G Knepley #endif
391567a56275SMatthew G Knepley 
391667a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
391798921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
391867a56275SMatthew G Knepley 
391967a56275SMatthew G Knepley foundconv:
39209566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert,dm,0,0,0));
39219566063dSJacob Faibussowitsch     PetscCall((*conv)(dm,newtype,M));
392212fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
392390b157c4SStefano Zampini     {
392490b157c4SStefano Zampini       PetscBool             isper;
392512fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
392612fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
39279566063dSJacob Faibussowitsch       PetscCall(DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd));
39289566063dSJacob Faibussowitsch       PetscCall(DMSetPeriodicity(*M, isper, maxCell,  L,  bd));
3929c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
39309566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
39319566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype,(char**)&(*M)->vectype));
39329566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
39339566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype,(char**)&(*M)->mattype));
393412fa691eSMatthew G. Knepley     }
39359566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert,dm,0,0,0));
393667a56275SMatthew G Knepley   }
39379566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject) *M));
393867a56275SMatthew G Knepley   PetscFunctionReturn(0);
393967a56275SMatthew G Knepley }
3940264ace61SBarry Smith 
3941264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3942264ace61SBarry Smith 
3943264ace61SBarry Smith /*@C
39441c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
39451c84c290SBarry Smith 
39461c84c290SBarry Smith   Not Collective
39471c84c290SBarry Smith 
39481c84c290SBarry Smith   Input Parameters:
39491c84c290SBarry Smith + name        - The name of a new user-defined creation routine
39501c84c290SBarry Smith - create_func - The creation routine itself
39511c84c290SBarry Smith 
39521c84c290SBarry Smith   Notes:
39531c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
39541c84c290SBarry Smith 
39551c84c290SBarry Smith   Sample usage:
39561c84c290SBarry Smith .vb
3957bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
39581c84c290SBarry Smith .ve
39591c84c290SBarry Smith 
39601c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
39611c84c290SBarry Smith .vb
39621c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
39631c84c290SBarry Smith     DMSetType(DM,"my_da");
39641c84c290SBarry Smith .ve
39651c84c290SBarry Smith    or at runtime via the option
39661c84c290SBarry Smith .vb
39671c84c290SBarry Smith     -da_type my_da
39681c84c290SBarry Smith .ve
3969264ace61SBarry Smith 
3970264ace61SBarry Smith   Level: advanced
39711c84c290SBarry Smith 
3972db781477SPatrick Sanan .seealso: `DMRegisterAll()`, `DMRegisterDestroy()`
39731c84c290SBarry Smith 
3974264ace61SBarry Smith @*/
3975bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3976264ace61SBarry Smith {
3977264ace61SBarry Smith   PetscFunctionBegin;
39789566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
39799566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList,sname,function));
3980264ace61SBarry Smith   PetscFunctionReturn(0);
3981264ace61SBarry Smith }
3982264ace61SBarry Smith 
3983b859378eSBarry Smith /*@C
398455849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3985b859378eSBarry Smith 
3986d083f849SBarry Smith   Collective on viewer
3987b859378eSBarry Smith 
3988b859378eSBarry Smith   Input Parameters:
3989b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3990b859378eSBarry Smith            some related function before a call to DMLoad().
3991b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3992b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3993b859378eSBarry Smith 
3994b859378eSBarry Smith    Level: intermediate
3995b859378eSBarry Smith 
3996b859378eSBarry Smith   Notes:
399755849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
3998b859378eSBarry Smith 
3999cd7e8a5eSksagiyam   Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
4000cd7e8a5eSksagiyam   meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
4001cd7e8a5eSksagiyam   before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
4002cd7e8a5eSksagiyam 
4003b859378eSBarry Smith   Notes for advanced users:
4004b859378eSBarry Smith   Most users should not need to know the details of the binary storage
4005b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
4006b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
4007b859378eSBarry Smith   format is
4008b859378eSBarry Smith .vb
4009b859378eSBarry Smith      has not yet been determined
4010b859378eSBarry Smith .ve
4011b859378eSBarry Smith 
4012db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4013b859378eSBarry Smith @*/
4014b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
4015b859378eSBarry Smith {
40169331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
4017b859378eSBarry Smith 
4018b859378eSBarry Smith   PetscFunctionBegin;
4019b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
4020b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
40219566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
40229566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary));
40239566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5));
40249566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load,viewer,0,0,0));
40259331c7a4SMatthew G. Knepley   if (isbinary) {
40269331c7a4SMatthew G. Knepley     PetscInt classid;
40279331c7a4SMatthew G. Knepley     char     type[256];
4028b859378eSBarry Smith 
40299566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT));
40307a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID,PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
40319566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR));
40329566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
40339566063dSJacob Faibussowitsch     if (newdm->ops->load) PetscCall((*newdm->ops->load)(newdm,viewer));
40349331c7a4SMatthew G. Knepley   } else if (ishdf5) {
40359566063dSJacob Faibussowitsch     if (newdm->ops->load) PetscCall((*newdm->ops->load)(newdm,viewer));
40369331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
40379566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load,viewer,0,0,0));
4038b859378eSBarry Smith   PetscFunctionReturn(0);
4039b859378eSBarry Smith }
4040b859378eSBarry Smith 
4041b2e4378dSMatthew G. Knepley /*@
4042b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
4043b2e4378dSMatthew G. Knepley 
4044b2e4378dSMatthew G. Knepley   Not collective
4045b2e4378dSMatthew G. Knepley 
4046b2e4378dSMatthew G. Knepley   Input Parameter:
4047b2e4378dSMatthew G. Knepley . dm - the DM
4048b2e4378dSMatthew G. Knepley 
4049b2e4378dSMatthew G. Knepley   Output Parameters:
4050b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
4051b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
4052b2e4378dSMatthew G. Knepley 
4053b2e4378dSMatthew G. Knepley   Level: beginner
4054b2e4378dSMatthew G. Knepley 
4055b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
4056b2e4378dSMatthew G. Knepley 
4057db781477SPatrick Sanan .seealso: `DMGetCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetBoundingBox()`
4058b2e4378dSMatthew G. Knepley @*/
4059b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
4060b2e4378dSMatthew G. Knepley {
4061b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
4062b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
4063b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
4064b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
4065b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
4066b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
4067b2e4378dSMatthew G. Knepley 
4068b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4069b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40709566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
40719566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinates(dm, &coords));
4072b2e4378dSMatthew G. Knepley   if (coords) {
40739566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(coords, &local_coords));
40749566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(coords, &N));
4075b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
4076b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
4077b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
4078b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4079b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4080b2e4378dSMatthew G. Knepley       }
4081b2e4378dSMatthew G. Knepley     }
40829566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(coords, &local_coords));
4083b2e4378dSMatthew G. Knepley   } else {
4084b2e4378dSMatthew G. Knepley     PetscBool isda;
4085b2e4378dSMatthew G. Knepley 
40869566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda));
40879566063dSJacob Faibussowitsch     if (isda) PetscCall(DMGetLocalBoundingIndices_DMDA(dm, min, max));
4088b2e4378dSMatthew G. Knepley   }
40899566063dSJacob Faibussowitsch   if (lmin) PetscCall(PetscArraycpy(lmin, min, cdim));
40909566063dSJacob Faibussowitsch   if (lmax) PetscCall(PetscArraycpy(lmax, max, cdim));
4091b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4092b2e4378dSMatthew G. Knepley }
4093b2e4378dSMatthew G. Knepley 
4094b2e4378dSMatthew G. Knepley /*@
4095b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
4096b2e4378dSMatthew G. Knepley 
4097b2e4378dSMatthew G. Knepley   Collective
4098b2e4378dSMatthew G. Knepley 
4099b2e4378dSMatthew G. Knepley   Input Parameter:
4100b2e4378dSMatthew G. Knepley . dm - the DM
4101b2e4378dSMatthew G. Knepley 
4102b2e4378dSMatthew G. Knepley   Output Parameters:
4103b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
4104b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
4105b2e4378dSMatthew G. Knepley 
4106b2e4378dSMatthew G. Knepley   Level: beginner
4107b2e4378dSMatthew G. Knepley 
4108db781477SPatrick Sanan .seealso: `DMGetLocalBoundingBox()`, `DMGetCoordinates()`, `DMGetCoordinatesLocal()`
4109b2e4378dSMatthew G. Knepley @*/
4110b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
4111b2e4378dSMatthew G. Knepley {
4112b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
41136ce308c4SMatthew G. Knepley   PetscInt       cdim;
41146ce308c4SMatthew G. Knepley   PetscMPIInt    count;
4115b2e4378dSMatthew G. Knepley 
4116b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4117b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41189566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
41199566063dSJacob Faibussowitsch   PetscCall(PetscMPIIntCast(cdim, &count));
41209566063dSJacob Faibussowitsch   PetscCall(DMGetLocalBoundingBox(dm, lmin, lmax));
41211c2dc1cbSBarry Smith   if (gmin) PetscCall(MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm)));
41221c2dc1cbSBarry Smith   if (gmax) PetscCall(MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm)));
4123b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4124b2e4378dSMatthew G. Knepley }
4125b2e4378dSMatthew G. Knepley 
41267da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41277da65231SMatthew G Knepley 
4128a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4129a6dfd86eSKarl Rupp {
41301d47ebbbSSatish Balay   PetscInt       f;
41311b30c384SMatthew G Knepley 
41327da65231SMatthew G Knepley   PetscFunctionBegin;
413363a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41341d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
41359566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41367da65231SMatthew G Knepley   }
41377da65231SMatthew G Knepley   PetscFunctionReturn(0);
41387da65231SMatthew G Knepley }
41397da65231SMatthew G Knepley 
4140a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4141a6dfd86eSKarl Rupp {
41421b30c384SMatthew G Knepley   PetscInt       f, g;
41437da65231SMatthew G Knepley 
41447da65231SMatthew G Knepley   PetscFunctionBegin;
414563a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41461d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
41479566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
41481d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
414963a3b9bcSJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f*cols+g])));
41507da65231SMatthew G Knepley     }
41519566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
41527da65231SMatthew G Knepley   }
41537da65231SMatthew G Knepley   PetscFunctionReturn(0);
41547da65231SMatthew G Knepley }
4155e7c4fc90SDmitry Karpeev 
41566113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4157e759306cSMatthew G. Knepley {
41580c5b8624SToby Isaac   PetscInt          localSize, bs;
41590c5b8624SToby Isaac   PetscMPIInt       size;
41600c5b8624SToby Isaac   Vec               x, xglob;
41610c5b8624SToby Isaac   const PetscScalar *xarray;
4162e759306cSMatthew G. Knepley 
4163e759306cSMatthew G. Knepley   PetscFunctionBegin;
41649566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size));
41659566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
41669566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
41679566063dSJacob Faibussowitsch   PetscCall(VecChop(x, tol));
41689566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name));
41690c5b8624SToby Isaac   if (size > 1) {
41709566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x,&localSize));
41719566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x,&xarray));
41729566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x,&bs));
41739566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob));
41740c5b8624SToby Isaac   } else {
41750c5b8624SToby Isaac     xglob = x;
41760c5b8624SToby Isaac   }
41779566063dSJacob Faibussowitsch   PetscCall(VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm))));
41780c5b8624SToby Isaac   if (size > 1) {
41799566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
41809566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x,&xarray));
41810c5b8624SToby Isaac   }
41829566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
4183e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4184e759306cSMatthew G. Knepley }
4185e759306cSMatthew G. Knepley 
418688ed4aceSMatthew G Knepley /*@
41871bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4188061576a5SJed Brown 
4189061576a5SJed Brown   Input Parameter:
4190061576a5SJed Brown . dm - The DM
4191061576a5SJed Brown 
4192061576a5SJed Brown   Output Parameter:
4193061576a5SJed Brown . section - The PetscSection
4194061576a5SJed Brown 
4195061576a5SJed Brown   Options Database Keys:
4196061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4197061576a5SJed Brown 
4198061576a5SJed Brown   Level: advanced
4199061576a5SJed Brown 
4200061576a5SJed Brown   Notes:
4201061576a5SJed Brown   Use DMGetLocalSection() in new code.
4202061576a5SJed Brown 
4203061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4204061576a5SJed Brown 
4205db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4206061576a5SJed Brown @*/
4207061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4208061576a5SJed Brown {
4209061576a5SJed Brown   PetscFunctionBegin;
42109566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm,section));
4211061576a5SJed Brown   PetscFunctionReturn(0);
4212061576a5SJed Brown }
4213061576a5SJed Brown 
4214061576a5SJed Brown /*@
4215061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
421688ed4aceSMatthew G Knepley 
421788ed4aceSMatthew G Knepley   Input Parameter:
421888ed4aceSMatthew G Knepley . dm - The DM
421988ed4aceSMatthew G Knepley 
422088ed4aceSMatthew G Knepley   Output Parameter:
422188ed4aceSMatthew G Knepley . section - The PetscSection
422288ed4aceSMatthew G Knepley 
4223e5893cccSMatthew G. Knepley   Options Database Keys:
4224e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4225e5893cccSMatthew G. Knepley 
422688ed4aceSMatthew G Knepley   Level: intermediate
422788ed4aceSMatthew G Knepley 
422888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
422988ed4aceSMatthew G Knepley 
4230db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetGlobalSection()`
423188ed4aceSMatthew G Knepley @*/
4232061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
42330adebc6cSBarry Smith {
423488ed4aceSMatthew G Knepley   PetscFunctionBegin;
423588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
423688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42371bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4238e5e52638SMatthew G. Knepley     PetscInt d;
4239e5e52638SMatthew G. Knepley 
424045480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
424145480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject) dm;
424245480ffeSMatthew G. Knepley       PetscViewer       viewer;
424345480ffeSMatthew G. Knepley       PetscViewerFormat format;
424445480ffeSMatthew G. Knepley       PetscBool         flg;
424545480ffeSMatthew G. Knepley 
42469566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
42479566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
424845480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
42499566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
42509566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
425145480ffeSMatthew G. Knepley       }
425245480ffeSMatthew G. Knepley       if (flg) {
42539566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
42549566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
42559566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
425645480ffeSMatthew G. Knepley       }
425745480ffeSMatthew G. Knepley     }
42589566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createlocalsection)(dm));
42599566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view"));
42602f0f8703SMatthew G. Knepley   }
42611bb6d2a8SBarry Smith   *section = dm->localSection;
426288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
426388ed4aceSMatthew G Knepley }
426488ed4aceSMatthew G Knepley 
426588ed4aceSMatthew G Knepley /*@
42661bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4267061576a5SJed Brown 
4268061576a5SJed Brown   Input Parameters:
4269061576a5SJed Brown + dm - The DM
4270061576a5SJed Brown - section - The PetscSection
4271061576a5SJed Brown 
4272061576a5SJed Brown   Level: advanced
4273061576a5SJed Brown 
4274061576a5SJed Brown   Notes:
4275061576a5SJed Brown   Use DMSetLocalSection() in new code.
4276061576a5SJed Brown 
4277061576a5SJed Brown   Any existing Section will be destroyed
4278061576a5SJed Brown 
4279db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4280061576a5SJed Brown @*/
4281061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4282061576a5SJed Brown {
4283061576a5SJed Brown   PetscFunctionBegin;
42849566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm,section));
4285061576a5SJed Brown   PetscFunctionReturn(0);
4286061576a5SJed Brown }
4287061576a5SJed Brown 
4288061576a5SJed Brown /*@
4289061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
429088ed4aceSMatthew G Knepley 
429188ed4aceSMatthew G Knepley   Input Parameters:
429288ed4aceSMatthew G Knepley + dm - The DM
429388ed4aceSMatthew G Knepley - section - The PetscSection
429488ed4aceSMatthew G Knepley 
429588ed4aceSMatthew G Knepley   Level: intermediate
429688ed4aceSMatthew G Knepley 
429788ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
429888ed4aceSMatthew G Knepley 
4299db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetGlobalSection()`
430088ed4aceSMatthew G Knepley @*/
4301061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
43020adebc6cSBarry Smith {
4303c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4304af122d2aSMatthew G Knepley   PetscInt       f;
430588ed4aceSMatthew G Knepley 
430688ed4aceSMatthew G Knepley   PetscFunctionBegin;
430788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4308b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
43099566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43109566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43111bb6d2a8SBarry Smith   dm->localSection = section;
43129566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4313af122d2aSMatthew G Knepley   if (numFields) {
43149566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4315af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43160f21e855SMatthew G. Knepley       PetscObject disc;
4317af122d2aSMatthew G Knepley       const char *name;
4318af122d2aSMatthew G Knepley 
43199566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43209566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43219566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4322af122d2aSMatthew G Knepley     }
4323af122d2aSMatthew G Knepley   }
4324e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43259566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
432688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
432788ed4aceSMatthew G Knepley }
432888ed4aceSMatthew G Knepley 
43299435951eSToby Isaac /*@
4330b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
43319435951eSToby Isaac 
4332e228b242SToby Isaac   not collective
4333e228b242SToby Isaac 
43349435951eSToby Isaac   Input Parameter:
43359435951eSToby Isaac . dm - The DM
43369435951eSToby Isaac 
4337d8d19677SJose E. Roman   Output Parameters:
43389435951eSToby 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.
433979769bd5SJed 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.
434079769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
43419435951eSToby Isaac 
43429435951eSToby Isaac   Level: advanced
43439435951eSToby Isaac 
434479769bd5SJed Brown   Note: This gets borrowed references, so the user should not destroy the PetscSection, Mat, or Vec.
43459435951eSToby Isaac 
4346db781477SPatrick Sanan .seealso: `DMSetDefaultConstraints()`
43479435951eSToby Isaac @*/
434879769bd5SJed Brown PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
43499435951eSToby Isaac {
43509435951eSToby Isaac   PetscFunctionBegin;
43519435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43529566063dSJacob Faibussowitsch   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscCall((*dm->ops->createdefaultconstraints)(dm));
43533b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
43543b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
435579769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
43569435951eSToby Isaac   PetscFunctionReturn(0);
43579435951eSToby Isaac }
43589435951eSToby Isaac 
43599435951eSToby Isaac /*@
4360b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
43619435951eSToby Isaac 
436279769bd5SJed 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().
43639435951eSToby Isaac 
436479769bd5SJed 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.
43659435951eSToby Isaac 
4366e228b242SToby Isaac   collective on dm
4367e228b242SToby Isaac 
43689435951eSToby Isaac   Input Parameters:
43699435951eSToby Isaac + dm - The DM
437079769bd5SJed 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).
437179769bd5SJed 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).
437279769bd5SJed 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).
43739435951eSToby Isaac 
43749435951eSToby Isaac   Level: advanced
43759435951eSToby Isaac 
437679769bd5SJed Brown   Note: This increments the references of the PetscSection, Mat, and Vec, so they user can destroy them.
43779435951eSToby Isaac 
4378db781477SPatrick Sanan .seealso: `DMGetDefaultConstraints()`
43799435951eSToby Isaac @*/
438079769bd5SJed Brown PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
43819435951eSToby Isaac {
4382e228b242SToby Isaac   PetscMPIInt result;
43839435951eSToby Isaac 
43849435951eSToby Isaac   PetscFunctionBegin;
43859435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4386e228b242SToby Isaac   if (section) {
4387e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
43889566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result));
43897a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4390e228b242SToby Isaac   }
4391e228b242SToby Isaac   if (mat) {
4392e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
43939566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result));
43947a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4395e228b242SToby Isaac   }
439679769bd5SJed Brown   if (bias) {
439779769bd5SJed Brown     PetscValidHeaderSpecific(bias,VEC_CLASSID,4);
43989566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)bias),&result));
439979769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint bias must have local communicator");
440079769bd5SJed Brown   }
44019566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44029566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44033b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44049566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44059566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44063b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44079566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44089566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
440979769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44109435951eSToby Isaac   PetscFunctionReturn(0);
44119435951eSToby Isaac }
44129435951eSToby Isaac 
4413497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4414507e4973SMatthew G. Knepley /*
4415507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4416507e4973SMatthew G. Knepley 
4417507e4973SMatthew G. Knepley   Input Parameters:
4418507e4973SMatthew G. Knepley + dm - The DM
4419507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4420507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4421507e4973SMatthew G. Knepley 
4422507e4973SMatthew G. Knepley   Level: intermediate
4423507e4973SMatthew G. Knepley 
4424db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`
4425507e4973SMatthew G. Knepley */
4426f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4427507e4973SMatthew G. Knepley {
4428507e4973SMatthew G. Knepley   MPI_Comm        comm;
4429507e4973SMatthew G. Knepley   PetscLayout     layout;
4430507e4973SMatthew G. Knepley   const PetscInt *ranges;
4431507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4432507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4433507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4434507e4973SMatthew G. Knepley 
4435507e4973SMatthew G. Knepley   PetscFunctionBegin;
44369566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
4437507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44389566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
44399566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44409566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
44419566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
44429566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
44439566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
44449566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
44459566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
44469566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4447507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4448f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4449507e4973SMatthew G. Knepley 
44509566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
44519566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
44529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
44539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
44549566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
44559566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4456507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
445763a3b9bcSJacob 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;}
445863a3b9bcSJacob 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;}
4459507e4973SMatthew G. Knepley     if (gdof < 0) {
4460507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4461507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4462507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4463507e4973SMatthew G. Knepley 
44649566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset,size+1,ranges,&r));
4465507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
446663a3b9bcSJacob 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;}
4467507e4973SMatthew G. Knepley       }
4468507e4973SMatthew G. Knepley     }
4469507e4973SMatthew G. Knepley   }
44709566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
44719566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
44721c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4473507e4973SMatthew G. Knepley   if (!gvalid) {
44749566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4475507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4476507e4973SMatthew G. Knepley   }
4477507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4478507e4973SMatthew G. Knepley }
4479f741bcd2SMatthew G. Knepley #endif
4480507e4973SMatthew G. Knepley 
448188ed4aceSMatthew G Knepley /*@
4482e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
448388ed4aceSMatthew G Knepley 
4484d083f849SBarry Smith   Collective on dm
44858b1ab98fSJed Brown 
448688ed4aceSMatthew G Knepley   Input Parameter:
448788ed4aceSMatthew G Knepley . dm - The DM
448888ed4aceSMatthew G Knepley 
448988ed4aceSMatthew G Knepley   Output Parameter:
449088ed4aceSMatthew G Knepley . section - The PetscSection
449188ed4aceSMatthew G Knepley 
449288ed4aceSMatthew G Knepley   Level: intermediate
449388ed4aceSMatthew G Knepley 
449488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
449588ed4aceSMatthew G Knepley 
4496db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`
449788ed4aceSMatthew G Knepley @*/
4498e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
44990adebc6cSBarry Smith {
450088ed4aceSMatthew G Knepley   PetscFunctionBegin;
450188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
450288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
45031bb6d2a8SBarry Smith   if (!dm->globalSection) {
4504fd59a867SMatthew G. Knepley     PetscSection s;
4505fd59a867SMatthew G. Knepley 
45069566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
45077a8be351SBarry Smith     PetscCheck(s,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
45087a8be351SBarry Smith     PetscCheck(dm->sf,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45099566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
45109566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
45119566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
45129566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
451388ed4aceSMatthew G Knepley   }
45141bb6d2a8SBarry Smith   *section = dm->globalSection;
451588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
451688ed4aceSMatthew G Knepley }
451788ed4aceSMatthew G Knepley 
4518b21d0597SMatthew G Knepley /*@
4519e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4520b21d0597SMatthew G Knepley 
4521b21d0597SMatthew G Knepley   Input Parameters:
4522b21d0597SMatthew G Knepley + dm - The DM
45235080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4524b21d0597SMatthew G Knepley 
4525b21d0597SMatthew G Knepley   Level: intermediate
4526b21d0597SMatthew G Knepley 
4527b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4528b21d0597SMatthew G Knepley 
4529db781477SPatrick Sanan .seealso: `DMGetGlobalSection()`, `DMSetLocalSection()`
4530b21d0597SMatthew G Knepley @*/
4531e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
45320adebc6cSBarry Smith {
4533b21d0597SMatthew G Knepley   PetscFunctionBegin;
4534b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45355080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
45369566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45379566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
45381bb6d2a8SBarry Smith   dm->globalSection = section;
4539497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
45409566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4541507e4973SMatthew G. Knepley #endif
4542b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4543b21d0597SMatthew G Knepley }
4544b21d0597SMatthew G Knepley 
454588ed4aceSMatthew G Knepley /*@
45461bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
454788ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
454888ed4aceSMatthew G Knepley 
454988ed4aceSMatthew G Knepley   Input Parameter:
455088ed4aceSMatthew G Knepley . dm - The DM
455188ed4aceSMatthew G Knepley 
455288ed4aceSMatthew G Knepley   Output Parameter:
455388ed4aceSMatthew G Knepley . sf - The PetscSF
455488ed4aceSMatthew G Knepley 
455588ed4aceSMatthew G Knepley   Level: intermediate
455688ed4aceSMatthew G Knepley 
455788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
455888ed4aceSMatthew G Knepley 
4559db781477SPatrick Sanan .seealso: `DMSetSectionSF()`, `DMCreateSectionSF()`
456088ed4aceSMatthew G Knepley @*/
45611bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
45620adebc6cSBarry Smith {
456388ed4aceSMatthew G Knepley   PetscInt       nroots;
456488ed4aceSMatthew G Knepley 
456588ed4aceSMatthew G Knepley   PetscFunctionBegin;
456688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
456788ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
45681bb6d2a8SBarry Smith   if (!dm->sectionSF) {
45699566063dSJacob Faibussowitsch     PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF));
457033907cc2SStefano Zampini   }
45719566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
457288ed4aceSMatthew G Knepley   if (nroots < 0) {
457388ed4aceSMatthew G Knepley     PetscSection section, gSection;
457488ed4aceSMatthew G Knepley 
45759566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
457631ea6d37SMatthew G Knepley     if (section) {
45779566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
45789566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
457931ea6d37SMatthew G Knepley     } else {
45800298fd71SBarry Smith       *sf = NULL;
458131ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
458231ea6d37SMatthew G Knepley     }
458388ed4aceSMatthew G Knepley   }
45841bb6d2a8SBarry Smith   *sf = dm->sectionSF;
458588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
458688ed4aceSMatthew G Knepley }
458788ed4aceSMatthew G Knepley 
458888ed4aceSMatthew G Knepley /*@
45891bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
459088ed4aceSMatthew G Knepley 
459188ed4aceSMatthew G Knepley   Input Parameters:
459288ed4aceSMatthew G Knepley + dm - The DM
459388ed4aceSMatthew G Knepley - sf - The PetscSF
459488ed4aceSMatthew G Knepley 
459588ed4aceSMatthew G Knepley   Level: intermediate
459688ed4aceSMatthew G Knepley 
459788ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
459888ed4aceSMatthew G Knepley 
4599db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMCreateSectionSF()`
460088ed4aceSMatthew G Knepley @*/
46011bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
46020adebc6cSBarry Smith {
460388ed4aceSMatthew G Knepley   PetscFunctionBegin;
460488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4605b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46069566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
46079566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
46081bb6d2a8SBarry Smith   dm->sectionSF = sf;
460988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
461088ed4aceSMatthew G Knepley }
461188ed4aceSMatthew G Knepley 
461288ed4aceSMatthew G Knepley /*@C
46131bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
461488ed4aceSMatthew G Knepley   describing the data layout.
461588ed4aceSMatthew G Knepley 
461688ed4aceSMatthew G Knepley   Input Parameters:
461788ed4aceSMatthew G Knepley + dm - The DM
461888ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
461988ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
462088ed4aceSMatthew G Knepley 
46211bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
462288ed4aceSMatthew G Knepley 
46231bb6d2a8SBarry Smith   Level: developer
46241bb6d2a8SBarry Smith 
46251bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
46261bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
46271bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
46281bb6d2a8SBarry Smith 
4629db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
463088ed4aceSMatthew G Knepley @*/
46311bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
463288ed4aceSMatthew G Knepley {
463388ed4aceSMatthew G Knepley   PetscFunctionBegin;
463488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46359566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
463688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
463788ed4aceSMatthew G Knepley }
4638af122d2aSMatthew G Knepley 
4639b21d0597SMatthew G Knepley /*@
4640b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4641b21d0597SMatthew G Knepley 
4642b21d0597SMatthew G Knepley   Input Parameter:
4643b21d0597SMatthew G Knepley . dm - The DM
4644b21d0597SMatthew G Knepley 
4645b21d0597SMatthew G Knepley   Output Parameter:
4646b21d0597SMatthew G Knepley . sf - The PetscSF
4647b21d0597SMatthew G Knepley 
4648b21d0597SMatthew G Knepley   Level: intermediate
4649b21d0597SMatthew G Knepley 
4650b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4651b21d0597SMatthew G Knepley 
4652db781477SPatrick Sanan .seealso: `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4653b21d0597SMatthew G Knepley @*/
46540adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
46550adebc6cSBarry Smith {
4656b21d0597SMatthew G Knepley   PetscFunctionBegin;
4657b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4658b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4659b21d0597SMatthew G Knepley   *sf = dm->sf;
4660b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4661b21d0597SMatthew G Knepley }
4662b21d0597SMatthew G Knepley 
4663057b4bcdSMatthew G Knepley /*@
4664057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4665057b4bcdSMatthew G Knepley 
4666057b4bcdSMatthew G Knepley   Input Parameters:
4667057b4bcdSMatthew G Knepley + dm - The DM
4668057b4bcdSMatthew G Knepley - sf - The PetscSF
4669057b4bcdSMatthew G Knepley 
4670057b4bcdSMatthew G Knepley   Level: intermediate
4671057b4bcdSMatthew G Knepley 
4672db781477SPatrick Sanan .seealso: `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4673057b4bcdSMatthew G Knepley @*/
46740adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
46750adebc6cSBarry Smith {
4676057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4677057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4678b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46799566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
46809566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4681057b4bcdSMatthew G Knepley   dm->sf = sf;
4682057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4683057b4bcdSMatthew G Knepley }
4684057b4bcdSMatthew G Knepley 
46854f37162bSMatthew G. Knepley /*@
46864f37162bSMatthew G. Knepley   DMGetNaturalSF - Get the PetscSF encoding the map back to the original mesh ordering
46874f37162bSMatthew G. Knepley 
46884f37162bSMatthew G. Knepley   Input Parameter:
46894f37162bSMatthew G. Knepley . dm - The DM
46904f37162bSMatthew G. Knepley 
46914f37162bSMatthew G. Knepley   Output Parameter:
46924f37162bSMatthew G. Knepley . sf - The PetscSF
46934f37162bSMatthew G. Knepley 
46944f37162bSMatthew G. Knepley   Level: intermediate
46954f37162bSMatthew G. Knepley 
46964f37162bSMatthew G. Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
46974f37162bSMatthew G. Knepley 
4698db781477SPatrick Sanan .seealso: `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
46994f37162bSMatthew G. Knepley @*/
47004f37162bSMatthew G. Knepley PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
47014f37162bSMatthew G. Knepley {
47024f37162bSMatthew G. Knepley   PetscFunctionBegin;
47034f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47044f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
47054f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
47064f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
47074f37162bSMatthew G. Knepley }
47084f37162bSMatthew G. Knepley 
47094f37162bSMatthew G. Knepley /*@
47104f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
47114f37162bSMatthew G. Knepley 
47124f37162bSMatthew G. Knepley   Input Parameters:
47134f37162bSMatthew G. Knepley + dm - The DM
47144f37162bSMatthew G. Knepley - sf - The PetscSF
47154f37162bSMatthew G. Knepley 
47164f37162bSMatthew G. Knepley   Level: intermediate
47174f37162bSMatthew G. Knepley 
4718db781477SPatrick Sanan .seealso: `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47194f37162bSMatthew G. Knepley @*/
47204f37162bSMatthew G. Knepley PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
47214f37162bSMatthew G. Knepley {
47224f37162bSMatthew G. Knepley   PetscFunctionBegin;
47234f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47244f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47259566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
47269566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
47274f37162bSMatthew G. Knepley   dm->sfNatural = sf;
47284f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
47294f37162bSMatthew G. Knepley }
47304f37162bSMatthew G. Knepley 
473134aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
473234aa8a36SMatthew G. Knepley {
473334aa8a36SMatthew G. Knepley   PetscClassId   id;
473434aa8a36SMatthew G. Knepley 
473534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
47369566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
473734aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
47389566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
473934aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
47409566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
474117c1d62eSMatthew G. Knepley   } else {
47429566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
474334aa8a36SMatthew G. Knepley   }
474434aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
474534aa8a36SMatthew G. Knepley }
474634aa8a36SMatthew G. Knepley 
474744a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
474844a7f3ddSMatthew G. Knepley {
474944a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
475044a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
475144a7f3ddSMatthew G. Knepley 
475244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
475344a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
47549566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
475544a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4756e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
47579566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
475844a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
475944a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
476044a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
476144a7f3ddSMatthew G. Knepley }
476244a7f3ddSMatthew G. Knepley 
476344a7f3ddSMatthew G. Knepley /*@
476444a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
476544a7f3ddSMatthew G. Knepley 
4766d083f849SBarry Smith   Logically collective on dm
476744a7f3ddSMatthew G. Knepley 
476844a7f3ddSMatthew G. Knepley   Input Parameter:
476944a7f3ddSMatthew G. Knepley . dm - The DM
477044a7f3ddSMatthew G. Knepley 
477144a7f3ddSMatthew G. Knepley   Level: intermediate
477244a7f3ddSMatthew G. Knepley 
4773db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
477444a7f3ddSMatthew G. Knepley @*/
477544a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
477644a7f3ddSMatthew G. Knepley {
477744a7f3ddSMatthew G. Knepley   PetscInt       f;
477844a7f3ddSMatthew G. Knepley 
477944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
478044a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
478144a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
47829566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
47839566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
478444a7f3ddSMatthew G. Knepley   }
47859566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
478644a7f3ddSMatthew G. Knepley   dm->fields = NULL;
478744a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
478844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
478944a7f3ddSMatthew G. Knepley }
479044a7f3ddSMatthew G. Knepley 
4791689b5837SMatthew G. Knepley /*@
4792689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4793689b5837SMatthew G. Knepley 
4794689b5837SMatthew G. Knepley   Not collective
4795689b5837SMatthew G. Knepley 
4796689b5837SMatthew G. Knepley   Input Parameter:
4797689b5837SMatthew G. Knepley . dm - The DM
4798689b5837SMatthew G. Knepley 
4799689b5837SMatthew G. Knepley   Output Parameter:
4800689b5837SMatthew G. Knepley . Nf - The number of fields
4801689b5837SMatthew G. Knepley 
4802689b5837SMatthew G. Knepley   Level: intermediate
4803689b5837SMatthew G. Knepley 
4804db781477SPatrick Sanan .seealso: `DMSetNumFields()`, `DMSetField()`
4805689b5837SMatthew G. Knepley @*/
48060f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
48070f21e855SMatthew G. Knepley {
48080f21e855SMatthew G. Knepley   PetscFunctionBegin;
48090f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4810534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
481144a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4812af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4813af122d2aSMatthew G Knepley }
4814af122d2aSMatthew G Knepley 
4815689b5837SMatthew G. Knepley /*@
4816689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4817689b5837SMatthew G. Knepley 
4818d083f849SBarry Smith   Logically collective on dm
4819689b5837SMatthew G. Knepley 
4820689b5837SMatthew G. Knepley   Input Parameters:
4821689b5837SMatthew G. Knepley + dm - The DM
4822689b5837SMatthew G. Knepley - Nf - The number of fields
4823689b5837SMatthew G. Knepley 
4824689b5837SMatthew G. Knepley   Level: intermediate
4825689b5837SMatthew G. Knepley 
4826db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetField()`
4827689b5837SMatthew G. Knepley @*/
4828af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4829af122d2aSMatthew G Knepley {
48300f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4831af122d2aSMatthew G Knepley 
4832af122d2aSMatthew G Knepley   PetscFunctionBegin;
4833af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48349566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
48350f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
48360f21e855SMatthew G. Knepley     PetscContainer obj;
48370f21e855SMatthew G. Knepley 
48389566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj));
48399566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject) obj));
48409566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4841af122d2aSMatthew G Knepley   }
4842af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4843af122d2aSMatthew G Knepley }
4844af122d2aSMatthew G Knepley 
4845c1929be8SMatthew G. Knepley /*@
4846c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4847c1929be8SMatthew G. Knepley 
4848c1929be8SMatthew G. Knepley   Not collective
4849c1929be8SMatthew G. Knepley 
4850c1929be8SMatthew G. Knepley   Input Parameters:
4851c1929be8SMatthew G. Knepley + dm - The DM
4852c1929be8SMatthew G. Knepley - f  - The field number
4853c1929be8SMatthew G. Knepley 
485444a7f3ddSMatthew G. Knepley   Output Parameters:
485544a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
485644a7f3ddSMatthew G. Knepley - field - The discretization object
4857c1929be8SMatthew G. Knepley 
485844a7f3ddSMatthew G. Knepley   Level: intermediate
4859c1929be8SMatthew G. Knepley 
4860db781477SPatrick Sanan .seealso: `DMAddField()`, `DMSetField()`
4861c1929be8SMatthew G. Knepley @*/
486244a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4863af122d2aSMatthew G Knepley {
4864af122d2aSMatthew G Knepley   PetscFunctionBegin;
4865af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4866064a246eSJacob Faibussowitsch   PetscValidPointer(field, 4);
48677a8be351SBarry 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);
486844a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
486944a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4870decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4871decb47aaSMatthew G. Knepley }
4872decb47aaSMatthew G. Knepley 
4873083401c6SMatthew G. Knepley /* Does not clear the DS */
4874083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4875083401c6SMatthew G. Knepley {
4876083401c6SMatthew G. Knepley   PetscFunctionBegin;
48779566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f+1));
48789566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
48799566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4880083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4881083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
48829566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
48839566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) field));
4884083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4885083401c6SMatthew G. Knepley }
4886083401c6SMatthew G. Knepley 
4887c1929be8SMatthew G. Knepley /*@
4888c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4889c1929be8SMatthew G. Knepley 
4890d083f849SBarry Smith   Logically collective on dm
4891c1929be8SMatthew G. Knepley 
4892c1929be8SMatthew G. Knepley   Input Parameters:
4893c1929be8SMatthew G. Knepley + dm    - The DM
4894c1929be8SMatthew G. Knepley . f     - The field number
489544a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4896c1929be8SMatthew G. Knepley - field - The discretization object
4897c1929be8SMatthew G. Knepley 
489844a7f3ddSMatthew G. Knepley   Level: intermediate
4899c1929be8SMatthew G. Knepley 
4900db781477SPatrick Sanan .seealso: `DMAddField()`, `DMGetField()`
4901c1929be8SMatthew G. Knepley @*/
490244a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4903decb47aaSMatthew G. Knepley {
4904decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4905decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4906e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
490744a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
49087a8be351SBarry Smith   PetscCheck(f >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
49099566063dSJacob Faibussowitsch   PetscCall(DMSetField_Internal(dm, f, label, field));
49109566063dSJacob Faibussowitsch   PetscCall(DMSetDefaultAdjacency_Private(dm, f, field));
49119566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
491244a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
491344a7f3ddSMatthew G. Knepley }
491444a7f3ddSMatthew G. Knepley 
491544a7f3ddSMatthew G. Knepley /*@
491644a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
491744a7f3ddSMatthew G. Knepley 
4918d083f849SBarry Smith   Logically collective on dm
491944a7f3ddSMatthew G. Knepley 
492044a7f3ddSMatthew G. Knepley   Input Parameters:
492144a7f3ddSMatthew G. Knepley + dm    - The DM
492244a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
492344a7f3ddSMatthew G. Knepley - field - The discretization object
492444a7f3ddSMatthew G. Knepley 
492544a7f3ddSMatthew G. Knepley   Level: intermediate
492644a7f3ddSMatthew G. Knepley 
4927db781477SPatrick Sanan .seealso: `DMSetField()`, `DMGetField()`
492844a7f3ddSMatthew G. Knepley @*/
492944a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
493044a7f3ddSMatthew G. Knepley {
493144a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
493244a7f3ddSMatthew G. Knepley 
493344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
493444a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4935064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
493644a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
49379566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf+1));
493844a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
493944a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
49409566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
49419566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) field));
49429566063dSJacob Faibussowitsch   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, field));
49439566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
4944af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4945af122d2aSMatthew G Knepley }
49466636e97aSMatthew G Knepley 
4947e5e52638SMatthew G. Knepley /*@
4948e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4949e0b68406SMatthew Knepley 
4950e0b68406SMatthew Knepley   Logically collective on dm
4951e0b68406SMatthew Knepley 
4952e0b68406SMatthew Knepley   Input Parameters:
4953e0b68406SMatthew Knepley + dm          - The DM
4954e0b68406SMatthew Knepley . f           - The field index
4955e0b68406SMatthew Knepley - avoidTensor - The flag to avoid defining the field on tensor cells
4956e0b68406SMatthew Knepley 
4957e0b68406SMatthew Knepley   Level: intermediate
4958e0b68406SMatthew Knepley 
4959db781477SPatrick Sanan .seealso: `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
4960e0b68406SMatthew Knepley @*/
4961e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
4962e0b68406SMatthew Knepley {
4963e0b68406SMatthew Knepley   PetscFunctionBegin;
496463a3b9bcSJacob 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);
4965e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
4966e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4967e0b68406SMatthew Knepley }
4968e0b68406SMatthew Knepley 
4969e0b68406SMatthew Knepley /*@
4970e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
4971e0b68406SMatthew Knepley 
4972e0b68406SMatthew Knepley   Logically collective on dm
4973e0b68406SMatthew Knepley 
4974e0b68406SMatthew Knepley   Input Parameters:
4975e0b68406SMatthew Knepley + dm          - The DM
4976e0b68406SMatthew Knepley - f           - The field index
4977e0b68406SMatthew Knepley 
4978e0b68406SMatthew Knepley   Output Parameter:
4979e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
4980e0b68406SMatthew Knepley 
4981e0b68406SMatthew Knepley   Level: intermediate
4982e0b68406SMatthew Knepley 
4983db781477SPatrick Sanan .seealso: `DMSetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
4984e0b68406SMatthew Knepley @*/
4985e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
4986e0b68406SMatthew Knepley {
4987e0b68406SMatthew Knepley   PetscFunctionBegin;
498863a3b9bcSJacob 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);
4989e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
4990e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4991e0b68406SMatthew Knepley }
4992e0b68406SMatthew Knepley 
4993e0b68406SMatthew Knepley /*@
4994e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4995e5e52638SMatthew G. Knepley 
4996d083f849SBarry Smith   Collective on dm
4997e5e52638SMatthew G. Knepley 
4998e5e52638SMatthew G. Knepley   Input Parameter:
4999e5e52638SMatthew G. Knepley . dm - The DM
5000e5e52638SMatthew G. Knepley 
5001e5e52638SMatthew G. Knepley   Output Parameter:
5002e5e52638SMatthew G. Knepley . newdm - The DM
5003e5e52638SMatthew G. Knepley 
5004e5e52638SMatthew G. Knepley   Level: advanced
5005e5e52638SMatthew G. Knepley 
5006db781477SPatrick Sanan .seealso: `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5007e5e52638SMatthew G. Knepley @*/
5008e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
5009e5e52638SMatthew G. Knepley {
5010e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
5011e5e52638SMatthew G. Knepley 
5012e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5013e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
50149566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
50159566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5016e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5017e5e52638SMatthew G. Knepley     DMLabel     label;
5018e5e52638SMatthew G. Knepley     PetscObject field;
501934aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5020e5e52638SMatthew G. Knepley 
50219566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
50229566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
50239566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
50249566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
502534aa8a36SMatthew G. Knepley   }
502634aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
502734aa8a36SMatthew G. Knepley }
502834aa8a36SMatthew G. Knepley 
502934aa8a36SMatthew G. Knepley /*@
503034aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
503134aa8a36SMatthew G. Knepley 
503234aa8a36SMatthew G. Knepley   Not collective
503334aa8a36SMatthew G. Knepley 
503434aa8a36SMatthew G. Knepley   Input Parameters:
503534aa8a36SMatthew G. Knepley + dm - The DM object
503634aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
503734aa8a36SMatthew G. Knepley 
5038d8d19677SJose E. Roman   Output Parameters:
503934aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
504034aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
504134aa8a36SMatthew G. Knepley 
504234aa8a36SMatthew G. Knepley   Notes:
504334aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
504434aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
504534aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5046979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
504734aa8a36SMatthew G. Knepley 
504834aa8a36SMatthew G. Knepley   Level: developer
504934aa8a36SMatthew G. Knepley 
5050db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
505134aa8a36SMatthew G. Knepley @*/
505234aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
505334aa8a36SMatthew G. Knepley {
505434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
505534aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5056534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
5057534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
505834aa8a36SMatthew G. Knepley   if (f < 0) {
505934aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
506034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
506134aa8a36SMatthew G. Knepley   } else {
506234aa8a36SMatthew G. Knepley     PetscInt       Nf;
506334aa8a36SMatthew G. Knepley 
50649566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
50657a8be351SBarry Smith     PetscCheck(f < Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
506634aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
506734aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
506834aa8a36SMatthew G. Knepley   }
506934aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
507034aa8a36SMatthew G. Knepley }
507134aa8a36SMatthew G. Knepley 
507234aa8a36SMatthew G. Knepley /*@
507334aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
507434aa8a36SMatthew G. Knepley 
507534aa8a36SMatthew G. Knepley   Not collective
507634aa8a36SMatthew G. Knepley 
507734aa8a36SMatthew G. Knepley   Input Parameters:
507834aa8a36SMatthew G. Knepley + dm         - The DM object
507934aa8a36SMatthew G. Knepley . f          - The field number
508034aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
508134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
508234aa8a36SMatthew G. Knepley 
508334aa8a36SMatthew G. Knepley   Notes:
508434aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
508534aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
508634aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5087979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
508834aa8a36SMatthew G. Knepley 
508934aa8a36SMatthew G. Knepley   Level: developer
509034aa8a36SMatthew G. Knepley 
5091db781477SPatrick Sanan .seealso: `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
509234aa8a36SMatthew G. Knepley @*/
509334aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
509434aa8a36SMatthew G. Knepley {
509534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
509634aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
509734aa8a36SMatthew G. Knepley   if (f < 0) {
509834aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
509934aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
510034aa8a36SMatthew G. Knepley   } else {
510134aa8a36SMatthew G. Knepley     PetscInt       Nf;
510234aa8a36SMatthew G. Knepley 
51039566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51047a8be351SBarry Smith     PetscCheck(f < Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
510534aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
510634aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5107e5e52638SMatthew G. Knepley   }
5108e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5109e5e52638SMatthew G. Knepley }
5110e5e52638SMatthew G. Knepley 
5111b0441da4SMatthew G. Knepley /*@
5112b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5113b0441da4SMatthew G. Knepley 
5114b0441da4SMatthew G. Knepley   Not collective
5115b0441da4SMatthew G. Knepley 
5116f899ff85SJose E. Roman   Input Parameter:
5117b0441da4SMatthew G. Knepley . dm - The DM object
5118b0441da4SMatthew G. Knepley 
5119d8d19677SJose E. Roman   Output Parameters:
5120b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5121b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5122b0441da4SMatthew G. Knepley 
5123b0441da4SMatthew G. Knepley   Notes:
5124b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5125b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5126b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5127b0441da4SMatthew G. Knepley 
5128b0441da4SMatthew G. Knepley   Level: developer
5129b0441da4SMatthew G. Knepley 
5130db781477SPatrick Sanan .seealso: `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5131b0441da4SMatthew G. Knepley @*/
5132b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5133b0441da4SMatthew G. Knepley {
5134b0441da4SMatthew G. Knepley   PetscInt       Nf;
5135b0441da4SMatthew G. Knepley 
5136b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5137b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5138064a246eSJacob Faibussowitsch   if (useCone)    PetscValidBoolPointer(useCone, 2);
5139064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
51409566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5141b0441da4SMatthew G. Knepley   if (!Nf) {
51429566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5143b0441da4SMatthew G. Knepley   } else {
51449566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5145b0441da4SMatthew G. Knepley   }
5146b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5147b0441da4SMatthew G. Knepley }
5148b0441da4SMatthew G. Knepley 
5149b0441da4SMatthew G. Knepley /*@
5150b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5151b0441da4SMatthew G. Knepley 
5152b0441da4SMatthew G. Knepley   Not collective
5153b0441da4SMatthew G. Knepley 
5154b0441da4SMatthew G. Knepley   Input Parameters:
5155b0441da4SMatthew G. Knepley + dm         - The DM object
5156b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5157b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5158b0441da4SMatthew G. Knepley 
5159b0441da4SMatthew G. Knepley   Notes:
5160b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5161b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5162b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5163b0441da4SMatthew G. Knepley 
5164b0441da4SMatthew G. Knepley   Level: developer
5165b0441da4SMatthew G. Knepley 
5166db781477SPatrick Sanan .seealso: `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5167b0441da4SMatthew G. Knepley @*/
5168b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5169b0441da4SMatthew G. Knepley {
5170b0441da4SMatthew G. Knepley   PetscInt       Nf;
5171b0441da4SMatthew G. Knepley 
5172b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5173b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51749566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5175b0441da4SMatthew G. Knepley   if (!Nf) {
51769566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5177b0441da4SMatthew G. Knepley   } else {
51789566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5179e5e52638SMatthew G. Knepley   }
5180e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5181e5e52638SMatthew G. Knepley }
5182e5e52638SMatthew G. Knepley 
5183*799db056SMatthew G. Knepley PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5184783e2ec8SMatthew G. Knepley {
5185*799db056SMatthew G. Knepley   DM           plex;
5186*799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5187*799db056SMatthew G. Knepley   const char **names;
5188*799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5189*799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5190*799db056SMatthew G. Knepley   size_t       len;
5191*799db056SMatthew G. Knepley   MPI_Comm     comm;
5192*799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5193783e2ec8SMatthew G. Knepley 
5194783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5195*799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject) dm, &comm));
5196*799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5197*799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5198*799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5199*799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5200*799db056SMatthew G. Knepley     PetscDS  dsBC;
5201*799db056SMatthew G. Knepley     PetscInt numBd;
5202*799db056SMatthew G. Knepley 
5203*799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5204*799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5205*799db056SMatthew G. Knepley     maxLabels += numBd;
5206*799db056SMatthew G. Knepley   }
5207*799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5208*799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5209*799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5210*799db056SMatthew G. Knepley     PetscDS  dsBC;
5211*799db056SMatthew G. Knepley     PetscInt numBd, bd;
5212*799db056SMatthew G. Knepley 
5213*799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5214*799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5215*799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5216*799db056SMatthew G. Knepley       DMLabel      label;
5217*799db056SMatthew G. Knepley       PetscInt     field;
5218*799db056SMatthew G. Knepley       PetscObject  obj;
5219*799db056SMatthew G. Knepley       PetscClassId id;
5220*799db056SMatthew G. Knepley 
5221*799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
52229566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
52239566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5224*799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
5225*799db056SMatthew G. Knepley       for (l = 0; l < Nl; ++l) if (labels[l] == label) break;
5226*799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5227783e2ec8SMatthew G. Knepley     }
5228*799db056SMatthew G. Knepley   }
5229*799db056SMatthew G. Knepley   /* Get label names */
5230*799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5231*799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject) labels[l], &names[l]));
5232*799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {PetscCall(PetscStrlen(names[l], &len)); maxLen = PetscMax(maxLen, (PetscInt) len+2);}
5233*799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5234*799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5235*799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5236*799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscStrcpy(&sendNames[gmaxLen*l], names[l]));
5237*799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5238*799db056SMatthew G. Knepley   /* Put all names on all processes */
5239*799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size+1, &displs));
5240*799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5241*799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p+1] = displs[p] + counts[p];
5242*799db056SMatthew G. Knepley   gNl = displs[size];
5243*799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) {counts[p] *= gmaxLen; displs[p] *= gmaxLen;}
5244*799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5245*799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5246*799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5247*799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5248*799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5249*799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l*gmaxLen], &glabels[gl]));
5250*799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l*gmaxLen], rank);
5251*799db056SMatthew G. Knepley     for (m = 0; m < gl; ++m) if (glabels[m] == glabels[gl]) continue;
52529566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5253*799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
52549566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5255*799db056SMatthew G. Knepley     ++gl;
5256783e2ec8SMatthew G. Knepley   }
5257*799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
5258783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5259783e2ec8SMatthew G. Knepley }
5260783e2ec8SMatthew G. Knepley 
5261e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5262e5e52638SMatthew G. Knepley {
5263e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5264e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5265e5e52638SMatthew G. Knepley 
5266e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5267e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
52689566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5269e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5270b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
52719566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5272e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5273e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5274e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5275e5e52638SMatthew G. Knepley }
5276e5e52638SMatthew G. Knepley 
5277e5e52638SMatthew G. Knepley /*@
5278e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5279e5e52638SMatthew G. Knepley 
5280e5e52638SMatthew G. Knepley   Not collective
5281e5e52638SMatthew G. Knepley 
5282e5e52638SMatthew G. Knepley   Input Parameter:
5283e5e52638SMatthew G. Knepley . dm - The DM
5284e5e52638SMatthew G. Knepley 
5285e5e52638SMatthew G. Knepley   Output Parameter:
5286e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5287e5e52638SMatthew G. Knepley 
5288e5e52638SMatthew G. Knepley   Level: intermediate
5289e5e52638SMatthew G. Knepley 
5290db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMGetCellDS()`
5291e5e52638SMatthew G. Knepley @*/
5292e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5293e5e52638SMatthew G. Knepley {
5294e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5295e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5296534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5297e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5298e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5299e5e52638SMatthew G. Knepley }
5300e5e52638SMatthew G. Knepley 
5301e5e52638SMatthew G. Knepley /*@
5302e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5303e5e52638SMatthew G. Knepley 
5304d083f849SBarry Smith   Logically collective on dm
5305e5e52638SMatthew G. Knepley 
5306e5e52638SMatthew G. Knepley   Input Parameter:
5307e5e52638SMatthew G. Knepley . dm - The DM
5308e5e52638SMatthew G. Knepley 
5309e5e52638SMatthew G. Knepley   Level: intermediate
5310e5e52638SMatthew G. Knepley 
5311db781477SPatrick Sanan .seealso: `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5312e5e52638SMatthew G. Knepley @*/
5313e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5314e5e52638SMatthew G. Knepley {
5315e5e52638SMatthew G. Knepley   PetscInt       s;
5316e5e52638SMatthew G. Knepley 
5317e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5318e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5319e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
53209566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
53219566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
53229566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5323e5e52638SMatthew G. Knepley   }
53249566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5325e5e52638SMatthew G. Knepley   dm->probs = NULL;
5326e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5327e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5328e5e52638SMatthew G. Knepley }
5329e5e52638SMatthew G. Knepley 
5330e5e52638SMatthew G. Knepley /*@
5331e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5332e5e52638SMatthew G. Knepley 
5333e5e52638SMatthew G. Knepley   Not collective
5334e5e52638SMatthew G. Knepley 
5335e5e52638SMatthew G. Knepley   Input Parameter:
5336e5e52638SMatthew G. Knepley . dm    - The DM
5337e5e52638SMatthew G. Knepley 
5338e5e52638SMatthew G. Knepley   Output Parameter:
5339e5e52638SMatthew G. Knepley . prob - The default PetscDS
5340e5e52638SMatthew G. Knepley 
5341e5e52638SMatthew G. Knepley   Level: intermediate
5342e5e52638SMatthew G. Knepley 
5343db781477SPatrick Sanan .seealso: `DMGetCellDS()`, `DMGetRegionDS()`
5344e5e52638SMatthew G. Knepley @*/
5345e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5346e5e52638SMatthew G. Knepley {
5347e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5348e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5349e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5350b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5351b0143b4dSMatthew G. Knepley     PetscDS ds;
5352b0143b4dSMatthew G. Knepley 
53539566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
53549566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, NULL, ds));
53559566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5356b0143b4dSMatthew G. Knepley   }
5357b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5358e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5359e5e52638SMatthew G. Knepley }
5360e5e52638SMatthew G. Knepley 
5361e5e52638SMatthew G. Knepley /*@
5362e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5363e5e52638SMatthew G. Knepley 
5364e5e52638SMatthew G. Knepley   Not collective
5365e5e52638SMatthew G. Knepley 
5366e5e52638SMatthew G. Knepley   Input Parameters:
5367e5e52638SMatthew G. Knepley + dm    - The DM
5368e5e52638SMatthew G. Knepley - point - Cell for the DS
5369e5e52638SMatthew G. Knepley 
5370e5e52638SMatthew G. Knepley   Output Parameter:
5371e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5372e5e52638SMatthew G. Knepley 
5373e5e52638SMatthew G. Knepley   Level: developer
5374e5e52638SMatthew G. Knepley 
5375db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMSetRegionDS()`
5376e5e52638SMatthew G. Knepley @*/
5377e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5378e5e52638SMatthew G. Knepley {
5379e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5380e5e52638SMatthew G. Knepley   PetscInt       s;
5381e5e52638SMatthew G. Knepley 
5382e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5383e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5384e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
538563a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
5386e5e52638SMatthew G. Knepley   *prob = NULL;
5387e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5388e5e52638SMatthew G. Knepley     PetscInt val;
5389e5e52638SMatthew G. Knepley 
5390e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5391e5e52638SMatthew G. Knepley     else {
53929566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
5393e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5394e5e52638SMatthew G. Knepley     }
5395e5e52638SMatthew G. Knepley   }
5396e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5397e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5398e5e52638SMatthew G. Knepley }
5399e5e52638SMatthew G. Knepley 
5400e5e52638SMatthew G. Knepley /*@
5401e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5402e5e52638SMatthew G. Knepley 
5403e5e52638SMatthew G. Knepley   Not collective
5404e5e52638SMatthew G. Knepley 
5405e5e52638SMatthew G. Knepley   Input Parameters:
5406e5e52638SMatthew G. Knepley + dm    - The DM
5407e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5408e5e52638SMatthew G. Knepley 
5409b3cf3223SMatthew G. Knepley   Output Parameters:
5410b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5411b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5412e5e52638SMatthew G. Knepley 
5413154ca461SJed Brown   Note:
5414154ca461SJed Brown   If a non-NULL label is given, but there is no PetscDS on that specific label,
5415154ca461SJed Brown   the PetscDS for the full domain (if present) is returned. Returns with
5416154ca461SJed Brown   fields=NULL and prob=NULL if there is no PetscDS for the full domain.
5417e5e52638SMatthew G. Knepley 
5418e5e52638SMatthew G. Knepley   Level: advanced
5419e5e52638SMatthew G. Knepley 
5420db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5421e5e52638SMatthew G. Knepley @*/
5422b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5423e5e52638SMatthew G. Knepley {
5424e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5425e5e52638SMatthew G. Knepley 
5426e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5427e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5428e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5429b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5430b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5431e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5432154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5433b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5434b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5435154ca461SJed Brown       if (dm->probs[s].label) PetscFunctionReturn(0);
5436b3cf3223SMatthew G. Knepley     }
5437e5e52638SMatthew G. Knepley   }
54382df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5439e5e52638SMatthew G. Knepley }
5440e5e52638SMatthew G. Knepley 
5441e5e52638SMatthew G. Knepley /*@
5442083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5443083401c6SMatthew G. Knepley 
5444083401c6SMatthew G. Knepley   Collective on dm
5445083401c6SMatthew G. Knepley 
5446083401c6SMatthew G. Knepley   Input Parameters:
5447083401c6SMatthew G. Knepley + dm     - The DM
5448083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5449083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5450083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5451083401c6SMatthew G. Knepley 
5452083401c6SMatthew 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,
5453083401c6SMatthew G. Knepley   the fields argument is ignored.
5454083401c6SMatthew G. Knepley 
5455083401c6SMatthew G. Knepley   Level: advanced
5456083401c6SMatthew G. Knepley 
5457db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5458083401c6SMatthew G. Knepley @*/
5459083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5460083401c6SMatthew G. Knepley {
5461083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5462083401c6SMatthew G. Knepley 
5463083401c6SMatthew G. Knepley   PetscFunctionBegin;
5464083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5465083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5466064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5467083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5468083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
54699566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
5470083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5471083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5472083401c6SMatthew G. Knepley     }
5473083401c6SMatthew G. Knepley   }
54749566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds+1));
54759566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
54769566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) fields));
54779566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) ds));
5478083401c6SMatthew G. Knepley   if (!label) {
5479083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5480083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5481083401c6SMatthew G. Knepley     Nds = 0;
5482083401c6SMatthew G. Knepley   }
5483083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5484083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5485083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5486083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5487083401c6SMatthew G. Knepley }
5488083401c6SMatthew G. Knepley 
5489083401c6SMatthew G. Knepley /*@
5490e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5491e5e52638SMatthew G. Knepley 
5492e5e52638SMatthew G. Knepley   Not collective
5493e5e52638SMatthew G. Knepley 
5494e5e52638SMatthew G. Knepley   Input Parameters:
5495e5e52638SMatthew G. Knepley + dm  - The DM
5496e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5497e5e52638SMatthew G. Knepley 
5498e5e52638SMatthew G. Knepley   Output Parameters:
5499b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5500b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5501083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5502e5e52638SMatthew G. Knepley 
5503e5e52638SMatthew G. Knepley   Level: advanced
5504e5e52638SMatthew G. Knepley 
5505db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5506e5e52638SMatthew G. Knepley @*/
5507b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5508e5e52638SMatthew G. Knepley {
5509e5e52638SMatthew G. Knepley   PetscInt       Nds;
5510e5e52638SMatthew G. Knepley 
5511e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5512e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55139566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
551463a3b9bcSJacob 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);
5515e5e52638SMatthew G. Knepley   if (label) {
5516e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5517e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5518e5e52638SMatthew G. Knepley   }
5519b3cf3223SMatthew G. Knepley   if (fields) {
5520b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5521b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5522b3cf3223SMatthew G. Knepley   }
5523e5e52638SMatthew G. Knepley   if (ds) {
5524b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5525e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5526e5e52638SMatthew G. Knepley   }
5527e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5528e5e52638SMatthew G. Knepley }
5529e5e52638SMatthew G. Knepley 
5530e5e52638SMatthew G. Knepley /*@
5531083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5532e5e52638SMatthew G. Knepley 
5533083401c6SMatthew G. Knepley   Not collective
5534e5e52638SMatthew G. Knepley 
5535e5e52638SMatthew G. Knepley   Input Parameters:
5536e5e52638SMatthew G. Knepley + dm     - The DM
5537083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5538083401c6SMatthew G. Knepley . label  - The region label, or NULL
5539083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5540083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5541e5e52638SMatthew G. Knepley 
5542e5e52638SMatthew G. Knepley   Level: advanced
5543e5e52638SMatthew G. Knepley 
5544db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5545e5e52638SMatthew G. Knepley @*/
5546083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5547e5e52638SMatthew G. Knepley {
5548083401c6SMatthew G. Knepley   PetscInt       Nds;
5549e5e52638SMatthew G. Knepley 
5550e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5551e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5552083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
55539566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
555463a3b9bcSJacob 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);
55559566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
55569566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5557083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5558083401c6SMatthew G. Knepley   if (fields) {
5559083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
55609566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) fields));
55619566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5562083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5563e5e52638SMatthew G. Knepley   }
5564083401c6SMatthew G. Knepley   if (ds) {
5565083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
55669566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) ds));
55679566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5568083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5569083401c6SMatthew G. Knepley   }
5570e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5571e5e52638SMatthew G. Knepley }
5572e5e52638SMatthew G. Knepley 
5573e5e52638SMatthew G. Knepley /*@
55741d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
55751d3af9e0SMatthew G. Knepley 
55761d3af9e0SMatthew G. Knepley   Not collective
55771d3af9e0SMatthew G. Knepley 
55781d3af9e0SMatthew G. Knepley   Input Parameters:
55791d3af9e0SMatthew G. Knepley + dm  - The DM
55801d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
55811d3af9e0SMatthew G. Knepley 
55821d3af9e0SMatthew G. Knepley   Output Parameter:
55831d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
55841d3af9e0SMatthew G. Knepley 
55851d3af9e0SMatthew G. Knepley   Level: advanced
55861d3af9e0SMatthew G. Knepley 
5587db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
55881d3af9e0SMatthew G. Knepley @*/
55891d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
55901d3af9e0SMatthew G. Knepley {
55911d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
55921d3af9e0SMatthew G. Knepley 
55931d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
55941d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55951d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
5596dadcf809SJacob Faibussowitsch   PetscValidIntPointer(num, 3);
55979566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
55981d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
55991d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
56001d3af9e0SMatthew G. Knepley   else          *num = n;
56011d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
56021d3af9e0SMatthew G. Knepley }
56031d3af9e0SMatthew G. Knepley 
56042df84da0SMatthew G. Knepley /*@C
56052df84da0SMatthew G. Knepley   DMCreateFEDefault - Create a PetscFE based on the celltype for the mesh
56062df84da0SMatthew G. Knepley 
56072df84da0SMatthew G. Knepley   Not collective
56082df84da0SMatthew G. Knepley 
5609f1a722f8SMatthew G. Knepley   Input Parameters:
56102df84da0SMatthew G. Knepley + dm     - The DM
56112df84da0SMatthew G. Knepley . Nc     - The number of components for the field
56122df84da0SMatthew G. Knepley . prefix - The options prefix for the output PetscFE, or NULL
56132df84da0SMatthew G. Knepley - qorder - The quadrature order or PETSC_DETERMINE to use PetscSpace polynomial degree
56142df84da0SMatthew G. Knepley 
56152df84da0SMatthew G. Knepley   Output Parameter:
56162df84da0SMatthew G. Knepley . fem - The PetscFE
56172df84da0SMatthew G. Knepley 
56182df84da0SMatthew G. Knepley   Note: This is a convenience method that just calls PetscFECreateByCell() underneath.
56192df84da0SMatthew G. Knepley 
56202df84da0SMatthew G. Knepley   Level: intermediate
56212df84da0SMatthew G. Knepley 
5622db781477SPatrick Sanan .seealso: `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
56232df84da0SMatthew G. Knepley @*/
56242df84da0SMatthew G. Knepley PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
56252df84da0SMatthew G. Knepley {
56262df84da0SMatthew G. Knepley   DMPolytopeType ct;
56272df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
56282df84da0SMatthew G. Knepley 
56292df84da0SMatthew G. Knepley   PetscFunctionBegin;
56302df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56312df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
56322df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
56332df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
56342df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
56359566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
56369566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
56379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
56389566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
56392df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
56402df84da0SMatthew G. Knepley }
56412df84da0SMatthew G. Knepley 
56421d3af9e0SMatthew G. Knepley /*@
5643e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5644e5e52638SMatthew G. Knepley 
5645d083f849SBarry Smith   Collective on dm
5646e5e52638SMatthew G. Knepley 
5647e5e52638SMatthew G. Knepley   Input Parameter:
5648e5e52638SMatthew G. Knepley . dm - The DM
5649e5e52638SMatthew G. Knepley 
565045480ffeSMatthew G. Knepley   Options Database Keys:
565145480ffeSMatthew G. Knepley . -dm_petscds_view - View all the PetscDS objects in this DM
565245480ffeSMatthew G. Knepley 
5653e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5654e5e52638SMatthew G. Knepley 
5655e5e52638SMatthew G. Knepley   Level: intermediate
5656e5e52638SMatthew G. Knepley 
5657db781477SPatrick Sanan .seealso: `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5658e5e52638SMatthew G. Knepley @*/
5659e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5660e5e52638SMatthew G. Knepley {
5661e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5662083401c6SMatthew G. Knepley   PetscDS        dsDef;
5663083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5664f9244615SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5665f9244615SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE, flg;
5666e5e52638SMatthew G. Knepley 
5667e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5668e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5669e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
56709566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject) dm, &comm));
56719566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5672083401c6SMatthew G. Knepley   /* Determine how many regions we have */
56739566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5674083401c6SMatthew G. Knepley   Nl   = 0;
5675083401c6SMatthew G. Knepley   Ndef = 0;
5676083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5677083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5678083401c6SMatthew G. Knepley     PetscInt l;
5679083401c6SMatthew G. Knepley 
5680f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5681f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5682f918ec44SMatthew G. Knepley     {
5683f918ec44SMatthew G. Knepley       PetscClassId id;
5684f918ec44SMatthew G. Knepley 
56859566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5686f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5687f918ec44SMatthew G. Knepley         Ceed ceed;
5688f918ec44SMatthew G. Knepley 
56899566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
56909566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE) dm->fields[f].disc, ceed));
5691f918ec44SMatthew G. Knepley       }
5692f918ec44SMatthew G. Knepley     }
5693f918ec44SMatthew G. Knepley #endif
5694083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5695083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5696083401c6SMatthew G. Knepley     if (l < Nl) continue;
5697083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5698083401c6SMatthew G. Knepley   }
5699083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
57009566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
5701083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5702b3cf3223SMatthew G. Knepley     IS        fields;
5703b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5704b3cf3223SMatthew G. Knepley 
5705b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
57067a8be351SBarry Smith     PetscCheck(nf,comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
57079566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
5708b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
57099566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
57109566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_"));
57119566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
57129566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
571388f0c812SMatthew G. Knepley 
57149566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
57159566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef));
57169566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
57179566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
57182df9ee95SMatthew G. Knepley   }
57199566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
57209566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5721083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5722083401c6SMatthew G. Knepley   if (Ndef && Nl) {
57230122748bSMatthew G. Knepley     DM              plex;
5724083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5725083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5726083401c6SMatthew G. Knepley     PetscInt       *fields;
5727083401c6SMatthew G. Knepley     const PetscInt *cells;
5728083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
57290122748bSMatthew G. Knepley 
57309566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
57319566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
57329566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
57339566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
57345fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5735083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5736083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5737083401c6SMatthew G. Knepley       IS      pointIS;
5738083401c6SMatthew G. Knepley 
57399566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
57409566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
57419566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
57429566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5743083401c6SMatthew G. Knepley     }
57449566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5745083401c6SMatthew G. Knepley 
57469566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
57479566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
57489566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
57499566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
57509566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
57519566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
57529566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5753083401c6SMatthew G. Knepley 
57549566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
5755083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
57569566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
57579566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_"));
57589566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
57599566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5760083401c6SMatthew G. Knepley 
57619566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
57629566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef));
57639566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
57649566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
57659566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
57669566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
57679566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5768083401c6SMatthew G. Knepley   }
5769083401c6SMatthew G. Knepley   /* Create label DSes
5770083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5771083401c6SMatthew G. Knepley   */
5772083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5773083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5774083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5775083401c6SMatthew G. Knepley     PetscDS   ds;
5776083401c6SMatthew G. Knepley     IS        fields;
5777083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5778083401c6SMatthew G. Knepley 
57799566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
5780083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
57819566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
5782083401c6SMatthew G. Knepley     for (f = 0, nf  = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
57839566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
57849566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_"));
57859566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
57869566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
57879566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, label, fields, ds));
57889566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
57899566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5790083401c6SMatthew G. Knepley     {
5791083401c6SMatthew G. Knepley       DMPolytopeType ct;
5792083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
57935fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
57940122748bSMatthew G. Knepley 
57959566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5796665f567fSMatthew G. Knepley       if (lStart >= 0) {
57979566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5798412e9a14SMatthew G. Knepley         switch (ct) {
5799412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5800412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5801412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5802412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
58035fedec97SMatthew G. Knepley             isCohesiveLocal = PETSC_TRUE;break;
5804083401c6SMatthew G. Knepley           default: break;
5805412e9a14SMatthew G. Knepley         }
5806665f567fSMatthew G. Knepley       }
58079566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
58085fedec97SMatthew G. Knepley       for (f = 0, nf  = 0; f < Nf; ++f) {
58095fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
58105fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
58119566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
58129566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
58135fedec97SMatthew G. Knepley           }
58145fedec97SMatthew G. Knepley           ++nf;
58155fedec97SMatthew G. Knepley         }
58165fedec97SMatthew G. Knepley       }
5817e5e52638SMatthew G. Knepley     }
58189566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5819e5e52638SMatthew G. Knepley   }
58209566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5821e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5822083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5823083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5824083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5825083401c6SMatthew G. Knepley     const PetscInt *fld;
58265fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
58275fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5828e5e52638SMatthew G. Knepley 
58299566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
58309566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
58319566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
58329566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5833083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5834083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
58355fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5836e5e52638SMatthew G. Knepley       PetscClassId id;
5837e5e52638SMatthew G. Knepley 
58385fedec97SMatthew G. Knepley       /* Handle DS with no fields */
58399566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
58405fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
58419566063dSJacob Faibussowitsch       if (isCohesive && !isCohesiveField) PetscCall(PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc));
58429566063dSJacob Faibussowitsch       PetscCall(PetscDSSetDiscretization(ds, f, disc));
5843083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
58449566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5845e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5846e5e52638SMatthew G. Knepley     }
58479566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
5848e5e52638SMatthew G. Knepley   }
5849f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
58509566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject) dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
5851f9244615SMatthew G. Knepley   if (flg) {
58523b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
58533b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
58543b4aee56SMatthew G. Knepley       PetscInt Nf, f;
58553b4aee56SMatthew G. Knepley 
58569566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
58579566063dSJacob Faibussowitsch       for (f = 0; f < Nf; ++f) PetscCall(PetscDSSetJetDegree(ds, f, k));
58583b4aee56SMatthew G. Knepley     }
5859f9244615SMatthew G. Knepley   }
5860e5e52638SMatthew G. Knepley   /* Setup DSes */
5861e5e52638SMatthew G. Knepley   if (doSetup) {
58629566063dSJacob Faibussowitsch     for (s = 0; s < dm->Nds; ++s) PetscCall(PetscDSSetUp(dm->probs[s].ds));
5863e5e52638SMatthew G. Knepley   }
5864e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5865e5e52638SMatthew G. Knepley }
5866e5e52638SMatthew G. Knepley 
5867e5e52638SMatthew G. Knepley /*@
58687f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
58697f96f943SMatthew G. Knepley 
5870f2cacb80SMatthew G. Knepley   Collective on DM
5871f2cacb80SMatthew G. Knepley 
58727f96f943SMatthew G. Knepley   Input Parameters:
58737f96f943SMatthew G. Knepley + dm   - The DM
58747f96f943SMatthew G. Knepley - time - The time
58757f96f943SMatthew G. Knepley 
58767f96f943SMatthew G. Knepley   Output Parameters:
5877f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5878f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
58797f96f943SMatthew G. Knepley 
58807f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
58817f96f943SMatthew G. Knepley 
58827f96f943SMatthew G. Knepley   Level: developer
58837f96f943SMatthew G. Knepley 
5884db781477SPatrick Sanan .seealso: `PetscDSSetExactSolution()`
58857f96f943SMatthew G. Knepley @*/
5886f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
58877f96f943SMatthew G. Knepley {
58887f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
58897f96f943SMatthew G. Knepley   void            **ectxs;
58907f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
58917f96f943SMatthew G. Knepley 
58927f96f943SMatthew G. Knepley   PetscFunctionBegin;
5893f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5894f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5895f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
58969566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
58979566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
58989566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
58997f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
59007f96f943SMatthew G. Knepley     PetscDS         ds;
59017f96f943SMatthew G. Knepley     DMLabel         label;
59027f96f943SMatthew G. Knepley     IS              fieldIS;
59037f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
59047f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
59057f96f943SMatthew G. Knepley 
59069566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
59079566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
59089566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
59099566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
59109566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
5911f2cacb80SMatthew G. Knepley     if (u) {
59127f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
59137f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
59149566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]));
59157f96f943SMatthew G. Knepley       }
59169566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
59177f96f943SMatthew G. Knepley       if (label) {
59189566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u));
59197f96f943SMatthew G. Knepley       } else {
59209566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u));
59217f96f943SMatthew G. Knepley       }
59227f96f943SMatthew G. Knepley     }
5923f2cacb80SMatthew G. Knepley     if (u_t) {
59249566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
59259566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
5926f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5927f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
59289566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]));
5929f2cacb80SMatthew G. Knepley       }
59309566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
5931f2cacb80SMatthew G. Knepley       if (label) {
59329566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t));
5933f2cacb80SMatthew G. Knepley       } else {
59349566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t));
5935f2cacb80SMatthew G. Knepley       }
5936f2cacb80SMatthew G. Knepley     }
5937f2cacb80SMatthew G. Knepley   }
5938f2cacb80SMatthew G. Knepley   if (u) {
59399566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) u, "Exact Solution"));
59409566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) u, "exact_"));
5941f2cacb80SMatthew G. Knepley   }
5942f2cacb80SMatthew G. Knepley   if (u_t) {
59439566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative"));
59449566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_"));
5945f2cacb80SMatthew G. Knepley   }
59469566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
59477f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
59487f96f943SMatthew G. Knepley }
59497f96f943SMatthew G. Knepley 
595045480ffeSMatthew G. Knepley PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
595145480ffeSMatthew G. Knepley {
595245480ffeSMatthew G. Knepley   PetscDS        dsNew;
595345480ffeSMatthew G. Knepley   DSBoundary     b;
59546a02485aSMatthew G. Knepley   PetscInt       cdim, Nf, f, d;
59555fedec97SMatthew G. Knepley   PetscBool      isCohesive;
595645480ffeSMatthew G. Knepley   void          *ctx;
595745480ffeSMatthew G. Knepley 
595845480ffeSMatthew G. Knepley   PetscFunctionBegin;
59599566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject) ds), &dsNew));
59609566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyConstants(ds, dsNew));
59619566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyExactSolutions(ds, dsNew));
59629566063dSJacob Faibussowitsch   PetscCall(PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew));
59639566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyEquations(ds, dsNew));
59649566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
596545480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
59669566063dSJacob Faibussowitsch     PetscCall(PetscDSGetContext(ds, f, &ctx));
59679566063dSJacob Faibussowitsch     PetscCall(PetscDSSetContext(dsNew, f, ctx));
59689566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCohesive(ds, f, &isCohesive));
59699566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCohesive(dsNew, f, isCohesive));
59706a02485aSMatthew G. Knepley     PetscCall(PetscDSGetJetDegree(ds, f, &d));
59716a02485aSMatthew G. Knepley     PetscCall(PetscDSSetJetDegree(dsNew, f, d));
597245480ffeSMatthew G. Knepley   }
597345480ffeSMatthew G. Knepley   if (Nf) {
59749566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCoordinateDimension(ds, &cdim));
59759566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsNew, cdim));
597645480ffeSMatthew G. Knepley   }
59779566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew));
597845480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
59799566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, b->lname, &b->label));
598045480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
59817a8be351SBarry Smith     //PetscCheck(b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
598245480ffeSMatthew G. Knepley   }
598345480ffeSMatthew G. Knepley 
59849566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(dm, label, fields, dsNew));
59859566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
598645480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
598745480ffeSMatthew G. Knepley }
598845480ffeSMatthew G. Knepley 
59897f96f943SMatthew G. Knepley /*@
5990e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5991e5e52638SMatthew G. Knepley 
5992d083f849SBarry Smith   Collective on dm
5993e5e52638SMatthew G. Knepley 
5994e5e52638SMatthew G. Knepley   Input Parameter:
5995e5e52638SMatthew G. Knepley . dm - The DM
5996e5e52638SMatthew G. Knepley 
5997e5e52638SMatthew G. Knepley   Output Parameter:
5998e5e52638SMatthew G. Knepley . newdm - The DM
5999e5e52638SMatthew G. Knepley 
6000e5e52638SMatthew G. Knepley   Level: advanced
6001e5e52638SMatthew G. Knepley 
6002db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6003e5e52638SMatthew G. Knepley @*/
6004e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
6005e5e52638SMatthew G. Knepley {
6006e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6007e5e52638SMatthew G. Knepley 
6008e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6009e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
60109566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
60119566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6012e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6013e5e52638SMatthew G. Knepley     DMLabel  label;
6014b3cf3223SMatthew G. Knepley     IS       fields;
601545480ffeSMatthew G. Knepley     PetscDS  ds, newds;
6016783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6017e5e52638SMatthew G. Knepley 
60189566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds));
6019b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
60209566063dSJacob Faibussowitsch     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds));
602145480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
60229566063dSJacob Faibussowitsch     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds));
60239566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6024783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6025b8025e53SMatthew G. Knepley       PetscWeakForm wf;
602645480ffeSMatthew G. Knepley       DMLabel       label;
6027783e2ec8SMatthew G. Knepley       PetscInt      field;
6028783e2ec8SMatthew G. Knepley 
60299566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
60309566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6031783e2ec8SMatthew G. Knepley     }
6032e5e52638SMatthew G. Knepley   }
6033*799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
6034e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6035e5e52638SMatthew G. Knepley }
6036e5e52638SMatthew G. Knepley 
6037e5e52638SMatthew G. Knepley /*@
6038e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
6039e5e52638SMatthew G. Knepley 
6040d083f849SBarry Smith   Collective on dm
6041e5e52638SMatthew G. Knepley 
6042e5e52638SMatthew G. Knepley   Input Parameter:
6043e5e52638SMatthew G. Knepley . dm - The DM
6044e5e52638SMatthew G. Knepley 
6045e5e52638SMatthew G. Knepley   Output Parameter:
6046e5e52638SMatthew G. Knepley . newdm - The DM
6047e5e52638SMatthew G. Knepley 
6048e5e52638SMatthew G. Knepley   Level: advanced
6049e5e52638SMatthew G. Knepley 
6050db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMCopyDS()`
6051e5e52638SMatthew G. Knepley @*/
6052e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6053e5e52638SMatthew G. Knepley {
6054e5e52638SMatthew G. Knepley   PetscFunctionBegin;
60559566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
60569566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
6057e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6058e5e52638SMatthew G. Knepley }
6059e5e52638SMatthew G. Knepley 
6060b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
6061b64e0483SPeter Brune {
6062b64e0483SPeter Brune   DM dm_coord,dmc_coord;
6063b64e0483SPeter Brune   Vec coords,ccoords;
60646dbf9973SLawrence Mitchell   Mat inject;
6065b64e0483SPeter Brune   PetscFunctionBegin;
60669566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm,&dm_coord));
60679566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmc,&dmc_coord));
60689566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinates(dm,&coords));
60699566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinates(dmc,&ccoords));
6070b64e0483SPeter Brune   if (coords && !ccoords) {
60719566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(dmc_coord,&ccoords));
60729566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)ccoords,"coordinates"));
60739566063dSJacob Faibussowitsch     PetscCall(DMCreateInjection(dmc_coord,dm_coord,&inject));
60749566063dSJacob Faibussowitsch     PetscCall(MatRestrict(inject,coords,ccoords));
60759566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&inject));
60769566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinates(dmc,ccoords));
60779566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ccoords));
6078b64e0483SPeter Brune   }
6079b64e0483SPeter Brune   PetscFunctionReturn(0);
6080b64e0483SPeter Brune }
6081b64e0483SPeter Brune 
608203dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
608303dadc2fSPeter Brune {
608403dadc2fSPeter Brune   DM dm_coord,subdm_coord;
608503dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
608603dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
608703dadc2fSPeter Brune   PetscFunctionBegin;
60889566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm,&dm_coord));
60899566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(subdm,&subdm_coord));
60909566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinates(dm,&coords));
60919566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinates(subdm,&ccoords));
609203dadc2fSPeter Brune   if (coords && !ccoords) {
60939566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(subdm_coord,&ccoords));
60949566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)ccoords,"coordinates"));
60959566063dSJacob Faibussowitsch     PetscCall(DMCreateLocalVector(subdm_coord,&clcoords));
60969566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)clcoords,"coordinates"));
60979566063dSJacob Faibussowitsch     PetscCall(DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g));
60989566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD));
60999566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD));
61009566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD));
61019566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD));
61029566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinates(subdm,ccoords));
61039566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm,clcoords));
61049566063dSJacob Faibussowitsch     PetscCall(VecScatterDestroy(&scat_i[0]));
61059566063dSJacob Faibussowitsch     PetscCall(VecScatterDestroy(&scat_g[0]));
61069566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ccoords));
61079566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&clcoords));
61089566063dSJacob Faibussowitsch     PetscCall(PetscFree(scat_i));
61099566063dSJacob Faibussowitsch     PetscCall(PetscFree(scat_g));
611003dadc2fSPeter Brune   }
611103dadc2fSPeter Brune   PetscFunctionReturn(0);
611203dadc2fSPeter Brune }
611303dadc2fSPeter Brune 
6114c73cfb54SMatthew G. Knepley /*@
6115c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
6116c73cfb54SMatthew G. Knepley 
6117c73cfb54SMatthew G. Knepley   Not collective
6118c73cfb54SMatthew G. Knepley 
6119c73cfb54SMatthew G. Knepley   Input Parameter:
6120c73cfb54SMatthew G. Knepley . dm - The DM
6121c73cfb54SMatthew G. Knepley 
6122c73cfb54SMatthew G. Knepley   Output Parameter:
6123c73cfb54SMatthew G. Knepley . dim - The topological dimension
6124c73cfb54SMatthew G. Knepley 
6125c73cfb54SMatthew G. Knepley   Level: beginner
6126c73cfb54SMatthew G. Knepley 
6127db781477SPatrick Sanan .seealso: `DMSetDimension()`, `DMCreate()`
6128c73cfb54SMatthew G. Knepley @*/
6129c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6130c73cfb54SMatthew G. Knepley {
6131c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6132c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6133534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6134c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6135c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6136c73cfb54SMatthew G. Knepley }
6137c73cfb54SMatthew G. Knepley 
6138c73cfb54SMatthew G. Knepley /*@
6139c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
6140c73cfb54SMatthew G. Knepley 
6141c73cfb54SMatthew G. Knepley   Collective on dm
6142c73cfb54SMatthew G. Knepley 
6143c73cfb54SMatthew G. Knepley   Input Parameters:
6144c73cfb54SMatthew G. Knepley + dm - The DM
6145c73cfb54SMatthew G. Knepley - dim - The topological dimension
6146c73cfb54SMatthew G. Knepley 
6147c73cfb54SMatthew G. Knepley   Level: beginner
6148c73cfb54SMatthew G. Knepley 
6149db781477SPatrick Sanan .seealso: `DMGetDimension()`, `DMCreate()`
6150c73cfb54SMatthew G. Knepley @*/
6151c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6152c73cfb54SMatthew G. Knepley {
6153e5e52638SMatthew G. Knepley   PetscDS        ds;
615445480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6155f17e8794SMatthew G. Knepley 
6156c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6157c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6158c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6159c73cfb54SMatthew G. Knepley   dm->dim = dim;
6160d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
61619566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
616245480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
61639566063dSJacob Faibussowitsch       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
61649566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
616545480ffeSMatthew G. Knepley     }
6166d17bd122SMatthew G. Knepley   }
6167c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6168c73cfb54SMatthew G. Knepley }
6169c73cfb54SMatthew G. Knepley 
6170793f3fe5SMatthew G. Knepley /*@
6171793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6172793f3fe5SMatthew G. Knepley 
6173d083f849SBarry Smith   Collective on dm
6174793f3fe5SMatthew G. Knepley 
6175793f3fe5SMatthew G. Knepley   Input Parameters:
6176793f3fe5SMatthew G. Knepley + dm - the DM
6177793f3fe5SMatthew G. Knepley - dim - the dimension
6178793f3fe5SMatthew G. Knepley 
6179793f3fe5SMatthew G. Knepley   Output Parameters:
6180793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6181aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6182793f3fe5SMatthew G. Knepley 
6183793f3fe5SMatthew G. Knepley   Note:
6184793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6185a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6186793f3fe5SMatthew G. Knepley   then the interval is empty.
6187793f3fe5SMatthew G. Knepley 
6188793f3fe5SMatthew G. Knepley   Level: intermediate
6189793f3fe5SMatthew G. Knepley 
6190db781477SPatrick Sanan .seealso: `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6191793f3fe5SMatthew G. Knepley @*/
6192793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6193793f3fe5SMatthew G. Knepley {
6194793f3fe5SMatthew G. Knepley   PetscInt       d;
6195793f3fe5SMatthew G. Knepley 
6196793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6197793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61989566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
61997a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
62007a8be351SBarry Smith   PetscCheck(dm->ops->getdimpoints,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
62019566063dSJacob Faibussowitsch   PetscCall((*dm->ops->getdimpoints)(dm, dim, pStart, pEnd));
6202793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6203793f3fe5SMatthew G. Knepley }
6204793f3fe5SMatthew G. Knepley 
62056636e97aSMatthew G Knepley /*@
62066636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
62076636e97aSMatthew G Knepley 
6208d083f849SBarry Smith   Collective on dm
62096636e97aSMatthew G Knepley 
62106636e97aSMatthew G Knepley   Input Parameters:
62116636e97aSMatthew G Knepley + dm - the DM
62126636e97aSMatthew G Knepley - c - coordinate vector
62136636e97aSMatthew G Knepley 
62142dd40e9bSPatrick Sanan   Notes:
62152dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
62162dd40e9bSPatrick Sanan 
62172dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
62186636e97aSMatthew G Knepley 
62196636e97aSMatthew G Knepley   Level: intermediate
62206636e97aSMatthew G Knepley 
6221db781477SPatrick Sanan .seealso: `DMSetCoordinatesLocal()`, `DMGetCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCoordinateDM()`, `DMDASetUniformCoordinates()`
62226636e97aSMatthew G Knepley @*/
62236636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
62246636e97aSMatthew G Knepley {
62256636e97aSMatthew G Knepley   PetscFunctionBegin;
62266636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
622710f43869SStefano Zampini   if (c) PetscValidHeaderSpecific(c,VEC_CLASSID,2);
62289566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) c));
62299566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->coordinates));
62306636e97aSMatthew G Knepley   dm->coordinates = c;
62319566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->coordinatesLocal));
62329566063dSJacob Faibussowitsch   PetscCall(DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL));
62339566063dSJacob Faibussowitsch   PetscCall(DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL));
62346636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62356636e97aSMatthew G Knepley }
62366636e97aSMatthew G Knepley 
62376636e97aSMatthew G Knepley /*@
62386636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
62396636e97aSMatthew G Knepley 
62407058e716SVaclav Hapla   Not collective
62416636e97aSMatthew G Knepley 
62426636e97aSMatthew G Knepley    Input Parameters:
62436636e97aSMatthew G Knepley +  dm - the DM
62446636e97aSMatthew G Knepley -  c - coordinate vector
62456636e97aSMatthew G Knepley 
62462dd40e9bSPatrick Sanan   Notes:
62476636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
62486636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
62496636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
62506636e97aSMatthew G Knepley 
62512dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
62522dd40e9bSPatrick Sanan 
62536636e97aSMatthew G Knepley   Level: intermediate
62546636e97aSMatthew G Knepley 
6255db781477SPatrick Sanan .seealso: `DMGetCoordinatesLocal()`, `DMSetCoordinates()`, `DMGetCoordinates()`, `DMGetCoordinateDM()`
62566636e97aSMatthew G Knepley @*/
62576636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
62586636e97aSMatthew G Knepley {
62596636e97aSMatthew G Knepley   PetscFunctionBegin;
62606636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6261b7f5c055SJed Brown   if (c) PetscValidHeaderSpecific(c,VEC_CLASSID,2);
62629566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) c));
62639566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->coordinatesLocal));
62648865f1eaSKarl Rupp 
62656636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
62668865f1eaSKarl Rupp 
62679566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->coordinates));
62686636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62696636e97aSMatthew G Knepley }
62706636e97aSMatthew G Knepley 
62716636e97aSMatthew G Knepley /*@
62726636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
62736636e97aSMatthew G Knepley 
6274d083f849SBarry Smith   Collective on dm
62756636e97aSMatthew G Knepley 
62766636e97aSMatthew G Knepley   Input Parameter:
62776636e97aSMatthew G Knepley . dm - the DM
62786636e97aSMatthew G Knepley 
62796636e97aSMatthew G Knepley   Output Parameter:
62806636e97aSMatthew G Knepley . c - global coordinate vector
62816636e97aSMatthew G Knepley 
62826636e97aSMatthew G Knepley   Note:
628360c22052SBarry Smith   This is a borrowed reference, so the user should NOT destroy this vector. When the DM is
628460c22052SBarry Smith   destroyed the array will no longer be valid.
62856636e97aSMatthew G Knepley 
6286959c7569SPatrick Sanan   Each process has only the locally-owned portion of the global coordinates (does NOT have the ghost coordinates).
62876636e97aSMatthew G Knepley 
62886636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
62896636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
62906636e97aSMatthew G Knepley 
62916636e97aSMatthew G Knepley   Level: intermediate
62926636e97aSMatthew G Knepley 
6293db781477SPatrick Sanan .seealso: `DMSetCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCoordinateDM()`, `DMDASetUniformCoordinates()`
62946636e97aSMatthew G Knepley @*/
62956636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
62966636e97aSMatthew G Knepley {
62976636e97aSMatthew G Knepley   PetscFunctionBegin;
62986636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62996636e97aSMatthew G Knepley   PetscValidPointer(c,2);
63001f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
63010298fd71SBarry Smith     DM        cdm = NULL;
63021970a576SMatthew G. Knepley     PetscBool localized;
63036636e97aSMatthew G Knepley 
63049566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
63059566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(cdm, &dm->coordinates));
63069566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocalized(dm, &localized));
63071970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
63081970a576SMatthew G. Knepley     if (localized) {
63091970a576SMatthew G. Knepley       PetscInt cdim;
63101970a576SMatthew G. Knepley 
63119566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateDim(dm, &cdim));
63129566063dSJacob Faibussowitsch       PetscCall(VecSetBlockSize(dm->coordinates, cdim));
63131970a576SMatthew G. Knepley     }
63149566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) dm->coordinates, "coordinates"));
63159566063dSJacob Faibussowitsch     PetscCall(DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates));
63169566063dSJacob Faibussowitsch     PetscCall(DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates));
63176636e97aSMatthew G Knepley   }
63186636e97aSMatthew G Knepley   *c = dm->coordinates;
63196636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63206636e97aSMatthew G Knepley }
63216636e97aSMatthew G Knepley 
63226636e97aSMatthew G Knepley /*@
632381e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
632481e9a530SVaclav Hapla 
6325d083f849SBarry Smith   Collective on dm
632681e9a530SVaclav Hapla 
632781e9a530SVaclav Hapla   Input Parameter:
632881e9a530SVaclav Hapla . dm - the DM
632981e9a530SVaclav Hapla 
633081e9a530SVaclav Hapla   Level: advanced
633181e9a530SVaclav Hapla 
6332db781477SPatrick Sanan .seealso: `DMGetCoordinatesLocalNoncollective()`
633381e9a530SVaclav Hapla @*/
633481e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
633581e9a530SVaclav Hapla {
633681e9a530SVaclav Hapla   PetscFunctionBegin;
633781e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
633881e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
63391970a576SMatthew G. Knepley     DM        cdm = NULL;
63401970a576SMatthew G. Knepley     PetscBool localized;
63411970a576SMatthew G. Knepley 
63429566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
63439566063dSJacob Faibussowitsch     PetscCall(DMCreateLocalVector(cdm, &dm->coordinatesLocal));
63449566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocalized(dm, &localized));
63451970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
63461970a576SMatthew G. Knepley     if (localized) {
63471970a576SMatthew G. Knepley       PetscInt cdim;
63481970a576SMatthew G. Knepley 
63499566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateDim(dm, &cdim));
63509566063dSJacob Faibussowitsch       PetscCall(VecSetBlockSize(dm->coordinates, cdim));
63511970a576SMatthew G. Knepley     }
63529566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates"));
63539566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal));
63549566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal));
635581e9a530SVaclav Hapla   }
635681e9a530SVaclav Hapla   PetscFunctionReturn(0);
635781e9a530SVaclav Hapla }
635881e9a530SVaclav Hapla 
635981e9a530SVaclav Hapla /*@
63606636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
63616636e97aSMatthew G Knepley 
6362d083f849SBarry Smith   Collective on dm
63636636e97aSMatthew G Knepley 
63646636e97aSMatthew G Knepley   Input Parameter:
63656636e97aSMatthew G Knepley . dm - the DM
63666636e97aSMatthew G Knepley 
63676636e97aSMatthew G Knepley   Output Parameter:
63686636e97aSMatthew G Knepley . c - coordinate vector
63696636e97aSMatthew G Knepley 
63706636e97aSMatthew G Knepley   Note:
63716636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
63726636e97aSMatthew G Knepley 
63736636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
63746636e97aSMatthew G Knepley 
63756636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
63766636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
63776636e97aSMatthew G Knepley 
63786636e97aSMatthew G Knepley   Level: intermediate
63796636e97aSMatthew G Knepley 
6380db781477SPatrick Sanan .seealso: `DMSetCoordinatesLocal()`, `DMGetCoordinates()`, `DMSetCoordinates()`, `DMGetCoordinateDM()`, `DMGetCoordinatesLocalNoncollective()`
63816636e97aSMatthew G Knepley @*/
63826636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
63836636e97aSMatthew G Knepley {
63846636e97aSMatthew G Knepley   PetscFunctionBegin;
63856636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63866636e97aSMatthew G Knepley   PetscValidPointer(c,2);
63879566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocalSetUp(dm));
63886636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
63896636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63906636e97aSMatthew G Knepley }
63916636e97aSMatthew G Knepley 
639281e9a530SVaclav Hapla /*@
639381e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
639481e9a530SVaclav Hapla 
639581e9a530SVaclav Hapla   Not collective
639681e9a530SVaclav Hapla 
639781e9a530SVaclav Hapla   Input Parameter:
639881e9a530SVaclav Hapla . dm - the DM
639981e9a530SVaclav Hapla 
640081e9a530SVaclav Hapla   Output Parameter:
640181e9a530SVaclav Hapla . c - coordinate vector
640281e9a530SVaclav Hapla 
640381e9a530SVaclav Hapla   Level: advanced
640481e9a530SVaclav Hapla 
6405db781477SPatrick Sanan .seealso: `DMGetCoordinatesLocalSetUp()`, `DMGetCoordinatesLocal()`, `DMSetCoordinatesLocal()`, `DMGetCoordinates()`, `DMSetCoordinates()`, `DMGetCoordinateDM()`
640681e9a530SVaclav Hapla @*/
640781e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
640881e9a530SVaclav Hapla {
640981e9a530SVaclav Hapla   PetscFunctionBegin;
641081e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
641181e9a530SVaclav Hapla   PetscValidPointer(c,2);
64127a8be351SBarry Smith   PetscCheck(dm->coordinatesLocal || !dm->coordinates,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
64136636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
64146636e97aSMatthew G Knepley   PetscFunctionReturn(0);
64156636e97aSMatthew G Knepley }
64166636e97aSMatthew G Knepley 
64172db98f8dSVaclav Hapla /*@
64182db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
64192db98f8dSVaclav Hapla 
64202db98f8dSVaclav Hapla   Not collective
64212db98f8dSVaclav Hapla 
6422d8d19677SJose E. Roman   Input Parameters:
64232db98f8dSVaclav Hapla + dm - the DM
64242db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
64252db98f8dSVaclav Hapla 
6426d8d19677SJose E. Roman   Output Parameters:
64272db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
64282db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
64292db98f8dSVaclav Hapla 
64302db98f8dSVaclav Hapla   Note:
64312db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
64322db98f8dSVaclav Hapla 
64332db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
64342db98f8dSVaclav Hapla 
64352db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
64362db98f8dSVaclav Hapla 
64372db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
64382db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
64392db98f8dSVaclav Hapla 
64402db98f8dSVaclav Hapla   Level: advanced
64412db98f8dSVaclav Hapla 
6442db781477SPatrick Sanan .seealso: `DMSetCoordinatesLocal()`, `DMGetCoordinatesLocal()`, `DMGetCoordinatesLocalNoncollective()`, `DMGetCoordinatesLocalSetUp()`, `DMGetCoordinates()`, `DMSetCoordinates()`, `DMGetCoordinateDM()`
64432db98f8dSVaclav Hapla @*/
64442db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
64452db98f8dSVaclav Hapla {
64462db98f8dSVaclav Hapla   PetscSection        cs, newcs;
64472db98f8dSVaclav Hapla   Vec                 coords;
64482db98f8dSVaclav Hapla   const PetscScalar   *arr;
64492db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
64502db98f8dSVaclav Hapla   PetscInt            n;
64512db98f8dSVaclav Hapla 
64522db98f8dSVaclav Hapla   PetscFunctionBegin;
64532db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64542db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
64552db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
64562db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
64577a8be351SBarry Smith   PetscCheck(dm->coordinatesLocal,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
64587a8be351SBarry Smith   PetscCheck(dm->coordinateDM && dm->coordinateDM->localSection,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
64591bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
64602db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
64619566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coords, &arr));
64629566063dSJacob Faibussowitsch   PetscCall(PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL));
64639566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coords, &arr));
64642db98f8dSVaclav Hapla   if (pCoord) {
64659566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(newcs, &n));
64662db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
64679566063dSJacob Faibussowitsch     PetscCall(VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord));
64689566063dSJacob Faibussowitsch     PetscCall(VecReplaceArray(*pCoord, newarr));
6469ad9ac99dSVaclav Hapla   } else {
64709566063dSJacob Faibussowitsch     PetscCall(PetscFree(newarr));
64712db98f8dSVaclav Hapla   }
6472ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
64739566063dSJacob Faibussowitsch   else               PetscCall(PetscSectionDestroy(&newcs));
64742db98f8dSVaclav Hapla   PetscFunctionReturn(0);
64752db98f8dSVaclav Hapla }
64762db98f8dSVaclav Hapla 
6477f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6478f19dbd58SToby Isaac {
6479f19dbd58SToby Isaac   PetscFunctionBegin;
6480f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6481f19dbd58SToby Isaac   PetscValidPointer(field,2);
6482f19dbd58SToby Isaac   if (!dm->coordinateField) {
6483f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
64849566063dSJacob Faibussowitsch       PetscCall((*dm->ops->createcoordinatefield)(dm,&dm->coordinateField));
6485f19dbd58SToby Isaac     }
6486f19dbd58SToby Isaac   }
6487f19dbd58SToby Isaac   *field = dm->coordinateField;
6488f19dbd58SToby Isaac   PetscFunctionReturn(0);
6489f19dbd58SToby Isaac }
6490f19dbd58SToby Isaac 
6491f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6492f19dbd58SToby Isaac {
6493f19dbd58SToby Isaac   PetscFunctionBegin;
6494f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6495f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
64969566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)field));
64979566063dSJacob Faibussowitsch   PetscCall(DMFieldDestroy(&dm->coordinateField));
6498f19dbd58SToby Isaac   dm->coordinateField = field;
6499f19dbd58SToby Isaac   PetscFunctionReturn(0);
6500f19dbd58SToby Isaac }
6501f19dbd58SToby Isaac 
65026636e97aSMatthew G Knepley /*@
65031cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
65046636e97aSMatthew G Knepley 
6505d083f849SBarry Smith   Collective on dm
65066636e97aSMatthew G Knepley 
65076636e97aSMatthew G Knepley   Input Parameter:
65086636e97aSMatthew G Knepley . dm - the DM
65096636e97aSMatthew G Knepley 
65106636e97aSMatthew G Knepley   Output Parameter:
65116636e97aSMatthew G Knepley . cdm - coordinate DM
65126636e97aSMatthew G Knepley 
65136636e97aSMatthew G Knepley   Level: intermediate
65146636e97aSMatthew G Knepley 
6515db781477SPatrick Sanan .seealso: `DMSetCoordinateDM()`, `DMSetCoordinates()`, `DMSetCoordinatesLocal()`, `DMGetCoordinates()`, `DMGetCoordinatesLocal()`
65166636e97aSMatthew G Knepley @*/
65176636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
65186636e97aSMatthew G Knepley {
65196636e97aSMatthew G Knepley   PetscFunctionBegin;
65206636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65216636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
65226636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6523308f8a94SToby Isaac     DM cdm;
6524308f8a94SToby Isaac 
65257a8be351SBarry Smith     PetscCheck(dm->ops->createcoordinatedm,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
65269566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createcoordinatedm)(dm, &cdm));
65279566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)cdm, "coordinateDM"));
6528308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6529308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
65309566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&dm->coordinateDM));
6531308f8a94SToby Isaac     dm->coordinateDM = cdm;
65326636e97aSMatthew G Knepley   }
65336636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
65346636e97aSMatthew G Knepley   PetscFunctionReturn(0);
65356636e97aSMatthew G Knepley }
6536e87bb0d3SMatthew G Knepley 
65371cfe2091SMatthew G. Knepley /*@
65381cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
65391cfe2091SMatthew G. Knepley 
6540d083f849SBarry Smith   Logically Collective on dm
65411cfe2091SMatthew G. Knepley 
65421cfe2091SMatthew G. Knepley   Input Parameters:
65431cfe2091SMatthew G. Knepley + dm - the DM
65441cfe2091SMatthew G. Knepley - cdm - coordinate DM
65451cfe2091SMatthew G. Knepley 
65461cfe2091SMatthew G. Knepley   Level: intermediate
65471cfe2091SMatthew G. Knepley 
6548db781477SPatrick Sanan .seealso: `DMGetCoordinateDM()`, `DMSetCoordinates()`, `DMSetCoordinatesLocal()`, `DMGetCoordinates()`, `DMGetCoordinatesLocal()`
65491cfe2091SMatthew G. Knepley @*/
65501cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
65511cfe2091SMatthew G. Knepley {
65521cfe2091SMatthew G. Knepley   PetscFunctionBegin;
65531cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65541cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
65559566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
65569566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coordinateDM));
65571cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
65581cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
65591cfe2091SMatthew G. Knepley }
65601cfe2091SMatthew G. Knepley 
656146e270d4SMatthew G. Knepley /*@
656246e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
656346e270d4SMatthew G. Knepley 
656446e270d4SMatthew G. Knepley   Not Collective
656546e270d4SMatthew G. Knepley 
656646e270d4SMatthew G. Knepley   Input Parameter:
656746e270d4SMatthew G. Knepley . dm - The DM object
656846e270d4SMatthew G. Knepley 
656946e270d4SMatthew G. Knepley   Output Parameter:
657046e270d4SMatthew G. Knepley . dim - The embedding dimension
657146e270d4SMatthew G. Knepley 
657246e270d4SMatthew G. Knepley   Level: intermediate
657346e270d4SMatthew G. Knepley 
6574db781477SPatrick Sanan .seealso: `DMSetCoordinateDim()`, `DMGetCoordinateSection()`, `DMGetCoordinateDM()`, `DMGetLocalSection()`, `DMSetLocalSection()`
657546e270d4SMatthew G. Knepley @*/
657646e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
657746e270d4SMatthew G. Knepley {
657846e270d4SMatthew G. Knepley   PetscFunctionBegin;
657946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6580534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
65819a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
65829a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
65839a9a41abSToby Isaac   }
658446e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
658546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
658646e270d4SMatthew G. Knepley }
658746e270d4SMatthew G. Knepley 
658846e270d4SMatthew G. Knepley /*@
658946e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
659046e270d4SMatthew G. Knepley 
659146e270d4SMatthew G. Knepley   Not Collective
659246e270d4SMatthew G. Knepley 
659346e270d4SMatthew G. Knepley   Input Parameters:
659446e270d4SMatthew G. Knepley + dm  - The DM object
659546e270d4SMatthew G. Knepley - dim - The embedding dimension
659646e270d4SMatthew G. Knepley 
659746e270d4SMatthew G. Knepley   Level: intermediate
659846e270d4SMatthew G. Knepley 
6599db781477SPatrick Sanan .seealso: `DMGetCoordinateDim()`, `DMSetCoordinateSection()`, `DMGetCoordinateSection()`, `DMGetLocalSection()`, `DMSetLocalSection()`
660046e270d4SMatthew G. Knepley @*/
660146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
660246e270d4SMatthew G. Knepley {
6603e5e52638SMatthew G. Knepley   PetscDS        ds;
660445480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6605f17e8794SMatthew G. Knepley 
660646e270d4SMatthew G. Knepley   PetscFunctionBegin;
660746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
660846e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6609d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
66109566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
661145480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
66129566063dSJacob Faibussowitsch       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
66139566063dSJacob Faibussowitsch       PetscCall(PetscDSSetCoordinateDimension(ds, dim));
661445480ffeSMatthew G. Knepley     }
6615d17bd122SMatthew G. Knepley   }
661646e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
661746e270d4SMatthew G. Knepley }
661846e270d4SMatthew G. Knepley 
6619e8abe2deSMatthew G. Knepley /*@
6620e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6621e8abe2deSMatthew G. Knepley 
6622d083f849SBarry Smith   Collective on dm
6623e8abe2deSMatthew G. Knepley 
6624e8abe2deSMatthew G. Knepley   Input Parameter:
6625e8abe2deSMatthew G. Knepley . dm - The DM object
6626e8abe2deSMatthew G. Knepley 
6627e8abe2deSMatthew G. Knepley   Output Parameter:
6628e8abe2deSMatthew G. Knepley . section - The PetscSection object
6629e8abe2deSMatthew G. Knepley 
6630e8abe2deSMatthew G. Knepley   Level: intermediate
6631e8abe2deSMatthew G. Knepley 
6632db781477SPatrick Sanan .seealso: `DMGetCoordinateDM()`, `DMGetLocalSection()`, `DMSetLocalSection()`
6633e8abe2deSMatthew G. Knepley @*/
6634e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6635e8abe2deSMatthew G. Knepley {
6636e8abe2deSMatthew G. Knepley   DM             cdm;
6637e8abe2deSMatthew G. Knepley 
6638e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6639e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6640e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
66419566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
66429566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(cdm, section));
6643e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6644e8abe2deSMatthew G. Knepley }
6645e8abe2deSMatthew G. Knepley 
6646e8abe2deSMatthew G. Knepley /*@
6647e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6648e8abe2deSMatthew G. Knepley 
6649e8abe2deSMatthew G. Knepley   Not Collective
6650e8abe2deSMatthew G. Knepley 
6651e8abe2deSMatthew G. Knepley   Input Parameters:
6652e8abe2deSMatthew G. Knepley + dm      - The DM object
665346e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6654e8abe2deSMatthew G. Knepley - section - The PetscSection object
6655e8abe2deSMatthew G. Knepley 
6656e8abe2deSMatthew G. Knepley   Level: intermediate
6657e8abe2deSMatthew G. Knepley 
6658db781477SPatrick Sanan .seealso: `DMGetCoordinateSection()`, `DMGetLocalSection()`, `DMSetLocalSection()`
6659e8abe2deSMatthew G. Knepley @*/
666046e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6661e8abe2deSMatthew G. Knepley {
6662e8abe2deSMatthew G. Knepley   DM             cdm;
6663e8abe2deSMatthew G. Knepley 
6664e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6665e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
666646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
66679566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
66689566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(cdm, section));
666946e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
66704c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
667146e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
667246e270d4SMatthew G. Knepley 
66739566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
66749566063dSJacob Faibussowitsch     PetscCall(DMGetDimPoints(dm, 0, &vStart, &vEnd));
667546e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
667646e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
667746e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
66789566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(section, v, &dd));
667946e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
668046e270d4SMatthew G. Knepley     }
66819566063dSJacob Faibussowitsch     if (d >= 0) PetscCall(DMSetCoordinateDim(dm, d));
668246e270d4SMatthew G. Knepley   }
6683e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6684e8abe2deSMatthew G. Knepley }
6685e8abe2deSMatthew G. Knepley 
6686d864a3eaSLisandro Dalcin /*@
6687d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6688d864a3eaSLisandro Dalcin 
6689d864a3eaSLisandro Dalcin   Input Parameters:
6690d864a3eaSLisandro Dalcin + dm      - The DM object
66914f9ab2b4SJed Brown - disc    - The new coordinate discretization or NULL to ensure a coordinate discretization exists
6692d864a3eaSLisandro Dalcin 
6693d864a3eaSLisandro Dalcin   Level: intermediate
6694d864a3eaSLisandro Dalcin 
6695db781477SPatrick Sanan .seealso: `DMGetCoordinateField()`
6696d864a3eaSLisandro Dalcin @*/
6697d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6698d864a3eaSLisandro Dalcin {
66994f9ab2b4SJed Brown   PetscFE        discOld;
6700d864a3eaSLisandro Dalcin   PetscClassId   classid;
6701d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6702d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6703d864a3eaSLisandro Dalcin   Mat            matInterp;
67044f9ab2b4SJed Brown   PetscBool      same_space = PETSC_TRUE;
6705d864a3eaSLisandro Dalcin 
6706d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6707d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
67084f9ab2b4SJed Brown   if (disc) PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6709d864a3eaSLisandro Dalcin 
67109566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdmOld));
6711d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
67129566063dSJacob Faibussowitsch   PetscCall(DMGetField(cdmOld, 0, NULL, (PetscObject*)&discOld));
67139566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId((PetscObject)discOld, &classid));
671429ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
671529ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
671629ad44c5SMatthew G. Knepley       PetscFE        feLinear;
671729ad44c5SMatthew G. Knepley       DMPolytopeType ct;
67188c4475acSStefano Zampini       PetscInt       dim, dE, cStart, cEnd;
671929ad44c5SMatthew G. Knepley       PetscBool      simplex;
672029ad44c5SMatthew G. Knepley 
672129ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
67229566063dSJacob Faibussowitsch       PetscCall(DMGetDimension(dm, &dim));
67239566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateDim(dm, &dE));
67248c4475acSStefano Zampini       PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
67258c4475acSStefano Zampini       if (cEnd > cStart) {
67269566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, cStart, &ct));
672729ad44c5SMatthew G. Knepley         switch (ct) {
672829ad44c5SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM:
672929ad44c5SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
673029ad44c5SMatthew G. Knepley             SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
673129ad44c5SMatthew G. Knepley           default: break;
673229ad44c5SMatthew G. Knepley         }
67338c4475acSStefano Zampini       }
67348c4475acSStefano Zampini       PetscCall(DMPlexIsSimplex(dm, &simplex));
67359566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear));
67369566063dSJacob Faibussowitsch       PetscCall(DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear));
67379566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&feLinear));
67389566063dSJacob Faibussowitsch       PetscCall(DMCreateDS(cdmOld));
67399566063dSJacob Faibussowitsch       PetscCall(DMGetField(cdmOld, 0, NULL, (PetscObject*)&discOld));
674029ad44c5SMatthew G. Knepley     } else {
674129ad44c5SMatthew G. Knepley       const char *discname;
674229ad44c5SMatthew G. Knepley 
67439566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetType((PetscObject)discOld, &discname));
67444f9ab2b4SJed Brown       SETERRQ(PetscObjectComm((PetscObject)discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
674529ad44c5SMatthew G. Knepley     }
674629ad44c5SMatthew G. Knepley   }
67474f9ab2b4SJed Brown   if (!disc) PetscFunctionReturn(0);
67484f9ab2b4SJed Brown   { // Check if the new space is the same as the old modulo quadrature
67494f9ab2b4SJed Brown     PetscDualSpace dsOld, ds;
67509566063dSJacob Faibussowitsch     PetscCall(PetscFEGetDualSpace(discOld, &dsOld));
67519566063dSJacob Faibussowitsch     PetscCall(PetscFEGetDualSpace(disc, &ds));
67529566063dSJacob Faibussowitsch     PetscCall(PetscDualSpaceEqual(dsOld, ds, &same_space));
67534f9ab2b4SJed Brown   }
6754d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
67559566063dSJacob Faibussowitsch   PetscCall(DMClone(cdmOld, &cdmNew));
67569566063dSJacob Faibussowitsch   PetscCall(DMSetField(cdmNew, 0, NULL, (PetscObject) disc));
67579566063dSJacob Faibussowitsch   PetscCall(DMCreateDS(cdmNew));
67589566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinates(dm, &coordsOld));
67594f9ab2b4SJed Brown   if (same_space) {
67609566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coordsOld));
67614f9ab2b4SJed Brown     coordsNew = coordsOld;
67624f9ab2b4SJed Brown   } else { // Project the coordinate vector from old to new space
67639566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(cdmNew, &coordsNew));
67649566063dSJacob Faibussowitsch     PetscCall(DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL));
67659566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(matInterp, coordsOld, coordsNew));
67669566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&matInterp));
67674f9ab2b4SJed Brown   }
6768d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
67699566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateField(dm, NULL));
67709566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dm, cdmNew));
67719566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinates(dm, coordsNew));
67729566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordsNew));
67739566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&cdmNew));
6774d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6775d864a3eaSLisandro Dalcin }
6776d864a3eaSLisandro Dalcin 
67775dc8c3f7SMatthew G. Knepley /*@C
677890b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
67795dc8c3f7SMatthew G. Knepley 
6780f899ff85SJose E. Roman   Input Parameter:
678190b157c4SStefano Zampini . dm      - The DM object
678290b157c4SStefano Zampini 
678390b157c4SStefano Zampini   Output Parameters:
678490b157c4SStefano Zampini + per     - Whether the DM is periodic or not
67855dc8c3f7SMatthew G. Knepley . maxCell - Over distances greater than this, we can assume a point has crossed over to another sheet, when trying to localize cell coordinates
67865dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
67875dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
67885dc8c3f7SMatthew G. Knepley 
67895dc8c3f7SMatthew G. Knepley   Level: developer
67905dc8c3f7SMatthew G. Knepley 
6791db781477SPatrick Sanan .seealso: `DMGetPeriodicity()`
67925dc8c3f7SMatthew G. Knepley @*/
679390b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6794c6b900c6SMatthew G. Knepley {
6795c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6796c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
679790b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6798c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6799c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
68005dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6801c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6802c6b900c6SMatthew G. Knepley }
6803c6b900c6SMatthew G. Knepley 
68045dc8c3f7SMatthew G. Knepley /*@C
68055dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
68065dc8c3f7SMatthew G. Knepley 
68075dc8c3f7SMatthew G. Knepley   Input Parameters:
68085dc8c3f7SMatthew G. Knepley + dm      - The DM object
6809db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6810db2bf62eSStefano Zampini . maxCell - Over distances greater than this, we can assume a point has crossed over to another sheet, when trying to localize cell coordinates. Pass NULL to remove such information.
68115dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
68125dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
68135dc8c3f7SMatthew G. Knepley 
6814db2bf62eSStefano Zampini   Notes: If per is PETSC_TRUE and maxCell is not provided, coordinates need to be already localized, or must be localized by hand by the user.
6815db2bf62eSStefano Zampini 
68165dc8c3f7SMatthew G. Knepley   Level: developer
68175dc8c3f7SMatthew G. Knepley 
6818db781477SPatrick Sanan .seealso: `DMGetPeriodicity()`
68195dc8c3f7SMatthew G. Knepley @*/
682090b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6821c6b900c6SMatthew G. Knepley {
6822c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6823c6b900c6SMatthew G. Knepley 
6824c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6825c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
682690b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6827412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6828412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6829412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
68309566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
683190b157c4SStefano Zampini   if (maxCell) {
68329566063dSJacob Faibussowitsch     if (!dm->maxCell) PetscCall(PetscMalloc1(dim, &dm->maxCell));
6833412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6834db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
68359566063dSJacob Faibussowitsch     PetscCall(PetscFree(dm->maxCell));
6836412e9a14SMatthew G. Knepley   }
6837db2bf62eSStefano Zampini 
6838412e9a14SMatthew G. Knepley   if (L) {
68399566063dSJacob Faibussowitsch     if (!dm->L) PetscCall(PetscMalloc1(dim, &dm->L));
6840412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6841412e9a14SMatthew G. Knepley   }
6842412e9a14SMatthew G. Knepley   if (bd) {
68439566063dSJacob Faibussowitsch     if (!dm->bdtype) PetscCall(PetscMalloc1(dim, &dm->bdtype));
6844412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
684590b157c4SStefano Zampini   }
6846072d7d67SStefano Zampini   dm->periodic = per;
6847c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6848c6b900c6SMatthew G. Knepley }
6849c6b900c6SMatthew G. Knepley 
68502e17dfb7SMatthew G. Knepley /*@
68512e17dfb7SMatthew G. Knepley   DMLocalizeCoordinate - If a mesh is periodic (a torus with lengths L_i, some of which can be infinite), project the coordinate onto [0, L_i) in each dimension.
68522e17dfb7SMatthew G. Knepley 
68532e17dfb7SMatthew G. Knepley   Input Parameters:
68542e17dfb7SMatthew G. Knepley + dm     - The DM
685565da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
685665da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
68572e17dfb7SMatthew G. Knepley 
68582e17dfb7SMatthew G. Knepley   Output Parameter:
68592e17dfb7SMatthew G. Knepley . out - The localized coordinate point
68602e17dfb7SMatthew G. Knepley 
68612e17dfb7SMatthew G. Knepley   Level: developer
68622e17dfb7SMatthew G. Knepley 
6863db781477SPatrick Sanan .seealso: `DMLocalizeCoordinates()`, `DMLocalizeAddCoordinate()`
68642e17dfb7SMatthew G. Knepley @*/
686565da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
68662e17dfb7SMatthew G. Knepley {
68672e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
68682e17dfb7SMatthew G. Knepley 
68692e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68709566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
68712e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68722e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
68732e17dfb7SMatthew G. Knepley   } else {
687465da65dcSMatthew G. Knepley     if (endpoint) {
687565da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6876da3333bfSMatthew G. Knepley         if ((PetscAbsReal(PetscRealPart(in[d])/dm->L[d] - PetscFloorReal(PetscRealPart(in[d])/dm->L[d])) < PETSC_SMALL) && (PetscRealPart(in[d])/dm->L[d] > PETSC_SMALL)) {
6877da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
687865da65dcSMatthew G. Knepley         } else {
6879da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
688065da65dcSMatthew G. Knepley         }
688165da65dcSMatthew G. Knepley       }
688265da65dcSMatthew G. Knepley     } else {
68832e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
68841118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
68852e17dfb7SMatthew G. Knepley       }
68862e17dfb7SMatthew G. Knepley     }
688765da65dcSMatthew G. Knepley   }
68882e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68892e17dfb7SMatthew G. Knepley }
68902e17dfb7SMatthew G. Knepley 
68912e17dfb7SMatthew G. Knepley /*
68922e17dfb7SMatthew G. Knepley   DMLocalizeCoordinate_Internal - If a mesh is periodic, and the input point is far from the anchor, pick the coordinate sheet of the torus which moves it closer.
68932e17dfb7SMatthew G. Knepley 
68942e17dfb7SMatthew G. Knepley   Input Parameters:
68952e17dfb7SMatthew G. Knepley + dm     - The DM
68962e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
68972e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
68982e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
68992e17dfb7SMatthew G. Knepley 
69002e17dfb7SMatthew G. Knepley   Output Parameter:
69012e17dfb7SMatthew G. Knepley . out - The localized coordinate point
69022e17dfb7SMatthew G. Knepley 
69032e17dfb7SMatthew G. Knepley   Level: developer
69042e17dfb7SMatthew G. Knepley 
69052e17dfb7SMatthew G. Knepley   Note: This is meant to get a set of coordinates close to each other, as in a cell. The anchor is usually the one of the vertices on a containing cell
69062e17dfb7SMatthew G. Knepley 
6907db781477SPatrick Sanan .seealso: `DMLocalizeCoordinates()`, `DMLocalizeAddCoordinate()`
69082e17dfb7SMatthew G. Knepley */
69092e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
69102e17dfb7SMatthew G. Knepley {
69112e17dfb7SMatthew G. Knepley   PetscInt d;
69122e17dfb7SMatthew G. Knepley 
69132e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
69142e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
69152e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
69162e17dfb7SMatthew G. Knepley   } else {
69172e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6918908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
69192e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
69202e17dfb7SMatthew G. Knepley       } else {
69212e17dfb7SMatthew G. Knepley         out[d] = in[d];
69222e17dfb7SMatthew G. Knepley       }
69232e17dfb7SMatthew G. Knepley     }
69242e17dfb7SMatthew G. Knepley   }
69252e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69262e17dfb7SMatthew G. Knepley }
6927a5801f52SStefano Zampini 
69282e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
69292e17dfb7SMatthew G. Knepley {
69302e17dfb7SMatthew G. Knepley   PetscInt d;
69312e17dfb7SMatthew G. Knepley 
69322e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
69332e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
69342e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
69352e17dfb7SMatthew G. Knepley   } else {
69362e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6937908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
69382e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
69392e17dfb7SMatthew G. Knepley       } else {
69402e17dfb7SMatthew G. Knepley         out[d] = in[d];
69412e17dfb7SMatthew G. Knepley       }
69422e17dfb7SMatthew G. Knepley     }
69432e17dfb7SMatthew G. Knepley   }
69442e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69452e17dfb7SMatthew G. Knepley }
69462e17dfb7SMatthew G. Knepley 
69472e17dfb7SMatthew G. Knepley /*
69482e17dfb7SMatthew G. Knepley   DMLocalizeAddCoordinate_Internal - If a mesh is periodic, and the input point is far from the anchor, pick the coordinate sheet of the torus which moves it closer.
69492e17dfb7SMatthew G. Knepley 
69502e17dfb7SMatthew G. Knepley   Input Parameters:
69512e17dfb7SMatthew G. Knepley + dm     - The DM
69522e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
69532e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
69542e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
69552e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
69562e17dfb7SMatthew G. Knepley 
69572e17dfb7SMatthew G. Knepley   Output Parameter:
69582e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
69592e17dfb7SMatthew G. Knepley 
69602e17dfb7SMatthew G. Knepley   Level: developer
69612e17dfb7SMatthew G. Knepley 
69622e17dfb7SMatthew G. Knepley   Note: This is meant to get a set of coordinates close to each other, as in a cell. The anchor is usually the one of the vertices on a containing cell
69632e17dfb7SMatthew G. Knepley 
6964db781477SPatrick Sanan .seealso: `DMLocalizeCoordinates()`, `DMLocalizeCoordinate()`
69652e17dfb7SMatthew G. Knepley */
69662e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
69672e17dfb7SMatthew G. Knepley {
69682e17dfb7SMatthew G. Knepley   PetscInt d;
69692e17dfb7SMatthew G. Knepley 
69702e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
69712e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
69722e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
69732e17dfb7SMatthew G. Knepley   } else {
69742e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6975412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6976412e9a14SMatthew G. Knepley 
6977412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6978412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6979412e9a14SMatthew G. Knepley 
6980412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
698163a3b9bcSJacob Faibussowitsch           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt_FMT "-Coordinate %g more than %g away from anchor %g", d, (double) PetscRealPart(in[d]), (double) maxC, (double) PetscRealPart(anchor[d]));
6982412e9a14SMatthew G. Knepley         out[d] += newCoord;
69832e17dfb7SMatthew G. Knepley       } else {
69842e17dfb7SMatthew G. Knepley         out[d] += in[d];
69852e17dfb7SMatthew G. Knepley       }
69862e17dfb7SMatthew G. Knepley     }
69872e17dfb7SMatthew G. Knepley   }
69882e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69892e17dfb7SMatthew G. Knepley }
69902e17dfb7SMatthew G. Knepley 
699136447a5eSToby Isaac /*@
69928f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
69938f700142SStefano Zampini 
69948f700142SStefano Zampini   Not collective
699536447a5eSToby Isaac 
699636447a5eSToby Isaac   Input Parameter:
699736447a5eSToby Isaac . dm - The DM
699836447a5eSToby Isaac 
699936447a5eSToby Isaac   Output Parameter:
700036447a5eSToby Isaac   areLocalized - True if localized
700136447a5eSToby Isaac 
700236447a5eSToby Isaac   Level: developer
700336447a5eSToby Isaac 
7004db781477SPatrick Sanan .seealso: `DMLocalizeCoordinates()`, `DMGetCoordinatesLocalized()`, `DMSetPeriodicity()`
700536447a5eSToby Isaac @*/
70068f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
700736447a5eSToby Isaac {
700836447a5eSToby Isaac   DM             cdm;
700936447a5eSToby Isaac   PetscSection   coordSection;
70102ccac677SMatthew G. Knepley   PetscInt       depth, cStart, cEnd, sStart, sEnd, c, dof;
701146a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
701236447a5eSToby Isaac 
701336447a5eSToby Isaac   PetscFunctionBegin;
701436447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7015534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
70168b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
701746a3a80fSLisandro Dalcin 
701836447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
70199566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
70209566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex));
70219f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
70229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(cdm, &depth));
70232ccac677SMatthew G. Knepley   if (!depth) PetscFunctionReturn(0);
702446a3a80fSLisandro Dalcin 
70259566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
70269566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd));
70279566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(coordSection, &sStart, &sEnd));
702836447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
702946a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
703046a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
70319566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, c, &dof));
703246a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
703336447a5eSToby Isaac   }
70348f700142SStefano Zampini   *areLocalized = alreadyLocalized;
703536447a5eSToby Isaac   PetscFunctionReturn(0);
703636447a5eSToby Isaac }
703736447a5eSToby Isaac 
70388f700142SStefano Zampini /*@
70398f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
70408f700142SStefano Zampini 
70418f700142SStefano Zampini   Collective on dm
70428f700142SStefano Zampini 
70438f700142SStefano Zampini   Input Parameter:
70448f700142SStefano Zampini . dm - The DM
70458f700142SStefano Zampini 
70468f700142SStefano Zampini   Output Parameter:
70478f700142SStefano Zampini   areLocalized - True if localized
70488f700142SStefano Zampini 
70498f700142SStefano Zampini   Level: developer
70508f700142SStefano Zampini 
7051db781477SPatrick Sanan .seealso: `DMLocalizeCoordinates()`, `DMSetPeriodicity()`, `DMGetCoordinatesLocalizedLocal()`
70528f700142SStefano Zampini @*/
70538f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
70548f700142SStefano Zampini {
70558f700142SStefano Zampini   PetscBool      localized;
70568f700142SStefano Zampini 
70578f700142SStefano Zampini   PetscFunctionBegin;
70588f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7059534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
70609566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocalizedLocal(dm,&localized));
70611c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm)));
70628f700142SStefano Zampini   PetscFunctionReturn(0);
70638f700142SStefano Zampini }
706436447a5eSToby Isaac 
70652e17dfb7SMatthew G. Knepley /*@
7066492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
70672e17dfb7SMatthew G. Knepley 
70688f700142SStefano Zampini   Collective on dm
70698f700142SStefano Zampini 
70702e17dfb7SMatthew G. Knepley   Input Parameter:
70712e17dfb7SMatthew G. Knepley . dm - The DM
70722e17dfb7SMatthew G. Knepley 
70732e17dfb7SMatthew G. Knepley   Level: developer
70742e17dfb7SMatthew G. Knepley 
7075db781477SPatrick Sanan .seealso: `DMSetPeriodicity()`, `DMLocalizeCoordinate()`, `DMLocalizeAddCoordinate()`
70762e17dfb7SMatthew G. Knepley @*/
70772e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
70782e17dfb7SMatthew G. Knepley {
70792e17dfb7SMatthew G. Knepley   DM             cdm;
70802e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
70812e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
70823e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
70833e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
7084e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
70853e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
70863e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
70872e17dfb7SMatthew G. Knepley 
70882e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
70892e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
709092c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
70919566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocalized(dm, &alreadyLocalized));
7092f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
7093f7cbd40bSStefano Zampini 
70942e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
70959566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
70962e17dfb7SMatthew G. Knepley   {
70972e17dfb7SMatthew G. Knepley     PetscBool isplex;
70982e17dfb7SMatthew G. Knepley 
70999566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex));
71002e17dfb7SMatthew G. Knepley     if (isplex) {
71019566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd));
71029566063dSJacob Faibussowitsch       PetscCall(DMPlexGetMaxProjectionHeight(cdm,&maxHeight));
71039566063dSJacob Faibussowitsch       PetscCall(DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart));
71043e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
71053e922f36SToby Isaac       newStart = vStart;
71063e922f36SToby Isaac       newEnd   = vEnd;
71073e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
71089566063dSJacob Faibussowitsch         PetscCall(DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]));
71093e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
71103e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
71113e922f36SToby Isaac       }
71122e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
71132e17dfb7SMatthew G. Knepley   }
71149566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
71157a8be351SBarry Smith   PetscCheck(coordinates,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
71169566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
71179566063dSJacob Faibussowitsch   PetscCall(VecGetBlockSize(coordinates, &bs));
71189566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(coordSection,&sStart,&sEnd));
71193e922f36SToby Isaac 
71209566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection));
71219566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(cSection, 1));
71229566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &Nc));
71239566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(cSection, 0, Nc));
71249566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(cSection, newStart, newEnd));
71253e922f36SToby Isaac 
71269566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor));
71273e922f36SToby Isaac   localized = &anchor[bs];
71283e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
71293e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
71303e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
71313e922f36SToby Isaac 
71323e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
71333e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
71343e922f36SToby Isaac       PetscInt     b;
71353e922f36SToby Isaac 
71363e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
71379566063dSJacob Faibussowitsch       PetscCall(DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords));
71383e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
71393e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
71409566063dSJacob Faibussowitsch         PetscCall(DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized));
71413e922f36SToby Isaac         for (b = 0; b < bs; b++) {
71423e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
71433e922f36SToby Isaac         }
71443e922f36SToby Isaac         if (b < bs) break;
71453e922f36SToby Isaac       }
71463e922f36SToby Isaac       if (d < dof/bs) {
71473e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
71483e922f36SToby Isaac           PetscInt cdof;
71493e922f36SToby Isaac 
71509566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(coordSection, c, &cdof));
71513e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
71523e922f36SToby Isaac         }
71539566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetDof(cSection, c, dof));
71549566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldDof(cSection, c, 0, dof));
71553e922f36SToby Isaac       }
71569566063dSJacob Faibussowitsch       PetscCall(DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords));
71573e922f36SToby Isaac     }
71583e922f36SToby Isaac   }
71599566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm)));
71603e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
71619566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor));
71629566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&cSection));
71639566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart));
71643e922f36SToby Isaac     PetscFunctionReturn(0);
71653e922f36SToby Isaac   }
71662e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
71679566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, v, &dof));
71689566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(cSection, v, dof));
71699566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(cSection, v, 0, dof));
71702e17dfb7SMatthew G. Knepley   }
71719566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(cSection));
71729566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(cSection, &coordSize));
71739566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &cVec));
71749566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)cVec,"coordinates"));
71759566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(cVec, bs));
71769566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(cVec, coordSize, PETSC_DETERMINE));
71779566063dSJacob Faibussowitsch   PetscCall(VecSetType(cVec, VECSTANDARD));
71789566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, (const PetscScalar**)&coords));
71799566063dSJacob Faibussowitsch   PetscCall(VecGetArray(cVec, &coords2));
71802e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
71819566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, v, &dof));
71829566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, v, &off));
71839566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(cSection,     v, &off2));
71842e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
71852e17dfb7SMatthew G. Knepley   }
71863e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
71873e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
71883e922f36SToby Isaac 
71892e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
71902e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
71913e922f36SToby Isaac       PetscInt     b, cdof;
71922e17dfb7SMatthew G. Knepley 
71939566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSection,c,&cdof));
71943e922f36SToby Isaac       if (!cdof) continue;
71959566063dSJacob Faibussowitsch       PetscCall(DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords));
71969566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(cSection, c, &off2));
71972e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
71989566063dSJacob Faibussowitsch       for (d = 0; d < dof/bs; ++d) PetscCall(DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]));
71999566063dSJacob Faibussowitsch       PetscCall(DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords));
72002e17dfb7SMatthew G. Knepley     }
72013e922f36SToby Isaac   }
72029566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor));
72039566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart));
72049566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords));
72059566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(cVec, &coords2));
72069566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection));
72079566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, cVec));
72089566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&cVec));
72099566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&cSection));
72102e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
72112e17dfb7SMatthew G. Knepley }
72122e17dfb7SMatthew G. Knepley 
7213e87bb0d3SMatthew G Knepley /*@
72143a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
7215e87bb0d3SMatthew G Knepley 
7216d083f849SBarry Smith   Collective on v (see explanation below)
7217e87bb0d3SMatthew G Knepley 
7218e87bb0d3SMatthew G Knepley   Input Parameters:
7219e87bb0d3SMatthew G Knepley + dm - The DM
72206b867d5aSJose E. Roman - ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
7221e87bb0d3SMatthew G Knepley 
72226b867d5aSJose E. Roman   Input/Output Parameters:
72236b867d5aSJose E. Roman + v - The Vec of points, on output contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
72246b867d5aSJose E. Roman - cellSF - Points to either NULL, or a PetscSF with guesses for which cells contain each point;
72256b867d5aSJose E. Roman            on output, the PetscSF containing the ranks and local indices of the containing points
72263a93e3b7SToby Isaac 
7227e87bb0d3SMatthew G Knepley   Level: developer
722861e3bb9bSMatthew G Knepley 
722962a38674SMatthew G. Knepley   Notes:
72303a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
723162a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
72323a93e3b7SToby Isaac 
72333a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
723462a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
72353a93e3b7SToby Isaac 
72363a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
72373a93e3b7SToby Isaac 
723862a38674SMatthew G. Knepley $    const PetscSFNode *cells;
723962a38674SMatthew G. Knepley $    PetscInt           nFound;
7240a6216909SToby Isaac $    const PetscInt    *found;
724162a38674SMatthew G. Knepley $
7242a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
72433a93e3b7SToby Isaac 
72443a93e3b7SToby Isaac   Where cells[i].rank is the rank of the cell containing point found[i] (or i if found == NULL), and cells[i].index is
72453a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
72463a93e3b7SToby Isaac 
7247db781477SPatrick Sanan .seealso: `DMSetCoordinates()`, `DMSetCoordinatesLocal()`, `DMGetCoordinates()`, `DMGetCoordinatesLocal()`, `DMPointLocationType`
724861e3bb9bSMatthew G Knepley @*/
724962a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
7250e87bb0d3SMatthew G Knepley {
7251e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
7252e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7253e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
7254e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
72553a93e3b7SToby Isaac   if (*cellSF) {
72563a93e3b7SToby Isaac     PetscMPIInt result;
72573a93e3b7SToby Isaac 
7258e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
72599566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result));
72607a8be351SBarry Smith     PetscCheck(result == MPI_IDENT || result == MPI_CONGRUENT,PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"cellSF must have a communicator congruent to v's");
7261e0fc9d1bSMatthew G. Knepley   } else {
72629566063dSJacob Faibussowitsch     PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF));
72633a93e3b7SToby Isaac   }
72647a8be351SBarry Smith   PetscCheck(dm->ops->locatepoints,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
72659566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_LocatePoints,dm,0,0,0));
72669566063dSJacob Faibussowitsch   PetscCall((*dm->ops->locatepoints)(dm,v,ltype,*cellSF));
72679566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_LocatePoints,dm,0,0,0));
7268e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
7269e87bb0d3SMatthew G Knepley }
727014f150ffSMatthew G. Knepley 
7271f4d763aaSMatthew G. Knepley /*@
7272f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
7273f4d763aaSMatthew G. Knepley 
72748f700142SStefano Zampini   Collective on dm
72758f700142SStefano Zampini 
7276f4d763aaSMatthew G. Knepley   Input Parameter:
7277f4d763aaSMatthew G. Knepley . dm - The original DM
7278f4d763aaSMatthew G. Knepley 
7279f4d763aaSMatthew G. Knepley   Output Parameter:
7280f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
7281f4d763aaSMatthew G. Knepley 
7282f4d763aaSMatthew G. Knepley   Level: intermediate
7283f4d763aaSMatthew G. Knepley 
7284db781477SPatrick Sanan .seealso: `VecView()`, `DMGetGlobalSection()`
7285f4d763aaSMatthew G. Knepley @*/
728614f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
728714f150ffSMatthew G. Knepley {
7288c26acbdeSMatthew G. Knepley   PetscSection   section;
72892d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
729014f150ffSMatthew G. Knepley 
729114f150ffSMatthew G. Knepley   PetscFunctionBegin;
729214f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
729314f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
72949566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
72959566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
72969566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm)));
72972d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
7298c26acbdeSMatthew G. Knepley     *odm = dm;
7299c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
7300c26acbdeSMatthew G. Knepley   }
730114f150ffSMatthew G. Knepley   if (!dm->dmBC) {
7302c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
730314f150ffSMatthew G. Knepley     PetscSF      sf;
730414f150ffSMatthew G. Knepley 
73059566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
73069566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
73079566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
73089566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
73099566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
73109566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
73119566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
73129566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
73139566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
731414f150ffSMatthew G. Knepley   }
731514f150ffSMatthew G. Knepley   *odm = dm->dmBC;
731614f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
731714f150ffSMatthew G. Knepley }
7318f4d763aaSMatthew G. Knepley 
7319f4d763aaSMatthew G. Knepley /*@
7320cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
7321f4d763aaSMatthew G. Knepley 
7322f4d763aaSMatthew G. Knepley   Input Parameter:
7323f4d763aaSMatthew G. Knepley . dm - The original DM
7324f4d763aaSMatthew G. Knepley 
7325cdb7a50dSMatthew G. Knepley   Output Parameters:
7326cdb7a50dSMatthew G. Knepley + num - The output sequence number
7327cdb7a50dSMatthew G. Knepley - val - The output sequence value
7328f4d763aaSMatthew G. Knepley 
7329f4d763aaSMatthew G. Knepley   Level: intermediate
7330f4d763aaSMatthew G. Knepley 
7331f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7332f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7333f4d763aaSMatthew G. Knepley 
7334db781477SPatrick Sanan .seealso: `VecView()`
7335f4d763aaSMatthew G. Knepley @*/
7336cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7337f4d763aaSMatthew G. Knepley {
7338f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7339f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7340534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7341534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7342f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7343f4d763aaSMatthew G. Knepley }
7344f4d763aaSMatthew G. Knepley 
7345f4d763aaSMatthew G. Knepley /*@
7346cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7347f4d763aaSMatthew G. Knepley 
7348f4d763aaSMatthew G. Knepley   Input Parameters:
7349f4d763aaSMatthew G. Knepley + dm - The original DM
7350cdb7a50dSMatthew G. Knepley . num - The output sequence number
7351cdb7a50dSMatthew G. Knepley - val - The output sequence value
7352f4d763aaSMatthew G. Knepley 
7353f4d763aaSMatthew G. Knepley   Level: intermediate
7354f4d763aaSMatthew G. Knepley 
7355f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7356f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7357f4d763aaSMatthew G. Knepley 
7358db781477SPatrick Sanan .seealso: `VecView()`
7359f4d763aaSMatthew G. Knepley @*/
7360cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7361f4d763aaSMatthew G. Knepley {
7362f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7363f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7364f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7365cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7366cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7367cdb7a50dSMatthew G. Knepley }
7368cdb7a50dSMatthew G. Knepley 
7369cdb7a50dSMatthew G. Knepley /*@C
7370cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7371cdb7a50dSMatthew G. Knepley 
7372cdb7a50dSMatthew G. Knepley   Input Parameters:
7373cdb7a50dSMatthew G. Knepley + dm   - The original DM
7374cdb7a50dSMatthew G. Knepley . name - The sequence name
7375cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7376cdb7a50dSMatthew G. Knepley 
7377cdb7a50dSMatthew G. Knepley   Output Parameter:
7378cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7379cdb7a50dSMatthew G. Knepley 
7380cdb7a50dSMatthew G. Knepley   Level: intermediate
7381cdb7a50dSMatthew G. Knepley 
7382cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7383cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7384cdb7a50dSMatthew G. Knepley 
7385db781477SPatrick Sanan .seealso: `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
7386cdb7a50dSMatthew G. Knepley @*/
7387cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7388cdb7a50dSMatthew G. Knepley {
7389cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7390cdb7a50dSMatthew G. Knepley 
7391cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7392cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7393cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7394064a246eSJacob Faibussowitsch   PetscValidRealPointer(val,5);
73959566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5));
7396cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7397cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7398cdb7a50dSMatthew G. Knepley     PetscScalar value;
7399cdb7a50dSMatthew G. Knepley 
74009566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
74014aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7402cdb7a50dSMatthew G. Knepley #endif
7403cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7404f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7405f4d763aaSMatthew G. Knepley }
74068e4ac7eaSMatthew G. Knepley 
74078e4ac7eaSMatthew G. Knepley /*@
74088e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
74098e4ac7eaSMatthew G. Knepley 
74108e4ac7eaSMatthew G. Knepley   Not collective
74118e4ac7eaSMatthew G. Knepley 
74128e4ac7eaSMatthew G. Knepley   Input Parameter:
74138e4ac7eaSMatthew G. Knepley . dm - The DM
74148e4ac7eaSMatthew G. Knepley 
74158e4ac7eaSMatthew G. Knepley   Output Parameter:
74168e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
74178e4ac7eaSMatthew G. Knepley 
74188e4ac7eaSMatthew G. Knepley   Level: beginner
74198e4ac7eaSMatthew G. Knepley 
7420db781477SPatrick Sanan .seealso: `DMSetUseNatural()`, `DMCreate()`
74218e4ac7eaSMatthew G. Knepley @*/
74228e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
74238e4ac7eaSMatthew G. Knepley {
74248e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
74258e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7426534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
74278e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
74288e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
74298e4ac7eaSMatthew G. Knepley }
74308e4ac7eaSMatthew G. Knepley 
74318e4ac7eaSMatthew G. Knepley /*@
74325d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
74338e4ac7eaSMatthew G. Knepley 
74348e4ac7eaSMatthew G. Knepley   Collective on dm
74358e4ac7eaSMatthew G. Knepley 
74368e4ac7eaSMatthew G. Knepley   Input Parameters:
74378e4ac7eaSMatthew G. Knepley + dm - The DM
74388e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
74398e4ac7eaSMatthew G. Knepley 
74405d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
74415d3b26e6SMatthew G. Knepley 
74428e4ac7eaSMatthew G. Knepley   Level: beginner
74438e4ac7eaSMatthew G. Knepley 
7444db781477SPatrick Sanan .seealso: `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
74458e4ac7eaSMatthew G. Knepley @*/
74468e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
74478e4ac7eaSMatthew G. Knepley {
74488e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
74498e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74508833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
74518e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
74528e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
74538e4ac7eaSMatthew G. Knepley }
7454c58f1c22SToby Isaac 
7455c58f1c22SToby Isaac /*@C
7456c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7457c58f1c22SToby Isaac 
7458c58f1c22SToby Isaac   Not Collective
7459c58f1c22SToby Isaac 
7460c58f1c22SToby Isaac   Input Parameters:
7461c58f1c22SToby Isaac + dm   - The DM object
7462c58f1c22SToby Isaac - name - The label name
7463c58f1c22SToby Isaac 
7464c58f1c22SToby Isaac   Level: intermediate
7465c58f1c22SToby Isaac 
7466db781477SPatrick Sanan .seealso: `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7467c58f1c22SToby Isaac @*/
7468c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7469c58f1c22SToby Isaac {
74705d80c0bfSVaclav Hapla   PetscBool      flg;
74715d80c0bfSVaclav Hapla   DMLabel        label;
7472c58f1c22SToby Isaac 
7473c58f1c22SToby Isaac   PetscFunctionBegin;
7474c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7475c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
74769566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
7477c58f1c22SToby Isaac   if (!flg) {
74789566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
74799566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
74809566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
7481c58f1c22SToby Isaac   }
7482c58f1c22SToby Isaac   PetscFunctionReturn(0);
7483c58f1c22SToby Isaac }
7484c58f1c22SToby Isaac 
7485c58f1c22SToby Isaac /*@C
74867cf350d0SPierre Jolivet   DMCreateLabelAtIndex - Create a label of the given name at the given index. If it already exists, move it to this index.
74870fdc7489SMatthew Knepley 
74880fdc7489SMatthew Knepley   Not Collective
74890fdc7489SMatthew Knepley 
74900fdc7489SMatthew Knepley   Input Parameters:
74910fdc7489SMatthew Knepley + dm   - The DM object
74920fdc7489SMatthew Knepley . l    - The index for the label
74930fdc7489SMatthew Knepley - name - The label name
74940fdc7489SMatthew Knepley 
74950fdc7489SMatthew Knepley   Level: intermediate
74960fdc7489SMatthew Knepley 
7497db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
74980fdc7489SMatthew Knepley @*/
74990fdc7489SMatthew Knepley PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
75000fdc7489SMatthew Knepley {
75010fdc7489SMatthew Knepley   DMLabelLink    orig, prev = NULL;
75020fdc7489SMatthew Knepley   DMLabel        label;
75030fdc7489SMatthew Knepley   PetscInt       Nl, m;
75040fdc7489SMatthew Knepley   PetscBool      flg, match;
75050fdc7489SMatthew Knepley   const char    *lname;
75060fdc7489SMatthew Knepley 
75070fdc7489SMatthew Knepley   PetscFunctionBegin;
75080fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7509064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
75109566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
75110fdc7489SMatthew Knepley   if (!flg) {
75129566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
75139566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
75149566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
75150fdc7489SMatthew Knepley   }
75169566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
751763a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
75180fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
75199566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) orig->label, &lname));
75209566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
75210fdc7489SMatthew Knepley     if (match) break;
75220fdc7489SMatthew Knepley   }
75230fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
75240fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
75250fdc7489SMatthew Knepley   else    prev->next = orig->next;
75260fdc7489SMatthew Knepley   if (!l) {
75270fdc7489SMatthew Knepley     orig->next = dm->labels;
75280fdc7489SMatthew Knepley     dm->labels = orig;
75290fdc7489SMatthew Knepley   } else {
75300fdc7489SMatthew Knepley     for (m = 0, prev = dm->labels; m < l-1; ++m, prev = prev->next);
75310fdc7489SMatthew Knepley     orig->next = prev->next;
75320fdc7489SMatthew Knepley     prev->next = orig;
75330fdc7489SMatthew Knepley   }
75340fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75350fdc7489SMatthew Knepley }
75360fdc7489SMatthew Knepley 
75370fdc7489SMatthew Knepley /*@C
75387cf350d0SPierre Jolivet   DMGetLabelValue - Get the value in a DMLabel for the given point, with -1 as the default
7539c58f1c22SToby Isaac 
7540c58f1c22SToby Isaac   Not Collective
7541c58f1c22SToby Isaac 
7542c58f1c22SToby Isaac   Input Parameters:
7543c58f1c22SToby Isaac + dm   - The DM object
7544c58f1c22SToby Isaac . name - The label name
7545c58f1c22SToby Isaac - point - The mesh point
7546c58f1c22SToby Isaac 
7547c58f1c22SToby Isaac   Output Parameter:
7548c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7549c58f1c22SToby Isaac 
7550c58f1c22SToby Isaac   Level: beginner
7551c58f1c22SToby Isaac 
7552db781477SPatrick Sanan .seealso: `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7553c58f1c22SToby Isaac @*/
7554c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7555c58f1c22SToby Isaac {
7556c58f1c22SToby Isaac   DMLabel        label;
7557c58f1c22SToby Isaac 
7558c58f1c22SToby Isaac   PetscFunctionBegin;
7559c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7560c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
75619566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
75627a8be351SBarry Smith   PetscCheck(label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
75639566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
7564c58f1c22SToby Isaac   PetscFunctionReturn(0);
7565c58f1c22SToby Isaac }
7566c58f1c22SToby Isaac 
7567c58f1c22SToby Isaac /*@C
75687cf350d0SPierre Jolivet   DMSetLabelValue - Add a point to a DMLabel with given value
7569c58f1c22SToby Isaac 
7570c58f1c22SToby Isaac   Not Collective
7571c58f1c22SToby Isaac 
7572c58f1c22SToby Isaac   Input Parameters:
7573c58f1c22SToby Isaac + dm   - The DM object
7574c58f1c22SToby Isaac . name - The label name
7575c58f1c22SToby Isaac . point - The mesh point
7576c58f1c22SToby Isaac - value - The label value for this point
7577c58f1c22SToby Isaac 
7578c58f1c22SToby Isaac   Output Parameter:
7579c58f1c22SToby Isaac 
7580c58f1c22SToby Isaac   Level: beginner
7581c58f1c22SToby Isaac 
7582db781477SPatrick Sanan .seealso: `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
7583c58f1c22SToby Isaac @*/
7584c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7585c58f1c22SToby Isaac {
7586c58f1c22SToby Isaac   DMLabel        label;
7587c58f1c22SToby Isaac 
7588c58f1c22SToby Isaac   PetscFunctionBegin;
7589c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7590c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
75919566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7592c58f1c22SToby Isaac   if (!label) {
75939566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
75949566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
7595c58f1c22SToby Isaac   }
75969566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
7597c58f1c22SToby Isaac   PetscFunctionReturn(0);
7598c58f1c22SToby Isaac }
7599c58f1c22SToby Isaac 
7600c58f1c22SToby Isaac /*@C
76017cf350d0SPierre Jolivet   DMClearLabelValue - Remove a point from a DMLabel with given value
7602c58f1c22SToby Isaac 
7603c58f1c22SToby Isaac   Not Collective
7604c58f1c22SToby Isaac 
7605c58f1c22SToby Isaac   Input Parameters:
7606c58f1c22SToby Isaac + dm   - The DM object
7607c58f1c22SToby Isaac . name - The label name
7608c58f1c22SToby Isaac . point - The mesh point
7609c58f1c22SToby Isaac - value - The label value for this point
7610c58f1c22SToby Isaac 
7611c58f1c22SToby Isaac   Output Parameter:
7612c58f1c22SToby Isaac 
7613c58f1c22SToby Isaac   Level: beginner
7614c58f1c22SToby Isaac 
7615db781477SPatrick Sanan .seealso: `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7616c58f1c22SToby Isaac @*/
7617c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7618c58f1c22SToby Isaac {
7619c58f1c22SToby Isaac   DMLabel        label;
7620c58f1c22SToby Isaac 
7621c58f1c22SToby Isaac   PetscFunctionBegin;
7622c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7623c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
76249566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7625c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
76269566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
7627c58f1c22SToby Isaac   PetscFunctionReturn(0);
7628c58f1c22SToby Isaac }
7629c58f1c22SToby Isaac 
7630c58f1c22SToby Isaac /*@C
7631c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7632c58f1c22SToby Isaac 
7633c58f1c22SToby Isaac   Not Collective
7634c58f1c22SToby Isaac 
7635c58f1c22SToby Isaac   Input Parameters:
7636c58f1c22SToby Isaac + dm   - The DM object
7637c58f1c22SToby Isaac - name - The label name
7638c58f1c22SToby Isaac 
7639c58f1c22SToby Isaac   Output Parameter:
7640c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7641c58f1c22SToby Isaac 
7642c58f1c22SToby Isaac   Level: beginner
7643c58f1c22SToby Isaac 
7644db781477SPatrick Sanan .seealso: `DMLabelGetNumValues()`, `DMSetLabelValue()`
7645c58f1c22SToby Isaac @*/
7646c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7647c58f1c22SToby Isaac {
7648c58f1c22SToby Isaac   DMLabel        label;
7649c58f1c22SToby Isaac 
7650c58f1c22SToby Isaac   PetscFunctionBegin;
7651c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7652c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7653534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
76549566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7655c58f1c22SToby Isaac   *size = 0;
7656c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
76579566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
7658c58f1c22SToby Isaac   PetscFunctionReturn(0);
7659c58f1c22SToby Isaac }
7660c58f1c22SToby Isaac 
7661c58f1c22SToby Isaac /*@C
7662c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7663c58f1c22SToby Isaac 
7664c58f1c22SToby Isaac   Not Collective
7665c58f1c22SToby Isaac 
7666c58f1c22SToby Isaac   Input Parameters:
7667c58f1c22SToby Isaac + mesh - The DM object
7668c58f1c22SToby Isaac - name - The label name
7669c58f1c22SToby Isaac 
7670c58f1c22SToby Isaac   Output Parameter:
7671c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7672c58f1c22SToby Isaac 
7673c58f1c22SToby Isaac   Level: beginner
7674c58f1c22SToby Isaac 
7675db781477SPatrick Sanan .seealso: `DMLabelGetValueIS()`, `DMGetLabelSize()`
7676c58f1c22SToby Isaac @*/
7677c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7678c58f1c22SToby Isaac {
7679c58f1c22SToby Isaac   DMLabel        label;
7680c58f1c22SToby Isaac 
7681c58f1c22SToby Isaac   PetscFunctionBegin;
7682c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7683c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7684c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
76859566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7686c58f1c22SToby Isaac   *ids = NULL;
7687dab2e251SBlaise Bourdin   if (label) {
76889566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
7689dab2e251SBlaise Bourdin   } else {
7690dab2e251SBlaise Bourdin     /* returning an empty IS */
76919566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids));
7692dab2e251SBlaise Bourdin   }
7693c58f1c22SToby Isaac   PetscFunctionReturn(0);
7694c58f1c22SToby Isaac }
7695c58f1c22SToby Isaac 
7696c58f1c22SToby Isaac /*@C
7697c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7698c58f1c22SToby Isaac 
7699c58f1c22SToby Isaac   Not Collective
7700c58f1c22SToby Isaac 
7701c58f1c22SToby Isaac   Input Parameters:
7702c58f1c22SToby Isaac + dm - The DM object
7703c58f1c22SToby Isaac . name - The label name
7704c58f1c22SToby Isaac - value - The stratum value
7705c58f1c22SToby Isaac 
7706c58f1c22SToby Isaac   Output Parameter:
7707c58f1c22SToby Isaac . size - The stratum size
7708c58f1c22SToby Isaac 
7709c58f1c22SToby Isaac   Level: beginner
7710c58f1c22SToby Isaac 
7711db781477SPatrick Sanan .seealso: `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
7712c58f1c22SToby Isaac @*/
7713c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7714c58f1c22SToby Isaac {
7715c58f1c22SToby Isaac   DMLabel        label;
7716c58f1c22SToby Isaac 
7717c58f1c22SToby Isaac   PetscFunctionBegin;
7718c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7719c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7720534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
77219566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7722c58f1c22SToby Isaac   *size = 0;
7723c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
77249566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
7725c58f1c22SToby Isaac   PetscFunctionReturn(0);
7726c58f1c22SToby Isaac }
7727c58f1c22SToby Isaac 
7728c58f1c22SToby Isaac /*@C
7729c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7730c58f1c22SToby Isaac 
7731c58f1c22SToby Isaac   Not Collective
7732c58f1c22SToby Isaac 
7733c58f1c22SToby Isaac   Input Parameters:
7734c58f1c22SToby Isaac + dm - The DM object
7735c58f1c22SToby Isaac . name - The label name
7736c58f1c22SToby Isaac - value - The stratum value
7737c58f1c22SToby Isaac 
7738c58f1c22SToby Isaac   Output Parameter:
7739c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7740c58f1c22SToby Isaac 
7741c58f1c22SToby Isaac   Level: beginner
7742c58f1c22SToby Isaac 
7743db781477SPatrick Sanan .seealso: `DMLabelGetStratumIS()`, `DMGetStratumSize()`
7744c58f1c22SToby Isaac @*/
7745c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7746c58f1c22SToby Isaac {
7747c58f1c22SToby Isaac   DMLabel        label;
7748c58f1c22SToby Isaac 
7749c58f1c22SToby Isaac   PetscFunctionBegin;
7750c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7751c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7752c58f1c22SToby Isaac   PetscValidPointer(points, 4);
77539566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7754c58f1c22SToby Isaac   *points = NULL;
7755c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
77569566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
7757c58f1c22SToby Isaac   PetscFunctionReturn(0);
7758c58f1c22SToby Isaac }
7759c58f1c22SToby Isaac 
77604de306b1SToby Isaac /*@C
77619044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
77624de306b1SToby Isaac 
77634de306b1SToby Isaac   Not Collective
77644de306b1SToby Isaac 
77654de306b1SToby Isaac   Input Parameters:
77664de306b1SToby Isaac + dm - The DM object
77674de306b1SToby Isaac . name - The label name
77684de306b1SToby Isaac . value - The stratum value
77694de306b1SToby Isaac - points - The stratum points
77704de306b1SToby Isaac 
77714de306b1SToby Isaac   Level: beginner
77724de306b1SToby Isaac 
7773db781477SPatrick Sanan .seealso: `DMLabelSetStratumIS()`, `DMGetStratumSize()`
77744de306b1SToby Isaac @*/
77754de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
77764de306b1SToby Isaac {
77774de306b1SToby Isaac   DMLabel        label;
77784de306b1SToby Isaac 
77794de306b1SToby Isaac   PetscFunctionBegin;
77804de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77814de306b1SToby Isaac   PetscValidCharPointer(name, 2);
77824de306b1SToby Isaac   PetscValidPointer(points, 4);
77839566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
77844de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
77859566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
77864de306b1SToby Isaac   PetscFunctionReturn(0);
77874de306b1SToby Isaac }
77884de306b1SToby Isaac 
7789c58f1c22SToby Isaac /*@C
77907cf350d0SPierre Jolivet   DMClearLabelStratum - Remove all points from a stratum from a DMLabel
7791c58f1c22SToby Isaac 
7792c58f1c22SToby Isaac   Not Collective
7793c58f1c22SToby Isaac 
7794c58f1c22SToby Isaac   Input Parameters:
7795c58f1c22SToby Isaac + dm   - The DM object
7796c58f1c22SToby Isaac . name - The label name
7797c58f1c22SToby Isaac - value - The label value for this point
7798c58f1c22SToby Isaac 
7799c58f1c22SToby Isaac   Output Parameter:
7800c58f1c22SToby Isaac 
7801c58f1c22SToby Isaac   Level: beginner
7802c58f1c22SToby Isaac 
7803db781477SPatrick Sanan .seealso: `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
7804c58f1c22SToby Isaac @*/
7805c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7806c58f1c22SToby Isaac {
7807c58f1c22SToby Isaac   DMLabel        label;
7808c58f1c22SToby Isaac 
7809c58f1c22SToby Isaac   PetscFunctionBegin;
7810c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7811c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
78129566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7813c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
78149566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
7815c58f1c22SToby Isaac   PetscFunctionReturn(0);
7816c58f1c22SToby Isaac }
7817c58f1c22SToby Isaac 
7818c58f1c22SToby Isaac /*@
7819c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7820c58f1c22SToby Isaac 
7821c58f1c22SToby Isaac   Not Collective
7822c58f1c22SToby Isaac 
7823c58f1c22SToby Isaac   Input Parameter:
7824c58f1c22SToby Isaac . dm   - The DM object
7825c58f1c22SToby Isaac 
7826c58f1c22SToby Isaac   Output Parameter:
7827c58f1c22SToby Isaac . numLabels - the number of Labels
7828c58f1c22SToby Isaac 
7829c58f1c22SToby Isaac   Level: intermediate
7830c58f1c22SToby Isaac 
7831db781477SPatrick Sanan .seealso: `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7832c58f1c22SToby Isaac @*/
7833c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7834c58f1c22SToby Isaac {
78355d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7836c58f1c22SToby Isaac   PetscInt  n    = 0;
7837c58f1c22SToby Isaac 
7838c58f1c22SToby Isaac   PetscFunctionBegin;
7839c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7840534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7841c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7842c58f1c22SToby Isaac   *numLabels = n;
7843c58f1c22SToby Isaac   PetscFunctionReturn(0);
7844c58f1c22SToby Isaac }
7845c58f1c22SToby Isaac 
7846c58f1c22SToby Isaac /*@C
7847c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7848c58f1c22SToby Isaac 
7849c58f1c22SToby Isaac   Not Collective
7850c58f1c22SToby Isaac 
7851c58f1c22SToby Isaac   Input Parameters:
7852c58f1c22SToby Isaac + dm - The DM object
7853c58f1c22SToby Isaac - n  - the label number
7854c58f1c22SToby Isaac 
7855c58f1c22SToby Isaac   Output Parameter:
7856c58f1c22SToby Isaac . name - the label name
7857c58f1c22SToby Isaac 
7858c58f1c22SToby Isaac   Level: intermediate
7859c58f1c22SToby Isaac 
7860db781477SPatrick Sanan .seealso: `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7861c58f1c22SToby Isaac @*/
7862c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7863c58f1c22SToby Isaac {
78645d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7865c58f1c22SToby Isaac   PetscInt       l    = 0;
7866c58f1c22SToby Isaac 
7867c58f1c22SToby Isaac   PetscFunctionBegin;
7868c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7869c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7870c58f1c22SToby Isaac   while (next) {
7871c58f1c22SToby Isaac     if (l == n) {
78729566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject) next->label, name));
7873c58f1c22SToby Isaac       PetscFunctionReturn(0);
7874c58f1c22SToby Isaac     }
7875c58f1c22SToby Isaac     ++l;
7876c58f1c22SToby Isaac     next = next->next;
7877c58f1c22SToby Isaac   }
787863a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7879c58f1c22SToby Isaac }
7880c58f1c22SToby Isaac 
7881c58f1c22SToby Isaac /*@C
7882c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7883c58f1c22SToby Isaac 
7884c58f1c22SToby Isaac   Not Collective
7885c58f1c22SToby Isaac 
7886c58f1c22SToby Isaac   Input Parameters:
7887c58f1c22SToby Isaac + dm   - The DM object
7888c58f1c22SToby Isaac - name - The label name
7889c58f1c22SToby Isaac 
7890c58f1c22SToby Isaac   Output Parameter:
7891c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7892c58f1c22SToby Isaac 
7893c58f1c22SToby Isaac   Level: intermediate
7894c58f1c22SToby Isaac 
7895db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7896c58f1c22SToby Isaac @*/
7897c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7898c58f1c22SToby Isaac {
78995d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7900d67d17b1SMatthew G. Knepley   const char    *lname;
7901c58f1c22SToby Isaac 
7902c58f1c22SToby Isaac   PetscFunctionBegin;
7903c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7904c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7905534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7906c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7907c58f1c22SToby Isaac   while (next) {
79089566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
79099566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7910c58f1c22SToby Isaac     if (*hasLabel) break;
7911c58f1c22SToby Isaac     next = next->next;
7912c58f1c22SToby Isaac   }
7913c58f1c22SToby Isaac   PetscFunctionReturn(0);
7914c58f1c22SToby Isaac }
7915c58f1c22SToby Isaac 
7916c58f1c22SToby Isaac /*@C
7917c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7918c58f1c22SToby Isaac 
7919c58f1c22SToby Isaac   Not Collective
7920c58f1c22SToby Isaac 
7921c58f1c22SToby Isaac   Input Parameters:
7922c58f1c22SToby Isaac + dm   - The DM object
7923c58f1c22SToby Isaac - name - The label name
7924c58f1c22SToby Isaac 
7925c58f1c22SToby Isaac   Output Parameter:
7926c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7927c58f1c22SToby Isaac 
79286d7c9049SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
79296d7c9049SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
79306d7c9049SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
79316d7c9049SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
79326d7c9049SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
79336d7c9049SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
79346d7c9049SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
79356d7c9049SMatthew G. Knepley 
7936c58f1c22SToby Isaac   Level: intermediate
7937c58f1c22SToby Isaac 
7938db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7939c58f1c22SToby Isaac @*/
7940c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7941c58f1c22SToby Isaac {
79425d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7943c58f1c22SToby Isaac   PetscBool      hasLabel;
7944d67d17b1SMatthew G. Knepley   const char    *lname;
7945c58f1c22SToby Isaac 
7946c58f1c22SToby Isaac   PetscFunctionBegin;
7947c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7948c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7949c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7950c58f1c22SToby Isaac   *label = NULL;
7951c58f1c22SToby Isaac   while (next) {
79529566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
79539566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7954c58f1c22SToby Isaac     if (hasLabel) {
7955c58f1c22SToby Isaac       *label = next->label;
7956c58f1c22SToby Isaac       break;
7957c58f1c22SToby Isaac     }
7958c58f1c22SToby Isaac     next = next->next;
7959c58f1c22SToby Isaac   }
7960c58f1c22SToby Isaac   PetscFunctionReturn(0);
7961c58f1c22SToby Isaac }
7962c58f1c22SToby Isaac 
7963c58f1c22SToby Isaac /*@C
7964c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7965c58f1c22SToby Isaac 
7966c58f1c22SToby Isaac   Not Collective
7967c58f1c22SToby Isaac 
7968c58f1c22SToby Isaac   Input Parameters:
7969c58f1c22SToby Isaac + dm - The DM object
7970c58f1c22SToby Isaac - n  - the label number
7971c58f1c22SToby Isaac 
7972c58f1c22SToby Isaac   Output Parameter:
7973c58f1c22SToby Isaac . label - the label
7974c58f1c22SToby Isaac 
7975c58f1c22SToby Isaac   Level: intermediate
7976c58f1c22SToby Isaac 
7977db781477SPatrick Sanan .seealso: `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7978c58f1c22SToby Isaac @*/
7979c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7980c58f1c22SToby Isaac {
79815d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7982c58f1c22SToby Isaac   PetscInt    l    = 0;
7983c58f1c22SToby Isaac 
7984c58f1c22SToby Isaac   PetscFunctionBegin;
7985c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7986c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7987c58f1c22SToby Isaac   while (next) {
7988c58f1c22SToby Isaac     if (l == n) {
7989c58f1c22SToby Isaac       *label = next->label;
7990c58f1c22SToby Isaac       PetscFunctionReturn(0);
7991c58f1c22SToby Isaac     }
7992c58f1c22SToby Isaac     ++l;
7993c58f1c22SToby Isaac     next = next->next;
7994c58f1c22SToby Isaac   }
799563a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7996c58f1c22SToby Isaac }
7997c58f1c22SToby Isaac 
7998c58f1c22SToby Isaac /*@C
7999c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
8000c58f1c22SToby Isaac 
8001c58f1c22SToby Isaac   Not Collective
8002c58f1c22SToby Isaac 
8003c58f1c22SToby Isaac   Input Parameters:
8004c58f1c22SToby Isaac + dm   - The DM object
8005c58f1c22SToby Isaac - label - The DMLabel
8006c58f1c22SToby Isaac 
8007c58f1c22SToby Isaac   Level: developer
8008c58f1c22SToby Isaac 
8009db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
8010c58f1c22SToby Isaac @*/
8011c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
8012c58f1c22SToby Isaac {
80135d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
8014c58f1c22SToby Isaac   PetscBool      hasLabel;
8015d67d17b1SMatthew G. Knepley   const char    *lname;
80165d80c0bfSVaclav Hapla   PetscBool      flg;
8017c58f1c22SToby Isaac 
8018c58f1c22SToby Isaac   PetscFunctionBegin;
8019c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80209566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject) label, &lname));
80219566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
80227a8be351SBarry Smith   PetscCheck(!hasLabel,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
80239566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
8024c58f1c22SToby Isaac   tmpLabel->label  = label;
8025c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
80265d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
80275d80c0bfSVaclav Hapla   *p = tmpLabel;
80289566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
80299566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
80305d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
80319566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
8032ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
8033c58f1c22SToby Isaac   PetscFunctionReturn(0);
8034c58f1c22SToby Isaac }
8035c58f1c22SToby Isaac 
8036c58f1c22SToby Isaac /*@C
80374a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
80384a7ee7d0SMatthew G. Knepley 
80394a7ee7d0SMatthew G. Knepley   Not Collective
80404a7ee7d0SMatthew G. Knepley 
80414a7ee7d0SMatthew G. Knepley   Input Parameters:
80424a7ee7d0SMatthew G. Knepley + dm    - The DM object
80434a7ee7d0SMatthew G. Knepley - label - The DMLabel, having the same name, to substitute
80444a7ee7d0SMatthew G. Knepley 
80454a7ee7d0SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
80464a7ee7d0SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
80474a7ee7d0SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
80484a7ee7d0SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
80494a7ee7d0SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
80504a7ee7d0SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
80514a7ee7d0SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
80524a7ee7d0SMatthew G. Knepley 
80534a7ee7d0SMatthew G. Knepley   Level: intermediate
80544a7ee7d0SMatthew G. Knepley 
8055db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
80564a7ee7d0SMatthew G. Knepley @*/
80574a7ee7d0SMatthew G. Knepley PetscErrorCode DMSetLabel(DM dm, DMLabel label)
80584a7ee7d0SMatthew G. Knepley {
80594a7ee7d0SMatthew G. Knepley   DMLabelLink    next = dm->labels;
80604a7ee7d0SMatthew G. Knepley   PetscBool      hasLabel, flg;
80614a7ee7d0SMatthew G. Knepley   const char    *name, *lname;
80624a7ee7d0SMatthew G. Knepley 
80634a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
80644a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80654a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
80669566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject) label, &name));
80674a7ee7d0SMatthew G. Knepley   while (next) {
80689566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
80699566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
80704a7ee7d0SMatthew G. Knepley     if (hasLabel) {
80719566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject) label));
80729566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
80734a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
80749566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
80754a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
80769566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
80774a7ee7d0SMatthew G. Knepley       next->label = label;
80784a7ee7d0SMatthew G. Knepley       break;
80794a7ee7d0SMatthew G. Knepley     }
80804a7ee7d0SMatthew G. Knepley     next = next->next;
80814a7ee7d0SMatthew G. Knepley   }
80824a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
80834a7ee7d0SMatthew G. Knepley }
80844a7ee7d0SMatthew G. Knepley 
80854a7ee7d0SMatthew G. Knepley /*@C
8086e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
8087c58f1c22SToby Isaac 
8088c58f1c22SToby Isaac   Not Collective
8089c58f1c22SToby Isaac 
8090c58f1c22SToby Isaac   Input Parameters:
8091c58f1c22SToby Isaac + dm   - The DM object
8092c58f1c22SToby Isaac - name - The label name
8093c58f1c22SToby Isaac 
8094c58f1c22SToby Isaac   Output Parameter:
8095c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
8096c58f1c22SToby Isaac 
8097c58f1c22SToby Isaac   Level: developer
8098c58f1c22SToby Isaac 
8099e5472504SVaclav Hapla   Notes:
8100e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
8101e5472504SVaclav Hapla   DMLabelDestroy() on the label.
8102e5472504SVaclav Hapla 
8103e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
8104e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
8105e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
8106e5472504SVaclav Hapla 
8107db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
8108c58f1c22SToby Isaac @*/
8109c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
8110c58f1c22SToby Isaac {
811195d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
8112c58f1c22SToby Isaac   PetscBool      hasLabel;
8113d67d17b1SMatthew G. Knepley   const char    *lname;
8114c58f1c22SToby Isaac 
8115c58f1c22SToby Isaac   PetscFunctionBegin;
8116c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8117e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
8118e5472504SVaclav Hapla   if (label) {
8119e5472504SVaclav Hapla     PetscValidPointer(label, 3);
8120c58f1c22SToby Isaac     *label = NULL;
8121e5472504SVaclav Hapla   }
81225d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
81239566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) link->label, &lname));
81249566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
8125c58f1c22SToby Isaac     if (hasLabel) {
812695d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
81279566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
812895d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
81299566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
8130ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
813195d578d6SVaclav Hapla       if (label) *label = link->label;
81329566063dSJacob Faibussowitsch       else       PetscCall(DMLabelDestroy(&link->label));
81339566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
8134c58f1c22SToby Isaac       break;
8135c58f1c22SToby Isaac     }
8136c58f1c22SToby Isaac   }
8137c58f1c22SToby Isaac   PetscFunctionReturn(0);
8138c58f1c22SToby Isaac }
8139c58f1c22SToby Isaac 
8140306894acSVaclav Hapla /*@
8141306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
8142306894acSVaclav Hapla 
8143306894acSVaclav Hapla   Not Collective
8144306894acSVaclav Hapla 
8145306894acSVaclav Hapla   Input Parameters:
8146306894acSVaclav Hapla + dm   - The DM object
8147876aa926SVaclav Hapla . label - The DMLabel to be removed from the DM
8148306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
8149306894acSVaclav Hapla 
8150306894acSVaclav Hapla   Level: developer
8151306894acSVaclav Hapla 
8152306894acSVaclav Hapla   Notes:
8153306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
8154306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
8155306894acSVaclav Hapla   *label nullified.
8156306894acSVaclav Hapla 
8157db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
8158306894acSVaclav Hapla @*/
8159306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
8160306894acSVaclav Hapla {
816143e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
8162306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
8163306894acSVaclav Hapla 
8164306894acSVaclav Hapla   PetscFunctionBegin;
8165306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8166306894acSVaclav Hapla   PetscValidPointer(label, 2);
8167f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
8168306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
8169306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
81705d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
817143e45a93SVaclav Hapla     if (*label == link->label) {
8172306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
817343e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
8174306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
8175ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
817643e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
81779566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
81789566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
8179306894acSVaclav Hapla       break;
8180306894acSVaclav Hapla     }
8181306894acSVaclav Hapla   }
81827a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
8183306894acSVaclav Hapla   PetscFunctionReturn(0);
8184306894acSVaclav Hapla }
8185306894acSVaclav Hapla 
8186c58f1c22SToby Isaac /*@C
8187c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
8188c58f1c22SToby Isaac 
8189c58f1c22SToby Isaac   Not Collective
8190c58f1c22SToby Isaac 
8191c58f1c22SToby Isaac   Input Parameters:
8192c58f1c22SToby Isaac + dm   - The DM object
8193c58f1c22SToby Isaac - name - The label name
8194c58f1c22SToby Isaac 
8195c58f1c22SToby Isaac   Output Parameter:
8196c58f1c22SToby Isaac . output - The flag for output
8197c58f1c22SToby Isaac 
8198c58f1c22SToby Isaac   Level: developer
8199c58f1c22SToby Isaac 
8200db781477SPatrick Sanan .seealso: `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
8201c58f1c22SToby Isaac @*/
8202c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
8203c58f1c22SToby Isaac {
82045d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8205d67d17b1SMatthew G. Knepley   const char    *lname;
8206c58f1c22SToby Isaac 
8207c58f1c22SToby Isaac   PetscFunctionBegin;
8208c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8209dadcf809SJacob Faibussowitsch   PetscValidCharPointer(name, 2);
8210dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(output, 3);
8211c58f1c22SToby Isaac   while (next) {
8212c58f1c22SToby Isaac     PetscBool flg;
8213c58f1c22SToby Isaac 
82149566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
82159566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
8216c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
8217c58f1c22SToby Isaac     next = next->next;
8218c58f1c22SToby Isaac   }
821998921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8220c58f1c22SToby Isaac }
8221c58f1c22SToby Isaac 
8222c58f1c22SToby Isaac /*@C
8223c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
8224c58f1c22SToby Isaac 
8225c58f1c22SToby Isaac   Not Collective
8226c58f1c22SToby Isaac 
8227c58f1c22SToby Isaac   Input Parameters:
8228c58f1c22SToby Isaac + dm     - The DM object
8229c58f1c22SToby Isaac . name   - The label name
8230c58f1c22SToby Isaac - output - The flag for output
8231c58f1c22SToby Isaac 
8232c58f1c22SToby Isaac   Level: developer
8233c58f1c22SToby Isaac 
8234db781477SPatrick Sanan .seealso: `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
8235c58f1c22SToby Isaac @*/
8236c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
8237c58f1c22SToby Isaac {
82385d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8239d67d17b1SMatthew G. Knepley   const char    *lname;
8240c58f1c22SToby Isaac 
8241c58f1c22SToby Isaac   PetscFunctionBegin;
8242c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8243534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
8244c58f1c22SToby Isaac   while (next) {
8245c58f1c22SToby Isaac     PetscBool flg;
8246c58f1c22SToby Isaac 
82479566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
82489566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
8249c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
8250c58f1c22SToby Isaac     next = next->next;
8251c58f1c22SToby Isaac   }
825298921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8253c58f1c22SToby Isaac }
8254c58f1c22SToby Isaac 
8255c58f1c22SToby Isaac /*@
8256c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
8257c58f1c22SToby Isaac 
8258d083f849SBarry Smith   Collective on dmA
8259c58f1c22SToby Isaac 
8260d8d19677SJose E. Roman   Input Parameters:
82615d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
82622cbb9b06SVaclav Hapla . dmB - The DM object to which labels are copied
82635d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
82642cbb9b06SVaclav Hapla . all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
82652cbb9b06SVaclav Hapla - emode - How to behave when a DMLabel in the source and destination DMs with the same name is encountered (see DMCopyLabelsMode)
8266c58f1c22SToby Isaac 
8267c58f1c22SToby Isaac   Level: intermediate
8268c58f1c22SToby Isaac 
82692cbb9b06SVaclav Hapla   Notes:
82702cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
8271c58f1c22SToby Isaac 
8272db781477SPatrick Sanan .seealso: `DMAddLabel()`, `DMCopyLabelsMode`
8273c58f1c22SToby Isaac @*/
82742cbb9b06SVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
8275c58f1c22SToby Isaac {
82762cbb9b06SVaclav Hapla   DMLabel        label, labelNew, labelOld;
8277c58f1c22SToby Isaac   const char    *name;
8278c58f1c22SToby Isaac   PetscBool      flg;
82795d80c0bfSVaclav Hapla   DMLabelLink    link;
8280c58f1c22SToby Isaac 
82815d80c0bfSVaclav Hapla   PetscFunctionBegin;
82825d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
82835d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
82845d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
82855d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
82867a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER,PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
82875d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
82885d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
82895d80c0bfSVaclav Hapla     label=link->label;
82909566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
82915d80c0bfSVaclav Hapla     if (!all) {
82929566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
8293c58f1c22SToby Isaac       if (flg) continue;
82949566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
82957d5acc75SStefano Zampini       if (flg) continue;
82969566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
8297ba2698f1SMatthew G. Knepley       if (flg) continue;
82985d80c0bfSVaclav Hapla     }
82999566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
83002cbb9b06SVaclav Hapla     if (labelOld) {
83012cbb9b06SVaclav Hapla       switch (emode) {
83022cbb9b06SVaclav Hapla         case DM_COPY_LABELS_KEEP:
83032cbb9b06SVaclav Hapla           continue;
83042cbb9b06SVaclav Hapla         case DM_COPY_LABELS_REPLACE:
83059566063dSJacob Faibussowitsch           PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
83062cbb9b06SVaclav Hapla           break;
83072cbb9b06SVaclav Hapla         case DM_COPY_LABELS_FAIL:
830898921bdaSJacob Faibussowitsch           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
83092cbb9b06SVaclav Hapla         default:
83107a8be351SBarry Smith           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
83112cbb9b06SVaclav Hapla       }
83122cbb9b06SVaclav Hapla     }
83135d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
83149566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
83155d80c0bfSVaclav Hapla     } else {
83165d80c0bfSVaclav Hapla       labelNew = label;
83175d80c0bfSVaclav Hapla     }
83189566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
83199566063dSJacob Faibussowitsch     if (mode==PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
8320c58f1c22SToby Isaac   }
8321c58f1c22SToby Isaac   PetscFunctionReturn(0);
8322c58f1c22SToby Isaac }
8323461a15a0SLisandro Dalcin 
8324609dae6eSVaclav Hapla /*@C
8325609dae6eSVaclav Hapla   DMCompareLabels - Compare labels of two DMPlex meshes
8326609dae6eSVaclav Hapla 
83275efe38ccSVaclav Hapla   Collective
8328609dae6eSVaclav Hapla 
8329609dae6eSVaclav Hapla   Input Parameters:
8330609dae6eSVaclav Hapla + dm0 - First DM object
8331609dae6eSVaclav Hapla - dm1 - Second DM object
8332609dae6eSVaclav Hapla 
8333609dae6eSVaclav Hapla   Output Parameters
83345efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
8335609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
8336609dae6eSVaclav Hapla 
8337609dae6eSVaclav Hapla   Level: intermediate
8338609dae6eSVaclav Hapla 
8339609dae6eSVaclav Hapla   Notes:
83405efe38ccSVaclav Hapla   The output flag equal is the same on all processes.
83415efe38ccSVaclav Hapla   If it is passed as NULL and difference is found, an error is thrown on all processes.
83425efe38ccSVaclav Hapla   Make sure to pass NULL on all processes.
8343609dae6eSVaclav Hapla 
83445efe38ccSVaclav Hapla   The output message is set independently on each rank.
83455efe38ccSVaclav Hapla   It is set to NULL if no difference was found on the current rank. It must be freed by user.
83465efe38ccSVaclav Hapla   If message is passed as NULL and difference is found, the difference description is printed to stderr in synchronized manner.
83475efe38ccSVaclav Hapla   Make sure to pass NULL on all processes.
8348609dae6eSVaclav Hapla 
8349609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
8350609dae6eSVaclav Hapla   DMLabelCompare() is used to compare each pair of labels with the same name.
8351609dae6eSVaclav Hapla 
8352609dae6eSVaclav Hapla   Fortran Notes:
8353609dae6eSVaclav Hapla   This function is currently not available from Fortran.
8354609dae6eSVaclav Hapla 
8355db781477SPatrick Sanan .seealso: `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
8356609dae6eSVaclav Hapla @*/
83575efe38ccSVaclav Hapla PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
8358609dae6eSVaclav Hapla {
83595efe38ccSVaclav Hapla   PetscInt        n, i;
8360609dae6eSVaclav Hapla   char            msg[PETSC_MAX_PATH_LEN] = "";
83615efe38ccSVaclav Hapla   PetscBool       eq;
8362609dae6eSVaclav Hapla   MPI_Comm        comm;
83635efe38ccSVaclav Hapla   PetscMPIInt     rank;
8364609dae6eSVaclav Hapla 
8365609dae6eSVaclav Hapla   PetscFunctionBegin;
8366609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0,DM_CLASSID,1);
8367609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1,DM_CLASSID,2);
8368609dae6eSVaclav Hapla   PetscCheckSameComm(dm0,1,dm1,2);
83695efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal,3);
8370609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
83719566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
83729566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
83735efe38ccSVaclav Hapla   {
83745efe38ccSVaclav Hapla     PetscInt n1;
83755efe38ccSVaclav Hapla 
83769566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
83779566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
83785efe38ccSVaclav Hapla     eq = (PetscBool) (n == n1);
83795efe38ccSVaclav Hapla     if (!eq) {
838063a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
8381609dae6eSVaclav Hapla     }
83829566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
83835efe38ccSVaclav Hapla     if (!eq) goto finish;
83845efe38ccSVaclav Hapla   }
83855efe38ccSVaclav Hapla   for (i=0; i<n; i++) {
8386609dae6eSVaclav Hapla     DMLabel     l0, l1;
8387609dae6eSVaclav Hapla     const char *name;
8388609dae6eSVaclav Hapla     char       *msgInner;
8389609dae6eSVaclav Hapla 
8390609dae6eSVaclav Hapla     /* Ignore label order */
83919566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
83929566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
83939566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
8394609dae6eSVaclav Hapla     if (!l1) {
839563a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
83965efe38ccSVaclav Hapla       eq = PETSC_FALSE;
83975efe38ccSVaclav Hapla       break;
8398609dae6eSVaclav Hapla     }
83999566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
84009566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
84019566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
84025efe38ccSVaclav Hapla     if (!eq) break;
8403609dae6eSVaclav Hapla   }
84049566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
8405609dae6eSVaclav Hapla finish:
84065efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
8407609dae6eSVaclav Hapla   if (message) {
8408609dae6eSVaclav Hapla     *message = NULL;
8409609dae6eSVaclav Hapla     if (msg[0]) {
84109566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(msg, message));
8411609dae6eSVaclav Hapla     }
84125efe38ccSVaclav Hapla   } else {
84135efe38ccSVaclav Hapla     if (msg[0]) {
84149566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
8415609dae6eSVaclav Hapla     }
84169566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
84175efe38ccSVaclav Hapla   }
84185efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
84195efe38ccSVaclav Hapla   if (equal) *equal = eq;
84207a8be351SBarry Smith   else PetscCheck(eq,comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
8421609dae6eSVaclav Hapla   PetscFunctionReturn(0);
8422609dae6eSVaclav Hapla }
8423609dae6eSVaclav Hapla 
8424461a15a0SLisandro Dalcin PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
8425461a15a0SLisandro Dalcin {
8426461a15a0SLisandro Dalcin   PetscFunctionBegin;
8427461a15a0SLisandro Dalcin   PetscValidPointer(label,2);
8428461a15a0SLisandro Dalcin   if (!*label) {
84299566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
84309566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
8431461a15a0SLisandro Dalcin   }
84329566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
8433461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
8434461a15a0SLisandro Dalcin }
8435461a15a0SLisandro Dalcin 
84360fdc7489SMatthew Knepley /*
84370fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
84380fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
84390fdc7489SMatthew Knepley   (label, id) pair in the DM.
84400fdc7489SMatthew Knepley 
84410fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
84420fdc7489SMatthew Knepley   each label.
84430fdc7489SMatthew Knepley */
84440fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
84450fdc7489SMatthew Knepley {
84460fdc7489SMatthew Knepley   DMUniversalLabel ul;
84470fdc7489SMatthew Knepley   PetscBool       *active;
84480fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
84490fdc7489SMatthew Knepley 
84500fdc7489SMatthew Knepley   PetscFunctionBegin;
84519566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
84529566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
84539566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
84549566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
84550fdc7489SMatthew Knepley   ul->Nl = 0;
84560fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
84570fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
84580fdc7489SMatthew Knepley     const char *name;
84590fdc7489SMatthew Knepley 
84609566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
84619566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
84629566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
84630fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
84640fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
84650fdc7489SMatthew Knepley   }
84669566063dSJacob 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));
84670fdc7489SMatthew Knepley   ul->Nv = 0;
84680fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
84690fdc7489SMatthew Knepley     DMLabel     label;
84700fdc7489SMatthew Knepley     PetscInt    nv;
84710fdc7489SMatthew Knepley     const char *name;
84720fdc7489SMatthew Knepley 
84730fdc7489SMatthew Knepley     if (!active[l]) continue;
84749566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
84759566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
84769566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
84779566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
84780fdc7489SMatthew Knepley     ul->indices[m]   = l;
84790fdc7489SMatthew Knepley     ul->Nv          += nv;
84800fdc7489SMatthew Knepley     ul->offsets[m+1] = nv;
84810fdc7489SMatthew Knepley     ul->bits[m+1]    = PetscCeilReal(PetscLog2Real(nv+1));
84820fdc7489SMatthew Knepley     ++m;
84830fdc7489SMatthew Knepley   }
84840fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
84850fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l-1] + ul->offsets[l];
84860fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l-1]    + ul->bits[l];
84870fdc7489SMatthew Knepley   }
84880fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
84890fdc7489SMatthew Knepley     PetscInt b;
84900fdc7489SMatthew Knepley 
84910fdc7489SMatthew Knepley     ul->masks[l] = 0;
84920fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l+1]; ++b) ul->masks[l] |= 1 << b;
84930fdc7489SMatthew Knepley   }
84949566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
84950fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
84960fdc7489SMatthew Knepley     DMLabel         label;
84970fdc7489SMatthew Knepley     IS              valueIS;
84980fdc7489SMatthew Knepley     const PetscInt *varr;
84990fdc7489SMatthew Knepley     PetscInt        nv, v;
85000fdc7489SMatthew Knepley 
85010fdc7489SMatthew Knepley     if (!active[l]) continue;
85029566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
85039566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
85049566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
85059566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
85060fdc7489SMatthew Knepley     for (v = 0; v < nv; ++v) {
85070fdc7489SMatthew Knepley       ul->values[ul->offsets[m]+v] = varr[v];
85080fdc7489SMatthew Knepley     }
85099566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
85109566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
85119566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
85120fdc7489SMatthew Knepley     ++m;
85130fdc7489SMatthew Knepley   }
85149566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
85150fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
85160fdc7489SMatthew Knepley     PetscInt  uval = 0;
85170fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
85180fdc7489SMatthew Knepley 
85190fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
85200fdc7489SMatthew Knepley       DMLabel  label;
85210649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
85220fdc7489SMatthew Knepley 
85230fdc7489SMatthew Knepley       if (!active[l]) continue;
85249566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
85259566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
85269566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
85270fdc7489SMatthew Knepley       if (val == defval) {++m; continue;}
85280649b39aSStefano Zampini       nv = ul->offsets[m+1]-ul->offsets[m];
85290fdc7489SMatthew Knepley       marked = PETSC_TRUE;
85309566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
853163a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
85320fdc7489SMatthew Knepley       uval += (loc+1) << ul->bits[m];
85330fdc7489SMatthew Knepley       ++m;
85340fdc7489SMatthew Knepley     }
85359566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
85360fdc7489SMatthew Knepley   }
85379566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
85380fdc7489SMatthew Knepley   *universal = ul;
85390fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85400fdc7489SMatthew Knepley }
85410fdc7489SMatthew Knepley 
85420fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
85430fdc7489SMatthew Knepley {
85440fdc7489SMatthew Knepley   PetscInt       l;
85450fdc7489SMatthew Knepley 
85460fdc7489SMatthew Knepley   PetscFunctionBegin;
85479566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
85489566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
85499566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
85509566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
85519566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
85520fdc7489SMatthew Knepley   *universal = NULL;
85530fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85540fdc7489SMatthew Knepley }
85550fdc7489SMatthew Knepley 
85560fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
85570fdc7489SMatthew Knepley {
85580fdc7489SMatthew Knepley   PetscFunctionBegin;
85590fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
85600fdc7489SMatthew Knepley   *ulabel = ul->label;
85610fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85620fdc7489SMatthew Knepley }
85630fdc7489SMatthew Knepley 
85640fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
85650fdc7489SMatthew Knepley {
85660fdc7489SMatthew Knepley   PetscInt       Nl = ul->Nl, l;
85670fdc7489SMatthew Knepley 
85680fdc7489SMatthew Knepley   PetscFunctionBegin;
8569064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
85700fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
85719566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
85729566063dSJacob Faibussowitsch     else               PetscCall(DMCreateLabel(dm, ul->names[l]));
85730fdc7489SMatthew Knepley   }
85740fdc7489SMatthew Knepley   if (preserveOrder) {
85750fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
85760fdc7489SMatthew Knepley       const char *name;
85770fdc7489SMatthew Knepley       PetscBool   match;
85780fdc7489SMatthew Knepley 
85799566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
85809566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
858163a3b9bcSJacob 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]);
85820fdc7489SMatthew Knepley     }
85830fdc7489SMatthew Knepley   }
85840fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85850fdc7489SMatthew Knepley }
85860fdc7489SMatthew Knepley 
85870fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
85880fdc7489SMatthew Knepley {
85890fdc7489SMatthew Knepley   PetscInt       l;
85900fdc7489SMatthew Knepley 
85910fdc7489SMatthew Knepley   PetscFunctionBegin;
85920fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
85930fdc7489SMatthew Knepley     DMLabel  label;
85940fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
85950fdc7489SMatthew Knepley 
85960fdc7489SMatthew Knepley     if (lval) {
85979566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
85989566063dSJacob Faibussowitsch       else          PetscCall(DMGetLabel(dm, ul->names[l], &label));
85999566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l]+lval-1]));
86000fdc7489SMatthew Knepley     }
86010fdc7489SMatthew Knepley   }
86020fdc7489SMatthew Knepley   PetscFunctionReturn(0);
86030fdc7489SMatthew Knepley }
8604a8fb8f29SToby Isaac 
8605a8fb8f29SToby Isaac /*@
8606a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
8607a8fb8f29SToby Isaac 
8608a8fb8f29SToby Isaac   Input Parameter:
8609a8fb8f29SToby Isaac . dm - The DM object
8610a8fb8f29SToby Isaac 
8611a8fb8f29SToby Isaac   Output Parameter:
8612a8fb8f29SToby Isaac . cdm - The coarse DM
8613a8fb8f29SToby Isaac 
8614a8fb8f29SToby Isaac   Level: intermediate
8615a8fb8f29SToby Isaac 
8616db781477SPatrick Sanan .seealso: `DMSetCoarseDM()`
8617a8fb8f29SToby Isaac @*/
8618a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
8619a8fb8f29SToby Isaac {
8620a8fb8f29SToby Isaac   PetscFunctionBegin;
8621a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8622a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
8623a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
8624a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8625a8fb8f29SToby Isaac }
8626a8fb8f29SToby Isaac 
8627a8fb8f29SToby Isaac /*@
8628a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
8629a8fb8f29SToby Isaac 
8630a8fb8f29SToby Isaac   Input Parameters:
8631a8fb8f29SToby Isaac + dm - The DM object
8632a8fb8f29SToby Isaac - cdm - The coarse DM
8633a8fb8f29SToby Isaac 
8634a8fb8f29SToby Isaac   Level: intermediate
8635a8fb8f29SToby Isaac 
8636db781477SPatrick Sanan .seealso: `DMGetCoarseDM()`
8637a8fb8f29SToby Isaac @*/
8638a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
8639a8fb8f29SToby Isaac {
8640a8fb8f29SToby Isaac   PetscFunctionBegin;
8641a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8642a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
86439566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
86449566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
8645a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
8646a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8647a8fb8f29SToby Isaac }
8648a8fb8f29SToby Isaac 
864988bdff64SToby Isaac /*@
865088bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
865188bdff64SToby Isaac 
865288bdff64SToby Isaac   Input Parameter:
865388bdff64SToby Isaac . dm - The DM object
865488bdff64SToby Isaac 
865588bdff64SToby Isaac   Output Parameter:
865688bdff64SToby Isaac . fdm - The fine DM
865788bdff64SToby Isaac 
865888bdff64SToby Isaac   Level: intermediate
865988bdff64SToby Isaac 
8660db781477SPatrick Sanan .seealso: `DMSetFineDM()`
866188bdff64SToby Isaac @*/
866288bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
866388bdff64SToby Isaac {
866488bdff64SToby Isaac   PetscFunctionBegin;
866588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
866688bdff64SToby Isaac   PetscValidPointer(fdm, 2);
866788bdff64SToby Isaac   *fdm = dm->fineMesh;
866888bdff64SToby Isaac   PetscFunctionReturn(0);
866988bdff64SToby Isaac }
867088bdff64SToby Isaac 
867188bdff64SToby Isaac /*@
867288bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
867388bdff64SToby Isaac 
867488bdff64SToby Isaac   Input Parameters:
867588bdff64SToby Isaac + dm - The DM object
867688bdff64SToby Isaac - fdm - The fine DM
867788bdff64SToby Isaac 
867888bdff64SToby Isaac   Level: intermediate
867988bdff64SToby Isaac 
8680db781477SPatrick Sanan .seealso: `DMGetFineDM()`
868188bdff64SToby Isaac @*/
868288bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
868388bdff64SToby Isaac {
868488bdff64SToby Isaac   PetscFunctionBegin;
868588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
868688bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
86879566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
86889566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
868988bdff64SToby Isaac   dm->fineMesh = fdm;
869088bdff64SToby Isaac   PetscFunctionReturn(0);
869188bdff64SToby Isaac }
869288bdff64SToby Isaac 
8693a6ba4734SToby Isaac /*=== DMBoundary code ===*/
8694a6ba4734SToby Isaac 
8695a6ba4734SToby Isaac /*@C
8696a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8697a6ba4734SToby Isaac 
8698783e2ec8SMatthew G. Knepley   Collective on dm
8699783e2ec8SMatthew G. Knepley 
8700a6ba4734SToby Isaac   Input Parameters:
87014c258f51SMatthew G. Knepley + dm       - The DM, with a PetscDS that matches the problem being constrained
8702f971fd6bSMatthew G. Knepley . type     - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8703a6ba4734SToby Isaac . name     - The BC name
870445480ffeSMatthew G. Knepley . label    - The label defining constrained points
870545480ffeSMatthew G. Knepley . Nv       - The number of DMLabel values for constrained points
870645480ffeSMatthew G. Knepley . values   - An array of values for constrained points
8707a6ba4734SToby Isaac . field    - The field to constrain
870845480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
8709a6ba4734SToby Isaac . comps    - An array of constrained component numbers
8710a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
871156cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
8712a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
8713a6ba4734SToby Isaac 
871445480ffeSMatthew G. Knepley   Output Parameter:
871545480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
871645480ffeSMatthew G. Knepley 
8717a6ba4734SToby Isaac   Options Database Keys:
8718a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8719a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8720a6ba4734SToby Isaac 
872156cf3b9cSMatthew G. Knepley   Note:
872256cf3b9cSMatthew 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:
872356cf3b9cSMatthew G. Knepley 
872456cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
872556cf3b9cSMatthew G. Knepley 
872656cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
872756cf3b9cSMatthew G. Knepley 
872856cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
872956cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
873056cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
873156cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
873256cf3b9cSMatthew G. Knepley 
873356cf3b9cSMatthew G. Knepley + dim - the spatial dimension
873456cf3b9cSMatthew G. Knepley . Nf - the number of fields
873556cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
873656cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
873756cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
873856cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
873956cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
874056cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
874156cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
874256cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
874356cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
874456cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
874556cf3b9cSMatthew G. Knepley . t - current time
874656cf3b9cSMatthew G. Knepley . x - coordinates of the current point
874756cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
874856cf3b9cSMatthew G. Knepley . constants - constant parameters
874956cf3b9cSMatthew G. Knepley - bcval - output values at the current point
875056cf3b9cSMatthew G. Knepley 
8751ed808b8fSJed Brown   Level: intermediate
8752a6ba4734SToby Isaac 
8753db781477SPatrick Sanan .seealso: `DSGetBoundary()`, `PetscDSAddBoundary()`
8754a6ba4734SToby Isaac @*/
875545480ffeSMatthew 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)
8756a6ba4734SToby Isaac {
8757e5e52638SMatthew G. Knepley   PetscDS        ds;
8758a6ba4734SToby Isaac 
8759a6ba4734SToby Isaac   PetscFunctionBegin;
8760a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8761783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
876245480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
876345480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
876445480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
876545480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
876601a5d20dSJed Brown   PetscCheck(!dm->localSection,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot add boundary to DM after creating local section");
87679566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8768*799db056SMatthew G. Knepley   /* Complete label */
8769*799db056SMatthew G. Knepley   if (label) {
8770*799db056SMatthew G. Knepley     PetscObject  obj;
8771*799db056SMatthew G. Knepley     PetscClassId id;
8772*799db056SMatthew G. Knepley 
8773*799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
8774*799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
8775*799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
8776*799db056SMatthew G. Knepley       DM plex;
8777*799db056SMatthew G. Knepley 
8778*799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
8779*799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
8780*799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
8781*799db056SMatthew G. Knepley     }
8782*799db056SMatthew G. Knepley   }
87839566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
8784a6ba4734SToby Isaac   PetscFunctionReturn(0);
8785a6ba4734SToby Isaac }
8786a6ba4734SToby Isaac 
878745480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8788e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8789e6f8dbb6SToby Isaac {
8790e5e52638SMatthew G. Knepley   PetscDS        ds;
8791dff059c6SToby Isaac   DMBoundary    *lastnext;
8792e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8793e6f8dbb6SToby Isaac 
8794e6f8dbb6SToby Isaac   PetscFunctionBegin;
87959566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8796e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
879747a1f5adSToby Isaac   if (dm->boundary) {
879847a1f5adSToby Isaac     DMBoundary next = dm->boundary;
879947a1f5adSToby Isaac 
880047a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
880147a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
880247a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
880347a1f5adSToby Isaac     while (next) {
880447a1f5adSToby Isaac       DMBoundary b = next;
880547a1f5adSToby Isaac 
880647a1f5adSToby Isaac       next = b->next;
88079566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
8808a6ba4734SToby Isaac     }
880947a1f5adSToby Isaac     dm->boundary = NULL;
8810a6ba4734SToby Isaac   }
881147a1f5adSToby Isaac 
8812dff059c6SToby Isaac   lastnext = &(dm->boundary);
8813e6f8dbb6SToby Isaac   while (dsbound) {
8814e6f8dbb6SToby Isaac     DMBoundary dmbound;
8815e6f8dbb6SToby Isaac 
88169566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
8817e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
881845480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
881947a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8820dff059c6SToby Isaac     *lastnext = dmbound;
8821dff059c6SToby Isaac     lastnext = &(dmbound->next);
8822dff059c6SToby Isaac     dsbound = dsbound->next;
8823a6ba4734SToby Isaac   }
8824a6ba4734SToby Isaac   PetscFunctionReturn(0);
8825a6ba4734SToby Isaac }
8826a6ba4734SToby Isaac 
8827a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8828a6ba4734SToby Isaac {
8829b95f2879SToby Isaac   DMBoundary     b;
8830a6ba4734SToby Isaac 
8831a6ba4734SToby Isaac   PetscFunctionBegin;
8832a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8833534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8834a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
88359566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
8836b95f2879SToby Isaac   b = dm->boundary;
8837a6ba4734SToby Isaac   while (b && !(*isBd)) {
8838e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8839e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8840a6ba4734SToby Isaac     PetscInt   i;
8841a6ba4734SToby Isaac 
884245480ffeSMatthew G. Knepley     if (label) {
88439566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8844a6ba4734SToby Isaac     }
8845a6ba4734SToby Isaac     b = b->next;
8846a6ba4734SToby Isaac   }
8847a6ba4734SToby Isaac   PetscFunctionReturn(0);
8848a6ba4734SToby Isaac }
88494d6f44ffSToby Isaac 
88504d6f44ffSToby Isaac /*@C
8851a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8852a6e0b375SMatthew G. Knepley 
8853a6e0b375SMatthew G. Knepley   Collective on DM
88544d6f44ffSToby Isaac 
88554d6f44ffSToby Isaac   Input Parameters:
88564d6f44ffSToby Isaac + dm      - The DM
88570709b2feSToby Isaac . time    - The time
88584d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
88594d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
88604d6f44ffSToby Isaac - mode    - The insertion mode for values
88614d6f44ffSToby Isaac 
88624d6f44ffSToby Isaac   Output Parameter:
88634d6f44ffSToby Isaac . X - vector
88644d6f44ffSToby Isaac 
88654d6f44ffSToby Isaac    Calling sequence of func:
886677b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
88674d6f44ffSToby Isaac 
88684d6f44ffSToby Isaac +  dim - The spatial dimension
88698ec8862eSJed Brown .  time - The time at which to sample
88704d6f44ffSToby Isaac .  x   - The coordinates
887177b739a6SMatthew Knepley .  Nc  - The number of components
88724d6f44ffSToby Isaac .  u   - The output field values
88734d6f44ffSToby Isaac -  ctx - optional user-defined function context
88744d6f44ffSToby Isaac 
88754d6f44ffSToby Isaac   Level: developer
88764d6f44ffSToby Isaac 
8877db781477SPatrick Sanan .seealso: `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
88784d6f44ffSToby Isaac @*/
88790709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
88804d6f44ffSToby Isaac {
88814d6f44ffSToby Isaac   Vec            localX;
88824d6f44ffSToby Isaac 
88834d6f44ffSToby Isaac   PetscFunctionBegin;
88844d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88859566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
88869566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
88879566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
88889566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
88899566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
88904d6f44ffSToby Isaac   PetscFunctionReturn(0);
88914d6f44ffSToby Isaac }
88924d6f44ffSToby Isaac 
8893a6e0b375SMatthew G. Knepley /*@C
8894a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8895a6e0b375SMatthew G. Knepley 
8896a6e0b375SMatthew G. Knepley   Not collective
8897a6e0b375SMatthew G. Knepley 
8898a6e0b375SMatthew G. Knepley   Input Parameters:
8899a6e0b375SMatthew G. Knepley + dm      - The DM
8900a6e0b375SMatthew G. Knepley . time    - The time
8901a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8902a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8903a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8904a6e0b375SMatthew G. Knepley 
8905a6e0b375SMatthew G. Knepley   Output Parameter:
8906a6e0b375SMatthew G. Knepley . localX - vector
8907a6e0b375SMatthew G. Knepley 
8908a6e0b375SMatthew G. Knepley    Calling sequence of func:
890977b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8910a6e0b375SMatthew G. Knepley 
8911a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8912a6e0b375SMatthew G. Knepley .  x   - The coordinates
891377b739a6SMatthew Knepley .  Nc  - The number of components
8914a6e0b375SMatthew G. Knepley .  u   - The output field values
8915a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8916a6e0b375SMatthew G. Knepley 
8917a6e0b375SMatthew G. Knepley   Level: developer
8918a6e0b375SMatthew G. Knepley 
8919db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8920a6e0b375SMatthew G. Knepley @*/
89210709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
89224d6f44ffSToby Isaac {
89234d6f44ffSToby Isaac   PetscFunctionBegin;
89244d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8925064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
89267a8be351SBarry Smith   PetscCheck(dm->ops->projectfunctionlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
89279566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX));
89284d6f44ffSToby Isaac   PetscFunctionReturn(0);
89294d6f44ffSToby Isaac }
89304d6f44ffSToby Isaac 
8931a6e0b375SMatthew G. Knepley /*@C
8932a6e0b375SMatthew 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.
8933a6e0b375SMatthew G. Knepley 
8934a6e0b375SMatthew G. Knepley   Collective on DM
8935a6e0b375SMatthew G. Knepley 
8936a6e0b375SMatthew G. Knepley   Input Parameters:
8937a6e0b375SMatthew G. Knepley + dm      - The DM
8938a6e0b375SMatthew G. Knepley . time    - The time
8939a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8940a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8941a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8942a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8943a6e0b375SMatthew G. Knepley 
8944a6e0b375SMatthew G. Knepley   Output Parameter:
8945a6e0b375SMatthew G. Knepley . X - vector
8946a6e0b375SMatthew G. Knepley 
8947a6e0b375SMatthew G. Knepley    Calling sequence of func:
894877b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8949a6e0b375SMatthew G. Knepley 
8950a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8951a6e0b375SMatthew G. Knepley .  x   - The coordinates
895277b739a6SMatthew Knepley .  Nc  - The number of components
8953a6e0b375SMatthew G. Knepley .  u   - The output field values
8954a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8955a6e0b375SMatthew G. Knepley 
8956a6e0b375SMatthew G. Knepley   Level: developer
8957a6e0b375SMatthew G. Knepley 
8958db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8959a6e0b375SMatthew G. Knepley @*/
89602c53366bSMatthew 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)
89612c53366bSMatthew G. Knepley {
89622c53366bSMatthew G. Knepley   Vec            localX;
89632c53366bSMatthew G. Knepley 
89642c53366bSMatthew G. Knepley   PetscFunctionBegin;
89652c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89669566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
89679566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
89689566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
89699566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
89709566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
89712c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
89722c53366bSMatthew G. Knepley }
89732c53366bSMatthew G. Knepley 
8974a6e0b375SMatthew G. Knepley /*@C
8975a6e0b375SMatthew 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.
8976a6e0b375SMatthew G. Knepley 
8977a6e0b375SMatthew G. Knepley   Not collective
8978a6e0b375SMatthew G. Knepley 
8979a6e0b375SMatthew G. Knepley   Input Parameters:
8980a6e0b375SMatthew G. Knepley + dm      - The DM
8981a6e0b375SMatthew G. Knepley . time    - The time
8982a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8983a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8984a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8985a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8986a6e0b375SMatthew G. Knepley 
8987a6e0b375SMatthew G. Knepley   Output Parameter:
8988a6e0b375SMatthew G. Knepley . localX - vector
8989a6e0b375SMatthew G. Knepley 
8990a6e0b375SMatthew G. Knepley    Calling sequence of func:
899177b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8992a6e0b375SMatthew G. Knepley 
8993a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8994a6e0b375SMatthew G. Knepley .  x   - The coordinates
899577b739a6SMatthew Knepley .  Nc  - The number of components
8996a6e0b375SMatthew G. Knepley .  u   - The output field values
8997a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8998a6e0b375SMatthew G. Knepley 
8999a6e0b375SMatthew G. Knepley   Level: developer
9000a6e0b375SMatthew G. Knepley 
9001db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
9002a6e0b375SMatthew G. Knepley @*/
90031c531cf8SMatthew 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)
90044d6f44ffSToby Isaac {
90054d6f44ffSToby Isaac   PetscFunctionBegin;
90064d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9007064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
90087a8be351SBarry Smith   PetscCheck(dm->ops->projectfunctionlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
90099566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
90104d6f44ffSToby Isaac   PetscFunctionReturn(0);
90114d6f44ffSToby Isaac }
90122716604bSToby Isaac 
9013a6e0b375SMatthew G. Knepley /*@C
9014a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
9015a6e0b375SMatthew G. Knepley 
9016a6e0b375SMatthew G. Knepley   Not collective
9017a6e0b375SMatthew G. Knepley 
9018a6e0b375SMatthew G. Knepley   Input Parameters:
9019a6e0b375SMatthew G. Knepley + dm      - The DM
9020a6e0b375SMatthew G. Knepley . time    - The time
9021a6e0b375SMatthew G. Knepley . localU  - The input field vector
9022a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9023a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9024a6e0b375SMatthew G. Knepley 
9025a6e0b375SMatthew G. Knepley   Output Parameter:
9026a6e0b375SMatthew G. Knepley . localX  - The output vector
9027a6e0b375SMatthew G. Knepley 
9028a6e0b375SMatthew G. Knepley    Calling sequence of func:
9029a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9030a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9031a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9032a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9033a6e0b375SMatthew G. Knepley 
9034a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
9035a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
9036a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9037a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
9038a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9039a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
9040a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9041a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9042a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9043a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9044a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9045a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9046a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9047a6e0b375SMatthew G. Knepley .  t            - The current time
9048a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
9049a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
9050a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
9051a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
9052a6e0b375SMatthew G. Knepley 
9053a6e0b375SMatthew 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.
9054a6e0b375SMatthew 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
9055a6e0b375SMatthew 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
9056a6e0b375SMatthew 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.
9057a6e0b375SMatthew G. Knepley 
9058a6e0b375SMatthew G. Knepley   Level: intermediate
9059a6e0b375SMatthew G. Knepley 
9060db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
9061a6e0b375SMatthew G. Knepley @*/
90628c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
90638c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
90648c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
90658c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9066191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
90678c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
90688c6c5593SMatthew G. Knepley {
90698c6c5593SMatthew G. Knepley   PetscFunctionBegin;
90708c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
90718c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
90728c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
90737a8be351SBarry Smith   PetscCheck(dm->ops->projectfieldlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
90749566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX));
90758c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
90768c6c5593SMatthew G. Knepley }
90778c6c5593SMatthew G. Knepley 
9078a6e0b375SMatthew G. Knepley /*@C
9079a6e0b375SMatthew 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.
9080a6e0b375SMatthew G. Knepley 
9081a6e0b375SMatthew G. Knepley   Not collective
9082a6e0b375SMatthew G. Knepley 
9083a6e0b375SMatthew G. Knepley   Input Parameters:
9084a6e0b375SMatthew G. Knepley + dm      - The DM
9085a6e0b375SMatthew G. Knepley . time    - The time
9086a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
9087a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
9088a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
9089a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9090a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9091a6e0b375SMatthew G. Knepley . localU  - The input field vector
9092a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9093a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9094a6e0b375SMatthew G. Knepley 
9095a6e0b375SMatthew G. Knepley   Output Parameter:
9096a6e0b375SMatthew G. Knepley . localX  - The output vector
9097a6e0b375SMatthew G. Knepley 
9098a6e0b375SMatthew G. Knepley    Calling sequence of func:
9099a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9100a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9101a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9102a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9103a6e0b375SMatthew G. Knepley 
9104a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
9105a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
9106a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9107a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
9108a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9109a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
9110a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9111a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9112a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9113a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9114a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9115a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9116a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9117a6e0b375SMatthew G. Knepley .  t            - The current time
9118a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
9119a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
9120a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
9121a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
9122a6e0b375SMatthew G. Knepley 
9123a6e0b375SMatthew 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.
9124a6e0b375SMatthew 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
9125a6e0b375SMatthew 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
9126a6e0b375SMatthew 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.
9127a6e0b375SMatthew G. Knepley 
9128a6e0b375SMatthew G. Knepley   Level: intermediate
9129a6e0b375SMatthew G. Knepley 
9130db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
9131a6e0b375SMatthew G. Knepley @*/
91321c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
91338c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
91348c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
91358c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9136191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
91378c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
91388c6c5593SMatthew G. Knepley {
91398c6c5593SMatthew G. Knepley   PetscFunctionBegin;
91408c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9141064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9142064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
91437a8be351SBarry Smith   PetscCheck(dm->ops->projectfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
91449566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
91458c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
91468c6c5593SMatthew G. Knepley }
91478c6c5593SMatthew G. Knepley 
91482716604bSToby Isaac /*@C
9149ece3a9fcSMatthew 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.
9150ece3a9fcSMatthew G. Knepley 
9151ece3a9fcSMatthew G. Knepley   Not collective
9152ece3a9fcSMatthew G. Knepley 
9153ece3a9fcSMatthew G. Knepley   Input Parameters:
9154ece3a9fcSMatthew G. Knepley + dm      - The DM
9155ece3a9fcSMatthew G. Knepley . time    - The time
9156ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
9157ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
9158ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
9159ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9160ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9161ece3a9fcSMatthew G. Knepley . localU  - The input field vector
9162ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9163ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
9164ece3a9fcSMatthew G. Knepley 
9165ece3a9fcSMatthew G. Knepley   Output Parameter:
9166ece3a9fcSMatthew G. Knepley . localX  - The output vector
9167ece3a9fcSMatthew G. Knepley 
9168ece3a9fcSMatthew G. Knepley    Calling sequence of func:
9169ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9170ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9171ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9172ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9173ece3a9fcSMatthew G. Knepley 
9174ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
9175ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
9176ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9177ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
9178ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9179ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
9180ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9181ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9182ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9183ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9184ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9185ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9186ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9187ece3a9fcSMatthew G. Knepley .  t            - The current time
9188ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
9189ece3a9fcSMatthew G. Knepley .  n            - The face normal
9190ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
9191ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
9192ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
9193ece3a9fcSMatthew G. Knepley 
9194ece3a9fcSMatthew G. Knepley   Note:
9195ece3a9fcSMatthew 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.
9196ece3a9fcSMatthew 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
9197ece3a9fcSMatthew 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
9198ece3a9fcSMatthew 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.
9199ece3a9fcSMatthew G. Knepley 
9200ece3a9fcSMatthew G. Knepley   Level: intermediate
9201ece3a9fcSMatthew G. Knepley 
9202db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
9203ece3a9fcSMatthew G. Knepley @*/
9204ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
9205ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
9206ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9207ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9208ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
9209ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
9210ece3a9fcSMatthew G. Knepley {
9211ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
9212ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9213064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9214064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
92157a8be351SBarry Smith   PetscCheck(dm->ops->projectbdfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
92169566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
9217ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
9218ece3a9fcSMatthew G. Knepley }
9219ece3a9fcSMatthew G. Knepley 
9220ece3a9fcSMatthew G. Knepley /*@C
92212716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
92222716604bSToby Isaac 
92232716604bSToby Isaac   Input Parameters:
92242716604bSToby Isaac + dm    - The DM
92250709b2feSToby Isaac . time  - The time
92262716604bSToby Isaac . funcs - The functions to evaluate for each field component
92272716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9228574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
92292716604bSToby Isaac 
92302716604bSToby Isaac   Output Parameter:
92312716604bSToby Isaac . diff - The diff ||u - u_h||_2
92322716604bSToby Isaac 
92332716604bSToby Isaac   Level: developer
92342716604bSToby Isaac 
9235db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
92362716604bSToby Isaac @*/
92370709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
92382716604bSToby Isaac {
92392716604bSToby Isaac   PetscFunctionBegin;
92402716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9241b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
92427a8be351SBarry Smith   PetscCheck(dm->ops->computel2diff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
92439566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff));
92442716604bSToby Isaac   PetscFunctionReturn(0);
92452716604bSToby Isaac }
9246b698f381SToby Isaac 
9247b698f381SToby Isaac /*@C
9248b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
9249b698f381SToby Isaac 
9250d083f849SBarry Smith   Collective on dm
9251d083f849SBarry Smith 
9252b698f381SToby Isaac   Input Parameters:
9253b698f381SToby Isaac + dm    - The DM
9254b698f381SToby Isaac , time  - The time
9255b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
9256b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9257574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
9258b698f381SToby Isaac - n     - The vector to project along
9259b698f381SToby Isaac 
9260b698f381SToby Isaac   Output Parameter:
9261b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
9262b698f381SToby Isaac 
9263b698f381SToby Isaac   Level: developer
9264b698f381SToby Isaac 
9265db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2Diff()`
9266b698f381SToby Isaac @*/
9267b698f381SToby 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)
9268b698f381SToby Isaac {
9269b698f381SToby Isaac   PetscFunctionBegin;
9270b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9271b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
92727a8be351SBarry Smith   PetscCheck(dm->ops->computel2gradientdiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
92739566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff));
9274b698f381SToby Isaac   PetscFunctionReturn(0);
9275b698f381SToby Isaac }
9276b698f381SToby Isaac 
92772a16baeaSToby Isaac /*@C
92782a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
92792a16baeaSToby Isaac 
9280d083f849SBarry Smith   Collective on dm
9281d083f849SBarry Smith 
92822a16baeaSToby Isaac   Input Parameters:
92832a16baeaSToby Isaac + dm    - The DM
92842a16baeaSToby Isaac . time  - The time
92852a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
92862a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9287574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
92882a16baeaSToby Isaac 
92892a16baeaSToby Isaac   Output Parameter:
92902a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
92912a16baeaSToby Isaac 
92922a16baeaSToby Isaac   Level: developer
92932a16baeaSToby Isaac 
9294db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
92952a16baeaSToby Isaac @*/
92961189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
92972a16baeaSToby Isaac {
92982a16baeaSToby Isaac   PetscFunctionBegin;
92992a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
93002a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
93017a8be351SBarry Smith   PetscCheck(dm->ops->computel2fielddiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
93029566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff));
93032a16baeaSToby Isaac   PetscFunctionReturn(0);
93042a16baeaSToby Isaac }
93052a16baeaSToby Isaac 
9306df0b854cSToby Isaac /*@C
9307502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
9308502a2867SDave May 
9309502a2867SDave May  Not Collective
9310502a2867SDave May 
9311502a2867SDave May  Input Parameter:
9312502a2867SDave May .  dm    - The DM
9313502a2867SDave May 
93140a19bb7dSprj-  Output Parameters:
93150a19bb7dSprj- +  nranks - the number of neighbours
93160a19bb7dSprj- -  ranks - the neighbors ranks
9317502a2867SDave May 
9318502a2867SDave May  Notes:
9319502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
9320502a2867SDave May 
9321502a2867SDave May  Level: beginner
9322502a2867SDave May 
9323db781477SPatrick Sanan  .seealso: `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
9324502a2867SDave May @*/
9325502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
9326502a2867SDave May {
9327502a2867SDave May   PetscFunctionBegin;
9328502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
93297a8be351SBarry Smith   PetscCheck(dm->ops->getneighbors,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
93309566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm,nranks,ranks));
9331502a2867SDave May   PetscFunctionReturn(0);
9332502a2867SDave May }
9333502a2867SDave May 
9334531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
9335531c7667SBarry Smith 
9336531c7667SBarry Smith /*
9337531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
9338531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
9339531c7667SBarry Smith */
9340531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
9341531c7667SBarry Smith {
9342531c7667SBarry Smith   PetscFunctionBegin;
9343531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9344531c7667SBarry Smith     Vec x1local;
9345531c7667SBarry Smith     DM  dm;
93469566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J,&dm));
93477a8be351SBarry Smith     PetscCheck(dm,PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
93489566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm,&x1local));
93499566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local));
93509566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local));
9351531c7667SBarry Smith     x1   = x1local;
9352531c7667SBarry Smith   }
93539566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J,coloring,x1,sctx));
9354531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9355531c7667SBarry Smith     DM  dm;
93569566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J,&dm));
93579566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm,&x1));
9358531c7667SBarry Smith   }
9359531c7667SBarry Smith   PetscFunctionReturn(0);
9360531c7667SBarry Smith }
9361531c7667SBarry Smith 
9362531c7667SBarry Smith /*@
9363531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
9364531c7667SBarry Smith 
9365531c7667SBarry Smith     Input Parameter:
9366531c7667SBarry Smith .    coloring - the MatFDColoring object
9367531c7667SBarry Smith 
936895452b02SPatrick Sanan     Developer Notes:
936995452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
9370531c7667SBarry Smith 
93711b266c99SBarry Smith     Level: advanced
93721b266c99SBarry Smith 
9373db781477SPatrick Sanan .seealso: `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
9374531c7667SBarry Smith @*/
9375531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
9376531c7667SBarry Smith {
9377531c7667SBarry Smith   PetscFunctionBegin;
9378531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
9379531c7667SBarry Smith   PetscFunctionReturn(0);
9380531c7667SBarry Smith }
93818320bc6fSPatrick Sanan 
93828320bc6fSPatrick Sanan /*@
93838320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
93848320bc6fSPatrick Sanan 
93858320bc6fSPatrick Sanan     Collective
93868320bc6fSPatrick Sanan 
93878320bc6fSPatrick Sanan     Input Parameters:
9388a5bc1bf3SBarry Smith +    dm1 - the first DM
93898320bc6fSPatrick Sanan -    dm2 - the second DM
93908320bc6fSPatrick Sanan 
93918320bc6fSPatrick Sanan     Output Parameters:
93928320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
93938320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
93948320bc6fSPatrick Sanan 
93958320bc6fSPatrick Sanan     Notes:
93968320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
93973d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
93988320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
93993d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
94003d862458SPatrick Sanan     decomposition, but hold different data.
94018320bc6fSPatrick Sanan 
94028320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
94038320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
94048320bc6fSPatrick Sanan 
94058320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
94068320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
94078320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
94088320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
94098320bc6fSPatrick Sanan 
94108320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
94118320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
94128320bc6fSPatrick Sanan .vb
94138320bc6fSPatrick Sanan   ...
94149566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
94158320bc6fSPatrick Sanan   if (set && compatible)  {
94169566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
94179566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
94189566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
94198320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
94208320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
94218320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
94228320bc6fSPatrick Sanan       }
94238320bc6fSPatrick Sanan     }
94249566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
94259566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
94268320bc6fSPatrick Sanan   } else {
94278320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
94288320bc6fSPatrick Sanan   }
94298320bc6fSPatrick Sanan   ...
94308320bc6fSPatrick Sanan .ve
94318320bc6fSPatrick Sanan 
94328320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
94338320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
94348320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
94358320bc6fSPatrick Sanan     always check the "set" output parameter.
94368320bc6fSPatrick Sanan 
94378320bc6fSPatrick Sanan     A DM is always compatible with itself.
94388320bc6fSPatrick Sanan 
94398320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
94408320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
94418320bc6fSPatrick Sanan     incompatible.
94428320bc6fSPatrick Sanan 
94438320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
94448320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
94458320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
94468320bc6fSPatrick Sanan 
94478320bc6fSPatrick Sanan     Developer Notes:
94483d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
94493d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
9450a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
94518320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
94528320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
94533d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
94543d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
94558320bc6fSPatrick Sanan 
94568320bc6fSPatrick Sanan     Level: advanced
94578320bc6fSPatrick Sanan 
9458db781477SPatrick Sanan .seealso: `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
94598320bc6fSPatrick Sanan @*/
94608320bc6fSPatrick Sanan 
9461a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
94628320bc6fSPatrick Sanan {
94638320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
94648320bc6fSPatrick Sanan   DMType         type,type2;
94658320bc6fSPatrick Sanan   PetscBool      sameType;
94668320bc6fSPatrick Sanan 
94678320bc6fSPatrick Sanan   PetscFunctionBegin;
9468a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
94698320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
94708320bc6fSPatrick Sanan 
94718320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
9472a5bc1bf3SBarry Smith   if (dm1 == dm2) {
94738320bc6fSPatrick Sanan     *set = PETSC_TRUE;
94748320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
94758320bc6fSPatrick Sanan     PetscFunctionReturn(0);
94768320bc6fSPatrick Sanan   }
94778320bc6fSPatrick Sanan 
94788320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
94798320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
94808320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
94818320bc6fSPatrick Sanan      determined by the implementation-specific logic */
94829566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult));
94838320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
94848320bc6fSPatrick Sanan     *set = PETSC_TRUE;
94858320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
94868320bc6fSPatrick Sanan     PetscFunctionReturn(0);
94878320bc6fSPatrick Sanan   }
94888320bc6fSPatrick Sanan 
94898320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
9490a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
94919566063dSJacob Faibussowitsch     PetscCall((*dm1->ops->getcompatibility)(dm1,dm2,compatible,set));
9492b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
94938320bc6fSPatrick Sanan   }
94948320bc6fSPatrick Sanan 
9495a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
94968320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
94979566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1,&type));
94989566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2,&type2));
94999566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type,type2,&sameType));
95008320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
95019566063dSJacob Faibussowitsch     PetscCall((*dm2->ops->getcompatibility)(dm2,dm1,compatible,set)); /* Note argument order */
95028320bc6fSPatrick Sanan   } else {
95038320bc6fSPatrick Sanan     *set = PETSC_FALSE;
95048320bc6fSPatrick Sanan   }
95058320bc6fSPatrick Sanan   PetscFunctionReturn(0);
95068320bc6fSPatrick Sanan }
9507c0f0dcc3SMatthew G. Knepley 
9508c0f0dcc3SMatthew G. Knepley /*@C
9509c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
9510c0f0dcc3SMatthew G. Knepley 
9511c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9512c0f0dcc3SMatthew G. Knepley 
9513c0f0dcc3SMatthew G. Knepley   Input Parameters:
9514c0f0dcc3SMatthew G. Knepley + DM - the DM
9515c0f0dcc3SMatthew G. Knepley . f - the monitor function
9516c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
9517c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
9518c0f0dcc3SMatthew G. Knepley 
9519c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9520c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
9521c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
9522c0f0dcc3SMatthew G. Knepley 
9523c0f0dcc3SMatthew G. Knepley   Notes:
9524c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
9525c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
9526c0f0dcc3SMatthew G. Knepley   order in which they were set.
9527c0f0dcc3SMatthew G. Knepley 
9528c0f0dcc3SMatthew G. Knepley   Fortran Notes:
9529c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
9530c0f0dcc3SMatthew G. Knepley 
9531c0f0dcc3SMatthew G. Knepley   Level: intermediate
9532c0f0dcc3SMatthew G. Knepley 
9533db781477SPatrick Sanan .seealso: `DMMonitorCancel()`
9534c0f0dcc3SMatthew G. Knepley @*/
9535c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
9536c0f0dcc3SMatthew G. Knepley {
9537c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9538c0f0dcc3SMatthew G. Knepley 
9539c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9540c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9541c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9542c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9543c0f0dcc3SMatthew G. Knepley 
95449566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
9545c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
9546c0f0dcc3SMatthew G. Knepley   }
95477a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9548c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9549c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9550c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
9551c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9552c0f0dcc3SMatthew G. Knepley }
9553c0f0dcc3SMatthew G. Knepley 
9554c0f0dcc3SMatthew G. Knepley /*@
9555c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9556c0f0dcc3SMatthew G. Knepley 
9557c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9558c0f0dcc3SMatthew G. Knepley 
9559c0f0dcc3SMatthew G. Knepley   Input Parameter:
9560c0f0dcc3SMatthew G. Knepley . dm - the DM
9561c0f0dcc3SMatthew G. Knepley 
9562c0f0dcc3SMatthew G. Knepley   Options Database Key:
9563c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9564c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9565c0f0dcc3SMatthew G. Knepley   set via the options database
9566c0f0dcc3SMatthew G. Knepley 
9567c0f0dcc3SMatthew G. Knepley   Notes:
9568c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9569c0f0dcc3SMatthew G. Knepley 
9570c0f0dcc3SMatthew G. Knepley   Level: intermediate
9571c0f0dcc3SMatthew G. Knepley 
9572db781477SPatrick Sanan .seealso: `DMMonitorSet()`
9573c0f0dcc3SMatthew G. Knepley @*/
9574c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9575c0f0dcc3SMatthew G. Knepley {
9576c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9577c0f0dcc3SMatthew G. Knepley 
9578c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9579c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9580c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
95819566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
9582c0f0dcc3SMatthew G. Knepley   }
9583c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9584c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9585c0f0dcc3SMatthew G. Knepley }
9586c0f0dcc3SMatthew G. Knepley 
9587c0f0dcc3SMatthew G. Knepley /*@C
9588c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9589c0f0dcc3SMatthew G. Knepley 
9590c0f0dcc3SMatthew G. Knepley   Collective on DM
9591c0f0dcc3SMatthew G. Knepley 
9592c0f0dcc3SMatthew G. Knepley   Input Parameters:
9593c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9594c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9595c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9596c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9597c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9598c0f0dcc3SMatthew 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
9599c0f0dcc3SMatthew G. Knepley 
9600c0f0dcc3SMatthew G. Knepley   Output Parameter:
9601c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9602c0f0dcc3SMatthew G. Knepley 
9603c0f0dcc3SMatthew G. Knepley   Level: developer
9604c0f0dcc3SMatthew G. Knepley 
9605db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
9606db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
9607db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
9608db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
9609c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
9610db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
9611db781477SPatrick Sanan           `PetscOptionsFList()`, `PetscOptionsEList()`
9612c0f0dcc3SMatthew G. Knepley @*/
9613c0f0dcc3SMatthew 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)
9614c0f0dcc3SMatthew G. Knepley {
9615c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9616c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9617c0f0dcc3SMatthew G. Knepley 
9618c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9619c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
96209566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg));
9621c0f0dcc3SMatthew G. Knepley   if (*flg) {
9622c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9623c0f0dcc3SMatthew G. Knepley 
96249566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
96259566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject) viewer));
96269566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
96279566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy));
9628c0f0dcc3SMatthew G. Knepley   }
9629c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9630c0f0dcc3SMatthew G. Knepley }
9631c0f0dcc3SMatthew G. Knepley 
9632c0f0dcc3SMatthew G. Knepley /*@
9633c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9634c0f0dcc3SMatthew G. Knepley 
9635c0f0dcc3SMatthew G. Knepley    Collective on DM
9636c0f0dcc3SMatthew G. Knepley 
9637c0f0dcc3SMatthew G. Knepley    Input Parameters:
9638c0f0dcc3SMatthew G. Knepley .  dm - The DM
9639c0f0dcc3SMatthew G. Knepley 
9640c0f0dcc3SMatthew G. Knepley    Level: developer
9641c0f0dcc3SMatthew G. Knepley 
9642db781477SPatrick Sanan .seealso: `DMMonitorSet()`
9643c0f0dcc3SMatthew G. Knepley @*/
9644c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9645c0f0dcc3SMatthew G. Knepley {
9646c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9647c0f0dcc3SMatthew G. Knepley 
9648c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9649c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9650c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9651c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
96529566063dSJacob Faibussowitsch     PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
9653c0f0dcc3SMatthew G. Knepley   }
9654c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9655c0f0dcc3SMatthew G. Knepley }
96562e4af2aeSMatthew G. Knepley 
96572e4af2aeSMatthew G. Knepley /*@
96582e4af2aeSMatthew G. Knepley   DMComputeError - Computes the error assuming the user has given exact solution functions
96592e4af2aeSMatthew G. Knepley 
96602e4af2aeSMatthew G. Knepley   Collective on DM
96612e4af2aeSMatthew G. Knepley 
96622e4af2aeSMatthew G. Knepley   Input Parameters:
96632e4af2aeSMatthew G. Knepley + dm     - The DM
96646b867d5aSJose E. Roman - sol    - The solution vector
96652e4af2aeSMatthew G. Knepley 
96666b867d5aSJose E. Roman   Input/Output Parameter:
96676b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
96686b867d5aSJose E. Roman            contains the error in each field
96696b867d5aSJose E. Roman 
96706b867d5aSJose E. Roman   Output Parameter:
96716b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
96722e4af2aeSMatthew G. Knepley 
96732e4af2aeSMatthew G. Knepley   Note: The exact solutions come from the PetscDS object, and the time comes from DMGetOutputSequenceNumber().
96742e4af2aeSMatthew G. Knepley 
96752e4af2aeSMatthew G. Knepley   Level: developer
96762e4af2aeSMatthew G. Knepley 
9677db781477SPatrick Sanan .seealso: `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
96782e4af2aeSMatthew G. Knepley @*/
96792e4af2aeSMatthew G. Knepley PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
96802e4af2aeSMatthew G. Knepley {
96812e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
96822e4af2aeSMatthew G. Knepley   void            **ctxs;
96832e4af2aeSMatthew G. Knepley   PetscReal         time;
96842e4af2aeSMatthew G. Knepley   PetscInt          Nf, f, Nds, s;
96852e4af2aeSMatthew G. Knepley 
96862e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
96879566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
96889566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
96899566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
96902e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
96912e4af2aeSMatthew G. Knepley     PetscDS         ds;
96922e4af2aeSMatthew G. Knepley     DMLabel         label;
96932e4af2aeSMatthew G. Knepley     IS              fieldIS;
96942e4af2aeSMatthew G. Knepley     const PetscInt *fields;
96952e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
96962e4af2aeSMatthew G. Knepley 
96979566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
96989566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
96999566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
97002e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
97012e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
97029566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
97032e4af2aeSMatthew G. Knepley     }
97049566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
97052e4af2aeSMatthew G. Knepley   }
97062e4af2aeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
970763a3b9bcSJacob 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);
97082e4af2aeSMatthew G. Knepley   }
97099566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
97109566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
97112e4af2aeSMatthew G. Knepley   if (errorVec) {
97122e4af2aeSMatthew G. Knepley     DM             edm;
97132e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
97142e4af2aeSMatthew G. Knepley     PetscBool      simplex;
97152e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
97162e4af2aeSMatthew G. Knepley 
97179566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
97189566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
97199566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
97209566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
97212e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
97229566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
97232e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
97242e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
97252e4af2aeSMatthew G. Knepley       PetscQuadrature q;
97262e4af2aeSMatthew G. Knepley       const char     *name;
97272e4af2aeSMatthew G. Knepley 
97289566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *) &fe));
97299566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
97309566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject) fe, &name));
97319566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject) efe, name));
97329566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
97339566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
97349566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject) efe));
97359566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
97362e4af2aeSMatthew G. Knepley     }
97379566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
97382e4af2aeSMatthew G. Knepley 
97399566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
97409566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) *errorVec, "Error"));
97419566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
97429566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
97432e4af2aeSMatthew G. Knepley   }
97449566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
97452e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
97462e4af2aeSMatthew G. Knepley }
97479a2a23afSMatthew G. Knepley 
97489a2a23afSMatthew G. Knepley /*@
97499a2a23afSMatthew G. Knepley   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this DM
97509a2a23afSMatthew G. Knepley 
97519a2a23afSMatthew G. Knepley   Not collective
97529a2a23afSMatthew G. Knepley 
97539a2a23afSMatthew G. Knepley   Input Parameter:
97549a2a23afSMatthew G. Knepley . dm     - The DM
97559a2a23afSMatthew G. Knepley 
97569a2a23afSMatthew G. Knepley   Output Parameter:
9757a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
97589a2a23afSMatthew G. Knepley 
97599a2a23afSMatthew G. Knepley   Level: advanced
97609a2a23afSMatthew G. Knepley 
9761db781477SPatrick Sanan .seealso: `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
97629a2a23afSMatthew G. Knepley @*/
97639a2a23afSMatthew G. Knepley PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
97649a2a23afSMatthew G. Knepley {
97659a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97669a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97679566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
97689a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
97699a2a23afSMatthew G. Knepley }
97709a2a23afSMatthew G. Knepley 
97719a2a23afSMatthew G. Knepley /*@
9772ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
97739a2a23afSMatthew G. Knepley 
97749a2a23afSMatthew G. Knepley   Not collective
97759a2a23afSMatthew G. Knepley 
97769a2a23afSMatthew G. Knepley   Input Parameters:
97779a2a23afSMatthew G. Knepley + dm     - The DM
97789a2a23afSMatthew G. Knepley . label  - The DMLabel
9779ac17215fSMatthew G. Knepley . value  - The label value indicating the region
9780ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
97819a2a23afSMatthew G. Knepley 
97829a2a23afSMatthew G. Knepley   Output Parameter:
97839a2a23afSMatthew G. Knepley . aux    - The Vec holding auxiliary field data
97849a2a23afSMatthew G. Knepley 
9785ac17215fSMatthew G. Knepley   Note: If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
978604c51a94SMatthew G. Knepley 
97879a2a23afSMatthew G. Knepley   Level: advanced
97889a2a23afSMatthew G. Knepley 
9789db781477SPatrick Sanan .seealso: `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`
97909a2a23afSMatthew G. Knepley @*/
9791ac17215fSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
97929a2a23afSMatthew G. Knepley {
9793ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
979404c51a94SMatthew G. Knepley   PetscBool       has;
97959a2a23afSMatthew G. Knepley 
97969a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97979a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97989a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
97999a2a23afSMatthew G. Knepley   key.label = label;
98009a2a23afSMatthew G. Knepley   key.value = value;
9801ac17215fSMatthew G. Knepley   key.part  = part;
98029566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
98039566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key,  aux));
98049566063dSJacob Faibussowitsch   else     PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
98059a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98069a2a23afSMatthew G. Knepley }
98079a2a23afSMatthew G. Knepley 
98089a2a23afSMatthew G. Knepley /*@
9809ac17215fSMatthew G. Knepley   DMSetAuxiliaryVec - Set the auxiliary vector for region specified by the given label and value, and equation part
98109a2a23afSMatthew G. Knepley 
98119a2a23afSMatthew G. Knepley   Not collective
98129a2a23afSMatthew G. Knepley 
98139a2a23afSMatthew G. Knepley   Input Parameters:
98149a2a23afSMatthew G. Knepley + dm     - The DM
98159a2a23afSMatthew G. Knepley . label  - The DMLabel
98169a2a23afSMatthew G. Knepley . value  - The label value indicating the region
9817ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
98189a2a23afSMatthew G. Knepley - aux    - The Vec holding auxiliary field data
98199a2a23afSMatthew G. Knepley 
98209a2a23afSMatthew G. Knepley   Level: advanced
98219a2a23afSMatthew G. Knepley 
9822db781477SPatrick Sanan .seealso: `DMGetAuxiliaryVec()`
98239a2a23afSMatthew G. Knepley @*/
9824ac17215fSMatthew G. Knepley PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
98259a2a23afSMatthew G. Knepley {
98269a2a23afSMatthew G. Knepley   Vec             old;
98279a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
98289a2a23afSMatthew G. Knepley 
98299a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98309a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98319a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
98329a2a23afSMatthew G. Knepley   key.label = label;
98339a2a23afSMatthew G. Knepley   key.value = value;
9834ac17215fSMatthew G. Knepley   key.part  = part;
98359566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
98369566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) aux));
98379566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject) old));
98389566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
98399566063dSJacob Faibussowitsch   else      PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
98409a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98419a2a23afSMatthew G. Knepley }
98429a2a23afSMatthew G. Knepley 
98439a2a23afSMatthew G. Knepley /*@C
9844ac17215fSMatthew G. Knepley   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this DM
98459a2a23afSMatthew G. Knepley 
98469a2a23afSMatthew G. Knepley   Not collective
98479a2a23afSMatthew G. Knepley 
98489a2a23afSMatthew G. Knepley   Input Parameter:
98499a2a23afSMatthew G. Knepley . dm      - The DM
98509a2a23afSMatthew G. Knepley 
98519a2a23afSMatthew G. Knepley   Output Parameters:
98529a2a23afSMatthew G. Knepley + labels  - The DMLabels for each Vec
9853ac17215fSMatthew G. Knepley . values  - The label values for each Vec
9854ac17215fSMatthew G. Knepley - parts   - The equation parts for each Vec
98559a2a23afSMatthew G. Knepley 
98569a2a23afSMatthew G. Knepley   Note: The arrays passed in must be at least as large as DMGetNumAuxiliaryVec().
98579a2a23afSMatthew G. Knepley 
98589a2a23afSMatthew G. Knepley   Level: advanced
98599a2a23afSMatthew G. Knepley 
9860db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
98619a2a23afSMatthew G. Knepley @*/
9862ac17215fSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
98639a2a23afSMatthew G. Knepley {
98649a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
98659a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
98669a2a23afSMatthew G. Knepley 
98679a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98689a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98699a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
9870dadcf809SJacob Faibussowitsch   PetscValidIntPointer(values, 3);
9871dadcf809SJacob Faibussowitsch   PetscValidIntPointer(parts,  4);
98729566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
98739566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
98749566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
9875ac17215fSMatthew G. Knepley   for (i = 0; i < n; ++i) {labels[i] = keys[i].label; values[i] = keys[i].value; parts[i] = keys[i].part;}
98769566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
98779a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98789a2a23afSMatthew G. Knepley }
98799a2a23afSMatthew G. Knepley 
98809a2a23afSMatthew G. Knepley /*@
98819a2a23afSMatthew G. Knepley   DMCopyAuxiliaryVec - Copy the auxiliary data to a new DM
98829a2a23afSMatthew G. Knepley 
98839a2a23afSMatthew G. Knepley   Not collective
98849a2a23afSMatthew G. Knepley 
98859a2a23afSMatthew G. Knepley   Input Parameter:
98869a2a23afSMatthew G. Knepley . dm    - The DM
98879a2a23afSMatthew G. Knepley 
98889a2a23afSMatthew G. Knepley   Output Parameter:
98899a2a23afSMatthew G. Knepley . dmNew - The new DM, now with the same auxiliary data
98909a2a23afSMatthew G. Knepley 
98919a2a23afSMatthew G. Knepley   Level: advanced
98929a2a23afSMatthew G. Knepley 
9893db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
98949a2a23afSMatthew G. Knepley @*/
98959a2a23afSMatthew G. Knepley PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
98969a2a23afSMatthew G. Knepley {
98979a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98989a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98999566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
99009566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
99019a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
99029a2a23afSMatthew G. Knepley }
9903b5a892a1SMatthew G. Knepley 
9904b5a892a1SMatthew G. Knepley /*@C
9905b5a892a1SMatthew G. Knepley   DMPolytopeMatchOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
9906b5a892a1SMatthew G. Knepley 
9907b5a892a1SMatthew G. Knepley   Not collective
9908b5a892a1SMatthew G. Knepley 
9909b5a892a1SMatthew G. Knepley   Input Parameters:
9910b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9911b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9912b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9913b5a892a1SMatthew G. Knepley 
9914b5a892a1SMatthew G. Knepley   Output Parameters:
9915b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
9916b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9917b5a892a1SMatthew G. Knepley 
9918b5a892a1SMatthew G. Knepley   Level: advanced
9919b5a892a1SMatthew G. Knepley 
9920db781477SPatrick Sanan .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`
9921b5a892a1SMatthew G. Knepley @*/
9922b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9923b5a892a1SMatthew G. Knepley {
9924b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9925b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
9926b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9927b5a892a1SMatthew G. Knepley 
9928b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9929b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
9930b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9931b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9932b5a892a1SMatthew G. Knepley 
9933b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceCone[arr[c*2]] != targetCone[c]) break;
9934b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
9935b5a892a1SMatthew G. Knepley   }
9936b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9937b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9938b5a892a1SMatthew G. Knepley }
9939b5a892a1SMatthew G. Knepley 
9940b5a892a1SMatthew G. Knepley /*@C
9941b5a892a1SMatthew G. Knepley   DMPolytopeGetOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
9942b5a892a1SMatthew G. Knepley 
9943b5a892a1SMatthew G. Knepley   Not collective
9944b5a892a1SMatthew G. Knepley 
9945b5a892a1SMatthew G. Knepley   Input Parameters:
9946b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9947b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9948b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9949b5a892a1SMatthew G. Knepley 
9950b5a892a1SMatthew G. Knepley   Output Parameters:
9951b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
9952b5a892a1SMatthew G. Knepley 
9953b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
9954b5a892a1SMatthew G. Knepley 
9955b5a892a1SMatthew G. Knepley   Level: advanced
9956b5a892a1SMatthew G. Knepley 
9957db781477SPatrick Sanan .seealso: `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`
9958b5a892a1SMatthew G. Knepley @*/
9959b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9960b5a892a1SMatthew G. Knepley {
9961b5a892a1SMatthew G. Knepley   PetscBool      found;
9962b5a892a1SMatthew G. Knepley 
9963b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
99649566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
99657a8be351SBarry Smith   PetscCheck(found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9966b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9967b5a892a1SMatthew G. Knepley }
9968b5a892a1SMatthew G. Knepley 
9969b5a892a1SMatthew G. Knepley /*@C
9970b5a892a1SMatthew G. Knepley   DMPolytopeMatchVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
9971b5a892a1SMatthew G. Knepley 
9972b5a892a1SMatthew G. Knepley   Not collective
9973b5a892a1SMatthew G. Knepley 
9974b5a892a1SMatthew G. Knepley   Input Parameters:
9975b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9976b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9977b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9978b5a892a1SMatthew G. Knepley 
9979b5a892a1SMatthew G. Knepley   Output Parameters:
9980b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
9981b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9982b5a892a1SMatthew G. Knepley 
9983b5a892a1SMatthew G. Knepley   Level: advanced
9984b5a892a1SMatthew G. Knepley 
9985db781477SPatrick Sanan .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`
9986b5a892a1SMatthew G. Knepley @*/
9987b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9988b5a892a1SMatthew G. Knepley {
9989b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9990b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
9991b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9992b5a892a1SMatthew G. Knepley 
9993b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9994b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
9995b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9996b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9997b5a892a1SMatthew G. Knepley 
9998b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceVert[arr[c]] != targetVert[c]) break;
9999b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
10000b5a892a1SMatthew G. Knepley   }
10001b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
10002b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10003b5a892a1SMatthew G. Knepley }
10004b5a892a1SMatthew G. Knepley 
10005b5a892a1SMatthew G. Knepley /*@C
10006b5a892a1SMatthew G. Knepley   DMPolytopeGetVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
10007b5a892a1SMatthew G. Knepley 
10008b5a892a1SMatthew G. Knepley   Not collective
10009b5a892a1SMatthew G. Knepley 
10010b5a892a1SMatthew G. Knepley   Input Parameters:
10011b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10012b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
10013b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
10014b5a892a1SMatthew G. Knepley 
10015b5a892a1SMatthew G. Knepley   Output Parameters:
10016b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
10017b5a892a1SMatthew G. Knepley 
10018b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
10019b5a892a1SMatthew G. Knepley 
10020b5a892a1SMatthew G. Knepley   Level: advanced
10021b5a892a1SMatthew G. Knepley 
10022db781477SPatrick Sanan .seealso: `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
10023b5a892a1SMatthew G. Knepley @*/
10024b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
10025b5a892a1SMatthew G. Knepley {
10026b5a892a1SMatthew G. Knepley   PetscBool      found;
10027b5a892a1SMatthew G. Knepley 
10028b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
100299566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
100307a8be351SBarry Smith   PetscCheck(found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
10031b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10032b5a892a1SMatthew G. Knepley }
10033012bc364SMatthew G. Knepley 
10034012bc364SMatthew G. Knepley /*@C
10035012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
10036012bc364SMatthew G. Knepley 
10037012bc364SMatthew G. Knepley   Not collective
10038012bc364SMatthew G. Knepley 
10039012bc364SMatthew G. Knepley   Input Parameters:
10040012bc364SMatthew G. Knepley + ct    - The DMPolytopeType
10041012bc364SMatthew G. Knepley - point - Coordinates of the point
10042012bc364SMatthew G. Knepley 
10043012bc364SMatthew G. Knepley   Output Parameters:
10044012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
10045012bc364SMatthew G. Knepley 
10046012bc364SMatthew G. Knepley   Level: advanced
10047012bc364SMatthew G. Knepley 
10048db781477SPatrick Sanan .seealso: `DMLocatePoints()`
10049012bc364SMatthew G. Knepley @*/
10050012bc364SMatthew G. Knepley PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
10051012bc364SMatthew G. Knepley {
10052012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
10053012bc364SMatthew G. Knepley   PetscInt  d;
10054012bc364SMatthew G. Knepley 
10055012bc364SMatthew G. Knepley   PetscFunctionBegin;
10056012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
10057012bc364SMatthew G. Knepley   switch (ct) {
10058012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
10059012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
10060012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
10061012bc364SMatthew G. Knepley       if (point[d] < -1.0) {*inside = PETSC_FALSE; break;}
10062012bc364SMatthew G. Knepley       sum += point[d];
10063012bc364SMatthew G. Knepley     }
10064012bc364SMatthew G. Knepley     if (sum > PETSC_SMALL) {*inside = PETSC_FALSE; break;}
10065012bc364SMatthew G. Knepley     break;
10066012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
10067012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
10068012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
10069012bc364SMatthew G. Knepley       if (PetscAbsReal(point[d]) > 1.+PETSC_SMALL) {*inside = PETSC_FALSE; break;}
10070012bc364SMatthew G. Knepley     break;
10071012bc364SMatthew G. Knepley   default:
1007298921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
10073012bc364SMatthew G. Knepley   }
10074012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
10075012bc364SMatthew G. Knepley }
10076